diff --git a/src/algorithms/gaco.cpp b/src/algorithms/gaco.cpp index e20456852..842a22b0c 100644 --- a/src/algorithms/gaco.cpp +++ b/src/algorithms/gaco.cpp @@ -851,9 +851,16 @@ void gaco::generate_new_ants(const population &pop, std::uniform_real_distributi g_h = sol_archive[k_omega][1 + h] + sigma[h] * gauss_pdf(m_e); if (g_h < lb[h] || g_h > ub[h]) { - - while ((g_h < lb[h] || g_h > ub[h])) { + int iter_while=0; + while ((g_h < lb[h] || g_h > ub[h]) && iter_while < 10) { g_h = sol_archive[k_omega][1 + h] + sigma[h] * gauss_pdf(m_e); + ++iter_while; + } + if (g_h < lb[h]) { + g_h = lb[h]; + } + if (g_h > ub[h]) { + g_h = ub[h]; } } if (h >= n_con) { diff --git a/src/algorithms/maco.cpp b/src/algorithms/maco.cpp index 764c8b2c2..886c06391 100644 --- a/src/algorithms/maco.cpp +++ b/src/algorithms/maco.cpp @@ -727,9 +727,16 @@ void maco::generate_new_ants(const population &pop, std::uniform_real_distributi g_h = sol_archive[k_omega][h] + sigma[h] * gauss_pdf(m_e); if (g_h < lb[h] || g_h > ub[h]) { - - while ((g_h < lb[h] || g_h > ub[h])) { + int iter_while=0; + while ((g_h < lb[h] || g_h > ub[h]) && iter_while < 10) { g_h = sol_archive[k_omega][h] + sigma[h] * gauss_pdf(m_e); + ++iter_while; + } + if (g_h < lb[h]){ + g_h = lb[h]; + } + if (g_h > ub[h]){ + g_h = ub[h]; } } if (h >= n_con) { diff --git a/tests/gaco.cpp b/tests/gaco.cpp index 8ca1768ca..4a9d6fc3c 100644 --- a/tests/gaco.cpp +++ b/tests/gaco.cpp @@ -354,3 +354,14 @@ BOOST_AUTO_TEST_CASE(bfe_usage_test) BOOST_CHECK_EQUAL(pop.champion_f()[0], pop_2.champion_f()[0]); } + +BOOST_AUTO_TEST_CASE(out_of_bounds_test) +{ + population pop{rosenbrock{}, 10u, 23u}; + pop.set_x(0, {-20., 12});//both out of bounds + gaco uda{10u, 10u, 1.0, 25.0, 0.01, 5u, 7u, 1000u, 1000u, 0.0, false, 23u}; + uda.set_verbosity(1u); + uda.set_seed(23u); + pop = uda.evolve(pop); +} + diff --git a/tests/maco.cpp b/tests/maco.cpp index 884362f93..8d22948a2 100644 --- a/tests/maco.cpp +++ b/tests/maco.cpp @@ -264,3 +264,15 @@ BOOST_AUTO_TEST_CASE(memory_test) pop_2 = uda_2.evolve(pop_2); BOOST_CHECK(pop_1.get_f() == pop_2.get_f()); } + +BOOST_AUTO_TEST_CASE(out_of_bounds_test) +{ + problem prob{dtlz(1, 5, 2)}; + population pop{prob, 43}; + + pop.set_x(0, {-2., 2., -2., 2., -2.});//both out of bounds + maco uda{1u, 20u, 1.0, 8u, 7u, 10000u, 0., true, 23u}; + uda.set_seed(23u); + pop = uda.evolve(pop); +} +