1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/**
* @file moead_test.cpp
* @author Nanubala Gnana Sai
*
* ensmallen is free software; you may redistribute it and/or modify it under
* the terms of the 3-clause BSD license. You should have received a copy of
* the 3-clause BSD license along with ensmallen. If not, see
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
*/
#if defined(ENS_USE_COOT)
#include <armadillo>
#include <bandicoot>
#endif
#include <ensmallen.hpp>
#include "catch.hpp"
#include "test_function_tools.hpp"
#include "test_types.hpp"
using namespace ens;
using namespace ens::test;
using namespace std;
/**
* Checks if low <= value <= high. Used by MOEADFonsecaFlemingTest.
*
* @param value The value being checked.
* @param low The lower bound.
* @param high The upper bound.
* @param roundoff To round off precision.
* @tparam The type of elements in the population set.
* @return true if value lies in the range [low, high].
* @return false if value does not lie in the range [low, high].
*/
template<typename ElemType>
bool IsInBounds(const ElemType& value,
const ElemType& low,
const ElemType& high,
const ElemType& roundoff)
{
return !(value < (low - roundoff)) && !((high + roundoff) < value);
}
/**
* Check if the final population lies in the optimal region in variable space.
*
* @param paretoSet The final population in variable space.
*/
template<typename CubeType>
bool VariableBoundsCheck(const CubeType& paretoSet)
{
typedef typename ForwardType<CubeType>::bmat BaseMatType;
bool inBounds = true;
const BaseMatType regions(
"0.0 0.182228780 0.4093136748 0.6183967944 0.8233317983; 0.0830015349 \
0.2577623634 0.4538821041 0.6525117038 0.8518328654");
for (size_t pointIdx = 0; pointIdx < paretoSet.n_slices; ++pointIdx)
{
const BaseMatType& point = paretoSet.slice(pointIdx);
const double firstVariable = point(0, 0);
const bool notInRegion0 = !IsInBounds<double>(firstVariable, regions(0, 0),
regions(1, 0), 1e-2);
const bool notInRegion1 = !IsInBounds<double>(firstVariable, regions(0, 1),
regions(1, 1), 1e-2);
const bool notInRegion2 = !IsInBounds<double>(firstVariable, regions(0, 2),
regions(1, 2), 1e-2);
const bool notInRegion3 = !IsInBounds<double>(firstVariable, regions(0, 3),
regions(1, 3), 1e-2);
const bool notInRegion4 = !IsInBounds<double>(firstVariable, regions(0, 4),
regions(1, 4), 1e-2);
if (notInRegion0 && notInRegion1 && notInRegion2 &&
notInRegion3 && notInRegion4)
{
inBounds = false;
break;
}
}
return inBounds;
}
TEMPLATE_TEST_CASE("DefaultMOEAD_SchafferFunctionN1", "[MOEAD]",
ENS_ALL_CPU_TEST_TYPES)
{
typedef typename TestType::elem_type ElemType;
SchafferFunctionN1<TestType> sch;
const double lowerBound = -1000;
const double upperBound = 1000;
const ElemType expectedLowerBound = 0;
const ElemType expectedUpperBound = 2;
MOEAD<Uniform, Tchebycheff> opt(
300, // Population size.
300, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
Tolerances<TestType>::Obj, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(sch.objectiveA) ObjectiveTypeA;
typedef decltype(sch.objectiveB) ObjectiveTypeB;
// We allow a few trials in case of poor convergence.
bool success = false;
for (size_t trial = 0; trial < 5; ++trial)
{
TestType coords = sch.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB> objectives = sch.GetObjectives();
arma::Cube<ElemType> paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
bool allInRange = true;
for (size_t solutionIdx = 0; solutionIdx < paretoSet.n_slices;
++solutionIdx)
{
ElemType val = arma::as_scalar(paretoSet.slice(solutionIdx));
if (!IsInBounds<ElemType>(
val, expectedLowerBound, expectedUpperBound, ElemType(0.1)))
{
allInRange = false;
break;
}
}
if (allInRange)
{
success = true;
break;
}
}
REQUIRE(success == true);
}
TEMPLATE_TEST_CASE("DefaultMOEAD_SchafferFunctionN1Vec", "[MOEAD]",
ENS_TEST_TYPES)
{
typedef typename TestType::elem_type ElemType;
// This test can be a little flaky, so we try it a few times.
SchafferFunctionN1<TestType> sch;
const arma::vec lowerBound = {-1000};
const arma::vec upperBound = {1000};
const ElemType expectedLowerBound = 0.0;
const ElemType expectedUpperBound = 2.0;
MOEAD<Uniform, Tchebycheff> opt(
300, // Population size.
300, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
1e-10, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(sch.objectiveA) ObjectiveTypeA;
typedef decltype(sch.objectiveB) ObjectiveTypeB;
bool success = false;
for (size_t trial = 0; trial < 5; ++trial)
{
TestType coords = sch.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB> objectives = sch.GetObjectives();
arma::Cube<ElemType> paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
bool allInRange = true;
for (size_t solutionIdx = 0; solutionIdx < paretoSet.n_slices;
++solutionIdx)
{
ElemType val = arma::as_scalar(paretoSet.slice(solutionIdx));
if (!IsInBounds<ElemType>(
val, expectedLowerBound, expectedUpperBound, 0.1))
{
allInRange = false;
break;
}
}
if (allInRange)
{
success = true;
break;
}
}
REQUIRE(success == true);
}
TEMPLATE_TEST_CASE("DefaultMOEAD_FonsecaFlemingFunction", "[MOEAD]",
ENS_TEST_TYPES)
{
typedef typename TestType::elem_type ElemType;
FonsecaFlemingFunction<TestType> fon;
const double lowerBound = -4;
const double upperBound = 4;
const ElemType expectedLowerBound = ElemType(-1) / sqrt(3);
const ElemType expectedUpperBound = ElemType(1) / sqrt(3);
MOEAD<Uniform, Tchebycheff> opt(
300, // Max generations.
300, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
1e-10, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(fon.objectiveA) ObjectiveTypeA;
typedef decltype(fon.objectiveB) ObjectiveTypeB;
TestType coords = fon.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB> objectives = fon.GetObjectives();
arma::Cube<ElemType> paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
bool allInRange = true;
for (size_t solutionIdx = 0; solutionIdx < paretoSet.n_slices; ++solutionIdx)
{
const TestType solution = paretoSet.slice(solutionIdx);
ElemType valX = arma::as_scalar(solution(0));
ElemType valY = arma::as_scalar(solution(1));
ElemType valZ = arma::as_scalar(solution(2));
if (!IsInBounds<ElemType>(
valX, expectedLowerBound, expectedUpperBound, 0.1) ||
!IsInBounds<ElemType>(
valY, expectedLowerBound, expectedUpperBound, 0.1) ||
!IsInBounds<ElemType>(
valZ, expectedLowerBound, expectedUpperBound, 0.1))
{
allInRange = false;
break;
}
}
REQUIRE(allInRange);
}
TEMPLATE_TEST_CASE("DefaultMOEAD_FonsecaFlemingFunctionVec", "[MOEAD]",
ENS_TEST_TYPES)
{
typedef typename TestType::elem_type ElemType;
FonsecaFlemingFunction<TestType> fon;
const arma::vec lowerBound = {-4, -4, -4};
const arma::vec upperBound = {4, 4, 4};
const ElemType expectedLowerBound = ElemType(-1) / sqrt(3);
const ElemType expectedUpperBound = ElemType(1) / sqrt(3);
MOEAD<Uniform, Tchebycheff> opt(
300, // Max generations.
300, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
1e-10, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(fon.objectiveA) ObjectiveTypeA;
typedef decltype(fon.objectiveB) ObjectiveTypeB;
TestType coords = fon.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB> objectives = fon.GetObjectives();
arma::Cube<ElemType> paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
bool allInRange = true;
for (size_t solutionIdx = 0; solutionIdx < paretoSet.n_slices; ++solutionIdx)
{
const TestType solution = paretoSet.slice(solutionIdx);
ElemType valX = arma::as_scalar(solution(0));
ElemType valY = arma::as_scalar(solution(1));
ElemType valZ = arma::as_scalar(solution(2));
if (!IsInBounds<ElemType>(
valX, expectedLowerBound, expectedUpperBound, 0.1) ||
!IsInBounds<ElemType>(
valY, expectedLowerBound, expectedUpperBound, 0.1) ||
!IsInBounds<ElemType>(
valZ, expectedLowerBound, expectedUpperBound, 0.1))
{
allInRange = false;
break;
}
}
REQUIRE(allInRange);
}
/**
* Test DirichletMOEAD against the third problem of ZDT Test Suite. MAF-3 is a 12
* variable-3 objective problem with disconnected Pareto Fronts.
*/
TEST_CASE("MOEADDIRICHLETMAF3Test", "[MOEAD]")
{
//! Parameters taken from original ZDT Paper.
MAF3<arma::mat> MAF_THREE;
const double lowerBound = 0;
const double upperBound = 1;
const double expectedLowerBound = 0.5;
const double expectedUpperBound = 0.5;
DirichletMOEAD opt(
105, // Population size.
1000, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
1e-10, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(MAF_THREE.objectiveF1) ObjectiveTypeA;
typedef decltype(MAF_THREE.objectiveF2) ObjectiveTypeB;
typedef decltype(MAF_THREE.objectiveF3) ObjectiveTypeC;
arma::mat coords = MAF_THREE.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB, ObjectiveTypeC> objectives =
MAF_THREE.GetObjectives();
arma::cube paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
bool success = true;
for (size_t i = 0; i < paretoSet.n_slices; i++)
{
arma::mat solution = paretoSet.slice(i);
bool allInRange = true;
for (size_t j = 2; j < MAF_THREE.GetNumVariables(); j++)
{
double val = arma::as_scalar(solution(j));
if (!IsInBounds<double>(val, expectedLowerBound, expectedUpperBound, 0.1))
{
allInRange = false;
break;
}
}
if(!allInRange)
{
success = false;
break;
}
}
REQUIRE(success == true);
}
/**
* Test DirichletMOEAD against the third problem of ZDT Test Suite. MAF-1 is a
* 12 variable-3 objective problem with disconnected Pareto Fronts.
*/
TEST_CASE("MOEADDIRICHLETMAF1Test", "[MOEAD]")
{
//! Parameters taken from original ZDT Paper.
MAF1<arma::mat> MAF_ONE;
const double lowerBound = 0;
const double upperBound = 1;
const double expectedLowerBound = 0.5;
const double expectedUpperBound = 0.5;
DirichletMOEAD opt(
105, // Population size.
1000, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
1e-10, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(MAF_ONE.objectiveF1) ObjectiveTypeA;
typedef decltype(MAF_ONE.objectiveF2) ObjectiveTypeB;
typedef decltype(MAF_ONE.objectiveF3) ObjectiveTypeC;
arma::mat coords = MAF_ONE.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB, ObjectiveTypeC> objectives =
MAF_ONE.GetObjectives();
arma::cube paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
bool success = true;
for (size_t i = 0; i < paretoSet.n_slices; i++)
{
arma::mat solution = paretoSet.slice(i);
bool allInRange = true;
for (size_t j = 2; j < MAF_ONE.GetNumVariables(); j++)
{
double val = arma::as_scalar(solution(j));
if (!IsInBounds<double>(val, expectedLowerBound, expectedUpperBound, 0.15))
{
allInRange = false;
break;
}
}
if (!allInRange)
{
success = false;
break;
}
}
REQUIRE(success == true);
}
/**
* Test DirichletMOEAD against the third problem of ZDT Test Suite. MAF-4 is a 12
* variable-3 objective problem with disconnected Pareto Fronts.
*/
TEST_CASE("MOEADDIRICHLETMAF4Test", "[MOEAD]")
{
//! Parameters taken from original ZDT Paper.
MAF4<arma::mat> maf4;
const double lowerBound = 0;
const double upperBound = 1;
const double expectedLowerBound = 0.5;
const double expectedUpperBound = 0.5;
DirichletMOEAD opt(
200, // Population size.
1000, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
1e-10, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(maf4.objectiveF1) ObjectiveTypeA;
typedef decltype(maf4.objectiveF2) ObjectiveTypeB;
typedef decltype(maf4.objectiveF3) ObjectiveTypeC;
std::tuple<ObjectiveTypeA, ObjectiveTypeB, ObjectiveTypeC> objectives =
maf4.GetObjectives();
bool success = false;
arma::mat coords = maf4.GetInitialPoint();
arma::cube paretoFront, paretoSet;
opt.Optimize(objectives, coords, paretoFront, paretoSet);
for (size_t i = 0; i < paretoSet.n_slices; i++)
{
arma::mat solution = paretoSet.slice(i);
bool allInRange = true;
for (size_t j = 2; j < maf4.GetNumVariables(); j++)
{
double val = arma::as_scalar(solution(j));
if (!IsInBounds<double>(val, expectedLowerBound, expectedUpperBound, 0.2))
{
allInRange = false;
break;
}
}
if (allInRange)
{
success = true;
break;
}
}
REQUIRE(success == true);
}
/**
* Test against the first problem of ZDT Test Suite. ZDT-1 is a 30
* variable-2 objective problem with a convex Pareto Front.
*
* NOTE: For the sake of runtime, only ZDT-1 is tested against the
* algorithm. Others have been tested separately.
*
* We run the test multiple times, since it sometimes fails, in order to get the
* probability of failure down.
*/
TEMPLATE_TEST_CASE("DefaultMOEAD_ZDT1Function", "[MOEAD]", ENS_TEST_TYPES)
{
typedef typename TestType::elem_type ElemType;
//! Parameters taken from original ZDT Paper.
ZDT1<TestType> zdt1(100);
const double lowerBound = 0;
const double upperBound = 1;
DefaultMOEAD opt(
300, // Population size.
150, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
Tolerances<TestType>::Obj, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(zdt1.objectiveF1) ObjectiveTypeA;
typedef decltype(zdt1.objectiveF2) ObjectiveTypeB;
const size_t trials = 8;
for (size_t trial = 0; trial < trials; ++trial)
{
TestType coords = zdt1.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB> objectives =
zdt1.GetObjectives();
opt.Optimize(objectives, coords);
//! Refer the zdt1 implementation for g objective implementation.
//! The optimal g value is taken from the docs of zdt1.
size_t numVariables = coords.size();
ElemType sum = arma::accu(coords(arma::span(1, numVariables - 1), 0));
const ElemType g = 1.0 + 9.0 * sum /
(static_cast<ElemType>(numVariables - 1));
if (trial < trials - 1 && g != Approx(1.0).margin(0.99))
continue;
REQUIRE(g == Approx(1.0).margin(0.99));
break;
}
}
/**
* Test DirichletMOEAD against the third problem of ZDT Test Suite. ZDT-3 is a
* 30 variable-2 objective problem with disconnected Pareto Fronts.
*/
TEMPLATE_TEST_CASE("DirichletMOEAD_ZDT3Function", "[MOEAD]", ENS_TEST_TYPES)
{
typedef typename TestType::elem_type ElemType;
//! Parameters taken from original ZDT Paper.
ZDT3<TestType> zdt3(300);
const double lowerBound = 0;
const double upperBound = 1;
DirichletMOEAD opt(
300, // Population size.
300, // Max generations.
1.0, // Crossover probability.
0.9, // Probability of sampling from neighbor.
20, // Neighborhood size.
20, // Perturbation index.
0.5, // Differential weight.
2, // Max childrens to replace parents.
Tolerances<TestType>::Obj, // epsilon.
lowerBound, // Lower bound.
upperBound); // Upper bound.
typedef decltype(zdt3.objectiveF1) ObjectiveTypeA;
typedef decltype(zdt3.objectiveF2) ObjectiveTypeB;
TestType coords = zdt3.GetInitialPoint();
std::tuple<ObjectiveTypeA, ObjectiveTypeB> objectives =
zdt3.GetObjectives();
typename ForwardType<TestType>::cube finalPopulation, finalFront;
opt.Optimize(objectives, coords, finalPopulation, finalFront);
REQUIRE(VariableBoundsCheck(finalPopulation));
}
|