Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Rosenbrock.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_TESTSET_ROSENBROCK_HH
14#define OPTIMIST_TESTSET_ROSENBROCK_HH
15
16#include "Optimist/Function.hh"
17
18namespace Optimist {
19
20 namespace TestSet {
21
41 template <typename Vector, Integer N>
42 requires(N % 2 == 0) && TypeTrait<Vector>::IsEigen &&
43 (!TypeTrait<Vector>::IsFixed || TypeTrait<Vector>::Dimension == N)
44 class Rosenbrock : public Function<Vector, Vector, Rosenbrock<Vector, N>> {
45 public:
47 using Scalar = typename Vector::Scalar;
49 FirstDerivative;
51 SecondDerivative;
52
54
55
59 this->m_solutions.resize(1);
60 this->m_guesses.resize(1);
61 if constexpr (VectorTrait::IsFixed) {
62 this->m_solutions[0].setConstant(1.0);
63 for (Integer i{0}; i < N; i += 2) {
64 this->m_guesses[0](i) = -1.2;
65 this->m_guesses[0](i + 1) = 1.0;
66 }
67 } else if constexpr (VectorTrait::IsDynamic) {
68 this->m_solutions[0].resize(N);
69 this->m_solutions[0].setConstant(1.0);
70 this->m_guesses[0].resize(N);
71 for (Integer i{0}; i < N; i += 2) {
72 this->m_guesses[0](i) = -1.2;
73 this->m_guesses[0](i + 1) = 1.0;
74 }
75 } else if constexpr (VectorTrait::IsSparse) {
76 this->m_solutions[0].resize(N);
77 this->m_solutions[0].reserve(N);
78 for (Integer i{0}; i < N; ++i) {
79 this->m_solutions[0].coeffRef(i) = 1.0;
80 }
81 this->m_guesses[0].resize(N);
82 this->m_guesses[0].reserve(N);
83 for (Integer i{0}; i < N; i += 2) {
84 this->m_guesses[0].coeffRef(i) = -1.2;
85 this->m_guesses[0].coeffRef(i + 1) = 1.0;
86 }
87 }
88 }
89
94 constexpr std::string name_impl() const {
95 return "Rosenbrock<" + std::to_string(N) + ">";
96 }
97
104 bool evaluate_impl(const Vector &x, Vector &out) const {
105#define CMD \
106 "Optimist::TestSet::Rosenbrock<" + std::to_string(N) + \
107 ">::evaluate_impl(...): "
108
109 if constexpr (VectorTrait::IsFixed) {
110 for (Integer i{0}; i < N; i += 2) {
111 out(i) = 10.0 * (x(i + 1) - x(i) * x(i));
112 out(i + 1) = 1.0 - x(i);
113 }
114 return out.allFinite();
115 } else if constexpr (VectorTrait::IsDynamic) {
116 out.resize(N);
117 for (Integer i{0}; i < N; i += 2) {
118 out(i) = 10.0 * (x(i + 1) - x(i) * x(i));
119 out(i + 1) = 1.0 - x(i);
120 }
121 return out.allFinite();
122 } else if constexpr (VectorTrait::IsSparse) {
123 out.resize(N);
124 out.reserve(N);
125 for (Integer i{0}; i < N; i += 2) {
126 out.coeffRef(i) = 10.0 * (x.coeff(i + 1) - x.coeff(i) * x.coeff(i));
127 out.coeffRef(i + 1) = 1.0 - x.coeff(i);
128 }
129 for (typename Vector::InnerIterator it(out); it; ++it) {
130 if (!std::isfinite(it.value())) {
131 return false;
132 }
133 }
134 return true;
135 } else {
136 OPTIMIST_ERROR(CMD "input type not supported.");
137 return false;
138 }
139
140#undef CMD
141 }
142
149 bool first_derivative_impl(const Vector &x, FirstDerivative &out) const {
150#define CMD \
151 "Optimist::TestSet::Rosenbrock<" + std::to_string(N) + \
152 ">::first_derivative_impl(...): "
153
154 if constexpr (VectorTrait::IsFixed) {
155 out.setZero();
156 for (Integer i{0}; i < N; i += 2) {
157 out(i, i) = -20.0 * x(i);
158 out(i, i + 1) = 10.0;
159 out(i + 1, i) = -1.0;
160 }
161 return out.allFinite();
162 } else if constexpr (VectorTrait::IsDynamic) {
163 out.resize(N, N);
164 out.setZero();
165 for (Integer i{0}; i < N; i += 2) {
166 out(i, i) = -20.0 * x(i);
167 out(i, i + 1) = 10.0;
168 out(i + 1, i) = -1.0;
169 }
170 return out.allFinite();
171 } else if constexpr (VectorTrait::IsSparse) {
172 out.resize(N, N);
173 out.reserve(N * N);
174 for (Integer i{0}; i < N; i += 2) {
175 out.coeffRef(i, i) = -20.0 * x.coeff(i);
176 out.coeffRef(i, i + 1) = 10.0;
177 out.coeffRef(i + 1, i) = -1.0;
178 }
179 for (Integer k{0}; k < out.outerSize(); ++k) {
180 for (typename FirstDerivative::InnerIterator it(out, k); it; ++it) {
181 if (!std::isfinite(it.value())) {
182 return false;
183 }
184 }
185 }
186 return true;
187 } else {
188 OPTIMIST_ERROR(CMD "input type not supported.");
189 return false;
190 }
191
192#undef CMD
193 }
194
201 bool second_derivative_impl(const Vector & /*x*/,
202 SecondDerivative &out) const {
203#define CMD \
204 "Optimist::TestSet::Rosenbrock<" + std::to_string(N) + \
205 ">::second_derivative_impl(...): "
206
207 out.resize(N);
208 std::for_each(out.begin(), out.end(), [](auto &m) {
209 if constexpr (VectorTrait::IsFixed) {
210 m.setZero();
211 for (Integer j{0}; j < N; j += 2) {
212 m(j, j) = -20.0;
213 }
214 } else if constexpr (VectorTrait::IsDynamic) {
215 m.resize(N, N);
216 m.setZero();
217 for (Integer j{0}; j < N; j += 2) {
218 m(j, j) = -20.0;
219 }
220 } else if constexpr (VectorTrait::IsSparse) {
221 m.resize(N, N);
222 m.setZero();
223 for (Integer j{0}; j < N; j += 2) {
224 m.coeffRef(j, j) = -20.0;
225 }
226 } else {
227 OPTIMIST_ERROR(CMD "input type not supported.");
228 }
229 });
230 return true;
231
232#undef CMD
233 }
234
235 }; // class Rosenbrock
236
241 template <typename Vector>
243
248 template <typename Vector>
250
255 template <typename Vector>
257
262 template <typename Vector>
264
269 template <typename Vector>
271
272 } // namespace TestSet
273
274} // namespace Optimist
275
276#endif // OPTIMIST_TESTSET_ROSENBROCK_HH
#define OPTIMIST_ERROR(MSG)
Definition Optimist.hh:38
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
#define CMD
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, std::vector< Eigen::SparseMatrix< Scalar > >, std::vector< Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > > >, Scalar > SecondDerivative
Definition Function.hh:64
std::vector< Vector > m_guesses
Definition Function.hh:79
std::vector< Vector > m_solutions
Definition Function.hh:77
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > >, Scalar > FirstDerivative
Definition Function.hh:56
Class container for the extended Rosenbrock function.
Definition Rosenbrock.hh:44
TypeTrait< Vector > VectorTrait
Definition Rosenbrock.hh:46
constexpr std::string name_impl() const
Definition Rosenbrock.hh:94
bool second_derivative_impl(const Vector &, SecondDerivative &out) const
Definition Rosenbrock.hh:201
Rosenbrock()
Definition Rosenbrock.hh:58
bool evaluate_impl(const Vector &x, Vector &out) const
Definition Rosenbrock.hh:104
bool first_derivative_impl(const Vector &x, FirstDerivative &out) const
Definition Rosenbrock.hh:149
typename Vector::Scalar Scalar
Definition Rosenbrock.hh:47
Rosenbrock< Vector, 6 > Rosenbrock6
Class container for the 6D Rosenbrock function.
Definition Rosenbrock.hh:256
Rosenbrock< Vector, 2 > Rosenbrock2
Class container for the 2D Rosenbrock function.
Definition Rosenbrock.hh:242
Rosenbrock< Vector, 10 > Rosenbrock10
Class container for the 10D Rosenbrock function.
Definition Rosenbrock.hh:270
Rosenbrock< Vector, 8 > Rosenbrock8
Class container for the 8D Rosenbrock function.
Definition Rosenbrock.hh:263
Rosenbrock< Vector, 4 > Rosenbrock4
Class container for the 4D Rosenbrock function.
Definition Rosenbrock.hh:249
Namespace for the Optimist library.
Definition Optimist.hh:90
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98
Definition Optimist.hh:114