Optimist
0.0.0
A C++ library for optimization
Toggle main menu visibility
Loading...
Searching...
No Matches
Ackley.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_ACKLEY_HH
14
#define OPTIMIST_TESTSET_ACKLEY_HH
15
16
#include <unsupported/Eigen/AutoDiff>
17
18
#include "
Optimist/TestSet/DenseFunction.hh
"
19
20
namespace
Optimist
{
21
22
namespace
TestSet {
23
41
template
<
typename
Vector, Integer N>
42
class
Ackley
:
public
DenseCostFunction
<Vector, N, Ackley<Vector, N>> {
43
public
:
44
using
Base
=
DenseCostFunction<Vector, N, Ackley<Vector, N>
>;
45
using
typename
Base::Dense
;
46
using
typename
Base::DenseMatrix
;
47
using
typename
Base::Scalar
;
48
52
Ackley
() {
53
this->
add_solution
(Dense::Zero(N));
54
// Off-diagonal guesses: on the diagonal the gradient points straight
55
// at the global minimizer
56
Dense
x0(N), x1(N);
57
for
(
Integer
i{0}; i < N; ++i) {
58
x0(i) = i % 2 == 0 ? 2.5 : -1.3;
59
x1(i) = i % 2 == 0 ? -3.5 : 3.2;
60
}
61
this->
add_guess
(x0);
62
this->
add_guess
(x1);
63
}
64
65
constexpr
std::string
name_impl
()
const
{
66
return
"Ackley<"
+ std::to_string(N) +
">"
;
67
}
68
72
static
constexpr
Scalar
domain
() {
73
return
5.0;
74
}
75
76
Scalar
value
(
const
Dense
&x)
const
{
77
const
Scalar
n{
static_cast<
Scalar
>
(N)};
78
const
Scalar
w{2 *
static_cast<
Scalar
>
(M_PI)};
79
const
Scalar
r{std::sqrt(x.squaredNorm() / n)};
80
const
Scalar
c{(w * x.array()).cos().sum() / n};
81
return
-20 * std::exp(-r / 5) - std::exp(c) + 20 +
82
static_cast<
Scalar
>
(M_E);
83
}
84
85
void
gradient
(
const
Dense
&x,
Dense
&g)
const
{
86
g =
gradient_t
(x);
87
}
88
89
void
hessian
(
const
Dense
&x,
DenseMatrix
&H)
const
{
90
// Seed one derivative direction per coordinate and differentiate the
91
// gradient: H(:, j) = d(grad f)/dx_j
92
using
AD = Eigen::AutoDiffScalar<Dense>;
93
Eigen::Matrix<AD, Eigen::Dynamic, 1> x_ad(N);
94
for
(
Integer
j{0}; j < N; ++j) {
95
x_ad(j) = AD(x(j), N, j);
96
}
97
const
Eigen::Matrix<AD, Eigen::Dynamic, 1> g_ad{
gradient_t
(x_ad)};
98
for
(
Integer
i{0}; i < N; ++i) {
99
H.row(i) = g_ad(i).derivatives().transpose();
100
}
101
H = (0.5 * (H + H.transpose())).eval();
102
}
103
104
private
:
109
template
<
typename
T>
110
static
Eigen::Matrix<T, Eigen::Dynamic, 1>
gradient_t
(
111
const
Eigen::Matrix<T, Eigen::Dynamic, 1> &x) {
112
using
std::cos;
113
using
std::exp;
114
using
std::sin;
115
using
std::sqrt;
116
const
Scalar
n{
static_cast<
Scalar
>
(N)};
117
const
Scalar
w{2 *
static_cast<
Scalar
>
(M_PI)};
118
T r2{x(0) * x(0)}, c{cos(w * x(0))};
119
for
(
Integer
i{1}; i < N; ++i) {
120
r2 += x(i) * x(i);
121
c += cos(w * x(i));
122
}
123
r2 /= n;
124
c /= n;
125
Eigen::Matrix<T, Eigen::Dynamic, 1> g(N);
126
const
T e_c{exp(c)};
127
const
bool
origin{r2 ==
Scalar
(0)};
128
const
T r{origin ? T(
Scalar
(1)) : T(sqrt(r2))};
129
const
T a{origin ? T(
Scalar
(0)) : T(4 * exp(-r / 5) / (n * r))};
130
for
(
Integer
i{0}; i < N; ++i) {
131
g(i) = e_c * (w / n) * sin(w * x(i)) + a * x(i);
132
}
133
return
g;
134
}
135
136
};
// class Ackley
137
138
template
<
typename
Vector>
139
using
Ackley2
=
Ackley<Vector, 2>
;
140
141
}
// namespace TestSet
142
143
}
// namespace Optimist
144
145
#endif
// OPTIMIST_TESTSET_ACKLEY_HH
DenseFunction.hh
Optimist::TestSet::Ackley
Ackley's function.
Definition
Ackley.hh:42
Optimist::TestSet::Ackley::Base
DenseCostFunction< Vector, N, Ackley< Vector, N > > Base
Definition
Ackley.hh:44
Optimist::TestSet::Ackley< Vector, 2 >::gradient_t
static Eigen::Matrix< T, Eigen::Dynamic, 1 > gradient_t(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Definition
Ackley.hh:110
Optimist::TestSet::Ackley::name_impl
constexpr std::string name_impl() const
Definition
Ackley.hh:65
Optimist::TestSet::Ackley< Vector, 2 >::Dense
Utils::DenseVector< Vector > Dense
Optimist::TestSet::Ackley::Ackley
Ackley()
Definition
Ackley.hh:52
Optimist::TestSet::Ackley::value
Scalar value(const Dense &x) const
Definition
Ackley.hh:76
Optimist::TestSet::Ackley::hessian
void hessian(const Dense &x, DenseMatrix &H) const
Definition
Ackley.hh:89
Optimist::TestSet::Ackley::domain
static constexpr Scalar domain()
Definition
Ackley.hh:72
Optimist::TestSet::Ackley< Vector, 2 >::DenseMatrix
Utils::DenseMatrix< Vector > DenseMatrix
Optimist::TestSet::Ackley::gradient
void gradient(const Dense &x, Dense &g) const
Definition
Ackley.hh:85
Optimist::TestSet::Ackley< Vector, 2 >::Scalar
typename Vector::Scalar Scalar
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, Ackley< Vector, N > >::add_solution
void add_solution(const Dense &x)
Definition
DenseFunction.hh:94
Optimist::TestSet::DenseCostFunction< Vector, N, Ackley< Vector, N > >::DenseMatrix
Utils::DenseMatrix< Vector > DenseMatrix
Definition
DenseFunction.hh:48
Optimist::TestSet::DenseCostFunction< Vector, N, Ackley< Vector, N > >::add_guess
void add_guess(const Dense &x)
Definition
DenseFunction.hh:101
Optimist::TestSet::DenseCostFunction< Vector, N, Ackley< Vector, N > >::Dense
Utils::DenseVector< Vector > Dense
Definition
DenseFunction.hh:47
Optimist::TestSet::DenseCostFunction< Vector, N, Ackley< Vector, N > >::Scalar
typename Vector::Scalar Scalar
Definition
DenseFunction.hh:46
Optimist::TestSet::Ackley2
Ackley< Vector, 2 > Ackley2
Definition
Ackley.hh:139
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
Ackley.hh
Generated by
1.18.0