proxsuite-nlp  0.11.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
21 using Manifold = ManifoldAbstractTpl<Scalar>;
22 using Workspace = WorkspaceTpl<Scalar>;
23
25 polymorphic<Manifold> manifold_;
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 {
86 cost().computeGradient(x, workspace.objective_gradient);
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 {
96 cost().computeHessian(x, workspace.objective_hessian);
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>
133void createConstraintWiseView(const ProblemTpl<Scalar> &prob,
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 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.
Packs a ConstraintSetTpl and C2FunctionTpl together.
Base class for differentiable cost functions.
std::size_t getNumConstraints() const
Get the number of constraint blocks.
polymorphic< Manifold > manifold_
ConstraintObjectTpl< Scalar > ConstraintObject
int nc_total_
Total number of 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.
CostFunctionBaseTpl< Scalar > CostType
std::vector< ConstraintObject > constraints_
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
Definition math.hpp:43