From 130c8cae143de4220ccdd63f489ffa5313506d5c Mon Sep 17 00:00:00 2001 From: Julien Schueller Date: Fri, 25 Sep 2026 11:28:49 +0200 Subject: [PATCH] Check algo parameters for nan values Closes #277 --- src/algorithms/cmaes.cpp | 8 ++++---- src/algorithms/de.cpp | 2 +- src/algorithms/gaco.cpp | 6 +++--- src/algorithms/ihs.cpp | 7 ++++--- src/algorithms/maco.cpp | 2 +- src/algorithms/moead.cpp | 8 ++++---- src/algorithms/moead_gen.cpp | 8 ++++---- src/algorithms/nsga2.cpp | 8 ++++---- src/algorithms/nspso.cpp | 6 +++--- src/algorithms/pso.cpp | 6 +++--- src/algorithms/pso_gen.cpp | 6 +++--- src/algorithms/sga.cpp | 10 +++++----- src/algorithms/xnes.cpp | 8 ++++---- tests/cmaes.cpp | 5 +++++ tests/de.cpp | 5 +++++ tests/gaco.cpp | 5 +++++ tests/ihs.cpp | 7 +++++++ tests/maco.cpp | 3 +++ tests/moead.cpp | 10 ++++++++++ tests/moead_gen.cpp | 10 ++++++++++ tests/nsga2.cpp | 5 +++++ tests/nspso.cpp | 12 ++++++++++++ tests/pso.cpp | 6 ++++++ tests/pso_gen.cpp | 6 ++++++ tests/sga.cpp | 12 ++++++++++++ tests/xnes.cpp | 5 +++++ 26 files changed, 134 insertions(+), 42 deletions(-) diff --git a/src/algorithms/cmaes.cpp b/src/algorithms/cmaes.cpp index e4a78fb3a..edb37f94e 100644 --- a/src/algorithms/cmaes.cpp +++ b/src/algorithms/cmaes.cpp @@ -60,22 +60,22 @@ cmaes::cmaes(unsigned gen, double cc, double cs, double c1, double cmu, double s : m_gen(gen), m_cc(cc), m_cs(cs), m_c1(c1), m_cmu(cmu), m_sigma0(sigma0), m_ftol(ftol), m_xtol(xtol), m_memory(memory), m_force_bounds(force_bounds), m_e(seed), m_seed(seed), m_verbosity(0u) { - if (((cc < 0.) || (cc > 1.)) && !(cc == -1)) { + if ((!(cc >= 0.) || !(cc <= 1.)) && !(cc == -1)) { pagmo_throw(std::invalid_argument, "cc must be in [0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(cc) + " was detected"); } - if (((cs < 0.) || (cs > 1.)) && !(cs == -1)) { + if ((!(cs >= 0.) || !(cs <= 1.)) && !(cs == -1)) { pagmo_throw(std::invalid_argument, "cs needs to be in [0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(cs) + " was detected"); } - if (((c1 < 0.) || (c1 > 1.)) && !(c1 == -1)) { + if ((!(c1 >= 0.) || !(c1 <= 1.)) && !(c1 == -1)) { pagmo_throw(std::invalid_argument, "c1 needs to be in [0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(c1) + " was detected"); } - if (((cmu < 0.) || (cmu > 1.)) && !(cmu == -1)) { + if ((!(cmu >= 0.) || !(cmu <= 1.)) && !(cmu == -1)) { pagmo_throw(std::invalid_argument, "cmu needs to be in [0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(cmu) + " was detected"); diff --git a/src/algorithms/de.cpp b/src/algorithms/de.cpp index 50fe6e428..888a52e84 100644 --- a/src/algorithms/de.cpp +++ b/src/algorithms/de.cpp @@ -62,7 +62,7 @@ de::de(unsigned gen, double F, double CR, unsigned variant, double ftol, double "The Differential Evolution variant must be in [1, .., 10], while a value of " + std::to_string(variant) + " was detected."); } - if (CR < 0. || F < 0. || CR > 1. || F > 1.) { + if (!(CR >= 0.) || !(CR <= 1.) || !(F >= 0.) || !(F <= 1.)) { pagmo_throw(std::invalid_argument, "The F and CR parameters must be in the [0,1] range"); } } diff --git a/src/algorithms/gaco.cpp b/src/algorithms/gaco.cpp index 5d742ffb9..920f0baad 100644 --- a/src/algorithms/gaco.cpp +++ b/src/algorithms/gaco.cpp @@ -64,11 +64,11 @@ gaco::gaco(unsigned gen, unsigned ker, double q, double oracle, double acc, unsi m_n_gen_mark(n_gen_mark), m_memory(memory), m_counter(0u), m_sol_archive(), m_n_evalstop(1u), m_n_impstop(1u), m_gen_mark(1u), m_fevals(0u) { - if (acc < 0.) { + if (!(acc >= 0.)) { pagmo_throw(std::invalid_argument, "The accuracy parameter must be >=0, while a value of " + std::to_string(acc) + " was detected"); } - if (focus < 0.) { + if (!(focus >= 0.)) { pagmo_throw(std::invalid_argument, "The focus parameter must be >=0 while a value of " + std::to_string(focus) + " was detected"); } @@ -81,7 +81,7 @@ gaco::gaco(unsigned gen, unsigned ker, double q, double oracle, double acc, unsi pagmo_throw(std::invalid_argument, "If memory is active, the threshold parameter must be >=1 while a value of " + std::to_string(threshold) + " was detected"); } - if (q < 0.) { + if (!(q >= 0.)) { pagmo_throw(std::invalid_argument, "The convergence speed parameter must be >=0 while a value of " + std::to_string(q) + " was detected"); } diff --git a/src/algorithms/ihs.cpp b/src/algorithms/ihs.cpp index a1c7321cf..067822fe1 100644 --- a/src/algorithms/ihs.cpp +++ b/src/algorithms/ihs.cpp @@ -52,15 +52,16 @@ ihs::ihs(unsigned gen, double phmcr, double ppar_min, double ppar_max, double bw : m_gen(gen), m_phmcr(phmcr), m_ppar_min(ppar_min), m_ppar_max(ppar_max), m_bw_min(bw_min), m_bw_max(bw_max), m_e(seed), m_seed(seed), m_verbosity(0u) { - if (phmcr > 1 || phmcr < 0 || ppar_min > 1 || ppar_min < 0 || ppar_max > 1 || ppar_max < 0) { + if (!(phmcr >= 0.) || !(phmcr <= 1.) || !(ppar_min >= 0.) || !(ppar_min <= 1.) || !(ppar_max >= 0.) + || !(ppar_max <= 1.)) { pagmo_throw(std::invalid_argument, "The probability of choosing from memory (phmcr) and the pitch " "adjustment rates (ppar_min, ppar_max) must all be in the [0,1] range"); } - if (ppar_min > ppar_max) { + if (!(ppar_min <= ppar_max)) { pagmo_throw(std::invalid_argument, "The minimum pitch adjustment rate must not be greater than maximum pitch adjustment rate"); } - if (bw_min <= 0 || bw_max < bw_min) { + if (!(bw_min > 0.) || !(bw_max >= bw_min)) { pagmo_throw(std::invalid_argument, "The bandwidth values must be positive, and minimum bandwidth must not " "be greater than maximum bandwidth"); } diff --git a/src/algorithms/maco.cpp b/src/algorithms/maco.cpp index 41d33e382..0df0b0d61 100644 --- a/src/algorithms/maco.cpp +++ b/src/algorithms/maco.cpp @@ -71,7 +71,7 @@ maco::maco(unsigned gen, unsigned ker, double q, unsigned threshold, unsigned n_ m_threshold(threshold), m_q(q), m_n_gen_mark(n_gen_mark), m_memory(memory), m_counter(0u), m_sol_archive(), m_n_evalstop(0u), m_gen_mark(1u), m_pop() { - if (focus < 0.) { + if (!(focus >= 0.)) { pagmo_throw(std::invalid_argument, "The focus parameter must be >=0 while a value of " + std::to_string(focus) + " was detected"); } diff --git a/src/algorithms/moead.cpp b/src/algorithms/moead.cpp index c32a0159e..dfd8683b0 100644 --- a/src/algorithms/moead.cpp +++ b/src/algorithms/moead.cpp @@ -65,24 +65,24 @@ moead::moead(unsigned gen, std::string weight_generation, std::string decomposit pagmo_throw(std::invalid_argument, "Weight generation method requested is '" + m_decomposition + "', but only one of 'tchebycheff', 'weighted', 'bi' is allowed"); } - if (CR > 1.0 || CR < 0.) { + if (!(CR >= 0.) || !(CR <= 1.)) { pagmo_throw( std::invalid_argument, "The parameter CR (used by the differential evolution operator) needs to be in [0,1], while a value of " + std::to_string(CR) + " was detected"); } - if (F > 1.0 || F < 0.) { + if (!(F >= 0.) || !(F <= 1.)) { pagmo_throw( std::invalid_argument, "The parameter F (used by the differential evolution operator) needs to be in [0,1], while a value of " + std::to_string(F) + " was detected"); } - if (eta_m < 0.) { + if (!(eta_m >= 0.)) { pagmo_throw(std::invalid_argument, "The distribution index for the polynomial mutation (eta_m) needs to be positive, while a value of " + std::to_string(eta_m) + " was detected"); } - if (realb > 1.0 || realb < 0.) { + if (!(realb >= 0.) || !(realb <= 1.)) { pagmo_throw(std::invalid_argument, "The chance of considering a neighbourhood (realb) needs to be in [0,1], while a value of " + std::to_string(realb) + " was detected"); diff --git a/src/algorithms/moead_gen.cpp b/src/algorithms/moead_gen.cpp index bbb19e422..754243abc 100644 --- a/src/algorithms/moead_gen.cpp +++ b/src/algorithms/moead_gen.cpp @@ -69,24 +69,24 @@ moead_gen::moead_gen(unsigned gen, std::string weight_generation, std::string de pagmo_throw(std::invalid_argument, "Weight generation method requested is '" + m_decomposition + "', but only one of 'tchebycheff', 'weighted', 'bi' is allowed"); } - if (CR > 1.0 || CR < 0.) { + if (!(CR >= 0.) || !(CR <= 1.)) { pagmo_throw( std::invalid_argument, "The parameter CR (used by the differential evolution operator) needs to be in [0,1], while a value of " + std::to_string(CR) + " was detected"); } - if (F > 1.0 || F < 0.) { + if (!(F >= 0.) || !(F <= 1.)) { pagmo_throw( std::invalid_argument, "The parameter F (used by the differential evolution operator) needs to be in [0,1], while a value of " + std::to_string(F) + " was detected"); } - if (eta_m < 0.) { + if (!(eta_m >= 0.)) { pagmo_throw(std::invalid_argument, "The distribution index for the polynomial mutation (eta_m) needs to be positive, while a value of " + std::to_string(eta_m) + " was detected"); } - if (realb > 1.0 || realb < 0.) { + if (!(realb >= 0.) || !(realb <= 1.)) { pagmo_throw(std::invalid_argument, "The chance of considering a neighbourhood (realb) needs to be in [0,1], while a value of " + std::to_string(realb) + " was detected"); diff --git a/src/algorithms/nsga2.cpp b/src/algorithms/nsga2.cpp index 17a6ac75d..ac301b640 100644 --- a/src/algorithms/nsga2.cpp +++ b/src/algorithms/nsga2.cpp @@ -60,19 +60,19 @@ namespace pagmo nsga2::nsga2(unsigned gen, double cr, double eta_c, double m, double eta_m, unsigned seed) : m_gen(gen), m_cr(cr), m_eta_c(eta_c), m_m(m), m_eta_m(eta_m), m_e(seed), m_seed(seed), m_verbosity(0u) { - if (cr >= 1. || cr < 0.) { + if (!(cr >= 0.) || !(cr < 1.)) { pagmo_throw(std::invalid_argument, "The crossover probability must be in the [0,1[ range, while a value of " + std::to_string(cr) + " was detected"); } - if (m < 0. || m > 1.) { + if (!(m >= 0.) || !(m <= 1.)) { pagmo_throw(std::invalid_argument, "The mutation probability must be in the [0,1] range, while a value of " + std::to_string(cr) + " was detected"); } - if (eta_c < 1. || eta_c > 100.) { + if (!(eta_c >= 1.) || !(eta_c <= 100.)) { pagmo_throw(std::invalid_argument, "The distribution index for crossover must be in [1, 100], while a value of " + std::to_string(eta_c) + " was detected"); } - if (eta_m < 1. || eta_m > 100.) { + if (!(eta_m >= 1.) || !(eta_m <= 100.)) { pagmo_throw(std::invalid_argument, "The distribution index for mutation must be in [1, 100], while a value of " + std::to_string(eta_m) + " was detected"); } diff --git a/src/algorithms/nspso.cpp b/src/algorithms/nspso.cpp index 95841337d..e822b8aa8 100644 --- a/src/algorithms/nspso.cpp +++ b/src/algorithms/nspso.cpp @@ -56,15 +56,15 @@ nspso::nspso(unsigned gen, double omega, double c1, double c2, double chi, doubl m_leader_selection_range(leader_selection_range), m_diversity_mechanism(diversity_mechanism), m_memory(memory), m_velocity(), m_e(seed), m_seed(seed), m_verbosity(0u) { - if (omega < 0. || omega > 1.) { + if (!(omega >= 0.) || !(omega <= 1.)) { pagmo_throw(std::invalid_argument, "The particles' inertia weight must be in the [0,1] range, while a value of " + std::to_string(m_omega) + " was detected"); } - if (c1 <= 0 || c2 <= 0 || chi <= 0) { + if (!(c1 > 0.) || !(c2 > 0.) || !(chi > 0.)) { pagmo_throw(std::invalid_argument, "first and second magnitude of the force " "coefficients and velocity scaling factor should be greater than 0"); } - if (v_coeff <= 0 || v_coeff > 1) { + if (!(v_coeff > 0.) || !(v_coeff <= 1.)) { pagmo_throw(std::invalid_argument, "velocity scaling factor should be in ]0,1] range, while a value of" + std::to_string(v_coeff) + " was detected"); } diff --git a/src/algorithms/pso.cpp b/src/algorithms/pso.cpp index dc783201d..ca080f283 100644 --- a/src/algorithms/pso.cpp +++ b/src/algorithms/pso.cpp @@ -80,18 +80,18 @@ pso::pso(unsigned gen, double omega, double eta1, double eta2, double max_vel, u m_neighb_type(neighb_type), m_neighb_param(neighb_param), m_memory(memory), m_e(seed), m_seed(seed), m_verbosity(0u), m_log() { - if (m_omega < 0. || m_omega > 1.) { + if (!(m_omega >= 0.) || !(m_omega <= 1.)) { // variants using Inertia weight pagmo_throw(std::invalid_argument, "The particles' inertia (or the constriction factor) must be in the [0,1] range, while a value of " + std::to_string(m_omega) + " was detected"); } - if (m_eta1 < 0. || m_eta2 < 0. || m_eta1 > 4. || m_eta2 > 4.) { + if (!(m_eta1 >= 0.) || !(m_eta2 >= 0.) || !(m_eta1 <= 4.) || !(m_eta2 <= 4.)) { pagmo_throw(std::invalid_argument, "The eta parameters must be in the [0,4] range, while eta1 = " + std::to_string(m_eta1) + ", eta2 = " + std::to_string(m_eta2) + " was detected"); } - if (m_max_vel <= 0. || m_max_vel > 1.) { + if (!(m_max_vel > 0.) || !(m_max_vel <= 1.)) { pagmo_throw(std::invalid_argument, "The maximum particle velocity (as a fraction of the bounds) should be " "in the (0,1] range, while a value of " + std::to_string(m_max_vel) + " was detected"); diff --git a/src/algorithms/pso_gen.cpp b/src/algorithms/pso_gen.cpp index 434f0bc17..94567d172 100644 --- a/src/algorithms/pso_gen.cpp +++ b/src/algorithms/pso_gen.cpp @@ -81,18 +81,18 @@ pso_gen::pso_gen(unsigned gen, double omega, double eta1, double eta2, double ma m_neighb_type(neighb_type), m_neighb_param(neighb_param), m_memory(memory), m_e(seed), m_seed(seed), m_verbosity(0u), m_log() { - if (m_omega < 0. || m_omega > 1.) { + if (!(m_omega >= 0.) || !(m_omega <= 1.)) { // variants using Inertia weight pagmo_throw(std::invalid_argument, "The particles' inertia (or the constriction factor) must be in the [0,1] range, while a value of " + std::to_string(m_variant) + " was detected"); } - if (m_eta1 < 0. || m_eta2 < 0. || m_eta1 > 4. || m_eta2 > 4.) { + if (!(m_eta1 >= 0.) || !(m_eta2 >= 0.) || !(m_eta1 <= 4.) || !(m_eta2 <= 4.)) { pagmo_throw(std::invalid_argument, "The eta parameters must be in the [0,4] range, while eta1 = " + std::to_string(m_eta1) + ", eta2 = " + std::to_string(m_eta2) + " was detected"); } - if (m_max_vel <= 0. || m_max_vel > 1.) { + if (!(m_max_vel > 0.) || !(m_max_vel <= 1.)) { pagmo_throw(std::invalid_argument, "The maximum particle velocity (as a fraction of the bounds) should be " "in the (0,1] range, while a value of " + std::to_string(m_max_vel) + " was detected"); diff --git a/src/algorithms/sga.cpp b/src/algorithms/sga.cpp index dde743499..ac757cba1 100644 --- a/src/algorithms/sga.cpp +++ b/src/algorithms/sga.cpp @@ -118,16 +118,16 @@ sga::sga(unsigned gen, double cr, double eta_c, double m, double param_m, unsign : m_gen(gen), m_cr(cr), m_eta_c(eta_c), m_m(m), m_param_m(param_m), m_param_s(param_s), m_e(seed), m_seed(seed), m_verbosity(0u), m_log() { - if (cr > 1. || cr < 0.) { + if (!(cr >= 0.) || !(cr <= 1.)) { pagmo_throw(std::invalid_argument, "The crossover probability must be in the [0,1] range, while a value of " + std::to_string(cr) + " was detected"); } - if (eta_c < 1. || eta_c > 100.) { + if (!(eta_c >= 1.) || !(eta_c <= 100.)) { pagmo_throw(std::invalid_argument, "The distribution index for SBX crossover must be in [1, 100], while a value of " + std::to_string(eta_c) + " was detected"); } - if (m < 0. || m > 1.) { + if (!(m >= 0.) || !(m <= 1.)) { pagmo_throw(std::invalid_argument, "The mutation probability must be in the [0,1] range, while a value of " + std::to_string(cr) + " was detected"); } @@ -154,14 +154,14 @@ sga::sga(unsigned gen, double cr, double eta_c, double m, double param_m, unsign + crossover); } // param_m represents the distribution index if polynomial mutation is selected - if (mutation == "polynomial" && (param_m < 1. || param_m > 100.)) { + if (mutation == "polynomial" && (!(param_m >= 1.) || !(param_m <= 100.))) { pagmo_throw(std::invalid_argument, "Polynomial mutation was selected, the mutation parameter (distribution " "index) must be in [1, 100], while a value of " + std::to_string(param_m) + " was detected"); } // otherwise param_m represents the width of the mutation relative to the box bounds - if (mutation != "polynomial" && (param_m < 0 || param_m > 1.)) { + if (mutation != "polynomial" && (!(param_m >= 0.) || !(param_m <= 1.))) { pagmo_throw(std::invalid_argument, "The mutation parameter must be in [0,1], while a value of " + std::to_string(param_m) + " was detected"); } diff --git a/src/algorithms/xnes.cpp b/src/algorithms/xnes.cpp index e034c9c32..656224ff2 100644 --- a/src/algorithms/xnes.cpp +++ b/src/algorithms/xnes.cpp @@ -55,22 +55,22 @@ xnes::xnes(unsigned gen, double eta_mu, double eta_sigma, double eta_b, double s : m_gen(gen), m_eta_mu(eta_mu), m_eta_sigma(eta_sigma), m_eta_b(eta_b), m_sigma0(sigma0), m_ftol(ftol), m_xtol(xtol), m_memory(memory), m_force_bounds(force_bounds), m_e(seed), m_seed(seed), m_verbosity(0u), m_log() { - if (((eta_mu <= 0.) || (eta_mu > 1.)) && !(eta_mu == -1)) { + if ((!(eta_mu > 0.) || !(eta_mu <= 1.)) && !(eta_mu == -1)) { pagmo_throw(std::invalid_argument, "eta_mu must be in ]0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(eta_mu) + " was detected"); } - if (((eta_sigma <= 0.) || (eta_sigma > 1.)) && !(eta_sigma == -1)) { + if ((!(eta_sigma > 0.) || !(eta_sigma <= 1.)) && !(eta_sigma == -1)) { pagmo_throw(std::invalid_argument, "eta_sigma needs to be in ]0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(eta_sigma) + " was detected"); } - if (((eta_b <= 0.) || (eta_b > 1.)) && !(eta_b == -1)) { + if ((!(eta_b > 0.) || !(eta_b <= 1.)) && !(eta_b == -1)) { pagmo_throw(std::invalid_argument, "eta_b needs to be in ]0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(eta_b) + " was detected"); } - if (((sigma0 <= 0.) || (sigma0 > 1.)) && !(sigma0 == -1)) { + if ((!(sigma0 > 0.) || !(sigma0 <= 1.)) && !(sigma0 == -1)) { pagmo_throw(std::invalid_argument, "sigma0 needs to be in ]0,1] or -1 if its value has to be initialized automatically, a value of " + std::to_string(sigma0) + " was detected"); diff --git a/tests/cmaes.cpp b/tests/cmaes.cpp index a431c8bb0..edda99227 100644 --- a/tests/cmaes.cpp +++ b/tests/cmaes.cpp @@ -85,6 +85,11 @@ BOOST_AUTO_TEST_CASE(cmaes_algorithm_construction) BOOST_CHECK_THROW((cmaes{10u, -1, -1, -1.2, -1, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); BOOST_CHECK_THROW((cmaes{10u, -1, -1, -1, -1.2, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); BOOST_CHECK_THROW((cmaes{10u, -1, -1, -1, -1.2, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((cmaes{10u, nan, -1, -1, -1, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((cmaes{10u, -1, nan, -1, -1, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((cmaes{10u, -1, -1, nan, -1, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((cmaes{10u, -1, -1, -1, nan, 0.5, 1e-6, 1e-6, false, false, 23u}), std::invalid_argument); } struct unbounded_lb { diff --git a/tests/de.cpp b/tests/de.cpp index 955158e74..0ef158354 100644 --- a/tests/de.cpp +++ b/tests/de.cpp @@ -31,6 +31,7 @@ see https://www.gnu.org/licenses/. */ #include #include +#include #include #include @@ -64,6 +65,10 @@ BOOST_AUTO_TEST_CASE(de_algorithm_construction) BOOST_CHECK_THROW((de{1234u, 0.7, 1.5, 2u, 1e-6, 1e-6, 23u}), std::invalid_argument); BOOST_CHECK_THROW((de{1234u, 0.7, -0.5, 2u, 1e-6, 1e-6, 23u}), std::invalid_argument); + + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((de{1234u, nan, 0.5, 2u, 1e-6, 1e-6, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((de{1234u, 0.7, nan, 2u, 1e-6, 1e-6, 23u}), std::invalid_argument); } BOOST_AUTO_TEST_CASE(de_evolve_test) diff --git a/tests/gaco.cpp b/tests/gaco.cpp index a52ff4d3e..56b33cb8f 100644 --- a/tests/gaco.cpp +++ b/tests/gaco.cpp @@ -31,6 +31,7 @@ see https://www.gnu.org/licenses/. */ #include #include +#include #include #include @@ -68,6 +69,10 @@ BOOST_AUTO_TEST_CASE(construction_test) BOOST_CHECK_THROW((gaco{2u, 13u, 1.0, 0.0, 0.01, 0u, 7u, 1000u, 1000u, 0.0, false, 23u}), std::invalid_argument); BOOST_CHECK_THROW((gaco{2u, 13u, 1.0, 0.0, 0.01, 0u, 7u, 1000u, 1000u, 0.0, true, 23u}), std::invalid_argument); BOOST_CHECK_THROW((gaco{2u, 1u, 1.0, 0.0, 0.01, 1u, 7u, 1000u, 1000u, 0.0, false, 23u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((gaco{2u, 13u, 1.0, 0.0, nan, 1u, 7u, 1000u, 1000u, 0.0, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((gaco{2u, 13u, 1.0, 0.0, 0.01, 1u, 7u, 1000u, 1000u, nan, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((gaco{2u, 13u, nan, 0.0, 0.01, 1u, 7u, 1000u, 1000u, 0.0, false, 23u}), std::invalid_argument); } BOOST_AUTO_TEST_CASE(evolve_test) diff --git a/tests/ihs.cpp b/tests/ihs.cpp index 8d990f3ed..78f66ea05 100644 --- a/tests/ihs.cpp +++ b/tests/ihs.cpp @@ -31,6 +31,7 @@ see https://www.gnu.org/licenses/. */ #include #include +#include #include #include #include @@ -65,6 +66,12 @@ BOOST_AUTO_TEST_CASE(ihs_algorithm_construction) BOOST_CHECK_THROW((ihs{1u, 0.85, 0.35, 0.34, 1e-5, 1., 42u}), std::invalid_argument); BOOST_CHECK_THROW((ihs{1u, 0.85, 0.35, 0.99, -0.43, 1., 42u}), std::invalid_argument); BOOST_CHECK_THROW((ihs{1u, 0.85, 0.35, 0.99, 0.4, 0.3, 42u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((ihs{1u, nan, 0.35, 0.99, 1e-5, 1., 42u}), std::invalid_argument); + BOOST_CHECK_THROW((ihs{1u, 0.85, nan, 0.99, 1e-5, 1., 42u}), std::invalid_argument); + BOOST_CHECK_THROW((ihs{1u, 0.85, 0.35, nan, 1e-5, 1., 42u}), std::invalid_argument); + BOOST_CHECK_THROW((ihs{1u, 0.85, 0.35, 0.99, nan, 1., 42u}), std::invalid_argument); + BOOST_CHECK_THROW((ihs{1u, 0.85, 0.35, 0.99, 1e-5, nan, 42u}), std::invalid_argument); } struct mo_many { diff --git a/tests/maco.cpp b/tests/maco.cpp index c2e198b6d..f4fd35d52 100644 --- a/tests/maco.cpp +++ b/tests/maco.cpp @@ -31,6 +31,7 @@ see https://www.gnu.org/licenses/. */ #include #include +#include #include #include @@ -67,6 +68,8 @@ BOOST_AUTO_TEST_CASE(maco_algorithm_construction) // Wrong threshold BOOST_CHECK_THROW((maco{1u, 63u, 1.0, 3u, 7u, 10000u, 0., false, 23u}), std::invalid_argument); BOOST_CHECK_THROW((maco{1u, 63u, 1.0, 0, 7u, 10000u, 0., true, 23u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((maco{1u, 63u, 1.0, 1u, 7u, 10000u, nan, false, 23u}), std::invalid_argument); } BOOST_AUTO_TEST_CASE(maco_evolve_test) diff --git a/tests/moead.cpp b/tests/moead.cpp index 0cb3d4f2b..ed8bab780 100644 --- a/tests/moead.cpp +++ b/tests/moead.cpp @@ -33,6 +33,7 @@ see https://www.gnu.org/licenses/. */ #include #include #include +#include #include #include @@ -78,6 +79,15 @@ BOOST_AUTO_TEST_CASE(moead_algorithm_construction) std::invalid_argument); // Wrong neighbours BOOST_CHECK_THROW((moead{10u, "grid", "tchebycheff", 1u, 1., 0.5, 20., 0.9, 2u, true, 23u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((moead{10u, "grid", "tchebycheff", 20u, nan, 0.5, 20., 0.9, 2u, true, 23u}), + std::invalid_argument); + BOOST_CHECK_THROW((moead{10u, "grid", "tchebycheff", 20u, 1., nan, 20., 0.9, 2u, true, 23u}), + std::invalid_argument); + BOOST_CHECK_THROW((moead{10u, "grid", "tchebycheff", 20u, 1., 0.5, nan, 0.9, 2u, true, 23u}), + std::invalid_argument); + BOOST_CHECK_THROW((moead{10u, "grid", "tchebycheff", 20u, 1., 0.5, 20., nan, 2u, true, 23u}), + std::invalid_argument); } struct mo_con { diff --git a/tests/moead_gen.cpp b/tests/moead_gen.cpp index 8f43b4aa7..aafb176d6 100644 --- a/tests/moead_gen.cpp +++ b/tests/moead_gen.cpp @@ -33,6 +33,7 @@ see https://www.gnu.org/licenses/. */ #include #include #include +#include #include #include @@ -78,6 +79,15 @@ BOOST_AUTO_TEST_CASE(moead_gen_algorithm_construction) std::invalid_argument); // Wrong neighbours BOOST_CHECK_THROW((moead_gen{10u, "grid", "tchebycheff", 1u, 1., 0.5, 20., 0.9, 2u, true, 23u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((moead_gen{10u, "grid", "tchebycheff", 20u, nan, 0.5, 20., 0.9, 2u, true, 23u}), + std::invalid_argument); + BOOST_CHECK_THROW((moead_gen{10u, "grid", "tchebycheff", 20u, 1., nan, 20., 0.9, 2u, true, 23u}), + std::invalid_argument); + BOOST_CHECK_THROW((moead_gen{10u, "grid", "tchebycheff", 20u, 1., 0.5, nan, 0.9, 2u, true, 23u}), + std::invalid_argument); + BOOST_CHECK_THROW((moead_gen{10u, "grid", "tchebycheff", 20u, 1., 0.5, 20., nan, 2u, true, 23u}), + std::invalid_argument); } struct mo_con { diff --git a/tests/nsga2.cpp b/tests/nsga2.cpp index 33f48287d..948febc7a 100644 --- a/tests/nsga2.cpp +++ b/tests/nsga2.cpp @@ -71,6 +71,11 @@ BOOST_AUTO_TEST_CASE(nsga2_algorithm_construction) // Wrong eta_m BOOST_CHECK_THROW((nsga2{1u, .95, 10., 0.01, 100.1, 32u}), std::invalid_argument); BOOST_CHECK_THROW((nsga2{1u, .95, 10., 0.01, .98, 32u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((nsga2{1u, nan, 10., 0.01, 50., 32u}), std::invalid_argument); + BOOST_CHECK_THROW((nsga2{1u, .95, nan, 0.01, 50., 32u}), std::invalid_argument); + BOOST_CHECK_THROW((nsga2{1u, .95, 10., nan, 50., 32u}), std::invalid_argument); + BOOST_CHECK_THROW((nsga2{1u, .95, 10., 0.01, nan, 32u}), std::invalid_argument); } struct mo_equal_bounds { diff --git a/tests/nspso.cpp b/tests/nspso.cpp index ea65cafe4..9fa390098 100644 --- a/tests/nspso.cpp +++ b/tests/nspso.cpp @@ -34,6 +34,7 @@ see https://www.gnu.org/licenses/. */ #include #include #include +#include #include #include @@ -79,6 +80,17 @@ BOOST_AUTO_TEST_CASE(nspso_algorithm_construction) std::invalid_argument); // Wrong eta_m BOOST_CHECK_THROW((nspso{1u, 0.95, 0.01, 0.5, 0.5, 0.5, 2u, "something else", false, 24u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((nspso{1u, nan, 0.01, 0.5, 0.5, 0.5, 2u, "crowding distance", false, 24u}), + std::invalid_argument); + BOOST_CHECK_THROW((nspso{1u, 0.95, nan, 0.5, 0.5, 0.5, 2u, "crowding distance", false, 24u}), + std::invalid_argument); + BOOST_CHECK_THROW((nspso{1u, 0.95, 0.01, nan, 0.5, 0.5, 2u, "crowding distance", false, 24u}), + std::invalid_argument); + BOOST_CHECK_THROW((nspso{1u, 0.95, 0.01, 0.5, nan, 0.5, 2u, "crowding distance", false, 24u}), + std::invalid_argument); + BOOST_CHECK_THROW((nspso{1u, 0.95, 0.01, 0.5, 0.5, nan, 2u, "crowding distance", false, 24u}), + std::invalid_argument); } BOOST_AUTO_TEST_CASE(nspso_evolve_test) diff --git a/tests/pso.cpp b/tests/pso.cpp index 1c646f560..2509a16bb 100644 --- a/tests/pso.cpp +++ b/tests/pso.cpp @@ -76,6 +76,12 @@ BOOST_AUTO_TEST_CASE(construction) BOOST_CHECK_THROW((pso{100, 0.79, 2., 2., 0.1, 5u, 0u, 4u, false, 23u}), std::invalid_argument); BOOST_CHECK_THROW((pso{100, 0.79, 2., 2., 0.1, 5u, 2u, 0u, false, 23u}), std::invalid_argument); + + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((pso{100, nan, 2., 2., 0.1, 5u, 2u, 4u, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((pso{100, 0.79, nan, 2., 0.1, 5u, 2u, 4u, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((pso{100, 0.79, 2., nan, 0.1, 5u, 2u, 4u, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((pso{100, 0.79, 2., 2., nan, 5u, 2u, 4u, false, 23u}), std::invalid_argument); } BOOST_AUTO_TEST_CASE(evolve_test) diff --git a/tests/pso_gen.cpp b/tests/pso_gen.cpp index 945597849..ece102f96 100644 --- a/tests/pso_gen.cpp +++ b/tests/pso_gen.cpp @@ -137,6 +137,12 @@ BOOST_AUTO_TEST_CASE(construction) BOOST_CHECK_THROW((pso_gen{100, 0.79, 2., 2., 0.1, 5u, 0u, 4u, false, 23u}), std::invalid_argument); BOOST_CHECK_THROW((pso_gen{100, 0.79, 2., 2., 0.1, 5u, 2u, 0u, false, 23u}), std::invalid_argument); + + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((pso_gen{100, nan, 2., 2., 0.1, 5u, 2u, 4u, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((pso_gen{100, 0.79, nan, 2., 0.1, 5u, 2u, 4u, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((pso_gen{100, 0.79, 2., nan, 0.1, 5u, 2u, 4u, false, 23u}), std::invalid_argument); + BOOST_CHECK_THROW((pso_gen{100, 0.79, 2., 2., nan, 5u, 2u, 4u, false, 23u}), std::invalid_argument); } BOOST_AUTO_TEST_CASE(evolve_test) diff --git a/tests/sga.cpp b/tests/sga.cpp index 529b2414f..d2cb4f719 100644 --- a/tests/sga.cpp +++ b/tests/sga.cpp @@ -33,6 +33,7 @@ see https://www.gnu.org/licenses/. */ #include #include +#include #include #include @@ -93,6 +94,17 @@ BOOST_AUTO_TEST_CASE(sga_algorithm_construction) std::invalid_argument); BOOST_CHECK_THROW((sga{1u, .95, 10., .02, 0.9, 0u, "exponential", "uniform", "tournament", 32u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((sga{1u, nan, 10., .02, .5, 5u, "exponential", "gaussian", "tournament", 32u}), + std::invalid_argument); + BOOST_CHECK_THROW((sga{1u, .95, nan, .02, .5, 5u, "exponential", "gaussian", "tournament", 32u}), + std::invalid_argument); + BOOST_CHECK_THROW((sga{1u, .95, 10., nan, .5, 5u, "exponential", "gaussian", "tournament", 32u}), + std::invalid_argument); + BOOST_CHECK_THROW((sga{1u, .95, 10., .02, nan, 5u, "exponential", "gaussian", "tournament", 32u}), + std::invalid_argument); + BOOST_CHECK_THROW((sga{1u, .95, 10., .02, nan, 5u, "exponential", "polynomial", "tournament", 32u}), + std::invalid_argument); } BOOST_AUTO_TEST_CASE(sga_evolve_test) { diff --git a/tests/xnes.cpp b/tests/xnes.cpp index 9031a1207..34575d13d 100644 --- a/tests/xnes.cpp +++ b/tests/xnes.cpp @@ -65,6 +65,11 @@ BOOST_AUTO_TEST_CASE(xnes_algorithm_construction) BOOST_CHECK_THROW((xnes{10u, -1, -1, 1.6, -1, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); BOOST_CHECK_THROW((xnes{10u, -1, -1, -1, -0.6, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); BOOST_CHECK_THROW((xnes{10u, -1, -1, 1, 1.6, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); + const auto nan = std::numeric_limits::quiet_NaN(); + BOOST_CHECK_THROW((xnes{10u, nan, -1, -1, -1, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); + BOOST_CHECK_THROW((xnes{10u, -1, nan, -1, -1, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); + BOOST_CHECK_THROW((xnes{10u, -1, -1, nan, -1, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); + BOOST_CHECK_THROW((xnes{10u, -1, -1, -1, nan, 1e-6, 1e-6, false, false, 32u}), std::invalid_argument); // We test the defaults are correctly xnes user_algo{10u, -1, -1, -1, -1, 1e-6, 1e-6, false, false, 32u}; BOOST_CHECK(user_algo.get_verbosity() == 0u);