Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Booth.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_BOOTH_HH
14#define OPTIMIST_TESTSET_BOOTH_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)
38 class Booth : public Function<Vector, Vector, Booth<Vector>> {
39 public:
41 using Scalar = typename Vector::Scalar;
44
46
47
51 this->m_solutions.resize(1);
52 this->m_guesses.resize(2);
53 if constexpr (VectorTrait::IsFixed) {
54 this->m_solutions[0] << 1.0, 3.0;
55 this->m_guesses[0] << -10, -10;
56 this->m_guesses[1] << 10, 10;
57 } else if constexpr (VectorTrait::IsDynamic) {
58 this->m_solutions[0].resize(2);
59 this->m_solutions[0] << 1.0, 3.0;
60 this->m_guesses[0].resize(2);
61 this->m_guesses[0] << -10, -10;
62 this->m_guesses[1].resize(2);
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].reserve(2);
67 this->m_solutions[0].coeffRef(0) = 1.0;
68 this->m_solutions[0].coeffRef(1) = 3.0;
69 this->m_guesses[0].resize(2);
70 this->m_guesses[0].reserve(2);
71 this->m_guesses[0].coeffRef(0) = -10;
72 this->m_guesses[0].coeffRef(1) = -10;
73 this->m_guesses[1].resize(2);
74 this->m_guesses[1].reserve(2);
75 this->m_guesses[1].coeffRef(0) = 10;
76 this->m_guesses[1].coeffRef(1) = 10;
77 }
78 }
79
84 constexpr std::string name_impl() const {
85 return "Booth";
86 }
87
94 bool evaluate_impl(const Vector &x, Vector &out) const {
95#define CMD "Optimist::TestSet::Booth::evaluate_impl(...): "
96
97 if constexpr (VectorTrait::IsFixed) {
98 out << x(0) + 2.0 * x(1) - 7.0, 2.0 * x(0) + x(1) - 5.0;
99 return out.allFinite();
100 } else if constexpr (VectorTrait::IsDynamic) {
101 out.resize(2);
102 out << x(0) + 2.0 * x(1) - 7.0, 2.0 * x(0) + x(1) - 5.0;
103 return out.allFinite();
104 } else if constexpr (VectorTrait::IsSparse) {
105 out.resize(2);
106 out.reserve(2);
107 out.coeffRef(0) = x.coeff(0) + 2.0 * x.coeff(1) - 7.0;
108 out.coeffRef(1) = 2.0 * x.coeff(0) + x.coeff(1) - 5.0;
109 for (typename Vector::InnerIterator it(out); it; ++it) {
110 if (!std::isfinite(it.value())) {
111 return false;
112 }
113 }
114 return true;
115 } else {
116 OPTIMIST_ERROR(CMD "input type not supported.");
117 return false;
118 }
119
120#undef CMD
121 }
122
129 bool first_derivative_impl(const Vector & /*x*/,
130 FirstDerivative &out) const {
131#define CMD "Optimist::TestSet::Booth::first_derivative_impl(...): "
132
133 if constexpr (VectorTrait::IsFixed) {
134 out << 1.0, 2.0, 2.0, 1.0;
135 } else if constexpr (VectorTrait::IsDynamic) {
136 out.resize(2, 2);
137 out << 1.0, 2.0, 2.0, 1.0;
138 } else if constexpr (VectorTrait::IsSparse) {
139 out.resize(2, 2);
140 out.reserve(4);
141 std::vector<Eigen::Triplet<Scalar>> triplets;
142 triplets.reserve(4);
143 triplets.emplace_back(0, 0, 1.0);
144 triplets.emplace_back(0, 1, 2.0);
145 triplets.emplace_back(1, 0, 2.0);
146 triplets.emplace_back(1, 1, 1.0);
147 out.setFromTriplets(triplets.begin(), triplets.end());
148 } else {
149 OPTIMIST_ERROR(CMD "input type not supported.");
150 return false;
151 }
152 return true;
153
154#undef CMD
155 }
156
163 bool second_derivative_impl(const Vector & /*x*/,
164 SecondDerivative &out) const {
165#define CMD "Optimist::TestSet::Booth::second_derivative_impl(...): "
166
167 out.resize(2);
168 std::for_each(out.begin(), out.end(), [](auto &m) {
169 if constexpr (VectorTrait::IsFixed) {
170 m.setZero();
171 }
172 if constexpr (VectorTrait::IsDynamic) {
173 m.resize(2, 2);
174 m.setZero();
175 }
176 if constexpr (VectorTrait::IsSparse) {
177 m.resize(2, 2);
178 m.reserve(4);
179 std::vector<Eigen::Triplet<Scalar>> triplets;
180 triplets.reserve(4);
181 triplets.emplace_back(0, 0, 0.0);
182 triplets.emplace_back(0, 1, 0.0);
183 triplets.emplace_back(1, 0, 0.0);
184 triplets.emplace_back(1, 1, 0.0);
185 m.setFromTriplets(triplets.begin(), triplets.end());
186 } else {
187 OPTIMIST_ERROR(CMD "input type not supported.");
188 }
189 });
190 return true;
191
192#undef CMD
193 }
194
195 }; // class Booth
196
197 } // namespace TestSet
198
199} // namespace Optimist
200
201#endif // OPTIMIST_TESTSET_BOOTH_HH
#define OPTIMIST_ERROR(MSG)
Definition Optimist.hh:38
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
#define CMD
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
TypeTrait< Vector > VectorTrait
Definition Booth.hh:40
typename Vector::Scalar Scalar
Definition Booth.hh:41
bool first_derivative_impl(const Vector &, FirstDerivative &out) const
Definition Booth.hh:129
Booth()
Definition Booth.hh:50
bool evaluate_impl(const Vector &x, Vector &out) const
Definition Booth.hh:94
bool second_derivative_impl(const Vector &, SecondDerivative &out) const
Definition Booth.hh:163
constexpr std::string name_impl() const
Definition Booth.hh:84
Namespace for the Optimist library.
Definition Optimist.hh:90
Definition Optimist.hh:114