File: fair_replace.cpp

package info (click to toggle)
pagmo 2.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 85,228 kB
  • sloc: cpp: 1,753,592; makefile: 223; sh: 121; python: 46
file content (245 lines) | stat: -rw-r--r-- 9,996 bytes parent folder | download | duplicates (2)
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
/* 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 fair_replace_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/r_policies/fair_replace.hpp>
#include <pagmo/r_policy.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/types.hpp>

using namespace pagmo;

BOOST_AUTO_TEST_CASE(fair_replace_basic)
{
    fair_replace f00;
    BOOST_CHECK(f00.get_migr_rate().which() == 0);
    BOOST_CHECK(boost::get<pop_size_t>(f00.get_migr_rate()) == 1u);

    fair_replace f01(.2);
    BOOST_CHECK(f01.get_migr_rate().which() == 1);
    BOOST_CHECK(boost::get<double>(f01.get_migr_rate()) == .2);

    fair_replace 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 = fair_replace(-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 = fair_replace(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 = fair_replace(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 = fair_replace(-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() == "Fair replace");
    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.
    {
        r_policy r0(f04);

        std::stringstream ss;
        {
            boost::archive::binary_oarchive oarchive(ss);
            oarchive << r0;
        }
        r_policy r1;
        {
            boost::archive::binary_iarchive iarchive(ss);
            iarchive >> r1;
        }
        BOOST_CHECK(r1.is<fair_replace>());
        BOOST_CHECK(r1.extract<fair_replace>()->get_migr_rate().which() == 0);
        BOOST_CHECK(boost::get<pop_size_t>(r1.extract<fair_replace>()->get_migr_rate()) == 2u);
    }
}

BOOST_AUTO_TEST_CASE(fair_replace_replace)
{
    fair_replace f00;

    BOOST_CHECK_EXCEPTION(f00.replace(individuals_group_t{}, 0, 0, 2, 1, 0, vector_double{}, individuals_group_t{}),
                          std::invalid_argument, [](const std::invalid_argument &ia) {
                              return boost::contains(ia.what(),
                                                     "The 'fair_replace' replacement policy is unable to deal with "
                                                     "multiobjective constrained optimisation problems");
                          });

    f00 = fair_replace(100);

    BOOST_CHECK_EXCEPTION(f00.replace(individuals_group_t{}, 0, 0, 1, 0, 0, vector_double{}, individuals_group_t{}),
                          std::invalid_argument, [](const std::invalid_argument &ia) {
                              return boost::contains(
                                  ia.what(), "The absolute migration rate (100) in a 'fair_replace' replacement policy "
                                             "is larger than the number of input individuals (0)");
                          });

    // Single-objective, unconstrained.
    f00 = fair_replace(.1);

    individuals_group_t inds{{1, 2, 3}, {{0}, {0}, {0}}, {{1}, {2}, {3}}};
    individuals_group_t mig{{4, 5, 6}, {{0}, {0}, {0}}, {{0.1}, {0.2}, {0.3}}};

    // Too few individuals in inds for fractional migration, no migration will happen.
    auto new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);
    BOOST_CHECK(new_inds == inds);

    // Top mig replaces the worst in inds.
    f00 = fair_replace(0.5);
    new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);
    BOOST_CHECK((new_inds == individuals_group_t{{4, 1, 2}, {{0}, {0}, {0}}, {{0.1}, {1}, {2}}}));

    // All replaced.
    f00 = fair_replace(1.);
    new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);
    BOOST_CHECK((new_inds == individuals_group_t{{4, 5, 6}, {{0}, {0}, {0}}, {{0.1}, {0.2}, {0.3}}}));

    // Absolute rate, no replacement.
    f00 = fair_replace(0);
    new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);
    BOOST_CHECK(new_inds == inds);

    // Absolute rate, replace 2.
    f00 = fair_replace(2);
    new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);
    BOOST_CHECK((new_inds == individuals_group_t{{4, 5, 1}, {{0}, {0}, {0}}, {{0.1}, {0.2}, {1}}}));

    // Absolute rate, replace all.
    f00 = fair_replace(3);
    new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);
    BOOST_CHECK((new_inds == individuals_group_t{{4, 5, 6}, {{0}, {0}, {0}}, {{0.1}, {0.2}, {0.3}}}));

    // Single-objective, constrained.
    inds = individuals_group_t{{1, 2, 3}, {{0}, {0}, {0}}, {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}};
    mig = individuals_group_t{{4, 5, 6}, {{0}, {0}, {0}}, {{0.1, 0.1, 0.1}, {0.2, 0.2, 0.2}, {0.3, 0.3, 0.3}}};
    f00 = fair_replace(.1);

    // Too few individuals in inds for fractional migration, no migration will happen.
    new_inds = f00.replace(inds, 1, 0, 1, 1, 1, {0., 0.}, mig);
    BOOST_CHECK(new_inds == inds);

    // Top mig replaces the worst in inds.
    f00 = fair_replace(0.5);
    new_inds = f00.replace(inds, 1, 0, 1, 1, 1, {0., 0.}, mig);
    BOOST_CHECK((new_inds == individuals_group_t{{4, 1, 2}, {{0}, {0}, {0}}, {{0.1, 0.1, 0.1}, {1, 1, 1}, {2, 2, 2}}}));

    // All replaced.
    f00 = fair_replace(1.);
    new_inds = f00.replace(inds, 1, 0, 1, 1, 1, {0., 0.}, mig);
    BOOST_CHECK(
        (new_inds
         == individuals_group_t{{4, 5, 6}, {{0}, {0}, {0}}, {{0.1, 0.1, 0.1}, {0.2, 0.2, 0.2}, {0.3, 0.3, 0.3}}}));

    // Absolute rate, no replacement.
    f00 = fair_replace(0);
    new_inds = f00.replace(inds, 1, 0, 1, 1, 1, {0., 0.}, mig);
    BOOST_CHECK(new_inds == inds);

    // Absolute rate, replace 2.
    f00 = fair_replace(2);
    new_inds = f00.replace(inds, 1, 0, 1, 1, 1, {0., 0.}, mig);
    BOOST_CHECK(
        (new_inds == individuals_group_t{{4, 5, 1}, {{0}, {0}, {0}}, {{0.1, 0.1, 0.1}, {0.2, 0.2, 0.2}, {1, 1, 1}}}));

    // Absolute rate, replace all.
    f00 = fair_replace(3);
    new_inds = f00.replace(inds, 1, 0, 1, 1, 1, {0., 0.}, mig);
    BOOST_CHECK(
        (new_inds
         == individuals_group_t{{4, 5, 6}, {{0}, {0}, {0}}, {{0.1, 0.1, 0.1}, {0.2, 0.2, 0.2}, {0.3, 0.3, 0.3}}}));

    // 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}}};
    mig = individuals_group_t{
        {6, 7, 8, 9, 10, 11}, {{0}, {0}, {0}, {0}, {0}, {0}}, {{10, 0}, {2, 6}, {4, 4}, {10, 2}, {6, 6}, {9, 5}}};
    f00 = fair_replace(1.);

    new_inds = f00.replace(inds, 1, 0, 2, 0, 0, {}, mig);
    BOOST_CHECK((
        new_inds
        == individuals_group_t{{1, 6, 5, 4, 2}, {{0}, {0}, {0}, {0}, {0}}, {{0, 7}, {10, 0}, {7, 1}, {4, 2}, {1, 5}}}));
}

// Check behaviour when, in unconstrained
// single-objective optimisation problems,
// the fitness is nan.
BOOST_AUTO_TEST_CASE(fair_replace_nan_fitness)
{
    fair_replace f00(1);

    individuals_group_t inds{{1, 2, 3}, {{0}, {0}, {0}}, {{1}, {2}, {3}}};
    individuals_group_t mig{
        {4, 5}, {{0}, {0}}, {{std::numeric_limits<double>::quiet_NaN()}, {std::numeric_limits<double>::quiet_NaN()}}};

    auto new_inds = f00.replace(inds, 1, 0, 1, 0, 0, {}, mig);

    // No individual was replaced, because the migrant has nan fitness.
    BOOST_CHECK(inds == new_inds);
}