Sleipnir C++ API
Loading...
Searching...
No Matches
sqp.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <chrono>
6#include <cmath>
7#include <functional>
8#include <span>
9
10#include <Eigen/Core>
11#include <Eigen/SparseCore>
12#include <gch/small_vector.hpp>
13
14#include "sleipnir/optimization/solver/exit_status.hpp"
15#include "sleipnir/optimization/solver/iteration_info.hpp"
16#include "sleipnir/optimization/solver/options.hpp"
17#include "sleipnir/optimization/solver/sqp_matrix_callbacks.hpp"
18#include "sleipnir/optimization/solver/util/all_finite.hpp"
19#include "sleipnir/optimization/solver/util/append_as_triplets.hpp"
20#include "sleipnir/optimization/solver/util/feasibility_restoration.hpp"
21#include "sleipnir/optimization/solver/util/filter.hpp"
22#include "sleipnir/optimization/solver/util/is_locally_infeasible.hpp"
23#include "sleipnir/optimization/solver/util/kkt_error.hpp"
24#include "sleipnir/optimization/solver/util/regularized_ldlt.hpp"
25#include "sleipnir/util/assert.hpp"
26#include "sleipnir/util/print_diagnostics.hpp"
27#include "sleipnir/util/profiler.hpp"
28#include "sleipnir/util/scope_exit.hpp"
29#include "sleipnir/util/symbol_exports.hpp"
30
31// See docs/algorithms.md#Works_cited for citation definitions.
32
33namespace slp {
34
55template <typename Scalar>
56ExitStatus sqp(const SQPMatrixCallbacks<Scalar>& matrix_callbacks,
57 std::span<std::function<bool(const IterationInfo<Scalar>& info)>>
58 iteration_callbacks,
59 const Options& options,
60 Eigen::Vector<Scalar, Eigen::Dynamic>& x) {
61 using DenseVector = Eigen::Vector<Scalar, Eigen::Dynamic>;
62
63 DenseVector y = DenseVector::Zero(matrix_callbacks.num_equality_constraints);
64
65 return sqp(matrix_callbacks, iteration_callbacks, options, x, y);
66}
67
90template <typename Scalar>
91ExitStatus sqp(const SQPMatrixCallbacks<Scalar>& matrix_callbacks,
92 std::span<std::function<bool(const IterationInfo<Scalar>& info)>>
93 iteration_callbacks,
94 const Options& options, Eigen::Vector<Scalar, Eigen::Dynamic>& x,
95 Eigen::Vector<Scalar, Eigen::Dynamic>& y) {
96 using DenseVector = Eigen::Vector<Scalar, Eigen::Dynamic>;
97 using SparseMatrix = Eigen::SparseMatrix<Scalar>;
98 using SparseVector = Eigen::SparseVector<Scalar>;
99
101 struct Step {
103 DenseVector p_x;
105 DenseVector p_y;
106 };
107
108 using std::isfinite;
109
110 const auto solve_start_time = std::chrono::steady_clock::now();
111
112 gch::small_vector<SolveProfiler> solve_profilers;
113 solve_profilers.emplace_back("solver");
114 solve_profilers.emplace_back("↳ setup");
115 solve_profilers.emplace_back("↳ iteration");
116 solve_profilers.emplace_back(" ↳ feasibility check");
117 solve_profilers.emplace_back(" ↳ callbacks");
118 solve_profilers.emplace_back(" ↳ KKT matrix build");
119 solve_profilers.emplace_back(" ↳ KKT matrix decomp");
120 solve_profilers.emplace_back(" ↳ KKT system solve");
121 solve_profilers.emplace_back(" ↳ line search");
122 solve_profilers.emplace_back(" ↳ SOC");
123 solve_profilers.emplace_back(" ↳ feas. restoration");
124 solve_profilers.emplace_back(" ↳ f(x)");
125 solve_profilers.emplace_back(" ↳ ∇f(x)");
126 solve_profilers.emplace_back(" ↳ ∇²ₓₓL");
127 solve_profilers.emplace_back(" ↳ ∇²ₓₓL_c");
128 solve_profilers.emplace_back(" ↳ cₑ(x)");
129 solve_profilers.emplace_back(" ↳ ∂cₑ/∂x");
130
131 auto& solver_prof = solve_profilers[0];
132 auto& setup_prof = solve_profilers[1];
133 auto& inner_iter_prof = solve_profilers[2];
134 auto& feasibility_check_prof = solve_profilers[3];
135 auto& iter_callbacks_prof = solve_profilers[4];
136 auto& kkt_matrix_build_prof = solve_profilers[5];
137 auto& kkt_matrix_decomp_prof = solve_profilers[6];
138 auto& kkt_system_solve_prof = solve_profilers[7];
139 auto& line_search_prof = solve_profilers[8];
140 auto& soc_prof = solve_profilers[9];
141 auto& feasibility_restoration_prof = solve_profilers[10];
142
143 // Set up profiled matrix callbacks
144#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
145 auto& f_prof = solve_profilers[11];
146 auto& g_prof = solve_profilers[12];
147 auto& H_prof = solve_profilers[13];
148 auto& H_c_prof = solve_profilers[14];
149 auto& c_e_prof = solve_profilers[15];
150 auto& A_e_prof = solve_profilers[16];
151
152 SQPMatrixCallbacks<Scalar> matrices{
153 matrix_callbacks.num_decision_variables,
154 matrix_callbacks.num_equality_constraints,
155 [&](const DenseVector& x) -> Scalar {
156 ScopedProfiler prof{f_prof};
157 return matrix_callbacks.f(x);
158 },
159 [&](const DenseVector& x) -> SparseVector {
160 ScopedProfiler prof{g_prof};
161 return matrix_callbacks.g(x);
162 },
163 [&](const DenseVector& x, const DenseVector& y) -> SparseMatrix {
164 ScopedProfiler prof{H_prof};
165 return matrix_callbacks.H(x, y);
166 },
167 [&](const DenseVector& x, const DenseVector& y) -> SparseMatrix {
168 ScopedProfiler prof{H_c_prof};
169 return matrix_callbacks.H_c(x, y);
170 },
171 [&](const DenseVector& x) -> DenseVector {
172 ScopedProfiler prof{c_e_prof};
173 return matrix_callbacks.c_e(x);
174 },
175 [&](const DenseVector& x) -> SparseMatrix {
176 ScopedProfiler prof{A_e_prof};
177 return matrix_callbacks.A_e(x);
178 },
179 matrix_callbacks.scaling};
180#else
181 const auto& matrices = matrix_callbacks;
182#endif
183
184 solver_prof.start();
185 setup_prof.start();
186
187 Scalar f = matrices.f(x);
188 SparseVector g = matrices.g(x);
189 SparseMatrix H = matrices.H(x, y);
190 DenseVector c_e = matrices.c_e(x);
191 SparseMatrix A_e = matrices.A_e(x);
192
193 // Ensure matrix callback dimensions are consistent
194 slp_assert(g.rows() == matrices.num_decision_variables);
195 slp_assert(H.rows() == matrices.num_decision_variables);
196 slp_assert(H.cols() == matrices.num_decision_variables);
197 slp_assert(c_e.rows() == matrices.num_equality_constraints);
198 slp_assert(A_e.rows() == matrices.num_equality_constraints);
199 slp_assert(A_e.cols() == matrices.num_decision_variables);
200
201 DenseVector trial_x;
202 DenseVector trial_y;
203
204 Scalar trial_f;
205 DenseVector trial_c_e;
206
207 // Check for overconstrained problem
208 if (matrices.num_equality_constraints > matrices.num_decision_variables) {
209 if (options.diagnostics) {
210 print_too_few_dofs_error(c_e);
211 }
212
213 return ExitStatus::TOO_FEW_DOFS;
214 }
215
216 // Check whether initial guess has finite cost, constraints, and derivatives
217 if (!isfinite(f) || !all_finite(g) || !all_finite(H) || !c_e.allFinite() ||
218 !all_finite(A_e)) {
219 return ExitStatus::NONFINITE_INITIAL_GUESS;
220 }
221
222 int iterations = 0;
223
224 Filter<Scalar> filter{c_e.template lpNorm<1>()};
225
226 // Kept outside the loop so its storage can be reused
227 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
228
229 const int lhs_rows =
230 matrices.num_decision_variables + matrices.num_equality_constraints;
231 RegularizedLDLT<Scalar> solver{
232 // Use sparse solver if lower triangle fills < 25% of system
233 H.nonZeros() + A_e.nonZeros() < 0.25 * lhs_rows * lhs_rows,
234 matrices.num_decision_variables, matrices.num_equality_constraints};
235
236 // Variables for determining when a step is acceptable
237 constexpr Scalar α_reduction_factor(0.5);
238 constexpr Scalar α_min(1e-7);
239
240 int full_step_rejected_counter = 0;
241
242 // Error
243 Scalar E_0 = unscaled_kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
244 matrices.scaling, g, A_e, c_e, y);
245
246 setup_prof.stop();
247
248 // Prints final solver diagnostics when the solver exits
249 scope_exit exit{[&] {
250 if (options.diagnostics) {
251 solver_prof.stop();
252 if (iterations > 0) {
253 print_bottom_iteration_diagnostics();
254 }
255 print_solver_diagnostics(solve_profilers);
256 }
257 }};
258
259 while (E_0 > Scalar(options.tolerance)) {
260 ScopedProfiler inner_iter_profiler{inner_iter_prof};
261 ScopedProfiler feasibility_check_profiler{feasibility_check_prof};
262
263 // Check for local equality constraint infeasibility
264 if (is_equality_locally_infeasible(A_e, c_e)) {
265 if (options.diagnostics) {
266 print_c_e_local_infeasibility_error(c_e);
267 }
268
269 return ExitStatus::LOCALLY_INFEASIBLE;
270 }
271
272 // Check for diverging iterates
273 if (x.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !x.allFinite()) {
274 return ExitStatus::DIVERGING_ITERATES;
275 }
276
277 feasibility_check_profiler.stop();
278 ScopedProfiler iter_callbacks_profiler{iter_callbacks_prof};
279
280 // Call iteration callbacks
281 for (const auto& callback : iteration_callbacks) {
282 if (callback({iterations, x, {}, y, {}, g, H, A_e, {}})) {
283 return ExitStatus::CALLBACK_REQUESTED_STOP;
284 }
285 }
286
287 iter_callbacks_profiler.stop();
288 ScopedProfiler kkt_matrix_build_profiler{kkt_matrix_build_prof};
289
290 // lhs = [H Aₑᵀ]
291 // [Aₑ 0 ]
292 //
293 // Don't assign upper triangle because solver only uses lower triangle.
294 triplets.clear();
295 triplets.reserve(H.nonZeros() + A_e.nonZeros());
296 append_as_triplets(triplets, 0, 0, {H, A_e});
297 SparseMatrix lhs(
298 matrices.num_decision_variables + matrices.num_equality_constraints,
299 matrices.num_decision_variables + matrices.num_equality_constraints);
300 lhs.setFromSortedTriplets(triplets.begin(), triplets.end());
301
302 // rhs = −[∇f − Aₑᵀy]
303 // [ cₑ ]
304 DenseVector rhs{x.rows() + y.rows()};
305 rhs.segment(0, x.rows()) = -g + A_e.transpose() * y;
306 rhs.segment(x.rows(), y.rows()) = -c_e;
307
308 kkt_matrix_build_profiler.stop();
309 ScopedProfiler kkt_matrix_decomp_profiler{kkt_matrix_decomp_prof};
310
311 Step step;
312 constexpr Scalar α_max(1);
313 Scalar α(1);
314 bool call_feasibility_restoration = false;
315
316 // Solve the Newton-KKT system
317 //
318 // [H Aₑᵀ][ pˣ] = −[∇f − Aₑᵀy]
319 // [Aₑ 0 ][−pʸ] [ cₑ ]
320 if (solver.compute(lhs).info() != Eigen::Success) [[unlikely]] {
321 return ExitStatus::FACTORIZATION_FAILED;
322 }
323
324 kkt_matrix_decomp_profiler.stop();
325 ScopedProfiler kkt_system_solve_profiler{kkt_system_solve_prof};
326
327 auto compute_step = [&](Step& step) {
328 // p = [ pˣ]
329 // [−pʸ]
330 DenseVector p = solver.solve(rhs);
331 step.p_x = p.segment(0, x.rows());
332 step.p_y = -p.segment(x.rows(), y.rows());
333 };
334 compute_step(step);
335
336 kkt_system_solve_profiler.stop();
337 ScopedProfiler line_search_profiler{line_search_prof};
338
339 α = α_max;
340
341 const FilterEntry<Scalar> current_entry{f, c_e};
342
343 // Loop until a step is accepted
344 while (1) {
345 trial_x = x + α * step.p_x;
346 trial_y = y + α * step.p_y;
347
348 trial_f = matrices.f(trial_x);
349 trial_c_e = matrices.c_e(trial_x);
350
351 // If f(xₖ + αpₖˣ) or cₑ(xₖ + αpₖˣ) aren't finite, reduce step size
352 // immediately
353 if (!isfinite(trial_f) || !trial_c_e.allFinite()) {
354 // Reduce step size
355 α *= α_reduction_factor;
356
357 if (α < α_min) {
358 call_feasibility_restoration = true;
359 break;
360 }
361 continue;
362 }
363
364 // Check whether filter accepts trial iterate
365 FilterEntry trial_entry{trial_f, trial_c_e};
366 if (filter.try_add(current_entry, trial_entry, step.p_x, g, α)) {
367 // Accept step
368 break;
369 }
370
371 Scalar prev_constraint_violation = c_e.template lpNorm<1>();
372 Scalar next_constraint_violation = trial_c_e.template lpNorm<1>();
373
374 // Second-order corrections
375 //
376 // If first trial point was rejected and constraint violation stayed the
377 // same or went up, apply second-order corrections
378 if (α == α_max &&
379 next_constraint_violation >= prev_constraint_violation) {
380 // Apply second-order corrections. See section 2.4 of [2].
381 auto soc_step = step;
382
383 Scalar α_soc = α;
384 DenseVector c_e_soc = c_e;
385
386 Scalar soc_constraint_violation = next_constraint_violation;
387
388 bool step_acceptable = false;
389 for (int soc_iteration = 0; soc_iteration < 5 && !step_acceptable;
390 ++soc_iteration) {
391 ScopedProfiler soc_profiler{soc_prof};
392
393 scope_exit soc_exit{[&] {
394 soc_profiler.stop();
395
396 if (options.diagnostics && step_acceptable) {
397 print_iteration_diagnostics(
398 iterations, IterationType::SECOND_ORDER_CORRECTION,
399 soc_profiler.current_duration(),
400 kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
401 g, A_e, trial_c_e, trial_y),
402 trial_f, trial_c_e.template lpNorm<1>(), Scalar(0), Scalar(0),
403 solver.hessian_regularization(),
404 solver.constraint_jacobian_regularization(),
405 soc_step.p_x.template lpNorm<Eigen::Infinity>(),
406 soc_step.p_y.template lpNorm<Eigen::Infinity>(), α_soc,
407 Scalar(1), α_reduction_factor, Scalar(1));
408 }
409 }};
410
411 // Rebuild Newton-KKT rhs with updated constraint values.
412 //
413 // rhs = −[∇f − Aₑᵀy]
414 // [ cₑˢᵒᶜ ]
415 //
416 // where cₑˢᵒᶜ = αc(xₖ) + c(xₖ + αpₖˣ)
417 c_e_soc = α_soc * c_e_soc + trial_c_e;
418 rhs.bottomRows(y.rows()) = -c_e_soc;
419
420 // Solve the Newton-KKT system
421 compute_step(soc_step);
422
423 trial_x = x + α_soc * soc_step.p_x;
424 trial_y = y + α_soc * soc_step.p_y;
425
426 trial_f = matrices.f(trial_x);
427 trial_c_e = matrices.c_e(trial_x);
428
429 // Check whether the filter accepts trial iterate
430 FilterEntry trial_entry{trial_f, trial_c_e};
431 if (filter.try_add(current_entry, trial_entry, step.p_x, g, α)) {
432 step = soc_step;
433 α = α_soc;
434 step_acceptable = true;
435 break;
436 }
437
438 // Constraint violation scale factor for second-order corrections
439 constexpr Scalar κ_soc(0.99);
440
441 // If constraint violation hasn't been sufficiently reduced, stop
442 // making second-order corrections
443 next_constraint_violation = trial_c_e.template lpNorm<1>();
444 if (next_constraint_violation > κ_soc * soc_constraint_violation) {
445 break;
446 }
447
448 soc_constraint_violation = next_constraint_violation;
449 }
450
451 if (step_acceptable) {
452 // Accept step
453 break;
454 }
455 }
456
457 // If we got here and α is the full step, the full step was rejected.
458 // Increment the full-step rejected counter to keep track of how many full
459 // steps have been rejected in a row.
460 if (α == α_max) {
461 ++full_step_rejected_counter;
462 }
463
464 // If the full step was rejected enough times in a row, reset the filter
465 // because it may be impeding progress.
466 //
467 // See section 3.2 case I of [2].
468 if (full_step_rejected_counter >= 4 &&
469 filter.max_constraint_violation >
470 current_entry.constraint_violation / Scalar(10) &&
471 filter.last_rejection_due_to_filter()) {
472 filter.max_constraint_violation *= Scalar(0.1);
473 filter.reset();
474 continue;
475 }
476
477 // Reduce step size
478 α *= α_reduction_factor;
479
480 // If step size hit a minimum, check if the KKT error was reduced. If it
481 // wasn't, invoke feasibility restoration.
482 if (α < α_min) {
483 Scalar current_kkt_error =
484 kkt_error<Scalar, KKTErrorType::ONE_NORM>(g, A_e, c_e, y);
485
486 trial_x = x + α_max * step.p_x;
487 trial_y = y + α_max * step.p_y;
488
489 trial_f = matrices.f(trial_x);
490 trial_c_e = matrices.c_e(trial_x);
491
492 Scalar next_kkt_error = kkt_error<Scalar, KKTErrorType::ONE_NORM>(
493 matrices.g(trial_x), matrices.A_e(trial_x), trial_c_e, trial_y);
494
495 // If the step using αᵐᵃˣ reduced the KKT error, accept it anyway
496 if (next_kkt_error <= Scalar(0.999) * current_kkt_error) {
497 // Accept step
498 break;
499 }
500
501 call_feasibility_restoration = true;
502 break;
503 }
504 }
505
506 line_search_profiler.stop();
507
508 if (call_feasibility_restoration) {
509 ScopedProfiler feasibility_restoration_profiler{
510 feasibility_restoration_prof};
511
512 FilterEntry initial_entry{matrices.f(x), c_e};
513
514 // Feasibility restoration phase
515 gch::small_vector<std::function<bool(const IterationInfo<Scalar>& info)>>
516 callbacks;
517 for (auto& callback : iteration_callbacks) {
518 callbacks.emplace_back(callback);
519 }
520 callbacks.emplace_back([&](const IterationInfo<Scalar>& info) {
521 DenseVector trial_x =
522 info.x.segment(0, matrices.num_decision_variables);
523
524 DenseVector trial_c_e = matrices.c_e(trial_x);
525
526 FilterEntry trial_entry{matrices.f(trial_x), trial_c_e};
527
528 // If the current iterate sufficiently reduces constraint violation and
529 // is accepted by the normal filter, stop feasibility restoration
530 return trial_entry.constraint_violation <
531 Scalar(0.9) * initial_entry.constraint_violation &&
532 filter.try_add(initial_entry, trial_entry, trial_x - x, g, α);
533 });
534 auto status =
535 feasibility_restoration<Scalar>(matrices, callbacks, options, x, y);
536
537 if (status != ExitStatus::SUCCESS) {
538 // Report failure
539 return status;
540 }
541
542 f = matrices.f(x);
543 c_e = matrices.c_e(x);
544 } else {
545 // If full step was accepted, reset full-step rejected counter
546 if (α == α_max) {
547 full_step_rejected_counter = 0;
548 }
549
550 // Update iterates
551 x = trial_x;
552 y = trial_y;
553
554 f = trial_f;
555 c_e = trial_c_e;
556 }
557
558 // Update autodiff for Jacobians and Hessian
559 A_e = matrices.A_e(x);
560 g = matrices.g(x);
561 H = matrices.H(x, y);
562
563 // Update the error
564 E_0 = unscaled_kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
565 matrices.scaling, g, A_e, c_e, y);
566
567 inner_iter_profiler.stop();
568
569 if (options.diagnostics) {
570 print_iteration_diagnostics(iterations, IterationType::NORMAL,
571 inner_iter_profiler.current_duration(), E_0,
572 f, c_e.template lpNorm<1>(), Scalar(0),
573 Scalar(0), solver.hessian_regularization(),
574 solver.constraint_jacobian_regularization(),
575 step.p_x.template lpNorm<Eigen::Infinity>(),
576 step.p_y.template lpNorm<Eigen::Infinity>(),
577 α, α_max, α_reduction_factor, α);
578 }
579
580 ++iterations;
581
582 // Check for max iterations
583 if (iterations >= options.max_iterations) {
584 return ExitStatus::MAX_ITERATIONS_EXCEEDED;
585 }
586
587 // Check for max wall clock time
588 if (std::chrono::steady_clock::now() - solve_start_time > options.timeout) {
589 return ExitStatus::TIMEOUT;
590 }
591 }
592
593 return ExitStatus::SUCCESS;
594}
595
596extern template SLEIPNIR_DLLEXPORT ExitStatus
597sqp(const SQPMatrixCallbacks<double>& matrix_callbacks,
598 std::span<std::function<bool(const IterationInfo<double>& info)>>
599 iteration_callbacks,
600 const Options& options, Eigen::Vector<double, Eigen::Dynamic>& x);
601
602} // namespace slp