proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
problem-base.hpp
Go to the documentation of this file.
1
3#pragma once
4
8
9namespace proxsuite {
10namespace nlp {
11
12template <typename _Scalar> struct ProblemTpl {
13public:
14 using Scalar = _Scalar;
16
23
27 shared_ptr<CostType> cost_;
29 std::vector<ConstraintObject> constraints_;
30
31 const CostType &cost() const { return *cost_; }
32 const Manifold &manifold() const { return *manifold_; }
33
34 template <class U>
35 ProblemTpl(U &&manifold, shared_ptr<CostType> cost,
36 const std::vector<ConstraintObject> &constraints = {})
37 : manifold_(std::forward<U>(manifold)), cost_(cost),
38 constraints_(constraints), nc_total_(0) {
40 }
41
43 const ConstraintObject &getConstraint(const std::size_t &i) const {
44 return constraints_[i];
45 }
46
48 std::size_t getNumConstraints() const { return constraints_.size(); }
49
50 int getTotalConstraintDim() const { return nc_total_; }
51
53 int getConstraintDim(std::size_t i) const { return ncs_[i]; }
54
55 int nx() const { return manifold_->nx(); }
56 int ndx() const { return manifold_->ndx(); }
57
59 template <typename T> void addConstraint(T &&cstr) {
60 constraints_.push_back(std::forward<T>(cstr));
62 }
63
64 auto getSegment(VectorXs &x, std::size_t i) const {
65 return x.segment(getIndex(i), getConstraintDim(i));
66 }
67
68 auto getConstSegment(const VectorXs &x, std::size_t i) const {
69 return x.segment(getIndex(i), getConstraintDim(i));
70 }
71
72 std::vector<int> getIndices() const { return indices_; }
73
74 int getIndex(std::size_t i) const { return indices_[i]; }
75
76 void evaluate(const ConstVectorRef &x, Workspace &workspace) const {
77 workspace.objective_value = cost().call(x);
78
79 for (std::size_t i = 0; i < getNumConstraints(); i++) {
80 const ConstraintObject &cstr = constraints_[i];
81 workspace.cstr_values[i] = cstr.func()(x);
82 }
83 }
84
85 void computeDerivatives(const ConstVectorRef &x, Workspace &workspace) const {
87
88 for (std::size_t i = 0; i < getNumConstraints(); i++) {
89 const ConstraintObject &cstr = constraints_[i];
90 cstr.func().computeJacobian(x, workspace.cstr_jacobians[i]);
91 }
92 }
93
94 void computeHessians(const ConstVectorRef &x, Workspace &workspace,
95 bool evaluate_all_constraint_hessians = false) const {
97 for (std::size_t i = 0; i < getNumConstraints(); i++) {
98 const ConstraintObject &cstr = getConstraint(i);
99 bool use_vhp =
100 !cstr.set_->disableGaussNewton() || evaluate_all_constraint_hessians;
101 if (use_vhp)
102 cstr.func().vectorHessianProduct(x, workspace.lams_pdal[i],
103 workspace.cstr_vector_hessian_prod[i]);
104 }
105 }
106
107protected:
110 std::vector<int> ncs_;
111 std::vector<int> indices_;
112
115 ncs_.clear();
116 indices_.clear();
117 int cursor = 0;
118 int nr = 0;
119 for (std::size_t i = 0; i < constraints_.size(); i++) {
120 const ConstraintObject &cstr = constraints_[i];
121 nr = cstr.func().nr();
122 ncs_.push_back(nr);
123 indices_.push_back(cursor);
124 cursor += nr;
125 }
126 nc_total_ = cursor;
127 }
128};
129
130namespace helpers {
131
132template <typename Scalar, typename VectorType>
134 typename math_types<Scalar>::VectorXs &input,
135 std::vector<Eigen::Ref<VectorType>> &out) {
136 static_assert(VectorType::IsVectorAtCompileTime,
137 "Function only supports compile-time vectors.");
138 out.reserve(prob.getNumConstraints());
139 for (std::size_t i = 0; i < prob.getNumConstraints(); i++) {
140 out.emplace_back(prob.getSegment(input, i));
141 }
142}
143
146template <typename Scalar>
148 const ProblemTpl<Scalar> &prob, typename math_types<Scalar>::VectorXs &data,
149 typename math_types<Scalar>::VectorOfRef &out) {
150 data.resize(prob.getTotalConstraintDim());
151 data.setZero();
152 createConstraintWiseView(prob, data, out);
153}
154
155} // namespace helpers
156
157} // namespace nlp
158} // namespace proxsuite
159
160#ifdef PROXSUITE_NLP_ENABLE_TEMPLATE_INSTANTIATION
161#include "proxsuite-nlp/problem-base.txx"
162#endif
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
void createConstraintWiseView(const ProblemTpl< Scalar > &prob, typename math_types< Scalar >::VectorXs &input, std::vector< Eigen::Ref< VectorType > > &out)
void allocateMultipliersOrResiduals(const ProblemTpl< Scalar > &prob, typename math_types< Scalar >::VectorXs &data, typename math_types< Scalar >::VectorOfRef &out)
Allocate a set of multipliers (or residuals) for a given problem instance.
Main package namespace.
Definition bcl-params.hpp:5
int nr() const
Get function codimension.
virtual void computeJacobian(const ConstVectorRef &x, MatrixRef Jout) const =0
Jacobian matrix of the constraint function.
virtual void vectorHessianProduct(const ConstVectorRef &, const ConstVectorRef &, MatrixRef Hout) const
Vector-hessian product.
Packs a ConstraintSetTpl and C2FunctionTpl together.
Definition fwd.hpp:112
const FunctionType & func() const
polymorphic< ConstraintSet > set_
Base class for differentiable cost functions.
Definition fwd.hpp:89
virtual void computeHessian(const ConstVectorRef &x, MatrixRef out) const =0
virtual Scalar call(const ConstVectorRef &x) const =0
Evaluate the cost function.
virtual void computeGradient(const ConstVectorRef &x, VectorRef out) const =0
std::vector< int > indices_
auto getSegment(VectorXs &x, std::size_t i) const
const Manifold & manifold() const
auto getConstSegment(const VectorXs &x, std::size_t i) const
std::size_t getNumConstraints() const
Get the number of constraint blocks.
void computeDerivatives(const ConstVectorRef &x, Workspace &workspace) const
shared_ptr< CostType > cost_
The cost function.
polymorphic< Manifold > manifold_
The working manifold .
void computeHessians(const ConstVectorRef &x, Workspace &workspace, bool evaluate_all_constraint_hessians=false) const
void evaluate(const ConstVectorRef &x, Workspace &workspace) const
const CostType & cost() const
int nc_total_
Total number of constraints.
std::vector< int > getIndices() const
ProblemTpl(U &&manifold, shared_ptr< CostType > cost, const std::vector< ConstraintObject > &constraints={})
void addConstraint(T &&cstr)
Add a constraint to the problem, after initialization.
void reset_constraint_dim_vars()
Set values of const data members for constraint dimensions.
int getConstraintDim(std::size_t i) const
Get dimension of constraint i.
const ConstraintObject & getConstraint(const std::size_t &i) const
Get a pointer to the -th constraint pointer.
int getIndex(std::size_t i) const
std::vector< ConstraintObject > constraints_
The set of constraints.
Scalar objective_value
Objective value.
Definition workspace.hpp:79
std::vector< VectorRef > cstr_values
Values of each constraint.
Definition workspace.hpp:76
VectorXs objective_gradient
Objective function gradient.
Definition workspace.hpp:81
std::vector< VectorRef > lams_pdal
Primal-dual multiplier estimates (from the pdBCL algorithm)
MatrixXs objective_hessian
Objective function Hessian.
Definition workspace.hpp:83
std::vector< MatrixRef > cstr_jacobians
Definition workspace.hpp:92
std::vector< MatrixRef > cstr_vector_hessian_prod
Definition workspace.hpp:93
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
Definition math.hpp:43