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_VECTOR_FUNCTION_ROSENBROCK_HH
14#define OPTIMIST_VECTOR_FUNCTION_ROSENBROCK_HH
15
16#include "Optimist/TestSet.hh"
18
19namespace Optimist
20{
21
22 namespace TestSet
23 {
24
42 template <typename Real, Integer N>
43 class Rosenbrock : public VectorFunction<Real, N, N, Rosenbrock<Real, N>>
44 {
45 static_assert(N > 0 && N % 2 == 0, "please use an even number of dimensions");
46
47 public:
49
50 using Vector = typename VectorFunction<Real, N, N, Rosenbrock<Real, N>>::InputVector;
51 using Matrix = typename VectorFunction<Real, N, N, Rosenbrock<Real, N>>::Matrix;
52 using Tensor = typename VectorFunction<Real, N, N, Rosenbrock<Real, N>>::Tensor;
53
58 {
59 this->m_solutions.emplace_back(Vector::Ones());
60 this->m_guesses.emplace_back(Vector::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
78 void evaluate_impl(const Vector & x, Vector & out) const
79 {
80 for (Integer i{0}; i < N; i += 2) {
81 out(i) = 10.0*(x(i+1) - x(i)*x(i));
82 out(i+1) = 1.0 - x(i);
83 }
84 }
85
90 void first_derivative_impl(const Vector & x, Matrix & out) const
91 {
92 out.setZero();
93 for (Integer i{0}; i < N; i += 2) {
94 out(i, i) = -20.0*x(i);
95 out(i, i+1) = 10.0;
96 out(i+1, i) = -1.0;
97 }
98 }
99
105 void second_derivative_impl(const Vector & /*x*/, Tensor & out) const
106 {
107 out.resize(this->output_dimension());
108 for (Integer i{0}; i < static_cast<Integer>(out.size()); ++i) {
109 out[i].setZero();
110 for (Integer j{0}; j < N; j += 2) {
111 out[i](j, j) = -20.0;
112 }
113 }
114 }
115
116 }; // class Rosenbrock
117
122 template <typename Real>
124
129 template <typename Real>
131
136 template <typename Real>
138
143 template <typename Real>
145
150 template <typename Real>
152
153
154 } // namespace TestSet
155
156} // namespace Optimist
157
158#endif // OPTIMIST_VECTOR_FUNCTION_ROSENBROCK_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
std::vector< InputType > m_solutions
Definition Function.hh:52
std::vector< InputType > m_guesses
Definition Function.hh:53
constexpr Integer output_dimension() const
Definition Function.hh:107
Class container for the extended Rosenbrock function.
Definition Rosenbrock.hh:44
void second_derivative_impl(const Vector &, Tensor &out) const
Definition Rosenbrock.hh:105
void first_derivative_impl(const Vector &x, Matrix &out) const
Definition Rosenbrock.hh:90
typename VectorFunction< Real, N, N, Rosenbrock< Real, N > >::InputVector Vector
Definition Rosenbrock.hh:50
Rosenbrock()
Definition Rosenbrock.hh:57
std::string name_impl() const
Definition Rosenbrock.hh:71
void evaluate_impl(const Vector &x, Vector &out) const
Definition Rosenbrock.hh:78
typename VectorFunction< Real, N, N, Rosenbrock< Real, N > >::Tensor Tensor
Definition Rosenbrock.hh:52
typename VectorFunction< Real, N, N, Rosenbrock< Real, N > >::Matrix Matrix
Definition Rosenbrock.hh:51
typename Function< Real, N, M, Rosenbrock< Real, N > >::InputType InputVector
Definition VectorFunction.hh:45
Rosenbrock< Real, 2 > Rosenbrock2
Class container for the 2D Rosenbrock function.
Definition Rosenbrock.hh:123
Rosenbrock< Real, 4 > Rosenbrock4
Class container for the 4D Rosenbrock function.
Definition Rosenbrock.hh:130
Rosenbrock< Real, 8 > Rosenbrock8
Class container for the 8D Rosenbrock function.
Definition Rosenbrock.hh:144
Rosenbrock< Real, 6 > Rosenbrock6
Class container for the 6D Rosenbrock function.
Definition Rosenbrock.hh:137
Rosenbrock< Real, 10 > Rosenbrock10
Class container for the 10D Rosenbrock function.
Definition Rosenbrock.hh:151
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