proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
cost-function.hpp
Go to the documentation of this file.
1
3#pragma once
4
7
8#include <boost/core/demangle.hpp>
9#include <ostream>
10
11namespace proxsuite {
12namespace nlp {
13
14template <typename Scalar>
16 -> shared_ptr<CostFunctionBaseTpl<Scalar>> {
17 if (func->nr() != 1) {
19 "Function cannot be cast to cost (codimension nr != 1).");
20 }
21 return std::make_shared<func_to_cost<Scalar>>(func);
22}
23
29template <typename _Scalar>
30struct CostFunctionBaseTpl : public C2FunctionTpl<_Scalar> {
31public:
32 using Scalar = _Scalar;
35
36 CostFunctionBaseTpl(const int nx, const int ndx) : Base(nx, ndx, 1) {}
38 : Base(manifold, 1) {}
39
40 /* Define cost function-specific API */
41
43 virtual Scalar call(const ConstVectorRef &x) const = 0;
44 virtual void computeGradient(const ConstVectorRef &x,
45 VectorRef out) const = 0;
46 virtual void computeHessian(const ConstVectorRef &x, MatrixRef out) const = 0;
47
48 /* Allocated versions */
49
50 VectorXs computeGradient(const ConstVectorRef &x) const {
51 VectorXs out(this->ndx());
52 computeGradient(x, out);
53 return out;
54 }
55
56 MatrixXs computeHessian(const ConstVectorRef &x) const {
57 MatrixXs out(this->ndx(), this->ndx());
58 computeHessian(x, out);
59 return out;
60 }
61
62 /* Implement C2FunctionTpl interface. */
63
64 VectorXs operator()(const ConstVectorRef &x) const {
65 VectorXs out(1, 1);
66 out << call(x);
67 return out;
68 }
69
70 void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const {
71 Eigen::Matrix<Scalar, 1, -1> gT = Jout.template topRows<1>();
72 computeGradient(x, gT.transpose());
73 Jout.row(0) = gT;
74 }
75
76 void vectorHessianProduct(const ConstVectorRef &x, const ConstVectorRef &v,
77 MatrixRef Hout) const {
78 computeHessian(x, Hout);
79 Hout *= v(0);
80 }
81
82 virtual ~CostFunctionBaseTpl() = default;
83
84 friend std::ostream &operator<<(std::ostream &ostr,
85 const CostFunctionBaseTpl<Scalar> &cost) {
86 const std::string name = boost::core::demangle(typeid(cost).name());
87 ostr << name;
88 return ostr;
89 }
90};
91
92template <typename _Scalar> struct func_to_cost : CostFunctionBaseTpl<_Scalar> {
93 using Scalar = _Scalar;
97
101 func_to_cost(const shared_ptr<C2Function> &func)
102 : Base(func->nx(), func->ndx()), underlying_(func) {}
103
104 Scalar call(const ConstVectorRef &x) const { return underlying()(x)(0); }
105
106 void computeGradient(const ConstVectorRef &x, VectorRef out) const {
107 underlying().computeJacobian(x, out.transpose());
108 }
109
110 void computeHessian(const ConstVectorRef &x, MatrixRef Hout) const {
111 VectorXs v = VectorXs::Ones(1);
112 underlying().vectorHessianProduct(x, v, Hout);
113 }
114
115private:
116 shared_ptr<C2Function> underlying_;
117 const C2Function &underlying() const { return *underlying_; }
118};
119
120} // namespace nlp
121} // namespace proxsuite
122
123#ifdef PROXSUITE_NLP_ENABLE_TEMPLATE_INSTANTIATION
124#include "proxsuite-nlp/cost-function.txx"
125#endif
#define PROXSUITE_NLP_RUNTIME_ERROR(msg)
Definition exceptions.hpp:8
Base definitions for function classes.
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
auto downcast_function_to_cost(const shared_ptr< C2FunctionTpl< Scalar > > &func) -> shared_ptr< CostFunctionBaseTpl< Scalar > >
Main package namespace.
Definition bcl-params.hpp:5
virtual void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const =0
Jacobian matrix of the constraint function.
Twice-differentiable function, with method Jacobian and vector-hessian product evaluation.
Definition fwd.hpp:76
virtual void vectorHessianProduct(const ConstVectorRef &, const ConstVectorRef &, MatrixRef Hout) const
Vector-hessian product.
Base class for differentiable cost functions.
Definition fwd.hpp:89
void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const
Jacobian matrix of the constraint function.
virtual void computeHessian(const ConstVectorRef &x, MatrixRef out) const =0
void vectorHessianProduct(const ConstVectorRef &x, const ConstVectorRef &v, MatrixRef Hout) const
Vector-hessian product.
virtual Scalar call(const ConstVectorRef &x) const =0
Evaluate the cost function.
VectorXs computeGradient(const ConstVectorRef &x) const
CostFunctionBaseTpl(const ManifoldAbstractTpl< Scalar > &manifold)
CostFunctionBaseTpl(const int nx, const int ndx)
virtual ~CostFunctionBaseTpl()=default
friend std::ostream & operator<<(std::ostream &ostr, const CostFunctionBaseTpl< Scalar > &cost)
virtual void computeGradient(const ConstVectorRef &x, VectorRef out) const =0
VectorXs operator()(const ConstVectorRef &x) const
Evaluate the residual at a given point x.
MatrixXs computeHessian(const ConstVectorRef &x) const
Scalar call(const ConstVectorRef &x) const
Evaluate the cost function.
func_to_cost(const shared_ptr< C2Function > &func)
Constructor.
void computeGradient(const ConstVectorRef &x, VectorRef out) const
C2FunctionTpl< Scalar > C2Function
void computeHessian(const ConstVectorRef &x, MatrixRef Hout) const