proxsuite-nlp  0.11.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
 
Loading...
Searching...
No Matches
workspace.hpp
Go to the documentation of this file.
1
3#pragma once
4
7
8#include <fmt/ostream.h>
9
10namespace proxsuite {
11namespace nlp {
12
13template <typename Scalar>
14auto allocate_ldlt_from_problem(const ProblemTpl<Scalar> &prob,
15 LDLTChoice choice) {
16 std::vector<isize> nduals(prob.getNumConstraints());
17 for (std::size_t i = 0; i < nduals.size(); ++i)
18 nduals[i] = prob.getConstraintDim(i);
19 return allocate_ldlt_from_sizes<Scalar>({prob.ndx()}, nduals, choice);
20}
21
25template <typename Scalar> struct WorkspaceTpl {
26public:
27 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
28
30 using Problem = ProblemTpl<Scalar>;
31
33
34 long nx;
35 long ndx;
36 std::size_t numblocks; // number of constraint blocks
37 long numdual; // total constraint dim
38
40 MatrixXs kkt_matrix;
42 VectorXs kkt_rhs;
44 VectorXs kkt_rhs_corr;
46 VectorXs kkt_err;
48 VectorXs pd_step;
49 VectorRef prim_step;
50 VectorRef dual_step;
52 Eigen::VectorXi signature;
53
55 LDLTVariant<Scalar> ldlt_;
56
58
59 VectorXs x_prev;
60 VectorXs x_trial;
61 VectorXs data_lams_prev;
62 VectorXs data_lams_trial;
63 VectorOfRef lams_prev;
64 VectorOfRef lams_trial;
65
66 VectorXs prox_grad;
67 MatrixXs prox_hess;
68
70
72 VectorXs dual_residual;
73
74 VectorXs data_cstr_values;
76 std::vector<VectorRef> cstr_values;
77
88
89 MatrixXs data_jacobians;
90 MatrixXs data_hessians;
91 MatrixXs data_jacobians_proj;
92 std::vector<MatrixRef> cstr_jacobians;
93 std::vector<MatrixRef> cstr_vector_hessian_prod;
94 std::vector<MatrixRef> cstr_jacobians_proj;
95
96 VectorXs data_shift_cstr_values;
97 VectorXs data_lams_plus;
98 VectorXs data_lams_plus_reproj;
99 VectorXs data_lams_pdal;
100 VectorXs data_lams_pdal_reproj;
101 VectorXs data_shift_cstr_pdal;
102
104 std::vector<VectorRef> lams_plus;
106 std::vector<VectorRef> lams_plus_reproj;
108 std::vector<VectorRef> shift_cstr_values;
110 std::vector<VectorRef> lams_pdal;
111 std::vector<VectorRef> lams_pdal_reproj;
112 std::vector<VectorRef> shift_cstr_pdal;
113
114 std::vector<Scalar> ls_alphas;
115 std::vector<Scalar> ls_values;
117 Scalar alpha_opt;
119 Scalar dmerit_dir = 0.;
120
121 VectorXs tmp_dx_scaled;
122
123 WorkspaceTpl(const Problem &prob, LDLTChoice ldlt_choice = LDLTChoice::DENSE)
124 : nx(long(prob.nx())), ndx(long(prob.ndx())),
125 numblocks(prob.getNumConstraints()),
126 numdual(prob.getTotalConstraintDim()),
127 kkt_matrix(ndx + numdual, ndx + numdual), kkt_rhs(ndx + numdual),
128 kkt_rhs_corr(ndx + numdual), kkt_err(kkt_rhs), pd_step(ndx + numdual),
129 prim_step(pd_step.head(ndx)), dual_step(pd_step.tail(numdual)),
130 signature(ndx + numdual),
131 ldlt_(allocate_ldlt_from_problem(prob, ldlt_choice)), x_prev(nx),
132 x_trial(nx), data_lams_prev(numdual), data_lams_trial(numdual),
133 prox_grad(ndx), prox_hess(ndx, ndx), dual_residual(ndx),
134 data_cstr_values(numdual), objective_gradient(ndx),
135 objective_hessian(ndx, ndx), merit_gradient(ndx),
136 merit_dual_gradient(numdual), data_jacobians(numdual, ndx),
137 data_hessians((long)numblocks * ndx, ndx), data_lams_plus(numdual),
138 data_lams_plus_reproj(numdual), data_lams_pdal(numdual),
139 tmp_dx_scaled(ndx) {
140 init(prob);
141 }
142
143 void init(const Problem &prob) {
144 kkt_matrix.setZero();
145 kkt_rhs.setZero();
146 kkt_rhs_corr.setZero();
147 pd_step.setZero();
148 signature.setZero();
149
150 x_prev.setZero();
151 x_trial.setZero();
152 helpers::allocateMultipliersOrResiduals(prob, data_lams_prev, lams_prev);
153 helpers::allocateMultipliersOrResiduals(prob, data_lams_trial, lams_trial);
154 prox_grad.setZero();
155 prox_hess.setZero();
156
157 dual_residual.setZero();
159 prob, data_cstr_values, cstr_values); // not multipliers but same dims
160
161 objective_gradient.setZero();
162 objective_hessian.setZero();
163 merit_gradient.setZero();
164 merit_dual_gradient.setZero();
165 data_jacobians.setZero();
166 data_hessians.setZero();
167
168 helpers::allocateMultipliersOrResiduals(prob, data_shift_cstr_values,
171 helpers::allocateMultipliersOrResiduals(prob, data_lams_plus_reproj,
174 helpers::allocateMultipliersOrResiduals(prob, data_lams_pdal_reproj,
175 lams_pdal_reproj);
176 helpers::allocateMultipliersOrResiduals(prob, data_shift_cstr_pdal,
177 shift_cstr_pdal);
178 tmp_dx_scaled.setZero();
179
180 cstr_jacobians.reserve(numblocks);
181 cstr_vector_hessian_prod.reserve(numblocks);
182
183 data_jacobians_proj = data_jacobians;
184
185 int cursor = 0;
186 int nr = 0;
187 for (std::size_t i = 0; i < numblocks; i++) {
188 cursor = prob.getIndex(i);
189 nr = prob.getConstraintDim(i);
190 cstr_jacobians.emplace_back(data_jacobians.middleRows(cursor, nr));
191 cstr_jacobians_proj.emplace_back(
192 data_jacobians_proj.middleRows(cursor, nr));
193 cstr_vector_hessian_prod.emplace_back(
194 data_hessians.middleRows((int)i * ndx, ndx));
195 }
196 }
197};
198
199} // namespace nlp
200} // namespace proxsuite
201
202template <typename Scalar>
203struct fmt::formatter<proxsuite::nlp::WorkspaceTpl<Scalar>>
204 : fmt::ostream_formatter {};
205
206#ifdef PROXSUITE_NLP_ENABLE_TEMPLATE_INSTANTIATION
207#include "proxsuite-nlp/workspace.txx"
208#endif
Utility function to allocate an LDLT solver for the Newton iterations.
@ DENSE
Use our dense LDLT.
#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
Scalar objective_value
Objective value.
Definition workspace.hpp:79
VectorXs dual_residual
Residuals.
Definition workspace.hpp:72
VectorXs merit_dual_gradient
Merit function gradient in the dual variables (if applicable)
Definition workspace.hpp:87
std::vector< VectorRef > lams_plus
First-order multipliers .
VectorXs pd_step
Primal-dual step .
Definition workspace.hpp:48
std::vector< VectorRef > cstr_values
Values of each constraint.
Definition workspace.hpp:76
MatrixXs kkt_matrix
KKT iteration matrix.
Definition workspace.hpp:40
VectorXs merit_gradient
Merit function gradient.
Definition workspace.hpp:85
Scalar dmerit_dir
Merit function derivative in descent direction.
VectorXs kkt_rhs
KKT iteration right-hand side.
Definition workspace.hpp:42
VectorXs kkt_rhs_corr
Correction for the kkt matrix.
Definition workspace.hpp:44
std::vector< VectorRef > lams_plus_reproj
Product of the projector Jacobians with the first-order multipliers.
VectorXs objective_gradient
Objective function gradient.
Definition workspace.hpp:81
Scalar alpha_opt
Optimal linesearch .
Eigen::VectorXi signature
Signature of the KKT matrix.
Definition workspace.hpp:52
std::vector< VectorRef > lams_pdal
Primal-dual multiplier estimates (from the pdBCL algorithm)
long nx
Newton iteration variables.
Definition workspace.hpp:34
MatrixXs objective_hessian
Objective function Hessian.
Definition workspace.hpp:83
VectorXs kkt_err
KKT linear system error (for refinement)
Definition workspace.hpp:46
std::vector< VectorRef > shift_cstr_values
Buffer for shifted constraints.
LDLTVariant< Scalar > ldlt_
LDLT storage.
Definition workspace.hpp:55