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, Mattia Piazza and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Mattia Piazza Enrico Bertolazzi *
7 * University of Trento University of Trento University of Trento *
8 * davide.stocco@unitn.it mattia.piazza@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/TestSet.hh"
17
18namespace Optimist
19{
20
21 namespace TestSet
22 {
23
41 template <typename Real, Integer N>
42 class Rosenbrock : public Function<Real, N, N, Rosenbrock<Real, N>>
43 {
44 static_assert(N > 0 && N % 2 == 0, "please use an even number of dimensions");
45
46 public:
48
53
58 {
59 this->m_solutions.emplace_back(OutputVector::Ones());
60 this->m_guesses.emplace_back(InputVector::Ones());
61 for (Integer i{0}; i < N; i += 2) {
62 this->m_guesses[0](i) = -1.2;
63 this->m_guesses[0](i+1) = 1.0;
64 }
65 }
66
71 std::string name_impl() const {return "Rosenbrock<" + std::to_string(N) + ">";}
72
79 bool evaluate_impl(InputVector const & x, OutputVector & out) const
80 {
81 for (Integer i{0}; i < N; i += 2) {
82 out(i) = 10.0*(x(i+1) - x(i)*x(i));
83 out(i+1) = 1.0 - x(i);
84 }
85 return out.allFinite();
86 }
87
94 bool first_derivative_impl(InputVector const & x, Matrix & out) const
95 {
96 out.setZero();
97 for (Integer i{0}; i < N; i += 2) {
98 out(i, i) = -20.0*x(i);
99 out(i, i+1) = 10.0;
100 out(i+1, i) = -1.0;
101 }
102 return out.allFinite();
103 }
104
111 bool second_derivative_impl(InputVector const & /*x*/, Tensor & out) const
112 {
113 out.resize(this->output_dimension());
114 for (size_t i{0}; i < out.size(); ++i) {
115 out[i].setZero();
116 for (Integer j{0}; j < N; j += 2) {
117 out[i](j, j) = -20.0;
118 }
119 }
120 return true;
121 }
122
123 }; // class Rosenbrock
124
129 template <typename Real>
131
136 template <typename Real>
138
143 template <typename Real>
145
150 template <typename Real>
152
157 template <typename Real>
159
160
161 } // namespace TestSet
162
163} // namespace Optimist
164
165#endif // OPTIMIST_TESTSET_ROSENBROCK_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:71
std::vector< InputType > m_guesses
Definition Function.hh:66
std::vector< InputType > m_solutions
Definition Function.hh:65
constexpr Integer output_dimension() const
Definition Function.hh:123
typename FunctionBase< Real, N, M, Rosenbrock< Real, N >, false >::OutputType OutputVector
Definition Function.hh:200
typename FunctionBase< Real, N, M, Rosenbrock< Real, N >, false >::InputType InputVector
Definition Function.hh:199
typename FunctionBase< Real, N, M, Rosenbrock< Real, N >, false >::FirstDerivativeType Matrix
Definition Function.hh:203
typename FunctionBase< Real, N, M, Rosenbrock< Real, N >, false >::SecondDerivativeType Tensor
Definition Function.hh:204
Class container for the extended Rosenbrock function.
Definition Rosenbrock.hh:43
bool second_derivative_impl(InputVector const &, Tensor &out) const
Definition Rosenbrock.hh:111
Rosenbrock()
Definition Rosenbrock.hh:57
std::string name_impl() const
Definition Rosenbrock.hh:71
bool first_derivative_impl(InputVector const &x, Matrix &out) const
Definition Rosenbrock.hh:94
bool evaluate_impl(InputVector const &x, OutputVector &out) const
Definition Rosenbrock.hh:79
Rosenbrock< Real, 2 > Rosenbrock2
Class container for the 2D Rosenbrock function.
Definition Rosenbrock.hh:130
Rosenbrock< Real, 4 > Rosenbrock4
Class container for the 4D Rosenbrock function.
Definition Rosenbrock.hh:137
Rosenbrock< Real, 8 > Rosenbrock8
Class container for the 8D Rosenbrock function.
Definition Rosenbrock.hh:151
Rosenbrock< Real, 6 > Rosenbrock6
Class container for the 6D Rosenbrock function.
Definition Rosenbrock.hh:144
Rosenbrock< Real, 10 > Rosenbrock10
Class container for the 10D Rosenbrock function.
Definition Rosenbrock.hh:158
Namespace for the Optimist library.
Definition Optimist.hh:88
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:96