Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Function.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_FUNCTION_HH
14#define OPTIMIST_FUNCTION_HH
15
16#include "Optimist.hh"
17
18namespace Optimist {
19
20 /*\
21 | _____ _ _ ____
22 | | ___| _ _ __ ___| |_(_) ___ _ __ | __ ) __ _ ___ ___
23 | | |_ | | | | '_ \ / __| __| |/ _ \| '_ \| _ \ / _` / __|/ _ \
24 | | _|| |_| | | | | (__| |_| | (_) | | | | |_) | (_| \__ \ __/
25 | |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|____/ \__,_|___/\___|
26 |
27 \*/
28
36 template <typename Input, typename Output, typename DerivedFunction>
37 requires std::is_same<typename TypeTrait<Input>::Scalar,
38 typename TypeTrait<Output>::Scalar>::value &&
39 (TypeTrait<Input>::IsScalar || TypeTrait<Input>::IsEigen) &&
40 (TypeTrait<Output>::IsScalar || TypeTrait<Output>::IsEigen) &&
41 (!TypeTrait<Input>::IsFixed || TypeTrait<Input>::Dimension > 0) &&
42 (!TypeTrait<Output>::IsFixed ||
43 TypeTrait<Output>::Dimension > 0) &&
44 (!(TypeTrait<Input>::IsEigen && TypeTrait<Output>::IsEigen) ||
45 (TypeTrait<Input>::IsFixed && TypeTrait<Output>::IsFixed) ||
46 (TypeTrait<Input>::IsDynamic && TypeTrait<Output>::IsDynamic) ||
47 (TypeTrait<Input>::IsSparse && TypeTrait<Output>::IsSparse))
49 public:
50 // Input and output types
53 using Scalar = typename InputTrait::Scalar;
54
55 // Derivative types
56 using FirstDerivative = std::conditional_t<
57 InputTrait::IsEigen || OutputTrait::IsEigen,
58 std::conditional_t<InputTrait::IsSparse || OutputTrait::IsSparse,
59 Eigen::SparseMatrix<Scalar>,
60 Eigen::Matrix<Scalar,
61 OutputTrait::Dimension,
62 InputTrait::Dimension>>,
63 Scalar>;
64 using SecondDerivative = std::conditional_t<
65 InputTrait::IsEigen || OutputTrait::IsEigen,
66 std::conditional_t<InputTrait::IsSparse || OutputTrait::IsSparse,
67 std::vector<Eigen::SparseMatrix<Scalar>>,
68 std::vector<Eigen::Matrix<Scalar,
69 OutputTrait::Dimension,
70 InputTrait::Dimension>>>,
71 Scalar>;
72
74
75 protected:
76 std::vector<Input>
78 std::vector<Input>
80
81 public:
86
91 constexpr std::string name() const {
92 return static_cast<const DerivedFunction *>(this)->name();
93 };
94
101 bool evaluate(const Input &x, Output &out) const {
102 return static_cast<const DerivedFunction *>(this)->evaluate_impl(x, out);
103 }
104
111 bool first_derivative(const Input &x, FirstDerivative &out) const {
112 return static_cast<const DerivedFunction *>(this)->first_derivative_impl(
113 x,
114 out);
115 }
116
123 bool second_derivative(const Input &x, SecondDerivative &out) const {
124 return static_cast<const DerivedFunction *>(this)->second_derivative_impl(
125 x,
126 out);
127 }
128
133 constexpr Integer input_dimension() const {
134 return InputTrait::Dimension;
135 }
136
141 constexpr Integer output_dimension() const {
142 return OutputTrait::Dimension;
143 }
144
149 const std::vector<Input> &solutions() const {
150 return this->m_solutions;
151 }
152
157 const std::vector<Input> &guesses() const {
158 return this->m_guesses;
159 }
160
166 const Input &solution(const Integer i) const {
167 return this->m_solutions.at(i);
168 }
169
175 const Input &guess(const Integer i) const {
176 return this->m_guesses.at(i);
177 }
178
185 bool is_solution(const Input &x,
186 const Scalar tol = FunctionBase::SQRT_EPSILON) const {
187 for (const auto &s : this->m_solutions) {
188 if constexpr (InputTrait::IsEigen) {
189 if ((x - s).norm() < tol) {
190 return true;
191 }
192 } else {
193 if (std::abs(x - s) < tol) {
194 return true;
195 }
196 }
197 }
198 return false;
199 }
200
201 }; // class FunctionBase
202
203 /*\
204 | _____ _ _
205 | | ___| _ _ __ ___| |_(_) ___ _ __
206 | | |_ | | | | '_ \ / __| __| |/ _ \| '_ \
207 | | _|| |_| | | | | (__| |_| | (_) | | | |
208 | |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|
209 |
210 \*/
211
220 template <typename Input, typename Output, typename DerivedFunction>
221 class Function : public FunctionBase<Input, Output, DerivedFunction> {
222 public:
223 friend class FunctionBase<Input, Output, DerivedFunction>;
224
225 // Derivative types
226 using
228 using
230
235
240 constexpr std::string name() const {
241 return static_cast<const DerivedFunction *>(this)->name_impl();
242 }
243
250 bool evaluate(const Input &x, Output &out) const {
251 return static_cast<const DerivedFunction *>(this)->evaluate_impl(x, out);
252 }
253
260 bool jacobian(const Input &x, FirstDerivative &out) const {
261 return static_cast<const DerivedFunction *>(this)->first_derivative_impl(
262 x,
263 out);
264 }
265
272 bool hessian(const Input &x, SecondDerivative &out) const {
273 return static_cast<const DerivedFunction *>(this)->second_derivative_impl(
274 x,
275 out);
276 }
277
278 }; // class Function
279
280 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281 // -
282 // - - - - - - - - -
283
290 template <typename T, typename DerivedFunction>
291 requires(TypeTrait<T>::IsScalar || TypeTrait<T>::IsEigen) &&
292 (!TypeTrait<T>::IsFixed || TypeTrait<T>::Dimension > 0)
293 class Function<T, T, DerivedFunction>
294 : public FunctionBase<T, T, DerivedFunction> {
295 public:
296 friend class FunctionBase<T, T, DerivedFunction>;
297
298 // Input and output types
299 using Scalar = typename TypeTrait<T>::Scalar;
300 using Input = T;
301 using Output = T;
302
303 // Derivative types
314 constexpr std::string name() const {
315 return static_cast<const DerivedFunction *>(this)->name_impl();
316 }
317
324 bool evaluate(const Input &x, Output &out) const {
325 return static_cast<const DerivedFunction *>(this)->evaluate_impl(x, out);
326 }
327
334 bool gradient(const Input &x, FirstDerivative &out) const {
335 return static_cast<const DerivedFunction *>(this)->first_derivative_impl(
336 x,
337 out);
338 }
339
346 bool hessian(const Input &x, SecondDerivative &out) const {
347 return static_cast<const DerivedFunction *>(this)->second_derivative_impl(
348 x,
349 out);
350 }
351
352 }; // class Function
353
354} // namespace Optimist
355
356#endif // OPTIMIST_FUNCTION_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:69
constexpr std::string name() const
Definition Function.hh:314
Function()
Definition Function.hh:309
bool gradient(const Input &x, FirstDerivative &out) const
Definition Function.hh:334
bool evaluate(const Input &x, Output &out) const
Definition Function.hh:324
bool hessian(const Input &x, SecondDerivative &out) const
Definition Function.hh:346
T Output
Definition Function.hh:301
typename TypeTrait< T >::Scalar Scalar
Definition Function.hh:299
bool second_derivative(const Input &x, SecondDerivative &out) const
Definition Function.hh:123
typename InputTrait::Scalar Scalar
Definition Function.hh:53
bool evaluate(const Input &x, Output &out) const
Definition Function.hh:101
constexpr std::string name() const
Definition Function.hh:91
bool first_derivative(const Input &x, FirstDerivative &out) const
Definition Function.hh:111
const Input & guess(const Integer i) const
Definition Function.hh:175
const std::vector< Input > & guesses() const
Definition Function.hh:157
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< Input > m_guesses
Definition Function.hh:79
std::vector< Input > m_solutions
Definition Function.hh:77
TypeTrait< Input > InputTrait
Definition Function.hh:51
bool is_solution(const Input &x, const Scalar tol=FunctionBase::SQRT_EPSILON) const
Definition Function.hh:185
const std::vector< Input > & solutions() const
Definition Function.hh:149
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
const Input & solution(const Integer i) const
Definition Function.hh:166
TypeTrait< Output > OutputTrait
Definition Function.hh:52
FunctionBase()
Definition Function.hh:85
constexpr Integer input_dimension() const
Definition Function.hh:133
constexpr Integer output_dimension() const
Definition Function.hh:141
bool hessian(const Input &x, SecondDerivative &out) const
Definition Function.hh:272
bool jacobian(const Input &x, FirstDerivative &out) const
Definition Function.hh:260
Function()
Definition Function.hh:234
bool evaluate(const Input &x, Output &out) const
Definition Function.hh:250
constexpr std::string name() const
Definition Function.hh:240
Namespace for the Optimist library.
Definition Optimist.hh:89
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:97
Definition Optimist.hh:113