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
49 using Vector = typename Function<Real, N, N, Rosenbrock<Real, N>>::InputVector;
50 using Matrix = typename Function<Real, N, N, Rosenbrock<Real, N>>::Matrix;
51 using Tensor = typename Function<Real, N, N, Rosenbrock<Real, N>>::Tensor;
52
57 {
58 this->m_solutions.emplace_back(Vector::Ones());
59 this->m_guesses.emplace_back(Vector::Ones());
60 for (Integer i{0}; i < N; i += 2) {
61 this->m_guesses[0](i) = -1.2;
62 this->m_guesses[0](i+1) = 1.0;
63 }
64 }
65
70 std::string name_impl() const {return "Rosenbrock<" + std::to_string(N) + ">";}
71
77 void evaluate_impl(const Vector & x, Vector & out) const
78 {
79 for (Integer i{0}; i < N; i += 2) {
80 out(i) = 10.0*(x(i+1) - x(i)*x(i));
81 out(i+1) = 1.0 - x(i);
82 }
83 }
84
89 void first_derivative_impl(const Vector & x, Matrix & out) const
90 {
91 out.setZero();
92 for (Integer i{0}; i < N; i += 2) {
93 out(i, i) = -20.0*x(i);
94 out(i, i+1) = 10.0;
95 out(i+1, i) = -1.0;
96 }
97 }
98
104 void second_derivative_impl(const Vector & /*x*/, Tensor & out) const
105 {
106 out.resize(this->output_dimension());
107 for (Integer i{0}; i < static_cast<Integer>(out.size()); ++i) {
108 out[i].setZero();
109 for (Integer j{0}; j < N; j += 2) {
110 out[i](j, j) = -20.0;
111 }
112 }
113 }
114
115 }; // class Rosenbrock
116
121 template <typename Real>
123
128 template <typename Real>
130
135 template <typename Real>
137
142 template <typename Real>
144
149 template <typename Real>
151
152
153 } // namespace TestSet
154
155} // namespace Optimist
156
157#endif // OPTIMIST_TESTSET_ROSENBROCK_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
std::vector< InputType > m_guesses
Definition Function.hh:63
std::vector< InputType > m_solutions
Definition Function.hh:62
constexpr Integer output_dimension() const
Definition Function.hh:117
typename FunctionBase< Real, N, M, Rosenbrock< Real, N >, false >::InputType InputVector
Definition Function.hh:193
Class container for the extended Rosenbrock function.
Definition Rosenbrock.hh:43
void second_derivative_impl(const Vector &, Tensor &out) const
Definition Rosenbrock.hh:104
typename Function< Real, N, N, Rosenbrock< Real, N > >::Tensor Tensor
Definition Rosenbrock.hh:51
void first_derivative_impl(const Vector &x, Matrix &out) const
Definition Rosenbrock.hh:89
Rosenbrock()
Definition Rosenbrock.hh:56
std::string name_impl() const
Definition Rosenbrock.hh:70
void evaluate_impl(const Vector &x, Vector &out) const
Definition Rosenbrock.hh:77
typename Function< Real, N, N, Rosenbrock< Real, N > >::InputVector Vector
Definition Rosenbrock.hh:49
typename Function< Real, N, N, Rosenbrock< Real, N > >::Matrix Matrix
Definition Rosenbrock.hh:50
Rosenbrock< Real, 2 > Rosenbrock2
Class container for the 2D Rosenbrock function.
Definition Rosenbrock.hh:122
Rosenbrock< Real, 4 > Rosenbrock4
Class container for the 4D Rosenbrock function.
Definition Rosenbrock.hh:129
Rosenbrock< Real, 8 > Rosenbrock8
Class container for the 8D Rosenbrock function.
Definition Rosenbrock.hh:143
Rosenbrock< Real, 6 > Rosenbrock6
Class container for the 6D Rosenbrock function.
Definition Rosenbrock.hh:136
Rosenbrock< Real, 10 > Rosenbrock10
Class container for the 10D Rosenbrock function.
Definition Rosenbrock.hh:150
Namespace for the Optimist library.
Definition Optimist.hh:87
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:95