-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathrosenbrock.cpp
More file actions
112 lines (93 loc) · 3.06 KB
/
rosenbrock.cpp
File metadata and controls
112 lines (93 loc) · 3.06 KB
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
/* 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/. */
#include <initializer_list>
#include <stdexcept>
#include <string>
#include <utility>
#include <pagmo/exceptions.hpp>
#include <pagmo/problem.hpp>
#include <pagmo/problems/rosenbrock.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/types.hpp>
namespace pagmo
{
rosenbrock::rosenbrock(vector_double::size_type dim) : m_dim(dim)
{
if (dim < 2u) {
pagmo_throw(std::invalid_argument,
"Rosenbrock Function must have minimum 2 dimensions, " + std::to_string(dim) + " requested");
}
}
/// Fitness computation
/**
* Computes the fitness for this UDP.
*
* @param x the decision vector.
*
* @return the fitness of \p x.
*/
vector_double rosenbrock::fitness(const vector_double &x) const
{
double retval = 0.;
for (decltype(m_dim) i = 0u; i < m_dim - 1u; ++i) {
retval += 100. * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1]) + (x[i] - 1) * (x[i] - 1);
}
return {retval};
}
/// Box-bounds
/**
* @return the lower (-5.) and upper (10.) bounds for each decision vector component.
*/
std::pair<vector_double, vector_double> rosenbrock::get_bounds() const
{
return {vector_double(m_dim, -5.), vector_double(m_dim, 10.)};
}
/// Gradient.
/**
* @param x the input decision vector.
*
* @return the gradient of the fitness function in \p x.
*/
vector_double rosenbrock::gradient(const vector_double &x) const
{
vector_double retval(m_dim);
retval[0] = -400. * x[0] * (x[1] - x[0] * x[0]) - 2. * (1 - x[0]);
for (unsigned i = 1; i < m_dim - 1u; ++i) {
retval[i] = -400. * x[i] * (x[i + 1u] - x[i] * x[i]) - 2. * (1 - x[i]) + 200. * (x[i] - x[i - 1u] * x[i - 1u]);
}
retval[m_dim - 1u] = 200. * (x[m_dim - 1u] - x[m_dim - 2u] * x[m_dim - 2u]);
return retval;
}
/// Optimal solution.
/**
* @return the decision vector corresponding to the best solution for this problem.
*/
vector_double rosenbrock::best_known() const
{
return vector_double(m_dim, 1.);
}
// Object serialization
template <typename Archive>
void rosenbrock::serialize(Archive &ar, unsigned)
{
ar &m_dim;
}
} // namespace pagmo
PAGMO_S11N_PROBLEM_IMPLEMENT(pagmo::rosenbrock)