Optimist
0.0.0
A C++ library for optimization
Toggle main menu visibility
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
16
#include "
Optimist/TestSet/DenseFunction.hh
"
17
18
namespace
Optimist
{
19
20
namespace
TestSet {
21
36
template
<
typename
Vector, Integer N>
37
class
Rastrigin
38
:
public
DenseCostFunction
<Vector, N, Rastrigin<Vector, N>> {
39
public
:
40
using
Base
=
DenseCostFunction<Vector, N, Rastrigin<Vector, N>
>;
41
using
typename
Base::Dense
;
42
using
typename
Base::DenseMatrix
;
43
using
typename
Base::Scalar
;
44
48
Rastrigin
() {
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>
91
using
Rastrigin2
=
Rastrigin<Vector, 2>
;
92
93
}
// namespace TestSet
94
95
}
// namespace Optimist
96
97
#endif
// OPTIMIST_TESTSET_RASTRIGIN_HH
DenseFunction.hh
Optimist::TestSet::DenseCostFunction
Helper base class for scalar cost functions written once in terms of dense vectors.
Definition
DenseFunction.hh:43
Optimist::TestSet::DenseCostFunction< Vector, N, Rastrigin< Vector, N > >::add_solution
void add_solution(const Dense &x)
Definition
DenseFunction.hh:94
Optimist::TestSet::DenseCostFunction< Vector, N, Rastrigin< Vector, N > >::DenseMatrix
Utils::DenseMatrix< Vector > DenseMatrix
Definition
DenseFunction.hh:48
Optimist::TestSet::DenseCostFunction< Vector, N, Rastrigin< Vector, N > >::add_guess
void add_guess(const Dense &x)
Definition
DenseFunction.hh:101
Optimist::TestSet::DenseCostFunction< Vector, N, Rastrigin< Vector, N > >::Dense
Utils::DenseVector< Vector > Dense
Definition
DenseFunction.hh:47
Optimist::TestSet::DenseCostFunction< Vector, N, Rastrigin< Vector, N > >::Scalar
typename Vector::Scalar Scalar
Definition
DenseFunction.hh:46
Optimist::TestSet::Rastrigin
Rastrigin's function.
Definition
Rastrigin.hh:38
Optimist::TestSet::Rastrigin::hessian
void hessian(const Dense &x, DenseMatrix &H) const
Definition
Rastrigin.hh:82
Optimist::TestSet::Rastrigin::Rastrigin
Rastrigin()
Definition
Rastrigin.hh:48
Optimist::TestSet::Rastrigin::Base
DenseCostFunction< Vector, N, Rastrigin< Vector, N > > Base
Definition
Rastrigin.hh:40
Optimist::TestSet::Rastrigin::value
Scalar value(const Dense &x) const
Definition
Rastrigin.hh:72
Optimist::TestSet::Rastrigin::gradient
void gradient(const Dense &x, Dense &g) const
Definition
Rastrigin.hh:77
Optimist::TestSet::Rastrigin::domain
static constexpr Scalar domain()
Definition
Rastrigin.hh:68
Optimist::TestSet::Rastrigin::name_impl
constexpr std::string name_impl() const
Definition
Rastrigin.hh:61
Optimist::TestSet::Rastrigin2
Rastrigin< Vector, 2 > Rastrigin2
Definition
Rastrigin.hh:91
Optimist
Namespace for the Optimist library.
Definition
Optimist.hh:90
Optimist::Integer
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition
Optimist.hh:98
core
include
Optimist
TestSet
Rastrigin.hh
Generated by
1.18.0