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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/* 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 generic_test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <cmath>
#include <initializer_list>
#include <limits>
#include <stdexcept>
#include <tuple>
#include <utility>
#include <boost/algorithm/string/predicate.hpp>
#include <pagmo/io.hpp>
#include <pagmo/problem.hpp>
#include <pagmo/rng.hpp>
#include <pagmo/types.hpp>
#include <pagmo/utils/generic.hpp>
using namespace pagmo;
// A UDP with user-defined bounds.
struct udp00 {
udp00() = default;
explicit udp00(vector_double lb, vector_double ub, vector_double::size_type nix = 0)
: m_lb(lb), m_ub(ub), m_nix(nix)
{
}
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {m_lb, m_ub};
}
vector_double::size_type get_nix() const
{
return m_nix;
}
vector_double m_lb, m_ub;
vector_double::size_type m_nix;
};
BOOST_AUTO_TEST_CASE(uniform_real_from_range_test)
{
auto inf = std::numeric_limits<double>::infinity();
auto big = std::numeric_limits<double>::max();
auto nan = std::numeric_limits<double>::quiet_NaN();
detail::random_engine_type r_engine(pagmo::random_device::next());
// Test the throws
BOOST_CHECK_THROW(uniform_real_from_range(1, 0, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(-big, big, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(-3, inf, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(-nan, nan, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(nan, nan, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(-nan, 3, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(-3, nan, r_engine), std::invalid_argument);
BOOST_CHECK_THROW(uniform_real_from_range(inf, inf, r_engine), std::invalid_argument);
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(1, 0, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Cannot generate a random integer if the lower bound is larger than the upper bound");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(0, inf, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(), "Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(-inf, 0, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(), "Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(-inf, inf, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(), "Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(0, nan, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(), "Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(-nan, 0, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(), "Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(-nan, nan, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(), "Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(0, .1, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Cannot generate a random integer if the lower/upper bounds are not integral values");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(0.1, 2, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Cannot generate a random integer if the lower/upper bounds are not integral values");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(0.1, 0.2, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(), "Cannot generate a random integer if the lower/upper bounds are not integral values");
});
if (big > static_cast<double>(std::numeric_limits<long long>::max())
&& -big < static_cast<double>(std::numeric_limits<long long>::min())) {
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(0, big, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the lower/upper bounds are not within "
"the bounds of the long long type");
});
BOOST_CHECK_EXCEPTION(
uniform_integral_from_range(-big, 0, r_engine), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the lower/upper bounds are not within "
"the bounds of the long long type");
});
}
}
BOOST_AUTO_TEST_CASE(random_decision_vector_test)
{
auto inf = std::numeric_limits<double>::infinity();
auto big = std::numeric_limits<double>::max();
detail::random_engine_type r_engine(pagmo::random_device::next());
// Test the throws
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{0}, {inf}}}, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{-inf}, {0}}}, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{-inf}, {inf}}}, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{-big}, {big}}}, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real within bounds that are too large");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{0, 0}, {1, inf}, 1}}, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{0, -inf}, {1, 0}, 1}}, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{0, -inf}, {1, inf}, 1}}, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the bounds are not finite");
});
if (big > static_cast<double>(std::numeric_limits<long long>::max())
&& -big < static_cast<double>(std::numeric_limits<long long>::min())) {
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{0, 0}, {1, big}, 1}}, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(),
"Cannot generate a random integer if the lower/upper bounds are not within "
"the bounds of the long long type");
});
BOOST_CHECK_EXCEPTION(random_decision_vector(problem{udp00{{0, -big}, {1, 0}, 1}}, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(),
"Cannot generate a random integer if the lower/upper bounds are not within "
"the bounds of the long long type");
});
}
// Test the results
BOOST_CHECK((random_decision_vector(problem{udp00{{3, 4}, {3, 4}}}, r_engine) == vector_double{3, 4}));
BOOST_CHECK((random_decision_vector(problem{udp00{{3, 4}, {3, 4}, 1}}, r_engine) == vector_double{3, 4}));
BOOST_CHECK((random_decision_vector(problem{udp00{{0, 0}, {1, 1}}}, r_engine)[0] >= 0.));
BOOST_CHECK((random_decision_vector(problem{udp00{{0, 0}, {1, 1}}}, r_engine)[1] < 1.));
BOOST_CHECK((random_decision_vector(problem{udp00{{0, 0}, {1, 0}}}, r_engine)[1] == 0.));
for (auto i = 0; i < 100; ++i) {
const auto tmp = random_decision_vector(problem{udp00{{0}, {2}, 1}}, r_engine)[0];
BOOST_CHECK(tmp == 0. || tmp == 1. || tmp == 2.);
}
for (auto i = 0; i < 100; ++i) {
const auto res = random_decision_vector(problem{udp00{{0, -20}, {1, 20}, 1}}, r_engine);
BOOST_CHECK(std::trunc(res[1]) == res[1]);
}
}
BOOST_AUTO_TEST_CASE(batch_random_decision_vector_test)
{
auto inf = std::numeric_limits<double>::infinity();
auto big = std::numeric_limits<double>::max();
detail::random_engine_type r_engine(pagmo::random_device::next());
// Test the throws
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{0}, {inf}}}, 0, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{-inf}, {0}}}, 0, r_engine), std::invalid_argument,
[](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{-inf}, {inf}}}, 0, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{-big}, {big}}}, 0, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random real within bounds that are too large");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{0, 0}, {1, inf}, 1}}, 0, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{0, -inf}, {1, 0}, 1}}, 0, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the bounds are not finite");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{0, -inf}, {1, inf}, 1}}, 0, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot generate a random integer if the bounds are not finite");
});
if (big > static_cast<double>(std::numeric_limits<long long>::max())
&& -big < static_cast<double>(std::numeric_limits<long long>::min())) {
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{0, 0}, {1, big}, 1}}, 10, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(),
"Cannot generate a random integer if the lower/upper bounds are not within "
"the bounds of the long long type");
});
BOOST_CHECK_EXCEPTION(batch_random_decision_vector(problem{udp00{{0, -big}, {1, 0}, 1}}, 10, r_engine),
std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(),
"Cannot generate a random integer if the lower/upper bounds are not within "
"the bounds of the long long type");
});
}
// Test the results
BOOST_CHECK(batch_random_decision_vector(problem{udp00{{3, 4}, {3, 4}}}, 0, r_engine).empty());
BOOST_CHECK(
(batch_random_decision_vector(problem{udp00{{3, 4}, {3, 4}}}, 3, r_engine) == vector_double{3, 4, 3, 4, 3, 4}));
BOOST_CHECK((batch_random_decision_vector(problem{udp00{{3, 4}, {3, 4}, 1}}, 3, r_engine)
== vector_double{3, 4, 3, 4, 3, 4}));
auto tmp = batch_random_decision_vector(problem{udp00{{0, 0}, {1, 1}}}, 3, r_engine);
BOOST_CHECK(tmp.size() == 6u);
BOOST_CHECK(tmp[0] >= 0.);
BOOST_CHECK(tmp[2] >= 0.);
BOOST_CHECK(tmp[4] >= 0.);
BOOST_CHECK(tmp[1] < 1.);
BOOST_CHECK(tmp[3] < 1.);
BOOST_CHECK(tmp[5] < 1.);
tmp = batch_random_decision_vector(problem{udp00{{0, 0}, {1, 0}}}, 3, r_engine);
BOOST_CHECK(tmp.size() == 6u);
BOOST_CHECK(tmp[1] == 0.);
BOOST_CHECK(tmp[3] == 0.);
BOOST_CHECK(tmp[5] == 0.);
for (auto i = 0; i < 100; ++i) {
tmp = batch_random_decision_vector(problem{udp00{{0}, {2}, 1}}, 3, r_engine);
BOOST_CHECK(tmp.size() == 3u);
BOOST_CHECK(tmp[0] == 0. || tmp[0] == 1. || tmp[0] == 2.);
BOOST_CHECK(tmp[1] == 0. || tmp[1] == 1. || tmp[1] == 2.);
BOOST_CHECK(tmp[2] == 0. || tmp[2] == 1. || tmp[2] == 2.);
}
for (auto i = 0; i < 100; ++i) {
tmp = batch_random_decision_vector(problem{udp00{{0, -20}, {1, 20}, 1}}, 3, r_engine);
BOOST_CHECK(tmp.size() == 6u);
BOOST_CHECK(std::trunc(tmp[1]) == tmp[1]);
BOOST_CHECK(std::trunc(tmp[3]) == tmp[3]);
BOOST_CHECK(std::trunc(tmp[5]) == tmp[5]);
}
}
BOOST_AUTO_TEST_CASE(force_bounds_test)
{
detail::random_engine_type r_engine(32u);
// force_bounds_random
{
vector_double x{1., 2., 3.};
vector_double x_fix = x;
detail::force_bounds_random(x_fix, {0., 0., 0.}, {3., 3., 3.}, r_engine);
BOOST_CHECK(x == x_fix);
detail::force_bounds_random(x_fix, {0., 0., 0.}, {1., 1., 1.}, r_engine);
BOOST_CHECK(x != x_fix);
BOOST_CHECK_EQUAL(x_fix[0], 1.);
BOOST_CHECK(x_fix[1] <= 1. && x_fix[1] >= 0.);
BOOST_CHECK(x_fix[2] <= 1. && x_fix[2] >= 0.);
}
// force_bounds_reflection
{
vector_double x{1., 2., 5.};
vector_double x_fix = x;
detail::force_bounds_reflection(x_fix, {0., 0., 0.}, {3., 3., 5.});
BOOST_CHECK(x == x_fix);
detail::force_bounds_reflection(x_fix, {0., 0., 0.}, {1., 1.9, 2.1});
BOOST_CHECK(x != x_fix);
BOOST_CHECK_EQUAL(x_fix[0], 1.);
BOOST_CHECK_CLOSE(x_fix[1], 1.8, 1e-8);
BOOST_CHECK_CLOSE(x_fix[2], 0.8, 1e-8);
}
// force_bounds_stick
{
vector_double x{1., 2., 5.};
vector_double x_fix = x;
detail::force_bounds_stick(x_fix, {0., 0., 0.}, {3., 3., 5.});
BOOST_CHECK(x == x_fix);
// ub
detail::force_bounds_stick(x_fix, {0., 0., 0.}, {1., 1.9, 2.1});
BOOST_CHECK(x != x_fix);
BOOST_CHECK_EQUAL(x_fix[0], 1.);
BOOST_CHECK_EQUAL(x_fix[1], 1.9);
BOOST_CHECK_EQUAL(x_fix[2], 2.1);
// lb
detail::force_bounds_stick(x_fix, {2., 2., 2.}, {3., 3., 3.});
BOOST_CHECK_EQUAL(x_fix[0], 2.);
BOOST_CHECK_EQUAL(x_fix[1], 2.);
BOOST_CHECK_EQUAL(x_fix[2], 2.1);
}
}
BOOST_AUTO_TEST_CASE(binomial_coefficient_test)
{
BOOST_CHECK_EQUAL(binomial_coefficient(0u, 0u), 1u);
BOOST_CHECK_EQUAL(binomial_coefficient(1u, 0u), 1u);
BOOST_CHECK_EQUAL(binomial_coefficient(1u, 1u), 1u);
BOOST_CHECK_EQUAL(binomial_coefficient(2u, 0u), 1u);
BOOST_CHECK_EQUAL(binomial_coefficient(2u, 1u), 2u);
BOOST_CHECK_EQUAL(binomial_coefficient(2u, 2u), 1u);
BOOST_CHECK_EQUAL(binomial_coefficient(13u, 5u), 1287u);
BOOST_CHECK_EQUAL(binomial_coefficient(21u, 10u), 352716u);
BOOST_CHECK_THROW(binomial_coefficient(10u, 21u), std::invalid_argument);
BOOST_CHECK_THROW(binomial_coefficient(0u, 1u), std::invalid_argument);
BOOST_CHECK_THROW(binomial_coefficient(4u, 7u), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(kNN_test)
{
// Corner cases
{
std::vector<vector_double> points = {};
std::vector<std::vector<vector_double::size_type>> res = {};
BOOST_CHECK(kNN(points, 2u) == res);
}
{
std::vector<vector_double> points = {{1.2, 1.2}};
std::vector<std::vector<vector_double::size_type>> res = {{}};
BOOST_CHECK(kNN(points, 2u) == res);
}
{
std::vector<vector_double> points = {{1.2, 1.2}, {1.2, 1.2}};
std::vector<std::vector<vector_double::size_type>> res = {{1u}, {0u}};
BOOST_CHECK(kNN(points, 2u) == res);
}
{
std::vector<vector_double> points = {{1, 1}, {2, 2}, {3.1, 3.1}};
std::vector<std::vector<vector_double::size_type>> res = {{1u, 2u}, {0u, 2u}, {1u, 0u}};
BOOST_CHECK(kNN(points, 2u) == res);
}
// Some test cases
{
std::vector<vector_double> points = {{1, 1}, {2, 2}, {3.1, 3.1}, {4.2, 4.2}, {5.4, 5.4}};
std::vector<std::vector<vector_double::size_type>> res
= {{1u, 2u, 3u}, {0u, 2u, 3u}, {1u, 3u, 0u}, {2u, 4u, 1u}, {3u, 2u, 1u}};
BOOST_CHECK(kNN(points, 3u) == res);
}
// throws
{
std::vector<vector_double> points = {{1, 1}, {2, 2}, {2, 3, 4}};
BOOST_CHECK_THROW(kNN(points, 3u), std::invalid_argument);
}
}
|