1 /* Copyright 2017-2021 PaGMO development team
2 
3 This file is part of the PaGMO library.
4 
5 The PaGMO library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8   * the GNU Lesser General Public License as published by the Free
9     Software Foundation; either version 3 of the License, or (at your
10     option) any later version.
11 
12 or
13 
14   * the GNU General Public License as published by the Free Software
15     Foundation; either version 3 of the License, or (at your option) any
16     later version.
17 
18 or both in parallel, as here.
19 
20 The PaGMO library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the PaGMO library.  If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #define BOOST_TEST_MODULE schwefel_test
30 #define BOOST_TEST_DYN_LINK
31 #include <boost/test/unit_test.hpp>
32 
33 #include <boost/lexical_cast.hpp>
34 #include <iostream>
35 #include <stdexcept>
36 #include <string>
37 
38 #include <pagmo/problem.hpp>
39 #include <pagmo/problems/schwefel.hpp>
40 #include <pagmo/types.hpp>
41 
42 using namespace pagmo;
43 
BOOST_AUTO_TEST_CASE(schwefel_test)44 BOOST_AUTO_TEST_CASE(schwefel_test)
45 {
46     // Problem construction
47     schwefel sch1{1u};
48     schwefel sch3{3u};
49     BOOST_CHECK_THROW(schwefel{0u}, std::invalid_argument);
50     BOOST_CHECK_NO_THROW(problem{sch3});
51     // Pick a few reference points
52     vector_double x1 = {1.12};
53     vector_double x3 = {-23.45, 12.34, 111.12};
54     // Fitness test
55     BOOST_CHECK_CLOSE(sch1.fitness(x1)[0], 418.0067810680098, 1e-13);
56     BOOST_CHECK_CLOSE(sch1.fitness(x3)[0], 1338.0260195323838, 1e-13);
57     // Bounds Test
58     BOOST_CHECK((sch3.get_bounds() == std::pair<vector_double, vector_double>{{-500, -500, -500}, {500, 500, 500}}));
59     // Name and extra info tests
60     BOOST_CHECK(sch3.get_name().find("Schwefel") != std::string::npos);
61     // Best known test
62     auto x_best = sch3.best_known();
63     BOOST_CHECK((x_best == vector_double{420.9687, 420.9687, 420.9687}));
64 }
65 
BOOST_AUTO_TEST_CASE(schwefel_serialization_test)66 BOOST_AUTO_TEST_CASE(schwefel_serialization_test)
67 {
68     problem p{schwefel{4u}};
69     // Call objfun to increase the internal counters.
70     p.fitness({1., 1., 1., 1.});
71     // Store the string representation of p.
72     std::stringstream ss;
73     auto before = boost::lexical_cast<std::string>(p);
74     // Now serialize, deserialize and compare the result.
75     {
76         boost::archive::binary_oarchive oarchive(ss);
77         oarchive << p;
78     }
79     // Change the content of p before deserializing.
80     p = problem{};
81     {
82         boost::archive::binary_iarchive iarchive(ss);
83         iarchive >> p;
84     }
85     auto after = boost::lexical_cast<std::string>(p);
86     BOOST_CHECK_EQUAL(before, after);
87 }
88