Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
DenseFunction.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_DENSE_FUNCTION_HH
14#define OPTIMIST_TESTSET_DENSE_FUNCTION_HH
15
16#include "Optimist/Function.hh"
17#include "Optimist/Utils.hh"
18
19namespace Optimist {
20
21 namespace TestSet {
22
39 template <typename Vector, Integer N, typename Derived>
40 requires TypeTrait<Vector>::IsEigen &&
41 (!TypeTrait<Vector>::IsFixed || TypeTrait<Vector>::Dimension == N)
43 : public Function<Vector, typename Vector::Scalar, Derived> {
44 public:
46 using Scalar = typename Vector::Scalar;
51
53
54
57 static constexpr Integer dimension() {
58 return N;
59 }
60
64 bool evaluate_impl(const Vector &x, Scalar &out) const {
65 out = this->derived().value(Utils::to_dense(x));
66 return std::isfinite(out);
67 }
68
72 bool first_derivative_impl(const Vector &x, FirstDerivative &out) const {
73 Dense g(N);
74 this->derived().gradient(Utils::to_dense(x), g);
76 return g.allFinite();
77 }
78
82 bool second_derivative_impl(const Vector &x,
83 SecondDerivative &out) const {
84 DenseMatrix H(N, N);
85 this->derived().hessian(Utils::to_dense(x), H);
87 return H.allFinite();
88 }
89
90 protected:
94 void add_solution(const Dense &x) {
95 this->m_solutions.push_back(Utils::from_dense<Vector>(x));
96 }
97
101 void add_guess(const Dense &x) {
102 this->m_guesses.push_back(Utils::from_dense<Vector>(x));
103 }
104
108 static Dense make(std::initializer_list<Scalar> values) {
109 Dense x(static_cast<Integer>(values.size()));
110 Integer i{0};
111 for (const auto v : values) {
112 x(i++) = v;
113 }
114 return x;
115 }
116
117 private:
118 const Derived &derived() const {
119 return *static_cast<const Derived *>(this);
120 }
121
122 }; // class DenseCostFunction
123
141 template <typename Vector,
142 Integer N,
143 typename Derived,
144 typename OutVector = Vector,
145 Integer M = N>
151 class DenseSystemFunction : public Function<Vector, OutVector, Derived> {
152 public:
154 using Scalar = typename Vector::Scalar;
159
161
162
165 static constexpr Integer dimension() {
166 return N;
167 }
168
172 static constexpr Integer equations() {
173 return M;
174 }
175
179 bool evaluate_impl(const Vector &x, OutVector &out) const {
180 Dense F(M);
181 this->derived().residual(Utils::to_dense(x), F);
183 return F.allFinite();
184 }
185
189 bool first_derivative_impl(const Vector &x, FirstDerivative &out) const {
190 DenseMatrix J(M, N);
191 this->derived().jacobian(Utils::to_dense(x), J);
193 return J.allFinite();
194 }
195
199 bool second_derivative_impl(const Vector &x,
200 SecondDerivative &out) const {
201 using Matrix = typename SecondDerivative::value_type;
202 const Dense xd(Utils::to_dense(x));
203 out.resize(M);
204 bool finite{true};
205 for (Integer i{0}; i < M; ++i) {
206 DenseMatrix H(N, N);
207 this->derived().hessian(xd, i, H);
208 finite = finite && H.allFinite();
210 }
211 return finite;
212 }
213
214 protected:
218 void add_solution(const Dense &x) {
219 this->m_solutions.push_back(Utils::from_dense<Vector>(x));
220 }
221
225 void add_guess(const Dense &x) {
226 this->m_guesses.push_back(Utils::from_dense<Vector>(x));
227 }
228
232 static Dense make(std::initializer_list<Scalar> values) {
233 Dense x(static_cast<Integer>(values.size()));
234 Integer i{0};
235 for (const auto v : values) {
236 x(i++) = v;
237 }
238 return x;
239 }
240
241 private:
242 const Derived &derived() const {
243 return *static_cast<const Derived *>(this);
244 }
245
246 }; // class DenseSystemFunction
247
248 } // namespace TestSet
249
250} // namespace Optimist
251
252#endif // OPTIMIST_TESTSET_DENSE_FUNCTION_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< OutputTrait::IsScalar, std::conditional_t< InputTrait::IsSparse, Eigen::SparseVector< Scalar >, Eigen::Matrix< Scalar, InputTrait::Dimension, 1 > >, std::conditional_t< InputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > > >, Scalar > FirstDerivative
Definition Function.hh:47
std::vector< Input > m_guesses
Definition Function.hh:85
std::vector< Input > m_solutions
Definition Function.hh:83
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< OutputTrait::IsScalar, std::conditional_t< InputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, InputTrait::Dimension, InputTrait::Dimension > >, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, std::vector< Eigen::SparseMatrix< Scalar > >, std::vector< Eigen::Matrix< Scalar, InputTrait::Dimension, InputTrait::Dimension > > > >, Scalar > SecondDerivative
Definition Function.hh:62
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
bool evaluate_impl(const Vector &x, Scalar &out) const
Definition DenseFunction.hh:64
const Ackley< Vector, N > & derived() const
Definition DenseFunction.hh:118
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
bool second_derivative_impl(const Vector &x, SecondDerivative &out) const
Definition DenseFunction.hh:82
static Dense make(std::initializer_list< Scalar > values)
Definition DenseFunction.hh:108
bool first_derivative_impl(const Vector &x, FirstDerivative &out) const
Definition DenseFunction.hh:72
typename Vector::Scalar Scalar
Definition DenseFunction.hh:46
TypeTrait< Vector > VectorTrait
Definition DenseFunction.hh:45
static constexpr Integer dimension()
Definition DenseFunction.hh:57
Helper base class for nonlinear systems written once in terms of dense vectors (square by default).
Definition DenseFunction.hh:151
void add_guess(const Dense &x)
Definition DenseFunction.hh:225
Utils::DenseMatrix< Vector > DenseMatrix
Definition DenseFunction.hh:156
static constexpr Integer dimension()
Definition DenseFunction.hh:165
bool evaluate_impl(const Vector &x, OutVector &out) const
Definition DenseFunction.hh:179
typename Vector::Scalar Scalar
Definition DenseFunction.hh:154
bool second_derivative_impl(const Vector &x, SecondDerivative &out) const
Definition DenseFunction.hh:199
Utils::DenseVector< Vector > Dense
Definition DenseFunction.hh:155
void add_solution(const Dense &x)
Definition DenseFunction.hh:218
static Dense make(std::initializer_list< Scalar > values)
Definition DenseFunction.hh:232
TypeTrait< Vector > VectorTrait
Definition DenseFunction.hh:153
static constexpr Integer equations()
Definition DenseFunction.hh:172
bool first_derivative_impl(const Vector &x, FirstDerivative &out) const
Definition DenseFunction.hh:189
const Booth< Vector > & derived() const
Definition DenseFunction.hh:242
Vector from_dense(const Eigen::MatrixBase< Derived > &x)
Definition Utils.hh:59
Eigen::Matrix< typename TypeTrait< Vector >::Scalar, Eigen::Dynamic, Eigen::Dynamic > DenseMatrix
Definition Utils.hh:38
Matrix from_dense_matrix(const Eigen::MatrixBase< Derived > &A)
Definition Utils.hh:79
Eigen::Matrix< typename TypeTrait< Vector >::Scalar, Eigen::Dynamic, 1 > DenseVector
Definition Utils.hh:31
DenseVector< Vector > to_dense(const Vector &x)
Definition Utils.hh:48
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
Definition Optimist.hh:105