Optimist  0.0.0
A C++ library for optimization
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
19
20namespace Optimist {
21
22 namespace TestSet {
23
41 template <typename Vector, Integer N>
42 class Ackley : public DenseCostFunction<Vector, N, Ackley<Vector, N>> {
43 public:
45 using typename Base::Dense;
46 using typename Base::DenseMatrix;
47 using typename Base::Scalar;
48
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>
140
141 } // namespace TestSet
142
143} // namespace Optimist
144
145#endif // OPTIMIST_TESTSET_ACKLEY_HH
Ackley's function.
Definition Ackley.hh:42
DenseCostFunction< Vector, N, Ackley< Vector, N > > Base
Definition Ackley.hh:44
static Eigen::Matrix< T, Eigen::Dynamic, 1 > gradient_t(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &x)
Definition Ackley.hh:110
constexpr std::string name_impl() const
Definition Ackley.hh:65
Utils::DenseVector< Vector > Dense
Ackley()
Definition Ackley.hh:52
Scalar value(const Dense &x) const
Definition Ackley.hh:76
void hessian(const Dense &x, DenseMatrix &H) const
Definition Ackley.hh:89
static constexpr Scalar domain()
Definition Ackley.hh:72
Utils::DenseMatrix< Vector > DenseMatrix
void gradient(const Dense &x, Dense &g) const
Definition Ackley.hh:85
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
Ackley< Vector, 2 > Ackley2
Definition Ackley.hh:139
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