Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
CostFunction.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_COST_FUNCTION_HH
14#define OPTIMIST_COST_FUNCTION_HH
15
16#include "Optimist/Function.hh"
17
18namespace Optimist
19{
20
29 template <typename Real, Integer N, typename DerivedFunction>
30 class CostFunction : public Function<Real, N, 1, DerivedFunction>
31 {
32 public:
34
35 // Fancy static assertions (just for fun, don't take it too seriously)
36 static_assert(N != static_cast<Integer>(0),
37 "Are you sure you want to a zero-dimensional system of equations?");
38 static_assert(N != static_cast<Integer>(1),
39 "C'mon, let's not kid ourselves. Use a scalar function...");
40
41 // I/O types
43
44 // Derivative types
47
52
57 std::string name() const {return static_cast<const DerivedFunction *>(this)->name_impl();}
58
64 void evaluate(const Vector & x, Vector & out) const
65 {
66 static_cast<const DerivedFunction *>(this)->evaluate_impl(x, out);
67 }
68
74 void gradient(const Vector & x, RowVector & out) const
75 {
76 static_cast<const DerivedFunction *>(this)->first_derivative_impl(x, out);
77 }
78
84 void hessian(const Vector & x, Matrix & out) const
85 {
86 static_cast<const DerivedFunction *>(this)->second_derivative_impl(x, out);
87 }
88
89 }; // class CostFunction
90
91} // namespace Optimist
92
93#endif // OPTIMIST_COST_FUNCTION_HH
typename Function< Real, N, 1, DerivedFunction >::InputType Vector
Definition CostFunction.hh:42
typename Function< Real, N, 1, DerivedFunction >::FirstDerivativeType RowVector
Definition CostFunction.hh:45
CostFunction()
Definition CostFunction.hh:51
std::string name() const
Definition CostFunction.hh:57
void gradient(const Vector &x, RowVector &out) const
Definition CostFunction.hh:74
void hessian(const Vector &x, Matrix &out) const
Definition CostFunction.hh:84
typename Function< Real, N, 1, DerivedFunction >::SecondDerivativeType Matrix
Definition CostFunction.hh:46
void evaluate(const Vector &x, Vector &out) const
Definition CostFunction.hh:64
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
typename std::conditional_t< FunInDim==1, Real, Eigen::Vector< Real, FunInDim > > InputType
Definition Function.hh:42
std::conditional_t< FunInDim==1 &&FunOutDim==1, Real, Eigen::Matrix< Real, FunOutDim, FunInDim > > FirstDerivativeType
Definition Function.hh:46
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