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, 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_FUNCTION_HH
14#define OPTIMIST_FUNCTION_HH
15
16#include "Optimist.hh"
17
18namespace Optimist
19{
20
31 template <typename Real, Integer FunInDim, Integer FunOutDim, typename DerivedFunction>
33 {
34 public:
35 // Fancy static assertios (just for fun, don't take it too seriously)
36 static_assert(FunInDim > static_cast<Integer>(0) && FunOutDim > static_cast<Integer>(0),
37 "Negative-dimensional function? Are you serious?");
38
40
41 // I/O types
42 using InputType = typename std::conditional_t<FunInDim == 1, Real, Eigen::Vector<Real, FunInDim>>;
43 using OutputType = typename std::conditional_t<FunOutDim == 1, Real, Eigen::Vector<Real, FunOutDim>>;
44
45 // Derivative types
46 using FirstDerivativeType = std::conditional_t<FunInDim == 1 && FunOutDim == 1, Real, Eigen::Matrix<Real, FunOutDim, FunInDim>>;
47 using SecondDerivativeType = std::conditional_t<FunInDim == 1 && FunOutDim == 1, Real,
48 std::conditional_t<FunInDim == 1 || FunOutDim == 1, Eigen::Matrix<Real, FunInDim, FunInDim>,
49 std::vector<Eigen::Matrix<Real, FunInDim, FunInDim>>>>;
50
51 protected:
52 std::vector<InputType> m_solutions;
53 std::vector<InputType> m_guesses;
54
55 public:
60
65 std::string name() const {return static_cast<const DerivedFunction *>(this)->name();};
66
72 void evaluate(const InputType & x, OutputType & out) const
73 {
74 static_cast<const DerivedFunction *>(this)->evaluate_impl(x, out);
75 }
76
82 void first_derivative(const InputType & x, FirstDerivativeType & out) const
83 {
84 static_cast<const DerivedFunction *>(this)->first_derivative_impl(x, out);
85 }
86
93 {
94 static_cast<const DerivedFunction *>(this)->second_derivative_impl(x, out);
95 }
96
101 constexpr Integer input_dimension() const {return FunInDim;}
102
107 constexpr Integer output_dimension() const {return FunOutDim;}
108
113 const std::vector<InputType> & solutions() const {return this->m_solutions;}
114
119 const std::vector<InputType> & guesses() const {return this->m_guesses;}
120
126 const InputType & solution(const Integer i) const {return this->m_solutions.at(i);}
127
133 const InputType & guess(const Integer i) const {return this->m_guesses.at(i);}
134
141 bool is_solution(const InputType & x, const Real tol = EPSILON_LOW) const
142 {
143 for (const auto & s : this->m_solutions) {
144 if constexpr (FunInDim == 1) {
145 if (std::abs(x - s) < tol) {return true;}
146 } else if constexpr (FunInDim > 1) {
147 if((x - s).norm() < tol) {return true;}
148 } else {
149 OPTIMIST_ERROR("Optimist::Function::is_solution(...): invalid input dimension.");
150 return false;
151 }
152 }
153 return false;
154 }
155
156 }; // class Function
157
158} // namespace Optimist
159
160#endif // OPTIMIST_FUNCTION_HH
#define OPTIMIST_ERROR(MSG)
Definition Optimist.hh:33
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
bool is_solution(const InputType &x, const Real tol=EPSILON_LOW) const
Definition Function.hh:141
const InputType & guess(const Integer i) const
Definition Function.hh:133
std::conditional_t< FunInDim==1 &&FunOutDim==1, Real, std::conditional_t< FunInDim==1||FunOutDim==1, Eigen::Matrix< Real, FunInDim, FunInDim >, std::vector< Eigen::Matrix< Real, FunInDim, FunInDim > > > > SecondDerivativeType
Definition Function.hh:47
const std::vector< InputType > & solutions() const
Definition Function.hh:113
const std::vector< InputType > & guesses() const
Definition Function.hh:119
const InputType & solution(const Integer i) const
Definition Function.hh:126
std::vector< InputType > m_solutions
Definition Function.hh:52
typename std::conditional_t< FunInDim==1, Real, Eigen::Vector< Real, FunInDim > > InputType
Definition Function.hh:42
void second_derivative(const InputType &x, SecondDerivativeType &out) const
Definition Function.hh:92
void first_derivative(const InputType &x, FirstDerivativeType &out) const
Definition Function.hh:82
constexpr Integer input_dimension() const
Definition Function.hh:101
std::conditional_t< FunInDim==1 &&FunOutDim==1, Real, Eigen::Matrix< Real, FunOutDim, FunInDim > > FirstDerivativeType
Definition Function.hh:46
std::vector< InputType > m_guesses
Definition Function.hh:53
Function()
Definition Function.hh:59
void evaluate(const InputType &x, OutputType &out) const
Definition Function.hh:72
constexpr Integer output_dimension() const
Definition Function.hh:107
std::string name() const
Definition Function.hh:65
typename std::conditional_t< FunOutDim==1, Real, Eigen::Vector< Real, FunOutDim > > OutputType
Definition Function.hh:43
Namespace for the Optimist library.
Definition Optimist.hh:87
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:95