Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Rastrigin.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_RASTRIGIN_HH
14#define OPTIMIST_TESTSET_RASTRIGIN_HH
15
17
18namespace Optimist {
19
20 namespace TestSet {
21
36 template <typename Vector, Integer N>
38 : public DenseCostFunction<Vector, N, Rastrigin<Vector, N>> {
39 public:
41 using typename Base::Dense;
42 using typename Base::DenseMatrix;
43 using typename Base::Scalar;
44
49 this->add_solution(Dense::Zero(N));
50 // Off-diagonal guesses: on the diagonal the gradient points straight
51 // at the global minimizer
52 Dense x0(N), x1(N);
53 for (Integer i{0}; i < N; ++i) {
54 x0(i) = i % 2 == 0 ? 3.1 : -2.3;
55 x1(i) = i % 2 == 0 ? -4.2 : 1.7;
56 }
57 this->add_guess(x0);
58 this->add_guess(x1);
59 }
60
61 constexpr std::string name_impl() const {
62 return "Rastrigin<" + std::to_string(N) + ">";
63 }
64
68 static constexpr Scalar domain() {
69 return 5.12;
70 }
71
72 Scalar value(const Dense &x) const {
73 const Scalar w{2 * static_cast<Scalar>(M_PI)};
74 return 10 * N + (x.array().square() - 10 * (w * x.array()).cos()).sum();
75 }
76
77 void gradient(const Dense &x, Dense &g) const {
78 const Scalar w{2 * static_cast<Scalar>(M_PI)};
79 g = 2 * x.array() + 10 * w * (w * x.array()).sin();
80 }
81
82 void hessian(const Dense &x, DenseMatrix &H) const {
83 const Scalar w{2 * static_cast<Scalar>(M_PI)};
84 H.setZero();
85 H.diagonal() = 2 + 10 * w * w * (w * x.array()).cos();
86 }
87
88 }; // class Rastrigin
89
90 template <typename Vector>
92
93 } // namespace TestSet
94
95} // namespace Optimist
96
97#endif // OPTIMIST_TESTSET_RASTRIGIN_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
typename Vector::Scalar Scalar
Definition DenseFunction.hh:46
Rastrigin's function.
Definition Rastrigin.hh:38
void hessian(const Dense &x, DenseMatrix &H) const
Definition Rastrigin.hh:82
Rastrigin()
Definition Rastrigin.hh:48
DenseCostFunction< Vector, N, Rastrigin< Vector, N > > Base
Definition Rastrigin.hh:40
Scalar value(const Dense &x) const
Definition Rastrigin.hh:72
void gradient(const Dense &x, Dense &g) const
Definition Rastrigin.hh:77
static constexpr Scalar domain()
Definition Rastrigin.hh:68
constexpr std::string name_impl() const
Definition Rastrigin.hh:61
Rastrigin< Vector, 2 > Rastrigin2
Definition Rastrigin.hh:91
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