Sleipnir C++ API
Loading...
Searching...
No Matches
interior_point.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <chrono>
7#include <cmath>
8#include <functional>
9#include <span>
10
11#include <Eigen/Core>
12#include <Eigen/SparseCore>
13#include <gch/small_vector.hpp>
14
15#include "sleipnir/optimization/solver/exit_status.hpp"
16#include "sleipnir/optimization/solver/interior_point_matrix_callbacks.hpp"
17#include "sleipnir/optimization/solver/iteration_info.hpp"
18#include "sleipnir/optimization/solver/options.hpp"
19#include "sleipnir/optimization/solver/util/all_finite.hpp"
20#include "sleipnir/optimization/solver/util/append_as_triplets.hpp"
21#include "sleipnir/optimization/solver/util/feasibility_restoration.hpp"
22#include "sleipnir/optimization/solver/util/filter.hpp"
23#include "sleipnir/optimization/solver/util/fraction_to_the_boundary_rule.hpp"
24#include "sleipnir/optimization/solver/util/is_locally_infeasible.hpp"
25#include "sleipnir/optimization/solver/util/kkt_error.hpp"
26#include "sleipnir/optimization/solver/util/regularized_ldlt.hpp"
27#include "sleipnir/util/assert.hpp"
28#include "sleipnir/util/print_diagnostics.hpp"
29#include "sleipnir/util/profiler.hpp"
30#include "sleipnir/util/scope_exit.hpp"
31#include "sleipnir/util/symbol_exports.hpp"
32
33// See docs/algorithms.md#Works_cited for citation definitions.
34//
35// See docs/algorithms.md#Interior-point_method for a derivation of the
36// interior-point method formulation being used.
37
38namespace slp {
39
62template <typename Scalar>
63ExitStatus interior_point(
64 const InteriorPointMatrixCallbacks<Scalar>& matrix_callbacks,
65 std::span<std::function<bool(const IterationInfo<Scalar>& info)>>
66 iteration_callbacks,
67 const Options& options,
68#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
69 const Eigen::ArrayX<bool>& bound_constraint_mask,
70#endif
71 Eigen::Vector<Scalar, Eigen::Dynamic>& x) {
72 using DenseVector = Eigen::Vector<Scalar, Eigen::Dynamic>;
73
74 DenseVector s =
75 DenseVector::Ones(matrix_callbacks.num_inequality_constraints);
76 DenseVector y = DenseVector::Zero(matrix_callbacks.num_equality_constraints);
77 DenseVector z =
78 DenseVector::Ones(matrix_callbacks.num_inequality_constraints);
79 Scalar μ = Scalar(0.1) * matrix_callbacks.scaling.f;
80
81 return interior_point(matrix_callbacks, iteration_callbacks, options, false,
82#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
83 bound_constraint_mask,
84#endif
85 x, s, y, z, μ);
86}
87
120template <typename Scalar>
121ExitStatus interior_point(
122 const InteriorPointMatrixCallbacks<Scalar>& matrix_callbacks,
123 std::span<std::function<bool(const IterationInfo<Scalar>& info)>>
124 iteration_callbacks,
125 const Options& options, bool in_feasibility_restoration,
126#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
127 const Eigen::ArrayX<bool>& bound_constraint_mask,
128#endif
129 Eigen::Vector<Scalar, Eigen::Dynamic>& x,
130 Eigen::Vector<Scalar, Eigen::Dynamic>& s,
131 Eigen::Vector<Scalar, Eigen::Dynamic>& y,
132 Eigen::Vector<Scalar, Eigen::Dynamic>& z, Scalar& μ) {
133 using DenseVector = Eigen::Vector<Scalar, Eigen::Dynamic>;
134 using SparseMatrix = Eigen::SparseMatrix<Scalar>;
135 using SparseVector = Eigen::SparseVector<Scalar>;
136
138 struct Step {
140 DenseVector p_x;
142 DenseVector p_s;
144 DenseVector p_y;
146 DenseVector p_z;
147 };
148
149 using std::isfinite;
150
151 const auto solve_start_time = std::chrono::steady_clock::now();
152
153 gch::small_vector<SolveProfiler> solve_profilers;
154 solve_profilers.emplace_back("solver");
155 solve_profilers.emplace_back("↳ setup");
156 solve_profilers.emplace_back("↳ iteration");
157 solve_profilers.emplace_back(" ↳ feasibility check");
158 solve_profilers.emplace_back(" ↳ callbacks");
159 solve_profilers.emplace_back(" ↳ KKT matrix build");
160 solve_profilers.emplace_back(" ↳ KKT matrix decomp");
161 solve_profilers.emplace_back(" ↳ KKT system solve");
162 solve_profilers.emplace_back(" ↳ line search");
163 solve_profilers.emplace_back(" ↳ SOC");
164 solve_profilers.emplace_back(" ↳ feas. restoration");
165 solve_profilers.emplace_back(" ↳ f(x)");
166 solve_profilers.emplace_back(" ↳ ∇f(x)");
167 solve_profilers.emplace_back(" ↳ ∇²ₓₓL");
168 solve_profilers.emplace_back(" ↳ ∇²ₓₓL_c");
169 solve_profilers.emplace_back(" ↳ cₑ(x)");
170 solve_profilers.emplace_back(" ↳ ∂cₑ/∂x");
171 solve_profilers.emplace_back(" ↳ cᵢ(x)");
172 solve_profilers.emplace_back(" ↳ ∂cᵢ/∂x");
173
174 auto& solver_prof = solve_profilers[0];
175 auto& setup_prof = solve_profilers[1];
176 auto& inner_iter_prof = solve_profilers[2];
177 auto& feasibility_check_prof = solve_profilers[3];
178 auto& iter_callbacks_prof = solve_profilers[4];
179 auto& kkt_matrix_build_prof = solve_profilers[5];
180 auto& kkt_matrix_decomp_prof = solve_profilers[6];
181 auto& kkt_system_solve_prof = solve_profilers[7];
182 auto& line_search_prof = solve_profilers[8];
183 auto& soc_prof = solve_profilers[9];
184 auto& feasibility_restoration_prof = solve_profilers[10];
185
186 // Set up profiled matrix callbacks
187#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
188 auto& f_prof = solve_profilers[11];
189 auto& g_prof = solve_profilers[12];
190 auto& H_prof = solve_profilers[13];
191 auto& H_c_prof = solve_profilers[14];
192 auto& c_e_prof = solve_profilers[15];
193 auto& A_e_prof = solve_profilers[16];
194 auto& c_i_prof = solve_profilers[17];
195 auto& A_i_prof = solve_profilers[18];
196
197 InteriorPointMatrixCallbacks<Scalar> matrices{
198 matrix_callbacks.num_decision_variables,
199 matrix_callbacks.num_equality_constraints,
200 matrix_callbacks.num_inequality_constraints,
201 [&](const DenseVector& x) -> Scalar {
202 ScopedProfiler prof{f_prof};
203 return matrix_callbacks.f(x);
204 },
205 [&](const DenseVector& x) -> SparseVector {
206 ScopedProfiler prof{g_prof};
207 return matrix_callbacks.g(x);
208 },
209 [&](const DenseVector& x, const DenseVector& y,
210 const DenseVector& z) -> SparseMatrix {
211 ScopedProfiler prof{H_prof};
212 return matrix_callbacks.H(x, y, z);
213 },
214 [&](const DenseVector& x, const DenseVector& y,
215 const DenseVector& z) -> SparseMatrix {
216 ScopedProfiler prof{H_c_prof};
217 return matrix_callbacks.H_c(x, y, z);
218 },
219 [&](const DenseVector& x) -> DenseVector {
220 ScopedProfiler prof{c_e_prof};
221 return matrix_callbacks.c_e(x);
222 },
223 [&](const DenseVector& x) -> SparseMatrix {
224 ScopedProfiler prof{A_e_prof};
225 return matrix_callbacks.A_e(x);
226 },
227 [&](const DenseVector& x) -> DenseVector {
228 ScopedProfiler prof{c_i_prof};
229 return matrix_callbacks.c_i(x);
230 },
231 [&](const DenseVector& x) -> SparseMatrix {
232 ScopedProfiler prof{A_i_prof};
233 return matrix_callbacks.A_i(x);
234 },
235 matrix_callbacks.scaling};
236#else
237 const auto& matrices = matrix_callbacks;
238#endif
239
240 solver_prof.start();
241 setup_prof.start();
242
243 Scalar f = matrices.f(x);
244 SparseVector g = matrices.g(x);
245 SparseMatrix H = matrices.H(x, y, z);
246 DenseVector c_e = matrices.c_e(x);
247 SparseMatrix A_e = matrices.A_e(x);
248 DenseVector c_i = matrices.c_i(x);
249 SparseMatrix A_i = matrices.A_i(x);
250
251 // Ensure matrix callback dimensions are consistent
252 slp_assert(g.rows() == matrices.num_decision_variables);
253 slp_assert(H.rows() == matrices.num_decision_variables);
254 slp_assert(H.cols() == matrices.num_decision_variables);
255 slp_assert(c_e.rows() == matrices.num_equality_constraints);
256 slp_assert(A_e.rows() == matrices.num_equality_constraints);
257 slp_assert(A_e.cols() == matrices.num_decision_variables);
258 slp_assert(c_i.rows() == matrices.num_inequality_constraints);
259 slp_assert(A_i.rows() == matrices.num_inequality_constraints);
260 slp_assert(A_i.cols() == matrices.num_decision_variables);
261
262 DenseVector trial_x;
263 DenseVector trial_s;
264 DenseVector trial_y;
265 DenseVector trial_z;
266
267 Scalar trial_f;
268 DenseVector trial_c_e;
269 DenseVector trial_c_i;
270
271 // Check for overconstrained problem
272 if (matrices.num_equality_constraints > matrices.num_decision_variables) {
273 if (options.diagnostics) {
274 print_too_few_dofs_error(c_e);
275 }
276
277 return ExitStatus::TOO_FEW_DOFS;
278 }
279
280 // Check whether initial guess has finite cost, constraints, and derivatives
281 if (!isfinite(f) || !all_finite(g) || !all_finite(H) || !c_e.allFinite() ||
282 !all_finite(A_e) || !c_i.allFinite() || !all_finite(A_i)) {
283 return ExitStatus::NONFINITE_INITIAL_GUESS;
284 }
285
286#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
287 // We set sʲ = cᵢʲ(x) for each bound inequality constraint index j
288 s = bound_constraint_mask.select(c_i, s);
289#endif
290
291 int iterations = 0;
292
293 // Barrier parameter minimum
294 const Scalar μ_min =
295 matrices.scaling.f * Scalar(options.tolerance) / Scalar(10);
296
297 // Fraction-to-the-boundary rule scale factor minimum
298 constexpr Scalar τ_min(0.99);
299
300 // Fraction-to-the-boundary rule scale factor τ
301 Scalar τ = τ_min;
302
303 Filter<Scalar> filter{c_e.template lpNorm<1>() +
304 (c_i - s).template lpNorm<1>()};
305
306 // This should be run when the error is below a desired threshold for the
307 // current barrier parameter
308 auto update_barrier_parameter_and_reset_filter = [&] {
309 // Barrier parameter linear decrease power in "κ_μ μ". Range of (0, 1).
310 constexpr Scalar κ_μ(0.2);
311
312 // Barrier parameter superlinear decrease power in "μ^(θ_μ)". Range of (1,
313 // 2).
314 constexpr Scalar θ_μ(1.5);
315
316 // Update the barrier parameter.
317 //
318 // μⱼ₊₁ = max(εₜₒₗ/10, min(κ_μ μⱼ, μⱼ^θ_μ))
319 //
320 // See equation (7) of [2].
321 using std::pow;
322 μ = std::max(μ_min, std::min(κ_μ * μ, pow(μ, θ_μ)));
323
324 // Update the fraction-to-the-boundary rule scaling factor.
325 //
326 // τⱼ = max(τₘᵢₙ, 1 − μⱼ)
327 //
328 // See equation (8) of [2].
329 τ = std::max(τ_min, Scalar(1) - μ);
330
331 // Reset the filter when the barrier parameter is updated
332 filter.reset();
333 };
334
335 // Kept outside the loop so its storage can be reused
336 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
337
338 const int lhs_rows =
339 matrices.num_decision_variables + matrices.num_equality_constraints;
340 RegularizedLDLT<Scalar> solver{
341 // Use sparse solver if lower triangle fills < 25% of system
342 H.nonZeros() +
343 (A_i.transpose() * A_i)
344 .template triangularView<Eigen::Lower>()
345 .eval()
346 .nonZeros() +
347 A_e.nonZeros() <
348 0.25 * lhs_rows * lhs_rows,
349 matrices.num_decision_variables, matrices.num_equality_constraints,
350 // Constraint regularization is forced to zero in feasibility restoration
351 // because the equality constraint Jacobian cannot be rank-deficient
352 in_feasibility_restoration ? Scalar(0) : Scalar(1e-10)};
353
354 // Variables for determining when a step is acceptable
355 constexpr Scalar α_reduction_factor(0.5);
356 constexpr Scalar α_min(1e-7);
357
358 int full_step_rejected_counter = 0;
359
360 // Error
361 Scalar E_0 = unscaled_kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
362 matrices.scaling, g, A_e, c_e, A_i, c_i, s, y, z, Scalar(0));
363
364 setup_prof.stop();
365
366 // Prints final solver diagnostics when the solver exits
367 scope_exit exit{[&] {
368 if (options.diagnostics) {
369 solver_prof.stop();
370
371 if (in_feasibility_restoration) {
372 return;
373 }
374
375 if (iterations > 0) {
376 print_bottom_iteration_diagnostics();
377 }
378 print_solver_diagnostics(solve_profilers);
379 }
380 }};
381
382 while (E_0 > Scalar(options.tolerance)) {
383 ScopedProfiler inner_iter_profiler{inner_iter_prof};
384 ScopedProfiler feasibility_check_profiler{feasibility_check_prof};
385
386 // Check for local equality constraint infeasibility
387 if (is_equality_locally_infeasible(A_e, c_e)) {
388 if (options.diagnostics) {
389 print_c_e_local_infeasibility_error(c_e);
390 }
391
392 return ExitStatus::LOCALLY_INFEASIBLE;
393 }
394
395 // Check for local inequality constraint infeasibility
396 if (is_inequality_locally_infeasible(A_i, c_i)) {
397 if (options.diagnostics) {
398 print_c_i_local_infeasibility_error(c_i);
399 }
400
401 return ExitStatus::LOCALLY_INFEASIBLE;
402 }
403
404 // Check for diverging iterates
405 if (x.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !x.allFinite() ||
406 s.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !s.allFinite()) {
407 return ExitStatus::DIVERGING_ITERATES;
408 }
409
410 feasibility_check_profiler.stop();
411 ScopedProfiler iter_callbacks_profiler{iter_callbacks_prof};
412
413 // Call iteration callbacks
414 for (const auto& callback : iteration_callbacks) {
415 if (callback({iterations, x, s, y, z, g, H, A_e, A_i})) {
416 return ExitStatus::CALLBACK_REQUESTED_STOP;
417 }
418 }
419
420 iter_callbacks_profiler.stop();
421 ScopedProfiler kkt_matrix_build_profiler{kkt_matrix_build_prof};
422
423 // S = diag(s)
424 // Z = diag(z)
425 // Σ = S⁻¹Z
426 const SparseMatrix Σ{s.cwiseInverse().asDiagonal() * z.asDiagonal()};
427
428 // lhs = [H + AᵢᵀΣAᵢ Aₑᵀ]
429 // [ Aₑ 0 ]
430 //
431 // Don't assign upper triangle because solver only uses lower triangle.
432 const SparseMatrix top_left =
433 H + (A_i.transpose() * Σ * A_i).template triangularView<Eigen::Lower>();
434 triplets.clear();
435 triplets.reserve(top_left.nonZeros() + A_e.nonZeros());
436 append_as_triplets(triplets, 0, 0, {top_left, A_e});
437 SparseMatrix lhs(
438 matrices.num_decision_variables + matrices.num_equality_constraints,
439 matrices.num_decision_variables + matrices.num_equality_constraints);
440 lhs.setFromSortedTriplets(triplets.begin(), triplets.end());
441
442 // rhs = −[∇f − Aₑᵀy − Aᵢᵀ(−Σcᵢ + μS⁻¹e + z)]
443 // [ cₑ ]
444 DenseVector rhs{x.rows() + y.rows()};
445 rhs.segment(0, x.rows()) =
446 -g + A_e.transpose() * y +
447 A_i.transpose() * (-Σ * c_i + μ * s.cwiseInverse() + z);
448 rhs.segment(x.rows(), y.rows()) = -c_e;
449
450 kkt_matrix_build_profiler.stop();
451 ScopedProfiler kkt_matrix_decomp_profiler{kkt_matrix_decomp_prof};
452
453 Step step;
454 Scalar α_max(1);
455 Scalar α(1);
456 Scalar α_z(1);
457 bool call_feasibility_restoration = false;
458
459 // Solve the Newton-KKT system
460 //
461 // [H + AᵢᵀΣAᵢ Aₑᵀ][ pˣ] = −[∇f − Aₑᵀy − Aᵢᵀ(−Σcᵢ + μS⁻¹e + z)]
462 // [ Aₑ 0 ][−pʸ] [ cₑ ]
463 if (solver.compute(lhs).info() != Eigen::Success) [[unlikely]] {
464 return ExitStatus::FACTORIZATION_FAILED;
465 }
466
467 kkt_matrix_decomp_profiler.stop();
468 ScopedProfiler kkt_system_solve_profiler{kkt_system_solve_prof};
469
470 auto compute_step = [&](Step& step) {
471 // p = [ pˣ]
472 // [−pʸ]
473 DenseVector p = solver.solve(rhs);
474 step.p_x = p.segment(0, x.rows());
475 step.p_y = -p.segment(x.rows(), y.rows());
476
477 // pˢ = cᵢ − s + Aᵢpˣ
478 // pᶻ = −Σcᵢ + μS⁻¹e − ΣAᵢpˣ
479 step.p_s = c_i - s + A_i * step.p_x;
480 step.p_z = -Σ * c_i + μ * s.cwiseInverse() - Σ * A_i * step.p_x;
481 };
482 compute_step(step);
483
484 kkt_system_solve_profiler.stop();
485 ScopedProfiler line_search_profiler{line_search_prof};
486
487 // αᵐᵃˣ = max(α ∈ (0, 1] : sₖ + αpₖˢ ≥ (1−τⱼ)sₖ)
488 α_max = fraction_to_the_boundary_rule<Scalar>(s, step.p_s, τ);
489 α = α_max;
490
491 // If maximum step size is below minimum, invoke feasibility restoration
492 if (α < α_min) {
493 call_feasibility_restoration = true;
494 }
495
496 // αₖᶻ = max(α ∈ (0, 1] : zₖ + αpₖᶻ ≥ (1−τⱼ)zₖ)
497 α_z = fraction_to_the_boundary_rule<Scalar>(z, step.p_z, τ);
498
499 const FilterEntry<Scalar> current_entry{f, s, c_e, c_i, μ};
500
501 // Loop until a step is accepted
502 while (1) {
503 trial_x = x + α * step.p_x;
504 if (options.feasible_ipm && c_i.cwiseGreater(Scalar(0)).all()) {
505 // If the inequality constraints are all feasible, prevent them from
506 // becoming infeasible again.
507 //
508 // See equation (19.30) in [1].
509 trial_s = trial_c_i;
510 } else {
511 trial_s = s + α * step.p_s;
512 }
513 trial_y = y + α_z * step.p_y;
514 trial_z = z + α_z * step.p_z;
515
516 trial_f = matrices.f(trial_x);
517 trial_c_e = matrices.c_e(trial_x);
518 trial_c_i = matrices.c_i(trial_x);
519
520 // If f(xₖ + αpₖˣ), cₑ(xₖ + αpₖˣ), or cᵢ(xₖ + αpₖˣ) aren't finite, reduce
521 // step size immediately
522 if (!isfinite(trial_f) || !trial_c_e.allFinite() ||
523 !trial_c_i.allFinite()) {
524 // Reduce step size
525 α *= α_reduction_factor;
526
527 if (α < α_min) {
528 call_feasibility_restoration = true;
529 break;
530 }
531 continue;
532 }
533
534 // Check whether filter accepts trial iterate
535 FilterEntry trial_entry{trial_f, trial_s, trial_c_e, trial_c_i, μ};
536 if (filter.try_add(current_entry, trial_entry, step.p_x, g, α)) {
537 // Accept step
538 break;
539 }
540
541 Scalar prev_constraint_violation =
542 c_e.template lpNorm<1>() + (c_i - s).template lpNorm<1>();
543 Scalar next_constraint_violation =
544 trial_c_e.template lpNorm<1>() +
545 (trial_c_i - trial_s).template lpNorm<1>();
546
547 // Second-order corrections
548 //
549 // If first trial point was rejected and constraint violation stayed the
550 // same or went up, apply second-order corrections
551 if (α == α_max &&
552 next_constraint_violation >= prev_constraint_violation) {
553 // Apply second-order corrections. See section 2.4 of [2].
554 auto soc_step = step;
555
556 Scalar α_soc = α;
557 Scalar α_z_soc = α_z;
558 DenseVector c_e_soc = c_e;
559
560 Scalar soc_constraint_violation = next_constraint_violation;
561
562 bool step_acceptable = false;
563 for (int soc_iteration = 0; soc_iteration < 5 && !step_acceptable;
564 ++soc_iteration) {
565 ScopedProfiler soc_profiler{soc_prof};
566
567 scope_exit soc_exit{[&] {
568 soc_profiler.stop();
569
570 if (options.diagnostics && step_acceptable) {
571 print_iteration_diagnostics(
572 iterations, IterationType::SECOND_ORDER_CORRECTION,
573 soc_profiler.current_duration(),
574 unscaled_kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
575 matrices.scaling, g, A_e, trial_c_e, A_i, trial_c_i,
576 trial_s, trial_y, trial_z, Scalar(0)),
577 trial_f,
578 trial_c_e.template lpNorm<1>() +
579 (trial_c_i - trial_s).template lpNorm<1>(),
580 trial_s.dot(trial_z), μ, solver.hessian_regularization(),
581 solver.constraint_jacobian_regularization(),
582 std::max(soc_step.p_x.template lpNorm<Eigen::Infinity>(),
583 soc_step.p_s.template lpNorm<Eigen::Infinity>()),
584 std::max(soc_step.p_y.template lpNorm<Eigen::Infinity>(),
585 soc_step.p_z.template lpNorm<Eigen::Infinity>()),
586 α_soc, Scalar(1), α_reduction_factor, α_z_soc);
587 }
588 }};
589
590 // Rebuild Newton-KKT rhs with updated constraint values.
591 //
592 // rhs = −[∇f − Aₑᵀy − Aᵢᵀ(−Σcᵢ + μS⁻¹e + z)]
593 // [ cₑˢᵒᶜ ]
594 //
595 // where cₑˢᵒᶜ = αc(xₖ) + c(xₖ + αpₖˣ)
596 c_e_soc = α_soc * c_e_soc + trial_c_e;
597 rhs.bottomRows(y.rows()) = -c_e_soc;
598
599 // Solve the Newton-KKT system
600 compute_step(soc_step);
601
602 // αˢᵒᶜ = max(α ∈ (0, 1] : sₖ + αpₖˢ ≥ (1−τⱼ)sₖ)
603 // αₖᶻˢᵒᶜ = max(α ∈ (0, 1] : zₖ + αpₖᶻ ≥ (1−τⱼ)zₖ)
604 α_soc = fraction_to_the_boundary_rule<Scalar>(s, soc_step.p_s, τ);
605 α_z_soc = fraction_to_the_boundary_rule<Scalar>(z, soc_step.p_z, τ);
606
607 trial_x = x + α_soc * soc_step.p_x;
608 trial_s = s + α_soc * soc_step.p_s;
609 trial_y = y + α_z_soc * soc_step.p_y;
610 trial_z = z + α_z_soc * soc_step.p_z;
611
612 trial_f = matrices.f(trial_x);
613 trial_c_e = matrices.c_e(trial_x);
614 trial_c_i = matrices.c_i(trial_x);
615
616 // Check whether filter accepts trial iterate
617 FilterEntry trial_entry{trial_f, trial_s, trial_c_e, trial_c_i, μ};
618 if (filter.try_add(current_entry, trial_entry, step.p_x, g, α)) {
619 step = soc_step;
620 α = α_soc;
621 α_z = α_z_soc;
622 step_acceptable = true;
623 break;
624 }
625
626 // Constraint violation scale factor for second-order corrections
627 constexpr Scalar κ_soc(0.99);
628
629 // If constraint violation hasn't been sufficiently reduced, stop
630 // making second-order corrections
631 next_constraint_violation =
632 trial_c_e.template lpNorm<1>() +
633 (trial_c_i - trial_s).template lpNorm<1>();
634 if (next_constraint_violation > κ_soc * soc_constraint_violation) {
635 break;
636 }
637
638 soc_constraint_violation = next_constraint_violation;
639 }
640
641 if (step_acceptable) {
642 // Accept step
643 break;
644 }
645 }
646
647 // If we got here and α is the full step, the full step was rejected.
648 // Increment the full-step rejected counter to keep track of how many full
649 // steps have been rejected in a row.
650 if (α == α_max) {
651 ++full_step_rejected_counter;
652 }
653
654 // If the full step was rejected enough times in a row, reset the filter
655 // because it may be impeding progress.
656 //
657 // See section 3.2 case I of [2].
658 if (full_step_rejected_counter >= 4 &&
659 filter.max_constraint_violation >
660 current_entry.constraint_violation / Scalar(10) &&
661 filter.last_rejection_due_to_filter()) {
662 filter.max_constraint_violation *= Scalar(0.1);
663 filter.reset();
664 continue;
665 }
666
667 // Reduce step size
668 α *= α_reduction_factor;
669
670 // If step size hit a minimum, check if the KKT error was reduced. If it
671 // wasn't, invoke feasibility restoration.
672 if (α < α_min) {
673 Scalar current_kkt_error = kkt_error<Scalar, KKTErrorType::ONE_NORM>(
674 g, A_e, c_e, A_i, c_i, s, y, z, μ);
675
676 trial_x = x + α_max * step.p_x;
677 trial_s = s + α_max * step.p_s;
678 trial_y = y + α_z * step.p_y;
679 trial_z = z + α_z * step.p_z;
680
681 trial_f = matrices.f(trial_x);
682 trial_c_e = matrices.c_e(trial_x);
683 trial_c_i = matrices.c_i(trial_x);
684
685 Scalar next_kkt_error = kkt_error<Scalar, KKTErrorType::ONE_NORM>(
686 matrices.g(trial_x), matrices.A_e(trial_x), trial_c_e,
687 matrices.A_i(trial_x), trial_c_i, trial_s, trial_y, trial_z, μ);
688
689 // If the step using αᵐᵃˣ reduced the KKT error, accept it anyway
690 if (next_kkt_error <= Scalar(0.999) * current_kkt_error) {
691 // Accept step
692 break;
693 }
694
695 call_feasibility_restoration = true;
696 break;
697 }
698 }
699
700 line_search_profiler.stop();
701
702 if (call_feasibility_restoration) {
703 ScopedProfiler feasibility_restoration_profiler{
704 feasibility_restoration_prof};
705
706 // If already in feasibility restoration mode, running it again won't help
707 if (in_feasibility_restoration) {
708 return ExitStatus::FEASIBILITY_RESTORATION_FAILED;
709 }
710
711 FilterEntry initial_entry{matrices.f(x), s, c_e, c_i, μ};
712
713 // Feasibility restoration phase
714 gch::small_vector<std::function<bool(const IterationInfo<Scalar>& info)>>
715 callbacks;
716 for (auto& callback : iteration_callbacks) {
717 callbacks.emplace_back(callback);
718 }
719 callbacks.emplace_back([&](const IterationInfo<Scalar>& info) {
720 DenseVector trial_x =
721 info.x.segment(0, matrices.num_decision_variables);
722 DenseVector trial_s =
723 info.s.segment(0, matrices.num_inequality_constraints);
724
725 DenseVector trial_c_e = matrices.c_e(trial_x);
726 DenseVector trial_c_i = matrices.c_i(trial_x);
727
728 // If the current iterate sufficiently reduces constraint violation and
729 // is accepted by the normal filter, stop feasibility restoration
730 FilterEntry trial_entry{matrices.f(trial_x), trial_s, trial_c_e,
731 trial_c_i, μ};
732 return trial_entry.constraint_violation <
733 Scalar(0.9) * initial_entry.constraint_violation &&
734 filter.try_add(initial_entry, trial_entry, trial_x - x, g, α);
735 });
736 auto status = feasibility_restoration<Scalar>(matrices, callbacks,
737 options, x, s, y, z, μ);
738
739 if (status != ExitStatus::SUCCESS) {
740 // Report failure
741 return status;
742 }
743
744 f = matrices.f(x);
745 c_e = matrices.c_e(x);
746 c_i = matrices.c_i(x);
747 } else {
748 // If full step was accepted, reset full-step rejected counter
749 if (α == α_max) {
750 full_step_rejected_counter = 0;
751 }
752
753 // Update iterates
754 x = trial_x;
755 s = trial_s;
756 y = trial_y;
757 z = trial_z;
758
759 // A requirement for the convergence proof is that the primal-dual barrier
760 // term Hessian Σₖ₊₁ does not deviate arbitrarily much from the primal
761 // barrier term Hessian μSₖ₊₁⁻².
762 //
763 // Σₖ₊₁ = μSₖ₊₁⁻²
764 // Sₖ₊₁⁻¹Zₖ₊₁ = μSₖ₊₁⁻²
765 // Zₖ₊₁ = μSₖ₊₁⁻¹
766 //
767 // We ensure this by resetting
768 //
769 // zₖ₊₁ = clamp(zₖ₊₁, 1/κ_Σ μ/sₖ₊₁, κ_Σ μ/sₖ₊₁)
770 //
771 // for some fixed κ_Σ ≥ 1 after each step. See equation (16) of [2].
772 for (int row = 0; row < z.rows(); ++row) {
773 constexpr Scalar κ_Σ(1e10);
774 z[row] =
775 std::clamp(z[row], Scalar(1) / κ_Σ * μ / s[row], κ_Σ * μ / s[row]);
776 }
777
778 f = trial_f;
779 c_e = trial_c_e;
780 c_i = trial_c_i;
781 }
782
783 // Update autodiff for Jacobians and Hessian
784 A_e = matrices.A_e(x);
785 A_i = matrices.A_i(x);
786 g = matrices.g(x);
787 H = matrices.H(x, y, z);
788
789 // Update the error
790 E_0 = unscaled_kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
791 matrices.scaling, g, A_e, c_e, A_i, c_i, s, y, z, Scalar(0));
792
793 // Update the barrier parameter if necessary
794 if (E_0 > Scalar(options.tolerance)) {
795 // Barrier parameter scale factor for tolerance checks
796 constexpr Scalar κ_ε(10);
797
798 // While the error is below the desired threshold for this barrier
799 // parameter value, decrease the barrier parameter further
800 Scalar E_μ = kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
801 g, A_e, c_e, A_i, c_i, s, y, z, μ);
802 while (μ > μ_min && E_μ <= κ_ε * μ) {
803 update_barrier_parameter_and_reset_filter();
804 E_μ = kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(g, A_e, c_e, A_i,
805 c_i, s, y, z, μ);
806 }
807 }
808
809 inner_iter_profiler.stop();
810
811 if (options.diagnostics) {
812 print_iteration_diagnostics(
813 iterations,
814 in_feasibility_restoration ? IterationType::FEASIBILITY_RESTORATION
815 : IterationType::NORMAL,
816 inner_iter_profiler.current_duration(), E_0, f,
817 c_e.template lpNorm<1>() + (c_i - s).template lpNorm<1>(), s.dot(z),
818 μ, solver.hessian_regularization(),
819 solver.constraint_jacobian_regularization(),
820 std::max(step.p_x.template lpNorm<Eigen::Infinity>(),
821 step.p_s.template lpNorm<Eigen::Infinity>()),
822 std::max(step.p_y.template lpNorm<Eigen::Infinity>(),
823 step.p_z.template lpNorm<Eigen::Infinity>()),
824 α, α_max, α_reduction_factor, α_z);
825 }
826
827 ++iterations;
828
829 // Check for max iterations
830 if (iterations >= options.max_iterations) {
831 return ExitStatus::MAX_ITERATIONS_EXCEEDED;
832 }
833
834 // Check for max wall clock time
835 if (std::chrono::steady_clock::now() - solve_start_time > options.timeout) {
836 return ExitStatus::TIMEOUT;
837 }
838 }
839
840 return ExitStatus::SUCCESS;
841}
842
843extern template SLEIPNIR_DLLEXPORT ExitStatus
844interior_point(const InteriorPointMatrixCallbacks<double>& matrix_callbacks,
845 std::span<std::function<bool(const IterationInfo<double>& info)>>
846 iteration_callbacks,
847 const Options& options,
848#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
849 const Eigen::ArrayX<bool>& bound_constraint_mask,
850#endif
851 Eigen::Vector<double, Eigen::Dynamic>& x);
852
853} // namespace slp