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
|
/* 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 default_bfe_test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <initializer_list>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <pagmo/batch_evaluators/default_bfe.hpp>
#include <pagmo/batch_evaluators/member_bfe.hpp>
#include <pagmo/bfe.hpp>
#include <pagmo/problem.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/threading.hpp>
#include <pagmo/types.hpp>
using namespace pagmo;
// UDP which implements batch_fitness.
struct bf0 {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double batch_fitness(const vector_double &dvs) const
{
++s_counter;
return vector_double(dvs.size(), 1.);
}
static unsigned s_counter;
};
unsigned bf0::s_counter = 0;
// UDP without batch_fitness, but supporting thread_bfe.
struct bf1 {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
// UDP without batch_fitness, and not thread-safe.
struct bf2 {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
thread_safety get_thread_safety() const
{
return thread_safety::none;
}
std::string get_name() const
{
return "baffo";
}
};
BOOST_AUTO_TEST_CASE(basic_tests)
{
BOOST_CHECK(is_udbfe<default_bfe>::value);
bfe bfe0{};
BOOST_CHECK(bfe0.is<default_bfe>());
BOOST_CHECK(bfe0.get_name() == "Default batch fitness evaluator");
BOOST_CHECK(bfe0.get_extra_info().empty());
BOOST_CHECK_EQUAL(bfe0.get_thread_safety(), thread_safety::basic);
// Use UDP member function.
problem p{bf0{}};
BOOST_CHECK(p.get_fevals() == 0u);
BOOST_CHECK(bfe0(p, {1., 2., 3.}) == vector_double(3, 1.));
// Check fevals counters.
BOOST_CHECK(p.get_fevals() == 3u);
BOOST_CHECK(bf0::s_counter == 1u);
// UDP without batch_fitness() member function, but supporting thread_bfe.
p = problem{bf1{}};
BOOST_CHECK(p.get_fevals() == 0u);
BOOST_CHECK(bfe0(p, {1., 2., 3.}) == vector_double(3, 0.));
// Check fevals counters.
BOOST_CHECK(p.get_fevals() == 3u);
// UDP without batch_fitness() member function, which is not thread-safe enough.
p = problem{bf2{}};
BOOST_CHECK(p.get_thread_safety() == thread_safety::none);
BOOST_CHECK_EXCEPTION(bfe0(p, {1.}), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"Cannot execute fitness evaluations in batch mode for a problem of type 'baffo': the "
"problem does not implement the batch_fitness() member function, and its thread safety "
"level is not sufficient to run a thread-based batch fitness evaluation implementation");
});
}
BOOST_AUTO_TEST_CASE(s11n)
{
bfe bfe0{default_bfe{}};
BOOST_CHECK(bfe0.is<default_bfe>());
// Store the string representation.
std::stringstream ss;
auto before = boost::lexical_cast<std::string>(bfe0);
// Now serialize, deserialize and compare the result.
{
boost::archive::binary_oarchive oarchive(ss);
oarchive << bfe0;
}
// Change the content of p before deserializing.
bfe0 = bfe{member_bfe{}};
BOOST_CHECK(!bfe0.is<default_bfe>());
{
boost::archive::binary_iarchive iarchive(ss);
iarchive >> bfe0;
}
auto after = boost::lexical_cast<std::string>(bfe0);
BOOST_CHECK_EQUAL(before, after);
BOOST_CHECK(bfe0.is<default_bfe>());
}
|