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
|
/* 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 hock_schittkowski_test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <pagmo/problems/hock_schittkowski_71.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/types.hpp>
using namespace pagmo;
BOOST_AUTO_TEST_CASE(hock_schittkowski_71_test)
{
// Problem instantiation
problem p{hock_schittkowski_71{}};
// Pick a few reference points
vector_double x1 = {1., 1., 1., 1.};
vector_double x2 = {2., 2., 2., 2.};
// Fitness test
BOOST_CHECK((p.fitness(x1) == vector_double{4, -36, 24}));
BOOST_CHECK((p.fitness(x2) == vector_double{26, -24, 9}));
// Gradient test
BOOST_CHECK((p.gradient(x1) == vector_double{4, 1, 2, 3, 2, 2, 2, 2, -1, -1, -1, -1}));
BOOST_CHECK((p.gradient(x2) == vector_double{16, 4, 5, 12, 4, 4, 4, 4, -8, -8, -8, -8}));
// Hessians test
auto hess1 = p.hessians(x1);
BOOST_CHECK(hess1.size() == 3);
BOOST_CHECK((hess1[0] == vector_double{2, 1, 1, 4, 1, 1}));
BOOST_CHECK((hess1[1] == vector_double{2, 2, 2, 2}));
BOOST_CHECK((hess1[2] == vector_double{-1, -1, -1, -1, -1, -1}));
// Hessians sparsity test
auto sp = p.hessians_sparsity();
BOOST_CHECK(sp.size() == 3);
BOOST_CHECK((sp[0] == sparsity_pattern{{0, 0}, {1, 0}, {2, 0}, {3, 0}, {3, 1}, {3, 2}}));
BOOST_CHECK((sp[1] == sparsity_pattern{{0, 0}, {1, 1}, {2, 2}, {3, 3}}));
BOOST_CHECK((sp[2] == sparsity_pattern{{1, 0}, {2, 0}, {2, 1}, {3, 0}, {3, 1}, {3, 2}}));
// Name and extra info tests
BOOST_CHECK(p.get_name().find("Schittkowski") != std::string::npos);
BOOST_CHECK(p.get_extra_info().find("Schittkowski") != std::string::npos);
// Best known test
auto x_best = p.extract<hock_schittkowski_71>()->best_known();
BOOST_CHECK_CLOSE(x_best[0], 1, 1e-13);
BOOST_CHECK_CLOSE(x_best[1], 4.74299963, 1e-13);
BOOST_CHECK_CLOSE(x_best[2], 3.82114998, 1e-13);
BOOST_CHECK_CLOSE(x_best[3], 1.37940829, 1e-13);
}
BOOST_AUTO_TEST_CASE(hock_schittkowski_71_serialization_test)
{
problem p{hock_schittkowski_71{}};
// Call objfun, grad and hess to increase
// the internal counters.
p.fitness({1., 1., 1., 1.});
p.gradient({1., 1., 1., 1.});
p.hessians({1., 1., 1., 1.});
// Store the string representation of p.
std::stringstream ss;
auto before = boost::lexical_cast<std::string>(p);
// Now serialize, deserialize and compare the result.
{
boost::archive::binary_oarchive oarchive(ss);
oarchive << p;
}
// Change the content of p before deserializing.
p = problem{};
{
boost::archive::binary_iarchive iarchive(ss);
iarchive >> p;
}
auto after = boost::lexical_cast<std::string>(p);
BOOST_CHECK_EQUAL(before, after);
}
|