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
|
/* Copyright 2017-2021 PaGMO development team
This file is part of the PaGMO library.
The PaGMO library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any
later version.
or both in parallel, as here.
The PaGMO library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the PaGMO library. If not,
see https://www.gnu.org/licenses/. */
#define BOOST_TEST_MODULE select_best_test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <initializer_list>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <utility>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/numeric/conversion/converter_policies.hpp>
#include <boost/variant/get.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/s_policies/select_best.hpp>
#include <pagmo/s_policy.hpp>
#include <pagmo/types.hpp>
using namespace pagmo;
BOOST_AUTO_TEST_CASE(select_best_basic)
{
select_best f00;
BOOST_CHECK(f00.get_migr_rate().which() == 0);
BOOST_CHECK(boost::get<pop_size_t>(f00.get_migr_rate()) == 1u);
select_best f01(.2);
BOOST_CHECK(f01.get_migr_rate().which() == 1);
BOOST_CHECK(boost::get<double>(f01.get_migr_rate()) == .2);
select_best f02(2);
BOOST_CHECK(f02.get_migr_rate().which() == 0);
BOOST_CHECK(boost::get<pop_size_t>(f02.get_migr_rate()) == 2u);
BOOST_CHECK_EXCEPTION(f02 = select_best(-1.), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Invalid fractional migration rate specified in the constructor of a replacement/selection "
"policy: the rate must be in the [0., 1.] range, but it is ");
});
BOOST_CHECK_EXCEPTION(f02 = select_best(2.), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Invalid fractional migration rate specified in the constructor of a replacement/selection "
"policy: the rate must be in the [0., 1.] range, but it is ");
});
BOOST_CHECK_EXCEPTION(
f02 = select_best(std::numeric_limits<double>::infinity()), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Invalid fractional migration rate specified in the constructor of a replacement/selection "
"policy: the rate must be in the [0., 1.] range, but it is ");
});
BOOST_CHECK_THROW(f02 = select_best(-1), boost::numeric::negative_overflow);
auto f03(f02);
BOOST_CHECK(f03.get_migr_rate().which() == 0);
BOOST_CHECK(boost::get<pop_size_t>(f03.get_migr_rate()) == 2u);
auto f04(std::move(f01));
BOOST_CHECK(f04.get_migr_rate().which() == 1);
BOOST_CHECK(boost::get<double>(f04.get_migr_rate()) == .2);
f03 = f04;
BOOST_CHECK(f03.get_migr_rate().which() == 1);
BOOST_CHECK(boost::get<double>(f03.get_migr_rate()) == .2);
f04 = std::move(f02);
BOOST_CHECK(f04.get_migr_rate().which() == 0);
BOOST_CHECK(boost::get<pop_size_t>(f04.get_migr_rate()) == 2u);
BOOST_CHECK(f04.get_name() == "Select best");
BOOST_CHECK(boost::contains(f04.get_extra_info(), "Absolute migration rate:"));
BOOST_CHECK(boost::contains(f03.get_extra_info(), "Fractional migration rate:"));
// Minimal serialization test.
{
s_policy r0(f04);
std::stringstream ss;
{
boost::archive::binary_oarchive oarchive(ss);
oarchive << r0;
}
s_policy r1;
{
boost::archive::binary_iarchive iarchive(ss);
iarchive >> r1;
}
BOOST_CHECK(r1.is<select_best>());
BOOST_CHECK(r1.extract<select_best>()->get_migr_rate().which() == 0);
BOOST_CHECK(boost::get<pop_size_t>(r1.extract<select_best>()->get_migr_rate()) == 2u);
}
}
BOOST_AUTO_TEST_CASE(select_best_select)
{
select_best f00;
BOOST_CHECK_EXCEPTION(f00.select(individuals_group_t{}, 0, 0, 2, 1, 0, vector_double{}), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"The 'Select best' selection policy is unable to deal with "
"multiobjective constrained optimisation problems");
});
f00 = select_best(100);
BOOST_CHECK_EXCEPTION(f00.select(individuals_group_t{}, 0, 0, 1, 0, 0, vector_double{}), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "The absolute migration rate (100) in a 'Select best' selection policy "
"is larger than the number of input individuals (0)");
});
// Single-objective, unconstrained.
f00 = select_best(.1);
individuals_group_t inds{{1, 2, 3}, {{0}, {0}, {0}}, {{1}, {2}, {3}}};
// Too few individuals in inds for fractional migration, no migration will happen.
auto new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
BOOST_CHECK(new_inds == individuals_group_t{});
// Select top 1.
f00 = select_best(0.5);
new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
BOOST_CHECK((new_inds == individuals_group_t{{1}, {{0}}, {{1}}}));
// All selected.
f00 = select_best(1.);
new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
BOOST_CHECK((new_inds == inds));
// Absolute rate, no selection.
f00 = select_best(0);
new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
BOOST_CHECK(new_inds == individuals_group_t{});
// Absolute rate, select 2.
f00 = select_best(2);
new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
BOOST_CHECK((new_inds == individuals_group_t{{1, 2}, {{0}, {0}}, {{1}, {2}}}));
// Absolute rate, select all.
f00 = select_best(3);
new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
BOOST_CHECK((new_inds == inds));
// Single-objective, constrained.
inds = individuals_group_t{{1, 2, 3}, {{0}, {0}, {0}}, {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}};
f00 = select_best(.1);
// Too few individuals in inds for fractional migration, no migration will happen.
new_inds = f00.select(inds, 1, 0, 1, 1, 1, {0., 0.});
BOOST_CHECK(new_inds == individuals_group_t{});
// Select top 1.
f00 = select_best(0.5);
new_inds = f00.select(inds, 1, 0, 1, 1, 1, {0., 0.});
BOOST_CHECK((new_inds == individuals_group_t{{1}, {{0}}, {{1, 1, 1}}}));
// All selected.
f00 = select_best(1.);
new_inds = f00.select(inds, 1, 0, 1, 1, 1, {0., 0.});
BOOST_CHECK((new_inds == inds));
// Absolute rate, no selection.
f00 = select_best(0);
new_inds = f00.select(inds, 1, 0, 1, 1, 1, {0., 0.});
BOOST_CHECK(new_inds == individuals_group_t{});
// Absolute rate, select 2.
f00 = select_best(2);
new_inds = f00.select(inds, 1, 0, 1, 1, 1, {0., 0.});
BOOST_CHECK((new_inds == individuals_group_t{{1, 2}, {{0}, {0}}, {{1, 1, 1}, {2, 2, 2}}}));
// Absolute rate, select all.
f00 = select_best(3);
new_inds = f00.select(inds, 1, 0, 1, 1, 1, {0., 0.});
BOOST_CHECK((new_inds == inds));
// Multi-objective, unconstrained.
// NOTE: these values are taken from a test in multi_objective.cpp.
inds = individuals_group_t{{1, 2, 3, 4, 5}, {{0}, {0}, {0}, {0}, {0}}, {{0, 7}, {1, 5}, {2, 3}, {4, 2}, {7, 1}}};
f00 = select_best(1.);
new_inds = f00.select(inds, 1, 0, 2, 0, 0, {});
BOOST_CHECK((new_inds == inds));
}
// Check behaviour when, in unconstrained
// single-objective optimisation problems,
// the fitness is nan.
BOOST_AUTO_TEST_CASE(select_best_nan_fitness)
{
select_best f00(2);
individuals_group_t inds{{1, 2, 3}, {{0}, {0}, {0}}, {{1}, {2}, {std::numeric_limits<double>::quiet_NaN()}}};
auto new_inds = f00.select(inds, 1, 0, 1, 0, 0, {});
// Check that the individual with nan fitness was not selected.
BOOST_CHECK((new_inds == individuals_group_t{{1, 2}, {{0}, {0}}, {{1}, {2}}}));
}
|