From d58258ff6c22f59838379bfd71731920bddb95c8 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sat, 16 Oct 2021 12:58:29 -0400 Subject: [PATCH 1/6] Fix MOEAD test stability. --- tests/moead_test.cpp | 83 ++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/tests/moead_test.cpp b/tests/moead_test.cpp index 7047f3e0f..b69316068 100644 --- a/tests/moead_test.cpp +++ b/tests/moead_test.cpp @@ -67,7 +67,7 @@ TEST_CASE("MOEADSchafferN1DoubleTest", "[MOEADTest]") // We allow a few trials in case of poor convergence. bool success = false; - for (size_t trial = 0; trial < 3; ++trial) + for (size_t trial = 0; trial < 5; ++trial) { arma::mat coords = SCH.GetInitialPoint(); std::tuple objectives = SCH.GetObjectives(); @@ -506,42 +506,65 @@ TEST_CASE("MOEADFonsecaFlemingTestVectorFloatBounds", "[MOEADTest]") * * 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. */ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") { - //! Parameters taken from original ZDT Paper. - ZDT1<> ZDT_ONE(100); - const double lowerBound = 0; - const double upperBound = 1; + bool success = false; + const size_t trials = 8; + for (size_t trial = 0; trial < trials; ++trial) + { + //! Parameters taken from original ZDT Paper. + ZDT1<> ZDT_ONE(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. - 1E-10, // epsilon. - lowerBound, // Lower bound. - upperBound // Upper bound. - ); + 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. + 1E-10, // epsilon. + lowerBound, // Lower bound. + upperBound // Upper bound. + ); - typedef decltype(ZDT_ONE.objectiveF1) ObjectiveTypeA; - typedef decltype(ZDT_ONE.objectiveF2) ObjectiveTypeB; + typedef decltype(ZDT_ONE.objectiveF1) ObjectiveTypeA; + typedef decltype(ZDT_ONE.objectiveF2) ObjectiveTypeB; - arma::mat coords = ZDT_ONE.GetInitialPoint(); - std::tuple objectives = ZDT_ONE.GetObjectives(); + arma::mat coords = ZDT_ONE.GetInitialPoint(); + std::tuple objectives = + ZDT_ONE.GetObjectives(); - opt.Optimize(objectives, coords); + opt.Optimize(objectives, coords); - //! Refer the ZDT_ONE implementation for g objective implementation. - //! The optimal g value is taken from the docs of ZDT_ONE. - size_t numVariables = coords.size(); - double sum = arma::accu(coords(arma::span(1, numVariables - 1), 0)); - double g = 1. + 9. * sum / (static_cast(numVariables - 1)); - REQUIRE(g == Approx(1.0).margin(0.99)); + //! Refer the ZDT_ONE implementation for g objective implementation. + //! The optimal g value is taken from the docs of ZDT_ONE. + size_t numVariables = coords.size(); + double sum = arma::accu(coords(arma::span(1, numVariables - 1), 0)); + double g = 1. + 9. * sum / (static_cast(numVariables - 1)); + if (trial < trials - 1) + { + if (g == Approx(1.0).margin(0.99)) + { + success = true; + break; + } + } + else + { + REQUIRE(g == Approx(1.0).margin(0.99)); + success = true; // If we get to here, it succeeded. + } + } + + REQUIRE(success == true); } /** @@ -615,4 +638,4 @@ TEST_CASE("MOEADDIRICHLETZDT3Test", "[MOEADTest]") const arma::cube& finalPopulation = opt.ParetoSet(); REQUIRE(VariableBoundsCheck(finalPopulation)); -} \ No newline at end of file +} From f3b0224187adb4b66f85a20bb3beedf81f3bef4e Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sat, 16 Oct 2021 13:02:44 -0400 Subject: [PATCH 2/6] Update HISTORY. --- HISTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index c0a5b930f..922abc4e1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -12,6 +12,9 @@ * Update Catch2 to 2.13.7 ([#322](https://github.com/mlpack/ensmallen/pull/322)). + * Fix MOEAD test stability + ([#327](https://github.com/mlpack/ensmallen/pull/327)). + ### ensmallen 2.17.0: "Pachis Din Me Pesa Double" ###### 2021-07-06 * CheckArbitraryFunctionTypeAPI extended for MOO support From df3a1cf3479425634cfb5f145febcfbf9f222fd7 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 18 Oct 2021 10:02:17 -0400 Subject: [PATCH 3/6] Update HISTORY for #324 too. --- HISTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 922abc4e1..ed7bf3250 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -12,6 +12,9 @@ * Update Catch2 to 2.13.7 ([#322](https://github.com/mlpack/ensmallen/pull/322)). + * Remove redundant template argument for C++20 compatibility + ([#324](https://github.com/mlpack/ensmallen/pull/324)). + * Fix MOEAD test stability ([#327](https://github.com/mlpack/ensmallen/pull/327)). From 23aab9f924596d03870ddd9a7cc10b94e743b508 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 18 Oct 2021 10:07:02 -0400 Subject: [PATCH 4/6] Factor out constant things from the loop. --- tests/moead_test.cpp | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/moead_test.cpp b/tests/moead_test.cpp index b69316068..2ae87cbf7 100644 --- a/tests/moead_test.cpp +++ b/tests/moead_test.cpp @@ -512,32 +512,32 @@ TEST_CASE("MOEADFonsecaFlemingTestVectorFloatBounds", "[MOEADTest]") */ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") { + //! Parameters taken from original ZDT Paper. + ZDT1<> ZDT_ONE(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. + 1E-10, // epsilon. + lowerBound, // Lower bound. + upperBound // Upper bound. + ); + + typedef decltype(ZDT_ONE.objectiveF1) ObjectiveTypeA; + typedef decltype(ZDT_ONE.objectiveF2) ObjectiveTypeB; + bool success = false; const size_t trials = 8; for (size_t trial = 0; trial < trials; ++trial) { - //! Parameters taken from original ZDT Paper. - ZDT1<> ZDT_ONE(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. - 1E-10, // epsilon. - lowerBound, // Lower bound. - upperBound // Upper bound. - ); - - typedef decltype(ZDT_ONE.objectiveF1) ObjectiveTypeA; - typedef decltype(ZDT_ONE.objectiveF2) ObjectiveTypeB; - arma::mat coords = ZDT_ONE.GetInitialPoint(); std::tuple objectives = ZDT_ONE.GetObjectives(); From bd285743b3503e65c23a722754ca7d235ff7b6f3 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 18 Oct 2021 10:29:23 -0400 Subject: [PATCH 5/6] Some cleanup. --- tests/moead_test.cpp | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/tests/moead_test.cpp b/tests/moead_test.cpp index 2ae87cbf7..051a3203d 100644 --- a/tests/moead_test.cpp +++ b/tests/moead_test.cpp @@ -516,6 +516,7 @@ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") ZDT1<> ZDT_ONE(100); const double lowerBound = 0; const double upperBound = 1; + double g = DBL_MAX; DefaultMOEAD opt( 300, // Population size. @@ -534,7 +535,6 @@ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") typedef decltype(ZDT_ONE.objectiveF1) ObjectiveTypeA; typedef decltype(ZDT_ONE.objectiveF2) ObjectiveTypeB; - bool success = false; const size_t trials = 8; for (size_t trial = 0; trial < trials; ++trial) { @@ -548,23 +548,13 @@ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") //! The optimal g value is taken from the docs of ZDT_ONE. size_t numVariables = coords.size(); double sum = arma::accu(coords(arma::span(1, numVariables - 1), 0)); - double g = 1. + 9. * sum / (static_cast(numVariables - 1)); - if (trial < trials - 1) - { - if (g == Approx(1.0).margin(0.99)) - { - success = true; - break; - } - } - else - { - REQUIRE(g == Approx(1.0).margin(0.99)); - success = true; // If we get to here, it succeeded. - } - } + g = 1. + 9. * sum / (static_cast(numVariables - 1)); + if (trial < trials - 1 && g != Approx(1.0).margin(0.99)) + continue; - REQUIRE(success == true); + REQUIRE(g == Approx(1.0).margin(0.99)); + break; + } } /** From 7a8c5ae6859bb0a9bf577b2c043ebb01760fabab Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Mon, 18 Oct 2021 11:37:18 -0400 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: Marcus Edel --- tests/moead_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/moead_test.cpp b/tests/moead_test.cpp index 051a3203d..33034b057 100644 --- a/tests/moead_test.cpp +++ b/tests/moead_test.cpp @@ -516,7 +516,6 @@ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") ZDT1<> ZDT_ONE(100); const double lowerBound = 0; const double upperBound = 1; - double g = DBL_MAX; DefaultMOEAD opt( 300, // Population size. @@ -548,7 +547,7 @@ TEST_CASE("MOEADZDTONETest", "[MOEADTest]") //! The optimal g value is taken from the docs of ZDT_ONE. size_t numVariables = coords.size(); double sum = arma::accu(coords(arma::span(1, numVariables - 1), 0)); - g = 1. + 9. * sum / (static_cast(numVariables - 1)); + const double g = 1.0 + 9.0 * sum / (static_cast(numVariables - 1)); if (trial < trials - 1 && g != Approx(1.0).margin(0.99)) continue;