Sleipnir C++ API
Loading...
Searching...
No Matches
feasibility_restoration.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <cmath>
7#include <functional>
8#include <span>
9#include <tuple>
10#include <utility>
11
12#include <Eigen/Core>
13#include <Eigen/SparseCore>
14#include <gch/small_vector.hpp>
15
16#include "sleipnir/optimization/solver/control_flow.hpp"
17#include "sleipnir/optimization/solver/exit_status.hpp"
18#include "sleipnir/optimization/solver/ipm_matrix_callbacks.hpp"
19#include "sleipnir/optimization/solver/iteration_info.hpp"
20#include "sleipnir/optimization/solver/options.hpp"
21#include "sleipnir/optimization/solver/sqp_matrix_callbacks.hpp"
22#include "sleipnir/optimization/solver/util/append_as_triplets.hpp"
23#include "sleipnir/optimization/solver/util/iterates.hpp"
24#include "sleipnir/optimization/solver/util/problem_scaling.hpp"
25#include "sleipnir/util/print_diagnostics.hpp"
26
27namespace slp {
28
29template <typename Scalar>
30ExitStatus ipm(
31 const IPMMatrixCallbacks<Scalar>& matrix_callbacks,
32 std::span<std::function<ControlFlow(const IterationInfo<Scalar>& info)>>
33 iteration_callbacks,
34 const Options<Scalar>& options, bool in_feasibility_restoration,
35#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
36 const Eigen::ArrayX<bool>& bound_constraint_mask,
37#endif
38 IPMIterates<Scalar>& iterates, Scalar& μ, int& iterations);
39
48template <typename Scalar>
49std::tuple<Eigen::VectorX<Scalar>, Eigen::VectorX<Scalar>> compute_p_n(
50 const Eigen::VectorX<Scalar>& c, Scalar ρ, Scalar μ) {
51 // From equation (33) of [2]:
52 // ______________________
53 // μ − ρ c(x) /(μ − ρ c(x))² μ c(x)
54 // n = −−−−−−−−−− + / (−−−−−−−−−−) + −−−−−− (1)
55 // 2ρ √ ( 2ρ ) 2ρ
56 //
57 // The quadratic formula:
58 // ________
59 // -b + √b² - 4ac
60 // x = −−−−−−−−−−−−−− (2)
61 // 2a
62 //
63 // Rearrange (1) to fit the quadratic formula better:
64 // _________________________
65 // μ - ρ c(x) + √(μ - ρ c(x))² + 2ρ μ c(x)
66 // n = −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
67 // 2ρ
68 //
69 // Solve for coefficients:
70 //
71 // a = ρ (3)
72 // b = ρ c(x) - μ (4)
73 //
74 // -4ac = 2ρ μ c(x)
75 // -4(ρ)c = 2ρ μ c(x)
76 // -4c = 2μ c(x)
77 // c = -μ c(x)/2 (5)
78 //
79 // p = c(x) + n (6)
80
81 using DenseVector = Eigen::VectorX<Scalar>;
82
83 using std::sqrt;
84
85 DenseVector p{c.rows()};
86 DenseVector n{c.rows()};
87 for (int row = 0; row < p.rows(); ++row) {
88 Scalar _a = ρ;
89 Scalar _b = ρ * c[row] - μ;
90 Scalar _c = -μ * c[row] / Scalar(2);
91
92 n[row] = (-_b + sqrt(_b * _b - Scalar(4) * _a * _c)) / (Scalar(2) * _a);
93 p[row] = c[row] + n[row];
94 }
95
96 return {std::move(p), std::move(n)};
97}
98
112template <typename Scalar>
113ExitStatus feasibility_restoration(
114 const SQPMatrixCallbacks<Scalar>& matrix_callbacks,
115 std::span<std::function<ControlFlow(const IterationInfo<Scalar>& info)>>
116 iteration_callbacks,
117 const Options<Scalar>& options, SQPIterates<Scalar>& iterates,
118 int& iterations) {
119 // Feasibility restoration
120 //
121 // min ρ Σ (pₑ + nₑ) + ζ/2 (x - xᵣ)ᵀDᵣ(x - xᵣ)
122 // x
123 // pₑ,nₑ
124 //
125 // s.t. cₑ(x) - pₑ + nₑ = 0
126 // pₑ ≥ 0
127 // nₑ ≥ 0
128 //
129 // where ρ = 1000, ζ = √μ where μ is the barrier parameter, xᵣ is original
130 // iterate before feasibility restoration, and Dᵣ is a scaling matrix defined
131 // by
132 //
133 // Dᵣ = diag(min(1, 1/xᵣ[i]²) for i in x.rows())
134
135 using DenseVector = Eigen::VectorX<Scalar>;
136 using DiagonalMatrix = Eigen::DiagonalMatrix<Scalar, Eigen::Dynamic>;
137 using SparseMatrix = Eigen::SparseMatrix<Scalar>;
138 using SparseVector = Eigen::SparseVector<Scalar>;
139
140 using std::sqrt;
141
142 auto& [x, y] = iterates;
143
144 const auto& matrices = matrix_callbacks;
145 const auto& num_vars = matrices.num_decision_variables;
146 const auto& num_eq = matrices.num_equality_constraints;
147
148 constexpr Scalar ρ(1e3);
149 const Scalar μ = options.tolerance / Scalar(10);
150
151 const DenseVector c_e = matrices.c_e(x);
152
153 Scalar fr_μ = std::max(μ, c_e.template lpNorm<Eigen::Infinity>());
154 const Scalar ζ = sqrt(fr_μ);
155
156 const auto& x_r = x;
157 const auto [p_e_0, n_e_0] = compute_p_n(c_e, ρ, fr_μ);
158
159 // Dᵣ = diag(min(1, 1/xᵣ[i]²) for i in x.rows())
160 const DiagonalMatrix D_r =
161 x.cwiseSquare().cwiseInverse().cwiseMin(Scalar(1)).asDiagonal();
162
163 DenseVector fr_x{num_vars + 2 * num_eq};
164 fr_x << x, p_e_0, n_e_0;
165
166 // Start the constraints pₑ, nₑ ≥ 0 with zero violation
167 DenseVector fr_s{2 * num_eq};
168 fr_s << p_e_0, n_e_0;
169
170 DenseVector fr_y = DenseVector::Zero(num_eq);
171
172 // Start the duals with perfect complementarity with respect to the slacks
173 DenseVector fr_z{2 * num_eq};
174 fr_z << fr_μ * p_e_0.cwiseInverse(), fr_μ * n_e_0.cwiseInverse();
175
176 // Inherit the parent problem's scaling for the constraints, and use no
177 // scaling for the cost function since it has changed. The new rows introduced
178 // are not scaled.
179 const ProblemScaling<Scalar> fr_scaling{Scalar(1), matrices.scaling.c_e,
180 DenseVector::Ones(2 * num_eq)};
181
182 IPMMatrixCallbacks<Scalar> fr_matrix_callbacks{
183 static_cast<int>(fr_x.rows()),
184 static_cast<int>(fr_y.rows()),
185 static_cast<int>(fr_z.rows()),
186 [&](const DenseVector& x_p) -> Scalar {
187 auto x = x_p.segment(0, num_vars);
188
189 // Cost function
190 //
191 // ρ Σ (pₑ + nₑ) + ζ/2 (x - xᵣ)ᵀDᵣ(x - xᵣ)
192
193 auto diff = x - x_r;
194 return ρ * x_p.segment(num_vars, 2 * num_eq).array().sum() +
195 ζ / Scalar(2) * diff.transpose() * D_r * diff;
196 },
197 [&](const DenseVector& x_p) -> SparseVector {
198 auto x = x_p.segment(0, num_vars);
199
200 // Cost function gradient
201 //
202 // [ζDᵣ(x − xᵣ)]
203 // [ ρ ]
204 // [ ρ ]
205 DenseVector g{x_p.rows()};
206 g.segment(0, num_vars) = ζ * D_r * (x - x_r);
207 g.segment(num_vars, 2 * num_eq).setConstant(ρ);
208 return g.sparseView();
209 },
210 [&](const DenseVector& x_p, const DenseVector& y_p,
211 [[maybe_unused]] const DenseVector& z_p) -> SparseMatrix {
212 auto x = x_p.segment(0, num_vars);
213 const auto& y = y_p;
214
215 // Cost function Hessian
216 //
217 // [ζDᵣ 0 0]
218 // [ 0 0 0]
219 // [ 0 0 0]
220 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
221 triplets.reserve(x_p.rows());
222 append_as_triplets(triplets, 0, 0, {SparseMatrix{ζ * D_r}});
223 SparseMatrix d2f_dx2{x_p.rows(), x_p.rows()};
224 d2f_dx2.setFromSortedTriplets(triplets.begin(), triplets.end());
225
226 // Constraint part of original problem's Lagrangian Hessian
227 //
228 // −∇ₓₓ²yᵀcₑ(x)
229 auto H_c = matrices.H_c(x, y);
230 H_c.conservativeResize(x_p.rows(), x_p.rows());
231
232 // Lagrangian Hessian
233 //
234 // [ζDᵣ 0 0]
235 // [ 0 0 0] − ∇ₓₓ²yᵀcₑ(x)
236 // [ 0 0 0]
237 return d2f_dx2 + H_c;
238 },
239 [&](const DenseVector& x_p, [[maybe_unused]] const DenseVector& y_p,
240 [[maybe_unused]] const DenseVector& z_p) -> SparseMatrix {
241 return SparseMatrix{x_p.rows(), x_p.rows()};
242 },
243 [&](const DenseVector& x_p) -> DenseVector {
244 auto x = x_p.segment(0, num_vars);
245 auto p_e = x_p.segment(num_vars, num_eq);
246 auto n_e = x_p.segment(num_vars + num_eq, num_eq);
247
248 // Equality constraints
249 //
250 // cₑ(x) - pₑ + nₑ = 0
251 return matrices.c_e(x) - p_e + n_e;
252 },
253 [&](const DenseVector& x_p) -> SparseMatrix {
254 auto x = x_p.segment(0, num_vars);
255
256 // Equality constraint Jacobian
257 //
258 // [Aₑ −I I]
259
260 SparseMatrix A_e = matrices.A_e(x);
261
262 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
263 triplets.reserve(A_e.nonZeros() + 2 * num_eq);
264
265 append_as_triplets(triplets, 0, 0, {A_e});
266 append_diagonal_as_triplets(
267 triplets, 0, num_vars,
268 DenseVector::Constant(num_eq, Scalar(-1)).eval());
269 append_diagonal_as_triplets(
270 triplets, 0, num_vars + num_eq,
271 DenseVector::Constant(num_eq, Scalar(1)).eval());
272
273 SparseMatrix A_e_p{A_e.rows(), x_p.rows()};
274 A_e_p.setFromSortedTriplets(triplets.begin(), triplets.end());
275 return A_e_p;
276 },
277 [&](const DenseVector& x_p) -> DenseVector {
278 // Inequality constraints
279 //
280 // pₑ ≥ 0
281 // nₑ ≥ 0
282 return x_p.segment(num_vars, 2 * num_eq);
283 },
284 [&](const DenseVector& x_p) -> SparseMatrix {
285 // Inequality constraint Jacobian
286 //
287 // [0 I 0]
288 // [0 0 I]
289
290 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
291 triplets.reserve(2 * num_eq);
292
293 append_diagonal_as_triplets(
294 triplets, 0, num_vars,
295 DenseVector::Constant(2 * num_eq, Scalar(1)).eval());
296
297 SparseMatrix A_i_p{2 * num_eq, x_p.rows()};
298 A_i_p.setFromSortedTriplets(triplets.begin(), triplets.end());
299 return A_i_p;
300 },
301 fr_scaling};
302
303 IPMIterates<Scalar> fr_iterates{std::move(fr_x), std::move(fr_s),
304 std::move(fr_y), std::move(fr_z)};
305
306 auto status =
307 ipm<Scalar>(fr_matrix_callbacks, iteration_callbacks, options, true,
308#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
309 Eigen::ArrayX<bool>::Constant(2 * num_eq, true),
310#endif
311 fr_iterates, fr_μ, iterations);
312
313 x = fr_iterates.x.segment(0, x.rows());
314
315 // If feasibility restoration converged, check the constraint violation in
316 // the original problem's scaling
317 DenseVector c_e_unscaled;
318 bool c_e_violated = false;
319 if (status == ExitStatus::SUCCESS) {
320 c_e_unscaled = matrices.c_e(x);
321 if (matrices.scaling.c_e.size() > 0) {
322 c_e_unscaled =
323 matrices.scaling.c_e.cwiseInverse().cwiseProduct(c_e_unscaled);
324 }
325
326 c_e_violated =
327 c_e_unscaled.template lpNorm<Eigen::Infinity>() > options.tolerance;
328 }
329
330 // Return to the normal phase if feasibility restoration stopped early, or if
331 // it converged to a feasible point (e.g., because it was run to convergence)
332 if (status == ExitStatus::CALLBACK_REQUESTED_STOP ||
333 (status == ExitStatus::SUCCESS && !c_e_violated)) {
334 // Reset the equality constraint duals since feasibility restoration didn't
335 // account for the cost function
336 y.setZero();
337
338 return ExitStatus::SUCCESS;
339 } else if (status == ExitStatus::SUCCESS) {
340 // Feasibility restoration converged to a minimizer of the constraint
341 // violation, and the constraint violation is still above the tolerance, so
342 // that minimizer is a certificate of local infeasibility. Declaring local
343 // infeasibility anywhere else risks false positives (e.g., a
344 // point-in-time test can reject iterates the solver would otherwise
345 // escape). See section 3.3, p. 14 of [2].
346 if (options.diagnostics) {
347 print_c_e_local_infeasibility_error(c_e_unscaled, options.tolerance);
348 }
349
350 return ExitStatus::LOCALLY_INFEASIBLE;
351 } else {
352 return ExitStatus::FEASIBILITY_RESTORATION_FAILED;
353 }
354}
355
369template <typename Scalar>
370ExitStatus feasibility_restoration(
371 const IPMMatrixCallbacks<Scalar>& matrix_callbacks,
372 std::span<std::function<ControlFlow(const IterationInfo<Scalar>& info)>>
373 iteration_callbacks,
374 const Options<Scalar>& options,
375#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
376 const Eigen::ArrayX<bool>& bound_constraint_mask,
377#endif
378 IPMIterates<Scalar>& iterates, Scalar μ, int& iterations) {
379 // Feasibility restoration
380 //
381 // min ρ Σ (pₑ + nₑ + pᵢ + nᵢ) + ζ/2 (x - xᵣ)ᵀDᵣ(x - xᵣ)
382 // x
383 // pₑ,nₑ
384 // pᵢ,nᵢ
385 //
386 // s.t. cₑ(x) - pₑ + nₑ = 0
387 // cᵢ(x) - pᵢ + nᵢ ≥ 0
388 // pₑ ≥ 0
389 // nₑ ≥ 0
390 // pᵢ ≥ 0
391 // nᵢ ≥ 0
392 //
393 // where ρ = 1000, ζ = √μ where μ is the barrier parameter, xᵣ is original
394 // iterate before feasibility restoration, and Dᵣ is a scaling matrix defined
395 // by
396 //
397 // Dᵣ = diag(min(1, 1/xᵣ[i]²) for i in x.rows())
398
399 using DenseVector = Eigen::VectorX<Scalar>;
400 using DiagonalMatrix = Eigen::DiagonalMatrix<Scalar, Eigen::Dynamic>;
401 using SparseMatrix = Eigen::SparseMatrix<Scalar>;
402 using SparseVector = Eigen::SparseVector<Scalar>;
403
404 using std::sqrt;
405
406 auto& [x, s, y, z] = iterates;
407
408 const auto& matrices = matrix_callbacks;
409 const auto& num_vars = matrices.num_decision_variables;
410 const auto& num_eq = matrices.num_equality_constraints;
411 const auto& num_ineq = matrices.num_inequality_constraints;
412
413 constexpr Scalar ρ(1e3);
414
415 const DenseVector c_e = matrices.c_e(x);
416 const DenseVector c_i = matrices.c_i(x);
417
418 Scalar fr_μ = std::max({μ, c_e.template lpNorm<Eigen::Infinity>(),
419 (c_i - s).template lpNorm<Eigen::Infinity>()});
420 const Scalar ζ = sqrt(fr_μ);
421
422 const auto& x_r = x;
423 const auto [p_e_0, n_e_0] = compute_p_n(c_e, ρ, fr_μ);
424 const auto [p_i_0, n_i_0] = compute_p_n((c_i - s).eval(), ρ, fr_μ);
425
426 // Dᵣ = diag(min(1, 1/xᵣ[i]²) for i in x.rows())
427 const DiagonalMatrix D_r =
428 x.cwiseSquare().cwiseInverse().cwiseMin(Scalar(1)).asDiagonal();
429
430 DenseVector fr_x{num_vars + 2 * num_eq + 2 * num_ineq};
431 fr_x << x, p_e_0, n_e_0, p_i_0, n_i_0;
432
433 // Use the original problem's slacks for cᵢ(x) − pᵢ + nᵢ ≥ 0 since
434 // cᵢ(x) − pᵢ + nᵢ = s by construction.
435 //
436 // Start the constraints pₑ, nₑ, pᵢ, nᵢ ≥ 0 with zero violation.
437 DenseVector fr_s{s.rows() + 2 * num_eq + 2 * num_ineq};
438 fr_s.segment(0, s.rows()) = s;
439 fr_s.segment(s.rows(), 2 * num_eq + 2 * num_ineq) << p_e_0, n_e_0, p_i_0,
440 n_i_0;
441
442 DenseVector fr_y = DenseVector::Zero(c_e.rows());
443
444 // Start the duals with perfect complementarity with respect to the slacks
445 DenseVector fr_z{c_i.rows() + 2 * num_eq + 2 * num_ineq};
446 fr_z << fr_μ * s.cwiseInverse(), fr_μ * p_e_0.cwiseInverse(),
447 fr_μ * n_e_0.cwiseInverse(), fr_μ * p_i_0.cwiseInverse(),
448 fr_μ * n_i_0.cwiseInverse();
449
450 // Inherit the parent problem's scaling for the constraints, and use no
451 // scaling for the cost function since it has changed. The new rows introduced
452 // are not scaled.
453 DenseVector fr_d_c_i{c_i.rows() + 2 * num_eq + 2 * num_ineq};
454 fr_d_c_i << matrices.scaling.c_i,
455 DenseVector::Ones(2 * num_eq + 2 * num_ineq);
456 const ProblemScaling<Scalar> fr_scaling{Scalar(1), matrices.scaling.c_e,
457 fr_d_c_i};
458
459 IPMMatrixCallbacks<Scalar> fr_matrix_callbacks{
460 static_cast<int>(fr_x.rows()),
461 static_cast<int>(fr_y.rows()),
462 static_cast<int>(fr_z.rows()),
463 [&](const DenseVector& x_p) -> Scalar {
464 auto x = x_p.segment(0, num_vars);
465
466 // Cost function
467 //
468 // ρ Σ (pₑ + nₑ + pᵢ + nᵢ) + ζ/2 (x - xᵣ)ᵀDᵣ(x - xᵣ)
469 auto diff = x - x_r;
470 return ρ * x_p.segment(num_vars, 2 * num_eq + 2 * num_ineq)
471 .array()
472 .sum() +
473 ζ / Scalar(2) * diff.transpose() * D_r * diff;
474 },
475 [&](const DenseVector& x_p) -> SparseVector {
476 auto x = x_p.segment(0, num_vars);
477
478 // Cost function gradient
479 //
480 // [ζDᵣ(x − xᵣ)]
481 // [ ρ ]
482 // [ ρ ]
483 // [ ρ ]
484 // [ ρ ]
485 DenseVector g{x_p.rows()};
486 g.segment(0, num_vars) = ζ * D_r * (x - x_r);
487 g.segment(num_vars, 2 * num_eq + 2 * num_ineq).setConstant(ρ);
488 return g.sparseView();
489 },
490 [&](const DenseVector& x_p, const DenseVector& y_p,
491 const DenseVector& z_p) -> SparseMatrix {
492 auto x = x_p.segment(0, num_vars);
493 const auto& y = y_p;
494 auto z = z_p.segment(0, num_ineq);
495
496 // Cost function Hessian
497 //
498 // [ζDᵣ 0 0 0 0]
499 // [ 0 0 0 0 0]
500 // [ 0 0 0 0 0]
501 // [ 0 0 0 0 0]
502 // [ 0 0 0 0 0]
503 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
504 triplets.reserve(x_p.rows());
505 append_as_triplets(triplets, 0, 0, {SparseMatrix{ζ * D_r}});
506 SparseMatrix d2f_dx2{x_p.rows(), x_p.rows()};
507 d2f_dx2.setFromSortedTriplets(triplets.begin(), triplets.end());
508
509 // Constraint part of original problem's Lagrangian Hessian
510 //
511 // −∇ₓₓ²yᵀcₑ(x) − ∇ₓₓ²zᵀcᵢ(x)
512 auto H_c = matrices.H_c(x, y, z);
513 H_c.conservativeResize(x_p.rows(), x_p.rows());
514
515 // Lagrangian Hessian
516 //
517 // [ζDᵣ 0 0 0 0]
518 // [ 0 0 0 0 0]
519 // [ 0 0 0 0 0] − ∇ₓₓ²yᵀcₑ(x) − ∇ₓₓ²zᵀcᵢ(x)
520 // [ 0 0 0 0 0]
521 // [ 0 0 0 0 0]
522 return d2f_dx2 + H_c;
523 },
524 [&](const DenseVector& x_p, [[maybe_unused]] const DenseVector& y_p,
525 [[maybe_unused]] const DenseVector& z_p) -> SparseMatrix {
526 return SparseMatrix{x_p.rows(), x_p.rows()};
527 },
528 [&](const DenseVector& x_p) -> DenseVector {
529 auto x = x_p.segment(0, num_vars);
530 auto p_e = x_p.segment(num_vars, num_eq);
531 auto n_e = x_p.segment(num_vars + num_eq, num_eq);
532
533 // Equality constraints
534 //
535 // cₑ(x) - pₑ + nₑ = 0
536 return matrices.c_e(x) - p_e + n_e;
537 },
538 [&](const DenseVector& x_p) -> SparseMatrix {
539 auto x = x_p.segment(0, num_vars);
540
541 // Equality constraint Jacobian
542 //
543 // [Aₑ −I I 0 0]
544
545 SparseMatrix A_e = matrices.A_e(x);
546
547 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
548 triplets.reserve(A_e.nonZeros() + 2 * num_eq);
549
550 append_as_triplets(triplets, 0, 0, {A_e});
551 append_diagonal_as_triplets(
552 triplets, 0, num_vars,
553 DenseVector::Constant(num_eq, Scalar(-1)).eval());
554 append_diagonal_as_triplets(
555 triplets, 0, num_vars + num_eq,
556 DenseVector::Constant(num_eq, Scalar(1)).eval());
557
558 SparseMatrix A_e_p{A_e.rows(), x_p.rows()};
559 A_e_p.setFromSortedTriplets(triplets.begin(), triplets.end());
560 return A_e_p;
561 },
562 [&](const DenseVector& x_p) -> DenseVector {
563 auto x = x_p.segment(0, num_vars);
564 auto p_i = x_p.segment(num_vars + 2 * num_eq, num_ineq);
565 auto n_i = x_p.segment(num_vars + 2 * num_eq + num_ineq, num_ineq);
566
567 // Inequality constraints
568 //
569 // cᵢ(x) - pᵢ + nᵢ ≥ 0
570 // pₑ ≥ 0
571 // nₑ ≥ 0
572 // pᵢ ≥ 0
573 // nᵢ ≥ 0
574 DenseVector c_i_p{c_i.rows() + 2 * num_eq + 2 * num_ineq};
575 c_i_p.segment(0, num_ineq) = matrices.c_i(x) - p_i + n_i;
576 c_i_p.segment(p_i.rows(), 2 * num_eq + 2 * num_ineq) =
577 x_p.segment(num_vars, 2 * num_eq + 2 * num_ineq);
578 return c_i_p;
579 },
580 [&](const DenseVector& x_p) -> SparseMatrix {
581 auto x = x_p.segment(0, num_vars);
582
583 // Inequality constraint Jacobian
584 //
585 // [Aᵢ 0 0 −I I]
586 // [0 I 0 0 0]
587 // [0 0 I 0 0]
588 // [0 0 0 I 0]
589 // [0 0 0 0 I]
590
591 SparseMatrix A_i = matrices.A_i(x);
592
593 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
594 triplets.reserve(A_i.nonZeros() + 2 * num_eq + 4 * num_ineq);
595
596 // Column 0
597 append_as_triplets(triplets, 0, 0, {A_i});
598
599 // Columns 1 and 2
600 append_diagonal_as_triplets(
601 triplets, num_ineq, num_vars,
602 DenseVector::Constant(2 * num_eq, Scalar(1)).eval());
603
604 SparseMatrix I_ineq{
605 DenseVector::Constant(num_ineq, Scalar(1)).asDiagonal()};
606
607 // Column 3
608 SparseMatrix Z_col3{2 * num_eq, num_ineq};
609 append_as_triplets(triplets, 0, num_vars + 2 * num_eq,
610 {(-I_ineq).eval(), Z_col3, I_ineq});
611
612 // Column 4
613 SparseMatrix Z_col4{2 * num_eq + num_ineq, num_ineq};
614 append_as_triplets(triplets, 0, num_vars + 2 * num_eq + num_ineq,
615 {I_ineq, Z_col4, I_ineq});
616
617 SparseMatrix A_i_p{2 * num_eq + 3 * num_ineq, x_p.rows()};
618 A_i_p.setFromSortedTriplets(triplets.begin(), triplets.end());
619 return A_i_p;
620 },
621 fr_scaling};
622
623#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
624 Eigen::ArrayX<bool> fr_bound_constraint_mask{2 * num_eq + 3 * num_ineq};
625 fr_bound_constraint_mask.segment(0, num_ineq) = bound_constraint_mask;
626 fr_bound_constraint_mask.segment(num_ineq, 2 * num_eq + 2 * num_ineq) = true;
627#endif
628
629 IPMIterates<Scalar> fr_iterates{std::move(fr_x), std::move(fr_s),
630 std::move(fr_y), std::move(fr_z)};
631
632 auto status =
633 ipm<Scalar>(fr_matrix_callbacks, iteration_callbacks, options, true,
634#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
635 fr_bound_constraint_mask,
636#endif
637 fr_iterates, fr_μ, iterations);
638
639 x = fr_iterates.x.segment(0, x.rows());
640
641 // If feasibility restoration converged, check the constraint violation in the
642 // original problem's scaling
643 DenseVector c_e_unscaled;
644 DenseVector c_i_unscaled;
645 bool c_e_violated = false;
646 bool c_i_violated = false;
647 if (status == ExitStatus::SUCCESS) {
648 c_e_unscaled = matrices.c_e(x);
649 if (matrices.scaling.c_e.size() > 0) {
650 c_e_unscaled =
651 matrices.scaling.c_e.cwiseInverse().cwiseProduct(c_e_unscaled);
652 }
653
654 c_i_unscaled = matrices.c_i(x);
655 if (matrices.scaling.c_i.size() > 0) {
656 c_i_unscaled =
657 matrices.scaling.c_i.cwiseInverse().cwiseProduct(c_i_unscaled);
658 }
659
660 // Inequality constraints cᵢ(x) ≥ 0 are only violated where they're
661 // negative
662 c_e_violated =
663 c_e_unscaled.template lpNorm<Eigen::Infinity>() > options.tolerance;
664 c_i_violated =
665 (-c_i_unscaled).cwiseMax(Scalar(0)).template lpNorm<Eigen::Infinity>() >
666 options.tolerance;
667 }
668
669 // Return to the normal phase if feasibility restoration stopped early, or if
670 // it converged to a feasible point (e.g., because it was run to convergence)
671 if (status == ExitStatus::CALLBACK_REQUESTED_STOP ||
672 (status == ExitStatus::SUCCESS && !c_e_violated && !c_i_violated)) {
673 const DenseVector s_old = s;
674
675 // Re-center the slacks on the original constraints cᵢ(x) if they're
676 // sufficiently feasible, which zeroes their constraint violation.
677 // Otherwise, keep feasibility restoration's slacks.
678 //
679 // Note that those slacks can make Σ = S⁻¹Z and μS⁻¹e very large because
680 // fr_μ drives them toward 0 independently of μ, so they can end up much
681 // smaller than μ. We can't fix that by raising the slacks because the added
682 // constraint violation would make the filter reject more returned iterates.
683 //
684 // The normal phase's early-exit callback computes the slacks the same way,
685 // so the returned iterate is the one the filter accepted.
686 const DenseVector c_i = matrices.c_i(x);
687 s = (c_i.array() >= Scalar(1e-2))
688 .select(c_i, fr_iterates.s.segment(0, s.rows()));
689
690 // Reset the equality constraint duals since feasibility restoration didn't
691 // account for the cost function
692 y.setZero();
693
694 // Update the inequality constraint duals with a Newton step for
695 // complementarity, taking the change in slacks during the restoration
696 // phase as the primal step.
697 //
698 // pᶻ = μS⁻¹e − z − Σpˢ
699 //
700 // If the result isn't strictly positive or is too large, reset all of them
701 // to 1.
702 const DenseVector Σ = z.cwiseQuotient(s_old);
703 const DenseVector p_s = s - s_old;
704 z += μ * s_old.cwiseInverse() - z - Σ.cwiseProduct(p_s);
705 if ((z.array() <= Scalar(0)).any() || (z.array() > Scalar(1e3)).any()) {
706 z.setOnes();
707 }
708
709 return ExitStatus::SUCCESS;
710 } else if (status == ExitStatus::SUCCESS) {
711 // Feasibility restoration converged to a minimizer of the constraint
712 // violation, and the constraint violation is still above the tolerance, so
713 // that minimizer is a certificate of local infeasibility. Declaring local
714 // infeasibility anywhere else risks false positives (e.g., a
715 // point-in-time test can reject iterates the solver would otherwise
716 // escape). See section 3.3, p. 14 of [2].
717
718 s = fr_iterates.s.segment(0, s.rows());
719
720 if (options.diagnostics) {
721 if (c_e_violated) {
722 print_c_e_local_infeasibility_error(c_e_unscaled, options.tolerance);
723 }
724 if (c_i_violated) {
725 print_c_i_local_infeasibility_error(c_i_unscaled, options.tolerance);
726 }
727 }
728
729 return ExitStatus::LOCALLY_INFEASIBLE;
730 } else {
731 s = fr_iterates.s.segment(0, s.rows());
732 return ExitStatus::FEASIBILITY_RESTORATION_FAILED;
733 }
734}
735
736} // namespace slp
737
738#include "sleipnir/optimization/solver/ipm.hpp"