Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
RosenbrockCost.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2026, 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_COST_HH
14#define OPTIMIST_TESTSET_ROSENBROCK_COST_HH
15
17
18namespace Optimist {
19
20 namespace TestSet {
21
38 template <typename Vector, Integer N>
39 requires(N >= 2)
41 : public DenseCostFunction<Vector, N, RosenbrockCost<Vector, N>> {
42 public:
44 using typename Base::Dense;
45 using typename Base::DenseMatrix;
46 using typename Base::Scalar;
47
52 this->add_solution(Dense::Ones(N));
53 // The classic (-1.2, 1, ..., 1) start is only used for N = 2: for
54 // 4 <= N <= 7 it leads to a genuine local minimizer near x_1 = -1
55 if constexpr (N == 2) {
56 this->add_guess(this->make({-1.2, 1.0}));
57 } else {
58 this->add_guess(Dense::Constant(N, -0.5));
59 }
60 this->add_guess(Dense::Zero(N));
61 this->add_guess(Dense::Constant(N, 2.0));
62 }
63
64 constexpr std::string name_impl() const {
65 return "RosenbrockCost<" + std::to_string(N) + ">";
66 }
67
68 Scalar value(const Dense &x) const {
69 Scalar f{0.0};
70 for (Integer i{0}; i < N - 1; ++i) {
71 const Scalar a{x(i + 1) - x(i) * x(i)}, b{1 - x(i)};
72 f += 100 * a * a + b * b;
73 }
74 return f;
75 }
76
77 void gradient(const Dense &x, Dense &g) const {
78 g.setZero();
79 for (Integer i{0}; i < N - 1; ++i) {
80 const Scalar a{x(i + 1) - x(i) * x(i)};
81 g(i) += -400 * x(i) * a - 2 * (1 - x(i));
82 g(i + 1) += 200 * a;
83 }
84 }
85
86 void hessian(const Dense &x, DenseMatrix &H) const {
87 H.setZero();
88 for (Integer i{0}; i < N - 1; ++i) {
89 H(i, i) += 1200 * x(i) * x(i) - 400 * x(i + 1) + 2;
90 H(i, i + 1) += -400 * x(i);
91 H(i + 1, i) += -400 * x(i);
92 H(i + 1, i + 1) += 200;
93 }
94 }
95
96 }; // class RosenbrockCost
97
98 template <typename Vector>
100
101 template <typename Vector>
103
104 } // namespace TestSet
105
106} // namespace Optimist
107
108#endif // OPTIMIST_TESTSET_ROSENBROCK_COST_HH
Helper base class for scalar cost functions written once in terms of dense vectors.
Definition DenseFunction.hh:43
void add_solution(const Dense &x)
Definition DenseFunction.hh:94
Utils::DenseMatrix< Vector > DenseMatrix
Definition DenseFunction.hh:48
void add_guess(const Dense &x)
Definition DenseFunction.hh:101
Utils::DenseVector< Vector > Dense
Definition DenseFunction.hh:47
static Dense make(std::initializer_list< Scalar > values)
Definition DenseFunction.hh:108
typename Vector::Scalar Scalar
Definition DenseFunction.hh:46
Rosenbrock's cost function in dimensions (chained form).
Definition RosenbrockCost.hh:41
Scalar value(const Dense &x) const
Definition RosenbrockCost.hh:68
constexpr std::string name_impl() const
Definition RosenbrockCost.hh:64
void gradient(const Dense &x, Dense &g) const
Definition RosenbrockCost.hh:77
DenseCostFunction< Vector, N, RosenbrockCost< Vector, N > > Base
Definition RosenbrockCost.hh:43
void hessian(const Dense &x, DenseMatrix &H) const
Definition RosenbrockCost.hh:86
RosenbrockCost()
Definition RosenbrockCost.hh:51
RosenbrockCost< Vector, 4 > RosenbrockCost4
Definition RosenbrockCost.hh:102
RosenbrockCost< Vector, 2 > RosenbrockCost2
Definition RosenbrockCost.hh:99
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