Loading [MathJax]/extensions/tex2jax.js
Optimist  0.0.0
A C++ library for optimization
All Classes Namespaces Files Functions Variables Typedefs Macros Pages
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_COST_FUNCTION_SCHAFFER2_HH
14#define OPTIMIST_COST_FUNCTION_SCHAFFER2_HH
15
16#include "Optimist/TestSet.hh"
18
19namespace Optimist
20{
21
22 namespace TestSet
23 {
24
36 template <typename Real>
37 class Schaffer2 : public CostFunction<Real, 2, Schaffer2<Real>>
38 {
39 public:
43
45
46
50 {
51 this->m_solutions.emplace_back(0.0, 0.0);
52 for (Real x{-100}; x < 100 + EPSILON; x += 100/25.0) {
53 for (Real y{-100}; y < 100 + EPSILON; y += 100/25.0) {
54 this->m_guesses.emplace_back(x, y);
55 }
56 }
57 }
58
63 std::string name_impl() const {return "Schaffer2";}
64
70 void evaluate_impl(const Vector & x, Real & out) const
71 {
72 Real xx_0{x(0)*x(0)};
73 Real xx_1{x(1)*x(1)};
74 Real xx_0_m_xx_1{xx_0 - xx_1};
75 Real xx_0_p_xx_1{xx_0 + xx_1};
76 out = 0.5 + (std::sin(xx_0_m_xx_1)*std::sin(xx_0_m_xx_1) - 0.5) /
77 ((1.0 + 0.001*(xx_0_p_xx_1))*(1.0 + 0.001*(xx_0_p_xx_1)));
78 }
79
85 void first_derivative_impl(const Vector & x, RowVector & out) const
86 {
87 Real xx_0{x(0)*x(0)};
88 Real xx_1{x(1)*x(1)};
89 Real xx_0_m_xx_1{xx_0 - xx_1};
90 Real xx_0_p_xx_1{xx_0 + xx_1};
91 Real tmp{1.0 + 0.001*(xx_0_p_xx_1)};
92 Real tmp2{tmp*tmp}, tmp3{tmp2*tmp};
93 out(0) = 2.0*x(0)*std::sin(xx_0_m_xx_1) / tmp2 -
94 2.0*0.001*x(0)*std::cos(xx_0_m_xx_1) / tmp3;
95 out(1) = -2.0*x(1)*std::sin(xx_0_m_xx_1) / tmp2 +
96 2.0*0.001*x(1)*std::cos(xx_0_m_xx_1) / tmp3;
97 }
98
104 void second_derivative_impl(const Vector & x, Matrix & out) const
105 {
106 Real xx_0{x(0)*x(0)};
107 Real xx_1{x(1)*x(1)};
108 Real xx_0_m_xx_1{xx_0 - xx_1};
109 Real xx_0_p_xx_1{xx_0 + xx_1};
110 Real tmp{1.0 + 0.001*(xx_0_p_xx_1)};
111 Real tmp2{tmp*tmp}, tmp3{tmp2*tmp}, tmp4{tmp2*tmp2};
112 out(0, 0) = 2.0*std::sin(xx_0_m_xx_1) / tmp2 -
113 4.0*x(0)*x(0)*std::sin(xx_0_m_xx_1) / tmp3 +
114 6.0*0.001*x(0)*x(0)*std::cos(xx_0_m_xx_1) / tmp4;
115 out(0, 1) = -2.0*x(0)*x(1)*std::sin(xx_0_m_xx_1) / tmp3 +
116 6.0*0.001*x(0)*x(1)*std::cos(xx_0_m_xx_1) / tmp4;
117 out(1, 0) = out(0, 1);
118 out(1, 1) = 2.0*std::sin(xx_0_m_xx_1) / tmp2 -
119 4.0*x(1)*x(1)*std::sin(xx_0_m_xx_1) / tmp3 +
120 6.0*0.001*x(1)*x(1)*std::cos(xx_0_m_xx_1) / tmp4;
121 }
122
123 }; // class Schaffer2
124
125 } // namespace TestSet
126
127} // namespace Optimist
128
129#endif // OPTIMIST_COST_FUNCTION_SCHAFFER2_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
std::vector< InputType > m_solutions
Definition Function.hh:52
std::vector< InputType > m_guesses
Definition Function.hh:53
void first_derivative_impl(const Vector &x, RowVector &out) const
Definition Schaffer2.hh:85
typename CostFunction< Real, 2, Schaffer2< Real > >::Matrix Matrix
Definition Schaffer2.hh:42
void evaluate_impl(const Vector &x, Real &out) const
Definition Schaffer2.hh:70
void second_derivative_impl(const Vector &x, Matrix &out) const
Definition Schaffer2.hh:104
typename CostFunction< Real, 2, Schaffer2< Real > >::Vector Vector
Definition Schaffer2.hh:40
std::string name_impl() const
Definition Schaffer2.hh:63
typename CostFunction< Real, 2, Schaffer2< Real > >::RowVector RowVector
Definition Schaffer2.hh:41
Schaffer2()
Definition Schaffer2.hh:49
Namespace for the Optimist library.
Definition Optimist.hh:87