Sleipnir C++ API
Loading...
Searching...
No Matches
ipm.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <chrono>
7#include <cmath> // IWYU pragma: keep
8#include <functional>
9#include <limits>
10#include <optional>
11#include <span>
12#include <utility>
13
14#include <Eigen/Core>
15#include <Eigen/SparseCore>
16#include <gch/small_vector.hpp>
17
18#include "sleipnir/optimization/solver/control_flow.hpp"
19#include "sleipnir/optimization/solver/exit_status.hpp"
20#include "sleipnir/optimization/solver/ipm_matrix_callbacks.hpp"
21#include "sleipnir/optimization/solver/iteration_info.hpp"
22#include "sleipnir/optimization/solver/options.hpp"
23#include "sleipnir/optimization/solver/util/all_finite.hpp"
24#include "sleipnir/optimization/solver/util/barrier_parameter.hpp"
25#include "sleipnir/optimization/solver/util/constraint_violation.hpp"
26#include "sleipnir/optimization/solver/util/feasibility_restoration.hpp"
27#include "sleipnir/optimization/solver/util/feasibility_restoration_cycle_detector.hpp"
28#include "sleipnir/optimization/solver/util/filter.hpp"
29#include "sleipnir/optimization/solver/util/first_derivatives.hpp"
30#include "sleipnir/optimization/solver/util/fraction_to_the_boundary_rule.hpp"
31#include "sleipnir/optimization/solver/util/function_values.hpp"
32#include "sleipnir/optimization/solver/util/iterates.hpp"
33#include "sleipnir/optimization/solver/util/kkt_error.hpp"
34#include "sleipnir/optimization/solver/util/kkt_system.hpp"
35#include "sleipnir/optimization/solver/util/time_trace.hpp"
36#include "sleipnir/util/assert.hpp"
37#include "sleipnir/util/print_diagnostics.hpp"
38#include "sleipnir/util/scope_exit.hpp"
39#include "sleipnir/util/symbol_exports.hpp"
40#include "sleipnir/util/time_tracer.hpp"
41
42// See docs/algorithms.md#Works_cited for citation definitions.
43//
44// See docs/algorithms.md#Interior-point_method for a derivation of the
45// interior-point method formulation being used.
46
47namespace slp {
48
71template <typename Scalar>
72ExitStatus ipm(
73 const IPMMatrixCallbacks<Scalar>& matrix_callbacks,
74 std::span<std::function<ControlFlow(const IterationInfo<Scalar>& info)>>
75 iteration_callbacks,
76 const Options<Scalar>& options,
77#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
78 const Eigen::ArrayX<bool>& bound_constraint_mask,
79#endif
80 Eigen::VectorX<Scalar>& x) {
81 using DenseVector = Eigen::VectorX<Scalar>;
82
83 const DenseVector c_i = matrix_callbacks.c_i(x);
84
85 IPMIterates<Scalar> iterates{
86 .x = std::move(x),
87 // If cᵢ(x) is sufficiently feasible, start the constraint with zero
88 // violation. Otherwise, make Σ = S⁻¹Z ≈ I.
89 .s = (c_i.array() >= Scalar(1e-2)).select(c_i, Scalar(1)),
90 .y = DenseVector::Zero(matrix_callbacks.num_equality_constraints),
91 .z = DenseVector::Ones(matrix_callbacks.num_inequality_constraints)};
92 Scalar μ = Scalar(0.1) * matrix_callbacks.scaling.f;
93 int iterations = 0;
94
95 const auto status = ipm(matrix_callbacks, iteration_callbacks, options, false,
96#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
97 bound_constraint_mask,
98#endif
99 iterates, μ, iterations);
100
101 x = std::move(iterates.x);
102
103 return status;
104}
105
133template <typename Scalar>
134ExitStatus ipm(
135 const IPMMatrixCallbacks<Scalar>& matrix_callbacks,
136 std::span<std::function<ControlFlow(const IterationInfo<Scalar>& info)>>
137 iteration_callbacks,
138 const Options<Scalar>& options, bool in_feasibility_restoration,
139#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
140 const Eigen::ArrayX<bool>& bound_constraint_mask,
141#endif
142 IPMIterates<Scalar>& iterates, Scalar& μ, int& iterations) {
143 using DenseVector = Eigen::VectorX<Scalar>;
144 using SparseMatrix = Eigen::SparseMatrix<Scalar>;
145
146 using std::isfinite;
147
148 auto& [x, s, y, z] = iterates;
149
150 const auto solve_start_time = std::chrono::steady_clock::now();
151
152 using enum SolveTimeTracerID;
153
154 // Prints final solver diagnostics when the solver exits
155 //
156 // Feasibility restoration still traces so its iterations can be printed, but
157 // the outer solver prints the final solver diagnostics.
158 SolveDiagnostics<ipm_time_trace> diagnostics{options.diagnostics,
159 !in_feasibility_restoration};
160
161 // Set up traced matrix callbacks if diagnostics are enabled. Feasibility
162 // restoration's callbacks call the outer solver's, which are already traced.
163#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
164 std::optional<IPMMatrixCallbacks<Scalar>> traced_matrices;
165 if (options.diagnostics && !in_feasibility_restoration) {
166 traced_matrices.emplace(
167 matrix_callbacks.num_decision_variables,
168 matrix_callbacks.num_equality_constraints,
169 matrix_callbacks.num_inequality_constraints,
170 traced(diagnostics[COST], matrix_callbacks.f),
171 traced(diagnostics[GRADIENT], matrix_callbacks.g),
172 traced(diagnostics[HESSIAN], matrix_callbacks.H),
173 traced(diagnostics[HESSIAN_CONSTRAINTS], matrix_callbacks.H_c),
174 traced(diagnostics[EQUALITY_CONSTRAINTS], matrix_callbacks.c_e),
175 traced(diagnostics[EQUALITY_JACOBIAN], matrix_callbacks.A_e),
176 traced(diagnostics[INEQUALITY_CONSTRAINTS], matrix_callbacks.c_i),
177 traced(diagnostics[INEQUALITY_JACOBIAN], matrix_callbacks.A_i),
178 matrix_callbacks.scaling);
179 }
180 const auto& matrices = traced_matrices ? *traced_matrices : matrix_callbacks;
181#else
182 const auto& matrices = matrix_callbacks;
183#endif
184
185 ScopedTimeTracer setup_tracer{diagnostics[SETUP]};
186
187 IPMFunctionValues<Scalar> function_values{
188 .f = matrices.f(x), .c_e = matrices.c_e(x), .c_i = matrices.c_i(x)};
189 IPMFirstDerivatives<Scalar> first_derivatives{
190 .g = matrices.g(x), .A_e = matrices.A_e(x), .A_i = matrices.A_i(x)};
191 auto& [f, c_e, c_i] = function_values;
192 auto& [g, A_e, A_i] = first_derivatives;
193 SparseMatrix H = matrices.H(x, y, z);
194
195 // Ensure matrix callback dimensions are consistent
196 slp_assert(g.rows() == matrices.num_decision_variables);
197 slp_assert(H.rows() == matrices.num_decision_variables);
198 slp_assert(H.cols() == matrices.num_decision_variables);
199 slp_assert(c_e.rows() == matrices.num_equality_constraints);
200 slp_assert(A_e.rows() == matrices.num_equality_constraints);
201 slp_assert(A_e.cols() == matrices.num_decision_variables);
202 slp_assert(c_i.rows() == matrices.num_inequality_constraints);
203 slp_assert(A_i.rows() == matrices.num_inequality_constraints);
204 slp_assert(A_i.cols() == matrices.num_decision_variables);
205
206 IPMIterates<Scalar> trial_iterates;
207 auto& [trial_x, trial_s, trial_y, trial_z] = trial_iterates;
208
209 IPMFunctionValues<Scalar> trial_function_values;
210 auto& [trial_f, trial_c_e, trial_c_i] = trial_function_values;
211
212 // Check for overconstrained problem
213 if (matrices.num_equality_constraints > matrices.num_decision_variables) {
214 if (options.diagnostics) {
215 print_too_few_dofs_error(c_e);
216 }
217
218 return ExitStatus::TOO_FEW_DOFS;
219 }
220
221 // Check whether initial guess has finite cost, constraints, and derivatives
222 if (!function_values.all_finite() || !first_derivatives.all_finite() ||
223 !all_finite(H)) {
224 return ExitStatus::NONFINITE_INITIAL_GUESS;
225 }
226
227#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
228 // We set sʲ = cᵢʲ(x) for each bound inequality constraint index j
229 s = bound_constraint_mask.select(c_i, s);
230#endif
231
232 // Barrier parameter minimum
233 const Scalar μ_min = matrices.scaling.f * options.tolerance / Scalar(10);
234
235 // Fraction-to-the-boundary rule scale factor minimum
236 constexpr Scalar τ_min(0.99);
237
238 // Fraction-to-the-boundary rule scale factor τ
239 Scalar τ = τ_min;
240
241 IPMKKTSystem<Scalar> kkt_system{H, A_e, A_i};
242
243 Filter<Scalar> filter{constraint_violation(c_e, c_i, s)};
244
245 // The smallest KKT error of a step accepted by the KKT error reduction
246 // fallback for the current barrier parameter. Each fallback step must improve
247 // on it, so finitely many fallback steps are taken before the barrier
248 // subproblem converges.
249 Scalar fallback_kkt_error = std::numeric_limits<Scalar>::infinity();
250
251 // This should be run when the error is below a desired threshold for the
252 // current barrier parameter
253 auto update_barrier_parameter_and_reset_filter = [&] {
254 μ = next_barrier_parameter(μ, μ_min);
255
256 // Update the fraction-to-the-boundary rule scaling factor.
257 //
258 // τⱼ = max(τₘᵢₙ, 1 − μⱼ)
259 //
260 // See equation (8) of [2].
261 τ = std::max(τ_min, Scalar(1) - μ);
262
263 // Reset the filter when the barrier parameter is updated
264 filter.reset();
265 fallback_kkt_error = std::numeric_limits<Scalar>::infinity();
266 };
267
268 // Step size reduction factor
269 constexpr Scalar α_reduction_factor(0.5);
270
271 int full_step_rejected_counter = 0;
272
273 StallDetector<Scalar> stall_detector;
274
275 FeasibilityRestorationCycleDetector<Scalar>
276 feasibility_restoration_cycle_detector;
277
278 // Error
279 Scalar E_0 = unscaled_kkt_error_inf_norm<Scalar>(
280 matrices.scaling, iterates, function_values, first_derivatives, Scalar(0),
281 options.tolerance);
282
283 setup_tracer.stop();
284
285 // Prints initial iterate diagnostics now and closes the iterate diagnostics
286 // table when the solver exits.
287 //
288 // Feasibility restoration's iterations are printed in the outer solver's
289 // table, so it doesn't open its own.
290 IterateDiagnosticsTable table{
291 options.diagnostics && !in_feasibility_restoration,
292 E_0,
293 f,
294 constraint_violation(c_e, c_i, s),
295 s.dot(z),
296 μ};
297
298 while (E_0 > options.tolerance) {
299 ScopedTimeTracer inner_iter_tracer{diagnostics[ITERATION]};
300
301 // Check for diverging iterates
302 if (x.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !x.allFinite() ||
303 s.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !s.allFinite()) {
304 return ExitStatus::DIVERGING_ITERATES;
305 }
306
307 PhaseTimeTracer phase_tracer{diagnostics[CALLBACKS]};
308
309 // Call iteration callbacks
310 for (const auto& callback : iteration_callbacks) {
311 if (callback({iterations, x, s, y, z, g, H, A_e, A_i}) ==
312 ControlFlow::BREAK) {
313 return ExitStatus::CALLBACK_REQUESTED_STOP;
314 }
315 }
316
317 phase_tracer.next(diagnostics[KKT_MATRIX_BUILD]);
318
319 kkt_system.build(H, A_e, A_i, s, z);
320
321 phase_tracer.next(diagnostics[KKT_MATRIX_FACTOR]);
322
323 if (!kkt_system.factorize()) [[unlikely]] {
324 return ExitStatus::FACTORIZATION_FAILED;
325 }
326
327 phase_tracer.next(diagnostics[KKT_SYSTEM_SOLVE]);
328
329 auto step = kkt_system.solve(g, A_e, A_i, y, s, z, μ, c_e, c_i - s);
330
331 phase_tracer.next(diagnostics[LINE_SEARCH]);
332
333 bool call_feasibility_restoration = false;
334
335 // αᵐᵃˣ = max(α ∈ (0, 1] : sₖ + αpₖˢ ≥ (1−τⱼ)sₖ)
336 Scalar α_max = fraction_to_the_boundary_rule<Scalar>(s, step.p_s, τ);
337 Scalar α = α_max;
338
339 // αₖᶻ = max(α ∈ (0, 1] : zₖ + αpₖᶻ ≥ (1−τⱼ)zₖ)
340 Scalar α_z = fraction_to_the_boundary_rule<Scalar>(z, step.p_z, τ);
341
342 const FilterEntry<Scalar> current_entry{s, function_values, μ};
343
344 // Compute the directional derivative of the log-barrier function along the
345 // search direction.
346 //
347 // ϕ_μ(x, s) = f(x) − μ∑ᵢ ln(sᵢ)
348 //
349 // D_ϕ = ∇ϕ_μ(x, s)ᵀ[pˣ pˢ]
350 // = ∇f(x)ᵀpˣ − μ∑ᵢ pᵢˢ/sᵢ
351 const Scalar D_ϕ =
352 g.transpose() * step.p_x - μ * s.cwiseInverse().dot(step.p_s);
353
354 const Scalar α_min = filter.min_step_size(current_entry, D_ϕ);
355
356 // If maximum step size is below minimum, invoke feasibility restoration
357 if (α < α_min) {
358 call_feasibility_restoration = true;
359 }
360
361 // α_min can be zero (e.g., when the constraint violation is zero), so also
362 // end the line search when the step becomes negligible relative to the
363 // iterate. This bounds the number of step size reductions.
364 const Scalar α_tiny = step.tiny_step_size(x, s);
365
366 // Loop until a step is accepted
367 while (!call_feasibility_restoration) {
368 trial_x = x + α * step.p_x;
369 trial_c_i = matrices.c_i(trial_x);
370 if (options.feasible_ipm && c_i.cwiseGreater(Scalar(0)).all()) {
371 // If the inequality constraints are all feasible, prevent them from
372 // becoming infeasible again.
373 //
374 // See equation (19.30) of [1].
375 trial_s = trial_c_i;
376 } else {
377 trial_s = s + α * step.p_s;
378 }
379 trial_y = y + α * step.p_y;
380 trial_z = z + α_z * step.p_z;
381
382 trial_f = matrices.f(trial_x);
383 trial_c_e = matrices.c_e(trial_x);
384
385 // If f(xₖ + αpₖˣ), cₑ(xₖ + αpₖˣ), or cᵢ(xₖ + αpₖˣ) aren't finite, reduce
386 // step size immediately
387 if (!trial_function_values.all_finite()) {
388 // Reduce step size
389 α *= α_reduction_factor;
390
391 if (α < α_min || α < α_tiny) {
392 call_feasibility_restoration = true;
393 break;
394 }
395 continue;
396 }
397
398 // Check whether filter accepts trial iterate
399 FilterEntry trial_entry{trial_s, trial_function_values, μ};
400 if (filter.try_add(current_entry, trial_entry, D_ϕ, α)) {
401 // Accept step
402 break;
403 }
404
405 Scalar prev_constraint_violation = constraint_violation(c_e, c_i, s);
406 Scalar next_constraint_violation =
407 constraint_violation(trial_c_e, trial_c_i, trial_s);
408
409 // Second-order corrections
410 //
411 // If first trial point was rejected and constraint violation stayed the
412 // same or went up, apply second-order corrections
413 if (α == α_max &&
414 next_constraint_violation >= prev_constraint_violation) {
415 // Apply second-order corrections. See section 2.4 of [2].
416 auto soc_step = step;
417
418 Scalar α_soc = α;
419 Scalar α_z_soc = α_z;
420 DenseVector c_e_soc = c_e;
421 DenseVector c_i_minus_s_soc = c_i - s;
422
423 Scalar soc_constraint_violation = next_constraint_violation;
424
425 bool step_acceptable = false;
426 for (int soc_iteration = 0; soc_iteration < 5 && !step_acceptable;
427 ++soc_iteration) {
428 ScopedTimeTracer soc_tracer{diagnostics[SOC]};
429
430 scope_exit soc_exit{[&] {
431 soc_tracer.stop();
432
433 if (options.diagnostics && step_acceptable) {
434 print_iterate_diagnostics(
435 iterations, IterationType::SECOND_ORDER_CORRECTION,
436 soc_tracer.current_duration(),
437 unscaled_kkt_error_inf_norm<Scalar>(
438 matrices.scaling, trial_iterates, trial_function_values,
439 first_derivatives, Scalar(0), options.tolerance),
440 trial_f, constraint_violation(trial_c_e, trial_c_i, trial_s),
441 trial_s.dot(trial_z), μ, kkt_system.hessian_regularization(),
442 kkt_system.constraint_jacobian_regularization(),
443 soc_step.primal_inf_norm(), soc_step.dual_inf_norm(), α_soc,
444 Scalar(1), α_reduction_factor, α_z_soc);
445 }
446 }};
447
448 // Solve the Newton-KKT system with corrected constraint values
449 //
450 // cₑˢᵒᶜ = αˢᵒᶜcₑ(xₖ) + cₑ(xₖ + αˢᵒᶜpˣˢᵒᶜ)
451 // (cᵢ − s)ˢᵒᶜ =
452 // αˢᵒᶜ(cᵢ(xₖ) − sₖ) + cᵢ(xₖ + αˢᵒᶜpˣˢᵒᶜ) − (sₖ + αˢᵒᶜpˢˢᵒᶜ)
453 c_e_soc = α_soc * c_e_soc + trial_c_e;
454 c_i_minus_s_soc = α_soc * c_i_minus_s_soc + trial_c_i - trial_s;
455 soc_step = kkt_system.solve(g, A_e, A_i, y, s, z, μ, c_e_soc,
456 c_i_minus_s_soc);
457
458 // αˢᵒᶜ = max(α ∈ (0, 1] : sₖ + αpₖˢ ≥ (1−τⱼ)sₖ)
459 // αₖᶻˢᵒᶜ = max(α ∈ (0, 1] : zₖ + αpₖᶻ ≥ (1−τⱼ)zₖ)
460 α_soc = fraction_to_the_boundary_rule<Scalar>(s, soc_step.p_s, τ);
461 α_z_soc = fraction_to_the_boundary_rule<Scalar>(z, soc_step.p_z, τ);
462
463 trial_x = x + α_soc * soc_step.p_x;
464 trial_s = s + α_soc * soc_step.p_s;
465 trial_y = y + α_soc * soc_step.p_y;
466 trial_z = z + α_z_soc * soc_step.p_z;
467
468 trial_f = matrices.f(trial_x);
469 trial_c_e = matrices.c_e(trial_x);
470 trial_c_i = matrices.c_i(trial_x);
471
472 // Check whether filter accepts trial iterate
473 FilterEntry trial_entry{trial_s, trial_function_values, μ};
474 if (filter.try_add(current_entry, trial_entry, D_ϕ, α)) {
475 step = soc_step;
476 α = α_soc;
477 α_z = α_z_soc;
478 step_acceptable = true;
479 break;
480 }
481
482 // Constraint violation scale factor for second-order corrections
483 constexpr Scalar κ_soc(0.99);
484
485 // If constraint violation hasn't been sufficiently reduced, stop
486 // making second-order corrections
487 next_constraint_violation =
488 constraint_violation(trial_c_e, trial_c_i, trial_s);
489 if (next_constraint_violation > κ_soc * soc_constraint_violation) {
490 break;
491 }
492
493 soc_constraint_violation = next_constraint_violation;
494 }
495
496 if (step_acceptable) {
497 // Accept step
498 break;
499 }
500 }
501
502 // If we got here and α is the full step, the full step was rejected.
503 // Increment the full-step rejected counter to keep track of how many full
504 // steps have been rejected in a row.
505 if (α == α_max) {
506 ++full_step_rejected_counter;
507 }
508
509 // If the full step was rejected enough times in a row, reset the filter
510 // if it may be impeding progress
511 if (full_step_rejected_counter >= 4 && filter.try_reset(current_entry)) {
512 continue;
513 }
514
515 // Reduce step size
516 α *= α_reduction_factor;
517
518 // If step size hit a minimum or the step became negligible, check if the
519 // KKT error was reduced. If it wasn't, invoke feasibility restoration.
520 if (α < α_min || α < α_tiny) {
521 Scalar current_kkt_error = kkt_error_one_norm<Scalar>(
522 iterates, function_values, first_derivatives, μ);
523
524 trial_x = x + α_max * step.p_x;
525 trial_s = s + α_max * step.p_s;
526 trial_y = y + α_max * step.p_y;
527 trial_z = z + α_z * step.p_z;
528
529 trial_f = matrices.f(trial_x);
530 trial_c_e = matrices.c_e(trial_x);
531 trial_c_i = matrices.c_i(trial_x);
532
533 Scalar next_kkt_error = kkt_error_one_norm<Scalar>(
534 trial_iterates, trial_function_values,
535 IPMFirstDerivatives<Scalar>{matrices.g(trial_x),
536 matrices.A_e(trial_x),
537 matrices.A_i(trial_x)},
538 μ);
539
540 // If the step using αᵐᵃˣ reduced the KKT error below the current
541 // iterate's and every previous fallback step's, accept it anyway
542 if (next_kkt_error <= Scalar(0.999) * std::min(current_kkt_error,
543 fallback_kkt_error) &&
544 constraint_violation(trial_c_e, trial_c_i, trial_s) <=
545 filter.max_constraint_violation) {
546 // Accept step
547 fallback_kkt_error = next_kkt_error;
548 α = α_max;
549 break;
550 }
551
552 call_feasibility_restoration = true;
553 break;
554 }
555 }
556
557 phase_tracer.stop();
558
559 // If solver stalled, invoke feasibility restoration
560 if (!call_feasibility_restoration && !in_feasibility_restoration) {
561 FilterEntry trial_entry{trial_s, trial_function_values, μ};
562 if (stall_detector.update(current_entry, trial_entry, α,
563 filter.min_constraint_violation)) {
564 call_feasibility_restoration = true;
565 }
566 }
567
568 if (call_feasibility_restoration) {
569 ScopedTimeTracer feasibility_restoration_tracer{
570 diagnostics[FEASIBILITY_RESTORATION]};
571
572 // If already in feasibility restoration mode, running it again won't help
573 if (in_feasibility_restoration) {
574 return ExitStatus::FEASIBILITY_RESTORATION_FAILED;
575 }
576
577 FilterEntry initial_entry{s, function_values, μ};
578
579 // Augment the filter with the current iterate so the normal phase can't
580 // return to it
581 filter.add(initial_entry);
582
583 feasibility_restoration_cycle_detector.record_entry(
584 initial_entry.constraint_violation);
585 const bool run_feasibility_restoration_to_convergence =
586 feasibility_restoration_cycle_detector.is_cycling();
587
588 // Feasibility restoration phase
589 gch::small_vector<
590 std::function<ControlFlow(const IterationInfo<Scalar>& info)>>
591 callbacks;
592 for (auto& callback : iteration_callbacks) {
593 callbacks.emplace_back(callback);
594 }
595 callbacks.emplace_back([&](const IterationInfo<Scalar>& info) {
596 if (run_feasibility_restoration_to_convergence) {
597 return ControlFlow::CONTINUE;
598 }
599
600 DenseVector trial_x =
601 info.x.segment(0, matrices.num_decision_variables);
602
603 const IPMFunctionValues<Scalar> trial_function_values{
604 matrices.f(trial_x), matrices.c_e(trial_x), matrices.c_i(trial_x)};
605
606 // Feasibility restoration re-centers the slacks when it returns, so
607 // check the iterate the normal phase will resume from
608 DenseVector trial_s =
609 (trial_function_values.c_i.array() >= Scalar(1e-2))
610 .select(trial_function_values.c_i,
611 info.s.segment(0, matrices.num_inequality_constraints));
612
613 // If the current iterate sufficiently reduces constraint violation and
614 // is accepted by the normal filter, stop feasibility restoration
615 FilterEntry trial_entry{trial_s, trial_function_values, μ};
616 if (trial_entry.constraint_violation <
617 Scalar(0.9) * initial_entry.constraint_violation &&
618 filter.is_acceptable(trial_entry)) {
619 return ControlFlow::BREAK;
620 } else {
621 return ControlFlow::CONTINUE;
622 }
623 });
624 auto status =
625 feasibility_restoration<Scalar>(matrices, callbacks, options,
626#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
627 bound_constraint_mask,
628#endif
629 iterates, μ, iterations);
630
631 if (status != ExitStatus::SUCCESS) {
632 // Report failure
633 return status;
634 }
635
636 function_values = {matrices.f(x), matrices.c_e(x), matrices.c_i(x)};
637
638 // Resuming from an iterate the filter rejects would break the filter's
639 // convergence guarantee, and continuing feasibility restoration from its
640 // converged point would return that point again, so report failure.
641 //
642 // The returned iterate can be rejected because feasibility restoration
643 // stops at its own convergence tolerance instead of driving constraint
644 // violation to zero, so the filter can still hold entries with lower cost
645 // and constraint violation.
646 if (!filter.is_acceptable(FilterEntry{s, function_values, μ})) {
647 return ExitStatus::FEASIBILITY_RESTORATION_FAILED;
648 }
649 } else {
650 // If full step was accepted, reset full-step rejected counter
651 if (α == α_max) {
652 full_step_rejected_counter = 0;
653 }
654
655 // Update iterates and function values
656 iterates = trial_iterates;
657 function_values = trial_function_values;
658 }
659
660 // A requirement for the convergence proof is that the primal-dual barrier
661 // term Hessian Σₖ₊₁ does not deviate arbitrarily much from the primal
662 // barrier term Hessian μSₖ₊₁⁻².
663 //
664 // Σₖ₊₁ = μSₖ₊₁⁻²
665 // Sₖ₊₁⁻¹Zₖ₊₁ = μSₖ₊₁⁻²
666 // Zₖ₊₁ = μSₖ₊₁⁻¹
667 //
668 // We ensure this by resetting
669 //
670 // zₖ₊₁ = clamp(zₖ₊₁, 1/κ_Σ μ/sₖ₊₁, κ_Σ μ/sₖ₊₁)
671 //
672 // for some fixed κ_Σ ≥ 1 after each step. See equation (16) of [2].
673 for (int row = 0; row < z.rows(); ++row) {
674 constexpr Scalar κ_Σ(1e10);
675 z[row] =
676 std::clamp(z[row], Scalar(1) / κ_Σ * μ / s[row], κ_Σ * μ / s[row]);
677 }
678
679 // Update autodiff for Jacobians and Hessian
680 A_e = matrices.A_e(x);
681 A_i = matrices.A_i(x);
682 g = matrices.g(x);
683 H = matrices.H(x, y, z);
684
685 // Update the error
686 E_0 = unscaled_kkt_error_inf_norm<Scalar>(
687 matrices.scaling, iterates, function_values, first_derivatives,
688 Scalar(0), options.tolerance);
689
690 // Update the barrier parameter if necessary
691 if (E_0 > options.tolerance) {
692 // While the error is below the desired threshold for this barrier
693 // parameter value, decrease the barrier parameter further
694 Scalar E_μ = kkt_error_inf_norm<Scalar>(
695 iterates, function_values, first_derivatives, μ, options.tolerance);
696 while (μ > μ_min && E_μ <= barrier_subproblem_tolerance(μ)) {
697 update_barrier_parameter_and_reset_filter();
698 E_μ = kkt_error_inf_norm<Scalar>(
699 iterates, function_values, first_derivatives, μ, options.tolerance);
700 }
701 }
702
703 inner_iter_tracer.stop();
704
705 if (options.diagnostics) {
706 print_iterate_diagnostics(iterations,
707 in_feasibility_restoration
708 ? IterationType::FEASIBILITY_RESTORATION
709 : IterationType::NORMAL,
710 inner_iter_tracer.current_duration(), E_0, f,
711 constraint_violation(c_e, c_i, s), s.dot(z), μ,
712 kkt_system.hessian_regularization(),
713 kkt_system.constraint_jacobian_regularization(),
714 step.primal_inf_norm(), step.dual_inf_norm(), α,
715 α_max, α_reduction_factor, α_z);
716 }
717
718 ++iterations;
719
720 // Check for max iterations
721 if (iterations >= options.max_iterations) {
722 return ExitStatus::MAX_ITERATIONS_EXCEEDED;
723 }
724
725 // Check for max wall clock time
726 if (std::chrono::steady_clock::now() - solve_start_time > options.timeout) {
727 return ExitStatus::TIMEOUT;
728 }
729 }
730
731 if (!isfinite(E_0)) {
732 return ExitStatus::DIVERGING_ITERATES;
733 } else {
734 return ExitStatus::SUCCESS;
735 }
736}
737
738extern template SLEIPNIR_DLLEXPORT ExitStatus
739ipm(const IPMMatrixCallbacks<double>& matrix_callbacks,
740 std::span<std::function<ControlFlow(const IterationInfo<double>& info)>>
741 iteration_callbacks,
742 const Options<double>& options,
743#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
744 const Eigen::ArrayX<bool>& bound_constraint_mask,
745#endif
746 Eigen::VectorX<double>& x);
747
748extern template SLEIPNIR_DLLEXPORT ExitStatus
749ipm(const IPMMatrixCallbacks<double>& matrix_callbacks,
750 std::span<std::function<ControlFlow(const IterationInfo<double>& info)>>
751 iteration_callbacks,
752 const Options<double>& options, bool in_feasibility_restoration,
753#ifdef SLEIPNIR_ENABLE_BOUND_PROJECTION
754 const Eigen::ArrayX<bool>& bound_constraint_mask,
755#endif
756 IPMIterates<double>& iterates, double& μ, int& iterations);
757
758} // namespace slp