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 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_SCHAFFER2_HH
14#define OPTIMIST_TESTSET_SCHAFFER2_HH
15
16#include "Optimist/Function.hh"
17
18namespace Optimist {
19
20 namespace TestSet {
21
35 template <typename Vector>
36 requires TypeTrait<Vector>::IsEigen && (!TypeTrait<Vector>::IsFixed ||
37 TypeTrait<Vector>::Dimension == 2)
39 : public Function<Vector, typename Vector::Scalar, Schaffer2<Vector>> {
40 public:
42 using Scalar = typename Vector::Scalar;
43 using typename Function<Vector,
44 typename Vector::Scalar,
46 using typename Function<Vector,
47 typename Vector::Scalar,
49
51
52
56 this->m_solutions.resize(1);
57 this->m_guesses.resize(2);
58 this->m_solutions.resize(1);
59 this->m_guesses.resize(2);
60 if constexpr (VectorTrait::IsFixed) {
61 this->m_solutions[0] << 0.0, 0.0;
62 this->m_guesses[0] << -10, -10;
63 this->m_guesses[1] << 10, 10;
64 } else if constexpr (!VectorTrait::IsSparse) {
65 this->m_solutions[0].resize(2);
66 this->m_solutions[0] << 0.0, 0.0;
67 this->m_guesses[0].resize(2);
68 this->m_guesses[0] << -10, -10;
69 this->m_guesses[1].resize(2);
70 this->m_guesses[1] << 10, 10;
71 } else if constexpr (VectorTrait::IsSparse) {
72 this->m_solutions[0].resize(2);
73 this->m_solutions[0].reserve(2);
74 this->m_solutions[0].coeffRef(0) = 0.0;
75 this->m_solutions[0].coeffRef(1) = 0.0;
76 this->m_guesses[0].resize(2);
77 this->m_guesses[0].reserve(2);
78 this->m_guesses[0].coeffRef(0) = -10;
79 this->m_guesses[0].coeffRef(1) = -10;
80 this->m_guesses[1].resize(2);
81 this->m_guesses[1].reserve(2);
82 this->m_guesses[1].coeffRef(0) = 10;
83 this->m_guesses[1].coeffRef(1) = 10;
84 }
85 }
86
91 constexpr std::string name_impl() const {
92 return "Schaffer2";
93 }
94
101 bool evaluate_impl(const Vector &x, Scalar &out) const {
102 Scalar xx_0, xx_1;
103 if constexpr (VectorTrait::IsSparse) {
104 xx_0 = x.coeff(0) * x.coeff(0);
105 xx_1 = x.coeff(1) * x.coeff(1);
106 } else {
107 xx_0 = x(0) * x(0);
108 xx_1 = x(1) * x(1);
109 }
110 Scalar xx_0_m_xx_1{xx_0 - xx_1};
111 Scalar xx_0_p_xx_1{xx_0 + xx_1};
112 out = 0.5 + (std::sin(xx_0_m_xx_1) * std::sin(xx_0_m_xx_1) - 0.5) /
113 ((1.0 + 0.001 * (xx_0_p_xx_1)) *
114 (1.0 + 0.001 * (xx_0_p_xx_1)));
115 return std::isfinite(out);
116 }
117
124 bool first_derivative_impl(const Vector &x, FirstDerivative &out) const {
125 Scalar xx_0, xx_1;
126 if constexpr (VectorTrait::IsSparse) {
127 xx_0 = x.coeff(0) * x.coeff(0);
128 xx_1 = x.coeff(1) * x.coeff(1);
129 } else {
130 xx_0 = x(0) * x(0);
131 xx_1 = x(1) * x(1);
132 }
133 Scalar xx_0_m_xx_1{xx_0 - xx_1};
134 Scalar xx_0_p_xx_1{xx_0 + xx_1};
135 Scalar tmp{static_cast<Scalar>(1.0) +
136 static_cast<Scalar>(0.001) * xx_0_p_xx_1};
137 Scalar tmp2{tmp * tmp}, tmp3{tmp2 * tmp};
138 if constexpr (VectorTrait::IsFixed) {
139 out << 2.0 * x(0) * std::sin(xx_0_m_xx_1) / tmp2 -
140 2.0 * 0.001 * x(0) * std::cos(xx_0_m_xx_1) / tmp3,
141 -2.0 * x(1) * std::sin(xx_0_m_xx_1) / tmp2 +
142 2.0 * 0.001 * x(1) * std::cos(xx_0_m_xx_1) / tmp3;
143 } else if constexpr (VectorTrait::IsDynamic) {
144 out.resize(2);
145 out << 2.0 * x(0) * std::sin(xx_0_m_xx_1) / tmp2 -
146 2.0 * 0.001 * x(0) * std::cos(xx_0_m_xx_1) / tmp3,
147 -2.0 * x(1) * std::sin(xx_0_m_xx_1) / tmp2 +
148 2.0 * 0.001 * x(1) * std::cos(xx_0_m_xx_1) / tmp3;
149 } else if constexpr (VectorTrait::IsSparse) {
150 out.resize(2);
151 out.reserve(2);
152 out.coeffRef(0) = +2.0 * x(0) * std::sin(xx_0_m_xx_1) / tmp2 -
153 2.0 * 0.001 * x(0) * std::cos(xx_0_m_xx_1) / tmp3;
154 out.coeffRef(1) = -2.0 * x(1) * std::sin(xx_0_m_xx_1) / tmp2 +
155 2.0 * 0.001 * x(1) * std::cos(xx_0_m_xx_1) / tmp3;
156 }
157
158 return out.allFinite();
159 }
160
167 bool second_derivative_impl(const Vector &x,
168 SecondDerivative &out) const {
169 Scalar xx_0, xx_1;
170 if constexpr (VectorTrait::IsSparse) {
171 xx_0 = x.coeff(0) * x.coeff(0);
172 xx_1 = x.coeff(1) * x.coeff(1);
173 } else {
174 xx_0 = x(0) * x(0);
175 xx_1 = x(1) * x(1);
176 }
177 Scalar xx_0_m_xx_1{xx_0 - xx_1};
178 Scalar xx_0_p_xx_1{xx_0 + xx_1};
179 Scalar tmp{static_cast<Scalar>(1.0) +
180 static_cast<Scalar>(0.001) * (xx_0_p_xx_1)};
181 Scalar tmp2{tmp * tmp}, tmp3{tmp2 * tmp}, tmp4{tmp2 * tmp2};
182 if constexpr (VectorTrait::IsFixed) {
183 out.resize(2, 2);
184 out(0, 0) = 2.0 * std::sin(xx_0_m_xx_1) / tmp2 -
185 4.0 * x(0) * x(0) * std::sin(xx_0_m_xx_1) / tmp3 +
186 6.0 * 0.001 * x(0) * x(0) * std::cos(xx_0_m_xx_1) / tmp4;
187 out(0, 1) = -2.0 * x(0) * x(1) * std::sin(xx_0_m_xx_1) / tmp3 +
188 6.0 * 0.001 * x(0) * x(1) * std::cos(xx_0_m_xx_1) / tmp4;
189 out(1, 0) = out(0, 1);
190 out(1, 1) = 2.0 * std::sin(xx_0_m_xx_1) / tmp2 -
191 4.0 * x(1) * x(1) * std::sin(xx_0_m_xx_1) / tmp3 +
192 6.0 * 0.001 * x(1) * x(1) * std::cos(xx_0_m_xx_1) / tmp4;
193 } else if constexpr (VectorTrait::IsDynamic) {
194 out.resize(2, 2);
195 out(0, 0) = 2.0 * std::sin(xx_0_m_xx_1) / tmp2 -
196 4.0 * x(0) * x(0) * std::sin(xx_0_m_xx_1) / tmp3 +
197 6.0 * 0.001 * x(0) * x(0) * std::cos(xx_0_m_xx_1) / tmp4;
198 out(0, 1) = -2.0 * x(0) * x(1) * std::sin(xx_0_m_xx_1) / tmp3 +
199 6.0 * 0.001 * x(0) * x(1) * std::cos(xx_0_m_xx_1) / tmp4;
200 out(1, 0) = out(0, 1);
201 out(1, 1) = 2.0 * std::sin(xx_0_m_xx_1) / tmp2 -
202 4.0 * x(1) * x(1) * std::sin(xx_0_m_xx_1) / tmp3 +
203 6.0 * 0.001 * x(1) * x(1) * std::cos(xx_0_m_xx_1) / tmp4;
204 } else if constexpr (VectorTrait::IsSparse) {
205 out.resize(2, 2);
206 out.reserve(4);
207 out.coeffRef(0, 0) =
208 2.0 * std::sin(xx_0_m_xx_1) / tmp2 -
209 4.0 * x(0) * x(0) * std::sin(xx_0_m_xx_1) / tmp3 +
210 6.0 * 0.001 * x(0) * x(0) * std::cos(xx_0_m_xx_1) / tmp4;
211 out.coeffRef(0, 1) =
212 -2.0 * x.coeff(0) * x.coeff(1) * std::sin(xx_0_m_xx_1) / tmp3 +
213 6.0 * 0.001 * x.coeff(0) * x.coeff(1) * std::cos(xx_0_m_xx_1) /
214 tmp4;
215 out.coeffRef(1, 0) = out.coeff(0, 1);
216 out.coeffRef(1, 1) =
217 2.0 * std::sin(xx_0_m_xx_1) / tmp2 -
218 4.0 * x.coeff(1) * x.coeff(1) * std::sin(xx_0_m_xx_1) / tmp3 +
219 6.0 * 0.001 * x.coeff(1) * x.coeff(1) * std::cos(xx_0_m_xx_1) /
220 tmp4;
221 }
222
223 return out.allFinite();
224 }
225
226 }; // class Schaffer2
227
228 } // namespace TestSet
229
230} // namespace Optimist
231
232#endif // OPTIMIST_TESTSET_SCHAFFER2_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, std::vector< Eigen::SparseMatrix< Scalar > >, std::vector< Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > > >, Scalar > SecondDerivative
Definition Function.hh:64
std::vector< Vector > m_guesses
Definition Function.hh:79
std::vector< Vector > m_solutions
Definition Function.hh:77
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > >, Scalar > FirstDerivative
Definition Function.hh:56
bool first_derivative_impl(const Vector &x, FirstDerivative &out) const
Definition Schaffer2.hh:124
TypeTrait< Vector > VectorTrait
Definition Schaffer2.hh:41
typename Vector::Scalar Scalar
Definition Schaffer2.hh:42
constexpr std::string name_impl() const
Definition Schaffer2.hh:91
bool evaluate_impl(const Vector &x, Scalar &out) const
Definition Schaffer2.hh:101
Schaffer2()
Definition Schaffer2.hh:55
bool second_derivative_impl(const Vector &x, SecondDerivative &out) const
Definition Schaffer2.hh:167
Namespace for the Optimist library.
Definition Optimist.hh:90
Definition Optimist.hh:114