Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Schaffer2.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco, Mattia Piazza and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Mattia Piazza Enrico Bertolazzi *
7 * University of Trento University of Trento University of Trento *
8 * davide.stocco@unitn.it mattia.piazza@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_TESTSET_SCHAFFER2_HH
14#define OPTIMIST_TESTSET_SCHAFFER2_HH
15
16#include "Optimist/TestSet.hh"
17
18namespace Optimist
19{
20
21 namespace TestSet
22 {
23
35 template <typename Real>
36 class Schaffer2 : public Function<Real, 2, 1, Schaffer2<Real>>
37 {
38 public:
42
44
45
49 {
50 this->m_solutions.emplace_back(0.0, 0.0);
51 for (Real x{-100}; x < 100 + EPSILON; x += 100/25.0) {
52 for (Real y{-100}; y < 100 + EPSILON; y += 100/25.0) {
53 this->m_guesses.emplace_back(x, y);
54 }
55 }
56 }
57
62 std::string name_impl() const {return "Schaffer2";}
63
69 void evaluate_impl(const Vector & x, Real & out) const
70 {
71 Real xx_0{x(0)*x(0)};
72 Real xx_1{x(1)*x(1)};
73 Real xx_0_m_xx_1{xx_0 - xx_1};
74 Real xx_0_p_xx_1{xx_0 + xx_1};
75 out = 0.5 + (std::sin(xx_0_m_xx_1)*std::sin(xx_0_m_xx_1) - 0.5) /
76 ((1.0 + 0.001*(xx_0_p_xx_1))*(1.0 + 0.001*(xx_0_p_xx_1)));
77 }
78
84 void first_derivative_impl(const Vector & x, RowVector & out) const
85 {
86 Real xx_0{x(0)*x(0)};
87 Real xx_1{x(1)*x(1)};
88 Real xx_0_m_xx_1{xx_0 - xx_1};
89 Real xx_0_p_xx_1{xx_0 + xx_1};
90 Real tmp{1.0 + 0.001*(xx_0_p_xx_1)};
91 Real tmp2{tmp*tmp}, tmp3{tmp2*tmp};
92 out(0) = 2.0*x(0)*std::sin(xx_0_m_xx_1) / tmp2 -
93 2.0*0.001*x(0)*std::cos(xx_0_m_xx_1) / tmp3;
94 out(1) = -2.0*x(1)*std::sin(xx_0_m_xx_1) / tmp2 +
95 2.0*0.001*x(1)*std::cos(xx_0_m_xx_1) / tmp3;
96 }
97
103 void second_derivative_impl(const Vector & x, Matrix & out) const
104 {
105 Real xx_0{x(0)*x(0)};
106 Real xx_1{x(1)*x(1)};
107 Real xx_0_m_xx_1{xx_0 - xx_1};
108 Real xx_0_p_xx_1{xx_0 + xx_1};
109 Real tmp{1.0 + 0.001*(xx_0_p_xx_1)};
110 Real tmp2{tmp*tmp}, tmp3{tmp2*tmp}, tmp4{tmp2*tmp2};
111 out(0, 0) = 2.0*std::sin(xx_0_m_xx_1) / tmp2 -
112 4.0*x(0)*x(0)*std::sin(xx_0_m_xx_1) / tmp3 +
113 6.0*0.001*x(0)*x(0)*std::cos(xx_0_m_xx_1) / tmp4;
114 out(0, 1) = -2.0*x(0)*x(1)*std::sin(xx_0_m_xx_1) / tmp3 +
115 6.0*0.001*x(0)*x(1)*std::cos(xx_0_m_xx_1) / tmp4;
116 out(1, 0) = out(0, 1);
117 out(1, 1) = 2.0*std::sin(xx_0_m_xx_1) / tmp2 -
118 4.0*x(1)*x(1)*std::sin(xx_0_m_xx_1) / tmp3 +
119 6.0*0.001*x(1)*x(1)*std::cos(xx_0_m_xx_1) / tmp4;
120 }
121
122 }; // class Schaffer2
123
124 } // namespace TestSet
125
126} // namespace Optimist
127
128#endif // OPTIMIST_TESTSET_SCHAFFER2_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
std::vector< InputType > m_guesses
Definition Function.hh:63
std::vector< InputType > m_solutions
Definition Function.hh:62
void first_derivative_impl(const Vector &x, RowVector &out) const
Definition Schaffer2.hh:84
typename Function< Real, 2, 1, Schaffer2< Real > >::RowVector RowVector
Definition Schaffer2.hh:40
void evaluate_impl(const Vector &x, Real &out) const
Definition Schaffer2.hh:69
void second_derivative_impl(const Vector &x, Matrix &out) const
Definition Schaffer2.hh:103
std::string name_impl() const
Definition Schaffer2.hh:62
typename Function< Real, 2, 1, Schaffer2< Real > >::Vector Vector
Definition Schaffer2.hh:39
typename Function< Real, 2, 1, Schaffer2< Real > >::Matrix Matrix
Definition Schaffer2.hh:41
Schaffer2()
Definition Schaffer2.hh:48
Namespace for the Optimist library.
Definition Optimist.hh:87