File: fully_connected.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 (284 lines) | stat: -rw-r--r-- 8,989 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
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
/* 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 fully_connected
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <utility>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/graph/adjacency_list.hpp>

#include <pagmo/s11n.hpp>
#include <pagmo/topologies/fully_connected.hpp>
#include <pagmo/topology.hpp>

using namespace pagmo;

void verify_fully_connected_topology(const fully_connected &f)
{
    const auto s = f.num_vertices();

    if (!s) {
        BOOST_CHECK_EXCEPTION(f.get_connections(0), std::invalid_argument, [](const std::invalid_argument &ia) {
            return boost::contains(ia.what(), "Cannot get the connections to the vertex at index 0 in a fully "
                                              "connected topology: the number of vertices in the topology is only 0");
        });

        return;
    }

    if (s == 1u) {
        BOOST_CHECK(f.get_connections(0).first.empty());
        BOOST_CHECK(f.get_connections(0).second.empty());

        BOOST_CHECK_EXCEPTION(f.get_connections(1), std::invalid_argument, [](const std::invalid_argument &ia) {
            return boost::contains(ia.what(), "Cannot get the connections to the vertex at index 1 in a fully "
                                              "connected topology: the number of vertices in the topology is only 1");
        });

        return;
    }

    const auto w = f.get_weight();

    for (std::size_t i = 0; i < s; ++i) {
        const auto conns = f.get_connections(i);

        BOOST_CHECK(conns.first.size() == s - 1u);
        BOOST_CHECK(conns.second.size() == s - 1u);

        BOOST_CHECK(std::all_of(conns.second.begin(), conns.second.end(), [w](double x) { return x == w; }));

        for (std::size_t j = 0; j < i; ++j) {
            BOOST_CHECK(std::find(conns.first.begin(), conns.first.end(), j) != conns.first.end());
        }
        for (std::size_t j = i + 1u; j < s; ++j) {
            BOOST_CHECK(std::find(conns.first.begin(), conns.first.end(), j) != conns.first.end());
        }
    }
}

BOOST_AUTO_TEST_CASE(basic_test)
{
    {
        // Default construct, push back a few times.
        fully_connected r0;
        BOOST_CHECK(r0.get_weight() == 1);
        BOOST_CHECK(r0.num_vertices() == 0u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 1u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 2u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 3u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        r0.push_back();
        r0.push_back();
        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 7u);
        verify_fully_connected_topology(r0);
    }

    {
        // Ctor from weight.
        fully_connected r0(.2);
        BOOST_CHECK(r0.get_weight() == .2);
        BOOST_CHECK(r0.num_vertices() == 0u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 1u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 2u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 3u);
        verify_fully_connected_topology(r0);

        r0.push_back();
        r0.push_back();
        r0.push_back();
        r0.push_back();
        BOOST_CHECK(r0.num_vertices() == 7u);
        verify_fully_connected_topology(r0);
    }

    {
        // Ctor from nedges and weight.
        fully_connected r0(0, .2);
        BOOST_CHECK(r0.get_weight() == .2);
        BOOST_CHECK(r0.num_vertices() == 0u);
        verify_fully_connected_topology(r0);
    }

    {
        // Ctor from nedges and weight.
        fully_connected r0(7, .2);
        BOOST_CHECK(r0.get_weight() == .2);
        BOOST_CHECK(r0.num_vertices() == 7u);
        verify_fully_connected_topology(r0);
    }

    {
        // Copy/move ctors.
        fully_connected r0(7, .2), r1(r0), r2(std::move(r0));

        BOOST_CHECK(r1.get_weight() == .2);
        BOOST_CHECK(r1.num_vertices() == 7u);

        BOOST_CHECK(r1.get_weight() == .2);
        BOOST_CHECK(r2.num_vertices() == 7u);

        verify_fully_connected_topology(r1);
        verify_fully_connected_topology(r2);
    }

    {
        // Name/extra info.
        fully_connected r0(7, .2);

        BOOST_CHECK(r0.get_name() == "Fully connected");
        BOOST_CHECK(boost::contains(r0.get_extra_info(), "Edges' weight:"));

        std::cout << r0.get_extra_info() << '\n';
    }

    // Minimal serialization test.
    fully_connected r0(7, .2);
    {
        topology t0(r0);
        std::stringstream ss;
        {
            boost::archive::binary_oarchive oarchive(ss);
            oarchive << t0;
        }
        topology t1;
        BOOST_CHECK(!t1.is<fully_connected>());
        {
            boost::archive::binary_iarchive iarchive(ss);
            iarchive >> t1;
        }
        BOOST_CHECK(t1.is<fully_connected>());
        BOOST_CHECK(t1.extract<fully_connected>()->num_vertices() == 7u);
        BOOST_CHECK(t1.extract<fully_connected>()->get_weight() == .2);
        verify_fully_connected_topology(*t1.extract<fully_connected>());
    }
}

BOOST_AUTO_TEST_CASE(to_bgl_test)
{
    BOOST_CHECK(has_to_bgl<fully_connected>::value);

    auto g0 = fully_connected{}.to_bgl();
    BOOST_CHECK(boost::num_vertices(g0) == 0u);

    g0 = fully_connected{1, .5}.to_bgl();
    BOOST_CHECK(boost::num_vertices(g0) == 1u);
    auto vi = boost::vertex(0, g0);
    auto av = boost::adjacent_vertices(vi, g0);
    BOOST_CHECK(av.first == av.second);

    g0 = fully_connected{2, .5}.to_bgl();
    BOOST_CHECK(boost::num_vertices(g0) == 2u);

    vi = boost::vertex(0, g0);
    av = boost::adjacent_vertices(vi, g0);
    auto e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 1);
    BOOST_CHECK(g0[e.first] == .5);
    BOOST_CHECK(++av.first == av.second);

    vi = boost::vertex(1, g0);
    av = boost::adjacent_vertices(vi, g0);
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 0);
    BOOST_CHECK(g0[e.first] == .5);
    BOOST_CHECK(++av.first == av.second);

    g0 = fully_connected{3, .5}.to_bgl();
    BOOST_CHECK(boost::num_vertices(g0) == 3u);

    vi = boost::vertex(0, g0);
    av = boost::adjacent_vertices(vi, g0);
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 1 || *av.first == 2);
    BOOST_CHECK(g0[e.first] == .5);
    ++av.first;
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 1 || *av.first == 2);
    BOOST_CHECK(g0[e.first] == .5);
    BOOST_CHECK(++av.first == av.second);

    vi = boost::vertex(1, g0);
    av = boost::adjacent_vertices(vi, g0);
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 0 || *av.first == 2);
    BOOST_CHECK(g0[e.first] == .5);
    ++av.first;
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 0 || *av.first == 2);
    BOOST_CHECK(g0[e.first] == .5);
    BOOST_CHECK(++av.first == av.second);

    vi = boost::vertex(2, g0);
    av = boost::adjacent_vertices(vi, g0);
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 0 || *av.first == 1);
    BOOST_CHECK(g0[e.first] == .5);
    ++av.first;
    e = boost::edge(boost::vertex(*av.first, g0), vi, g0);
    BOOST_CHECK(e.second);
    BOOST_CHECK(*av.first == 0 || *av.first == 1);
    BOOST_CHECK(g0[e.first] == .5);
    BOOST_CHECK(++av.first == av.second);
}