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
|
// Copyright (C) 2013 by Thomas Moulard, AIST, CNRS.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of 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.
//
// roboptim 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.
#include "common.hh"
namespace roboptim
{
namespace schittkowski
{
namespace problem1
{
struct ExpectedResult
{
static const double f0;
static const double x[];
static const double fx;
};
const double ExpectedResult::f0 = 909.;
const double ExpectedResult::x[] = {1., 1.};
const double ExpectedResult::fx = 0.;
template <typename T>
class F : public GenericDifferentiableFunction<T>
{
public:
ROBOPTIM_DIFFERENTIABLE_FUNCTION_FWD_TYPEDEFS
(GenericDifferentiableFunction<T>);
explicit F () throw ();
void
impl_compute (result_t& result, const argument_t& x) const throw ();
void
impl_gradient (gradient_t& grad, const argument_t& x, size_type)
const throw ();
};
template <typename T>
F<T>::F () throw ()
: GenericDifferentiableFunction<T>
(2, 1, "100 (x₁ - x₀²)² + (1 - x₀)²")
{}
template <typename T>
void
F<T>::impl_compute (result_t& result, const argument_t& x)
const throw ()
{
result[0] = 100 * std::pow (x[1] - std::pow (x[0], 2), 2)
+ std::pow (1 - x[0], 2);
}
template <>
void
F<EigenMatrixSparse>::impl_gradient
(gradient_t& grad, const argument_t& x, size_type)
const throw ()
{
grad.insert (0) =
-400 * x[0] * (x[1] - std::pow (x[0], 2)) - 2 * (1 - x[0]);
grad.insert (1) = 200 * (x[1] - std::pow (x[0], 2));
}
template <typename T>
void
F<T>::impl_gradient (gradient_t& grad, const argument_t& x, size_type)
const throw ()
{
grad[0] = -400 * x[0] * (x[1] - std::pow (x[0], 2)) - 2 * (1 - x[0]);
grad[1] = 200 * (x[1] - std::pow (x[0], 2));
}
} // end of namespace problem1.
} // end of namespace schittkowski.
} // end of namespace roboptim.
BOOST_FIXTURE_TEST_SUITE (schittkowski, TestSuiteConfiguration)
BOOST_AUTO_TEST_CASE (schittkowski_problem1)
{
using namespace roboptim;
using namespace roboptim::schittkowski::problem1;
// Build problem.
F<functionType_t> f;
solver_t::problem_t problem (f);
problem.argumentBounds ()[1] = F<functionType_t>::makeLowerInterval (-1.5);
F<functionType_t>::argument_t x (2);
x << -2., 1.;
problem.startingPoint () = x;
BOOST_CHECK_CLOSE (f (x)[0], ExpectedResult::f0, 1e-6);
std::cout << f.inputSize () << std::endl;
std::cout << problem.function ().inputSize () << std::endl;
// Initialize solver.
SolverFactory<solver_t> factory (SOLVER_NAME, problem);
solver_t& solver = factory ();
std::cout << f.inputSize () << std::endl;
std::cout << problem.function ().inputSize () << std::endl;
// Compute the minimum and retrieve the result.
solver_t::result_t res = solver.minimum ();
std::cout << f.inputSize () << std::endl;
std::cout << problem.function ().inputSize () << std::endl;
// Display solver information.
std::cout << solver << std::endl;
// Check if the minimization has succeed.
if (res.which () != solver_t::SOLVER_VALUE)
{
std::cout << "A solution should have been found. Failing..."
<< std::endl
<< boost::get<SolverError> (res).what ()
<< std::endl;
BOOST_CHECK_EQUAL (res.which (), solver_t::SOLVER_VALUE);
return;
}
// Get the result.
Result& result = boost::get<Result> (res);
// Check final x.
for (unsigned i = 0; i < result.x.size (); ++i)
BOOST_CHECK_CLOSE (result.x[i], ExpectedResult::x[i], 1e-6);
// Check final value.
BOOST_CHECK_CLOSE (1. + result.value[0], 1. + ExpectedResult::fx, 1e-6);
// Display the result.
std::cout << "A solution has been found: " << std::endl
<< result << std::endl;
}
BOOST_AUTO_TEST_SUITE_END ()
|