From c4eb04ab70badc5b8de3106fae00aa814bc12033 Mon Sep 17 00:00:00 2001 From: "Moritz v. Looz" Date: Thu, 24 Sep 2020 16:36:34 +0200 Subject: [PATCH 1/3] fix in unconstrain meta problem, NaNs in weighted constraints now lead to NaNs in fitness --- src/problems/unconstrain.cpp | 4 ++-- tests/unconstrain.cpp | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/problems/unconstrain.cpp b/src/problems/unconstrain.cpp index ac31f8d72..84bf12cad 100644 --- a/src/problems/unconstrain.cpp +++ b/src/problems/unconstrain.cpp @@ -81,7 +81,7 @@ void unconstrain::generic_ctor_impl(const std::string &method, const vector_doub // 3 - We throw if the method selected is not supported if (method != "death penalty" && method != "kuri" && method != "weighted" && method != "ignore_c" && method != "ignore_o") { - pagmo_throw(std::invalid_argument, "The method " + method + " is not supported (did you mispell?)"); + pagmo_throw(std::invalid_argument, "The method " + method + " is not supported (did you misspell?)"); } // 4 - We throw if a non empty weight vector is passed but the method weghted is not selected if (weights.size() != 0u && method != "weighted") { @@ -167,7 +167,7 @@ vector_double unconstrain::fitness(const vector_double &x) const } } for (decltype(nc) i = 0u; i < nc; ++i) { - if (c[i] > 0.) { + if (!(c[i] < 0.)) { penalty += m_weights[i] * c[i]; } } diff --git a/tests/unconstrain.cpp b/tests/unconstrain.cpp index fdea466d1..778c5d90e 100644 --- a/tests/unconstrain.cpp +++ b/tests/unconstrain.cpp @@ -32,6 +32,7 @@ see https://www.gnu.org/licenses/. */ #include #include +#include #include #include #include @@ -140,6 +141,9 @@ BOOST_AUTO_TEST_CASE(unconstrain_fitness_test) BOOST_CHECK(p0.fitness(vector_double{0., 0., 1., 1., -1., 1.}) == vector_double(2, 3.)); BOOST_CHECK(p0.fitness(vector_double{0., 0., 1., 1., -1., -1.}) == vector_double(2, 2.)); BOOST_CHECK(p0.fitness(vector_double{0., 0., 0., 1., 0., 0.}) == vector_double(2, 1.)); + vector_double nan_fitness = p0.fitness(vector_double{0., 0., std::numeric_limits::quiet_NaN(), 1., -1., 1.}); + BOOST_CHECK(boost::math::isnan(nan_fitness[0])); + BOOST_CHECK(boost::math::isnan(nan_fitness[1])); } { unconstrain p0{my_udp{}, "ignore_c"}; @@ -294,4 +298,4 @@ BOOST_AUTO_TEST_CASE(unconstrain_thread_safety_test) unconstrain t{p0}; BOOST_CHECK(t.get_thread_safety() == thread_safety::basic); BOOST_CHECK((unconstrain{ts2{}}.get_thread_safety() == thread_safety::none)); -} \ No newline at end of file +} From 63720285cb601f324a957353834e480ca3ada906 Mon Sep 17 00:00:00 2001 From: "Moritz v. Looz" Date: Thu, 24 Sep 2020 16:52:19 +0200 Subject: [PATCH 2/3] changed comparison to be a better reflection of the previous code --- src/problems/unconstrain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/unconstrain.cpp b/src/problems/unconstrain.cpp index 84bf12cad..6f968bba8 100644 --- a/src/problems/unconstrain.cpp +++ b/src/problems/unconstrain.cpp @@ -167,7 +167,7 @@ vector_double unconstrain::fitness(const vector_double &x) const } } for (decltype(nc) i = 0u; i < nc; ++i) { - if (!(c[i] < 0.)) { + if (!(c[i] <= 0.)) { penalty += m_weights[i] * c[i]; } } From f9340dc2dc5d953a123fe29ff1ba2c5e6bd78419 Mon Sep 17 00:00:00 2001 From: "Moritz v. Looz" Date: Thu, 24 Sep 2020 17:23:41 +0200 Subject: [PATCH 3/3] replaced boost nan test with cmath nan test --- tests/unconstrain.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unconstrain.cpp b/tests/unconstrain.cpp index 778c5d90e..ecd4e4eb7 100644 --- a/tests/unconstrain.cpp +++ b/tests/unconstrain.cpp @@ -32,7 +32,7 @@ see https://www.gnu.org/licenses/. */ #include #include -#include +#include #include #include #include @@ -142,8 +142,8 @@ BOOST_AUTO_TEST_CASE(unconstrain_fitness_test) BOOST_CHECK(p0.fitness(vector_double{0., 0., 1., 1., -1., -1.}) == vector_double(2, 2.)); BOOST_CHECK(p0.fitness(vector_double{0., 0., 0., 1., 0., 0.}) == vector_double(2, 1.)); vector_double nan_fitness = p0.fitness(vector_double{0., 0., std::numeric_limits::quiet_NaN(), 1., -1., 1.}); - BOOST_CHECK(boost::math::isnan(nan_fitness[0])); - BOOST_CHECK(boost::math::isnan(nan_fitness[1])); + BOOST_CHECK(std::isnan(nan_fitness[0])); + BOOST_CHECK(std::isnan(nan_fitness[1])); } { unconstrain p0{my_udp{}, "ignore_c"};