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#else
180 const auto& matrices = matrix_callbacks;
181#endif
182
183 solver_prof.start();
184 setup_prof.start();
185
186 Scalar f = matrices.f(x);
187 SparseVector g = matrices.g(x);
188 SparseMatrix H = matrices.H(x, y);
189 DenseVector c_e = matrices.c_e(x);
190 SparseMatrix A_e = matrices.A_e(x);
191
192 // Ensure matrix callback dimensions are consistent
193 slp_assert(g.rows() == matrices.num_decision_variables);
194 slp_assert(H.rows() == matrices.num_decision_variables);
195 slp_assert(H.cols() == matrices.num_decision_variables);
196 slp_assert(c_e.rows() == matrices.num_equality_constraints);
197 slp_assert(A_e.rows() == matrices.num_equality_constraints);
198 slp_assert(A_e.cols() == matrices.num_decision_variables);
199
200 DenseVector trial_x;
201 DenseVector trial_y;
202
203 Scalar trial_f;
204 DenseVector trial_c_e;
205
206 // Check for overconstrained problem
207 if (matrices.num_equality_constraints > matrices.num_decision_variables) {
208 if (options.diagnostics) {
209 print_too_few_dofs_error(c_e);
210 }
211
212 return ExitStatus::TOO_FEW_DOFS;
213 }
214
215 // Check whether initial guess has finite cost, constraints, and derivatives
216 if (!isfinite(f) || !all_finite(g) || !all_finite(H) || !c_e.allFinite() ||
217 !all_finite(A_e)) {
218 return ExitStatus::NONFINITE_INITIAL_GUESS;
219 }
220
221 int iterations = 0;
222
223 Filter<Scalar> filter{c_e.template lpNorm<1>()};
224
225 // Kept outside the loop so its storage can be reused
226 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
227
228 RegularizedLDLT<Scalar> solver{matrices.num_decision_variables,
229 matrices.num_equality_constraints};
230
231 // Variables for determining when a step is acceptable
232 constexpr Scalar α_reduction_factor(0.5);
233 constexpr Scalar α_min(1e-7);
234
235 int full_step_rejected_counter = 0;
236
237 // Error
238 Scalar E_0 = kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(g, A_e, c_e, y);
239
240 setup_prof.stop();
241
242 // Prints final solver diagnostics when the solver exits
243 scope_exit exit{[&] {
244 if (options.diagnostics) {
245 solver_prof.stop();
246 if (iterations > 0) {
247 print_bottom_iteration_diagnostics();
248 }
249 print_solver_diagnostics(solve_profilers);
250 }
251 }};
252
253 while (E_0 > Scalar(options.tolerance)) {
254 ScopedProfiler inner_iter_profiler{inner_iter_prof};
255 ScopedProfiler feasibility_check_profiler{feasibility_check_prof};
256
257 // Check for local equality constraint infeasibility
258 if (is_equality_locally_infeasible(A_e, c_e)) {
259 if (options.diagnostics) {
260 print_c_e_local_infeasibility_error(c_e);
261 }
262
263 return ExitStatus::LOCALLY_INFEASIBLE;
264 }
265
266 // Check for diverging iterates
267 if (x.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !x.allFinite()) {
268 return ExitStatus::DIVERGING_ITERATES;
269 }
270
271 feasibility_check_profiler.stop();
272 ScopedProfiler iter_callbacks_profiler{iter_callbacks_prof};
273
274 // Call iteration callbacks
275 for (const auto& callback : iteration_callbacks) {
276 if (callback({iterations, x, {}, y, {}, g, H, A_e, {}})) {
277 return ExitStatus::CALLBACK_REQUESTED_STOP;
278 }
279 }
280
281 iter_callbacks_profiler.stop();
282 ScopedProfiler kkt_matrix_build_profiler{kkt_matrix_build_prof};
283
284 // lhs = [H Aₑᵀ]
285 // [Aₑ 0 ]
286 //
287 // Don't assign upper triangle because solver only uses lower triangle.
288 triplets.clear();
289 triplets.reserve(H.nonZeros() + A_e.nonZeros());
290 append_as_triplets(triplets, 0, 0, {H, A_e});
291 SparseMatrix lhs(
292 matrices.num_decision_variables + matrices.num_equality_constraints,
293 matrices.num_decision_variables + matrices.num_equality_constraints);
294 lhs.setFromSortedTriplets(triplets.begin(), triplets.end());
295
296 // rhs = −[∇f − Aₑᵀy]
297 // [ cₑ ]
298 DenseVector rhs{x.rows() + y.rows()};
299 rhs.segment(0, x.rows()) = -g + A_e.transpose() * y;
300 rhs.segment(x.rows(), y.rows()) = -c_e;
301
302 kkt_matrix_build_profiler.stop();
303 ScopedProfiler kkt_matrix_decomp_profiler{kkt_matrix_decomp_prof};
304
305 Step step;
306 constexpr Scalar α_max(1);
307 Scalar α(1);
308 bool call_feasibility_restoration = false;
309
310 // Solve the Newton-KKT system
311 //
312 // [H Aₑᵀ][ pˣ] = −[∇f − Aₑᵀy]
313 // [Aₑ 0 ][−pʸ] [ cₑ ]
314 if (solver.compute(lhs).info() != Eigen::Success) [[unlikely]] {
315 return ExitStatus::FACTORIZATION_FAILED;
316 }
317
318 kkt_matrix_decomp_profiler.stop();
319 ScopedProfiler kkt_system_solve_profiler{kkt_system_solve_prof};
320
321 auto compute_step = [&](Step& step) {
322 // p = [ pˣ]
323 // [−pʸ]
324 DenseVector p = solver.solve(rhs);
325 step.p_x = p.segment(0, x.rows());
326 step.p_y = -p.segment(x.rows(), y.rows());
327 };
328 compute_step(step);
329
330 kkt_system_solve_profiler.stop();
331 ScopedProfiler line_search_profiler{line_search_prof};
332
333 α = α_max;
334
335 const FilterEntry<Scalar> current_entry{f, c_e};
336
337 // Loop until a step is accepted
338 while (1) {
339 trial_x = x + α * step.p_x;
340 trial_y = y + α * step.p_y;
341
342 trial_f = matrices.f(trial_x);
343 trial_c_e = matrices.c_e(trial_x);
344
345 // If f(xₖ + αpₖˣ) or cₑ(xₖ + αpₖˣ) aren't finite, reduce step size
346 // immediately
347 if (!isfinite(trial_f) || !trial_c_e.allFinite()) {
348 // Reduce step size
349 α *= α_reduction_factor;
350
351 if (α < α_min) {
352 call_feasibility_restoration = true;
353 break;
354 }
355 continue;
356 }
357
358 // Check whether filter accepts trial iterate
359 FilterEntry trial_entry{trial_f, trial_c_e};
360 if (filter.try_add(current_entry, trial_entry, step.p_x, g, α)) {
361 // Accept step
362 break;
363 }
364
365 Scalar prev_constraint_violation = c_e.template lpNorm<1>();
366 Scalar next_constraint_violation = trial_c_e.template lpNorm<1>();
367
368 // Second-order corrections
369 //
370 // If first trial point was rejected and constraint violation stayed the
371 // same or went up, apply second-order corrections
372 if (α == α_max &&
373 next_constraint_violation >= prev_constraint_violation) {
374 // Apply second-order corrections. See section 2.4 of [2].
375 auto soc_step = step;
376
377 Scalar α_soc = α;
378 DenseVector c_e_soc = c_e;
379
380 bool step_acceptable = false;
381 for (int soc_iteration = 0; soc_iteration < 5 && !step_acceptable;
382 ++soc_iteration) {
383 ScopedProfiler soc_profiler{soc_prof};
384
385 scope_exit soc_exit{[&] {
386 soc_profiler.stop();
387
388 if (options.diagnostics) {
389 print_iteration_diagnostics(
390 iterations,
391 step_acceptable ? IterationType::ACCEPTED_SOC
392 : IterationType::REJECTED_SOC,
393 soc_profiler.current_duration(),
394 kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(
395 g, A_e, trial_c_e, trial_y),
396 trial_f, trial_c_e.template lpNorm<1>(), Scalar(0), Scalar(0),
397 solver.hessian_regularization(), α_soc, Scalar(1),
398 α_reduction_factor, Scalar(1));
399 }
400 }};
401
402 // Rebuild Newton-KKT rhs with updated constraint values.
403 //
404 // rhs = −[∇f − Aₑᵀy]
405 // [ cₑˢᵒᶜ ]
406 //
407 // where cₑˢᵒᶜ = αc(xₖ) + c(xₖ + αpₖˣ)
408 c_e_soc = α_soc * c_e_soc + trial_c_e;
409 rhs.bottomRows(y.rows()) = -c_e_soc;
410
411 // Solve the Newton-KKT system
412 compute_step(soc_step);
413
414 trial_x = x + α_soc * soc_step.p_x;
415 trial_y = y + α_soc * soc_step.p_y;
416
417 trial_f = matrices.f(trial_x);
418 trial_c_e = matrices.c_e(trial_x);
419
420 // Constraint violation scale factor for second-order corrections
421 constexpr Scalar κ_soc(0.99);
422
423 // If constraint violation hasn't been sufficiently reduced, stop
424 // making second-order corrections
425 next_constraint_violation = trial_c_e.template lpNorm<1>();
426 if (next_constraint_violation > κ_soc * prev_constraint_violation) {
427 break;
428 }
429
430 // Check whether filter accepts trial iterate
431 FilterEntry trial_entry{trial_f, trial_c_e};
432 if (filter.try_add(current_entry, trial_entry, step.p_x, g, α)) {
433 step = soc_step;
434 α = α_soc;
435 step_acceptable = true;
436 }
437 }
438
439 if (step_acceptable) {
440 // Accept step
441 break;
442 }
443 }
444
445 // If we got here and α is the full step, the full step was rejected.
446 // Increment the full-step rejected counter to keep track of how many full
447 // steps have been rejected in a row.
448 if (α == α_max) {
449 ++full_step_rejected_counter;
450 }
451
452 // If the full step was rejected enough times in a row, reset the filter
453 // because it may be impeding progress.
454 //
455 // See section 3.2 case I of [2].
456 if (full_step_rejected_counter >= 4 &&
457 filter.max_constraint_violation >
458 current_entry.constraint_violation / Scalar(10) &&
459 filter.last_rejection_due_to_filter()) {
460 filter.max_constraint_violation *= Scalar(0.1);
461 filter.reset();
462 continue;
463 }
464
465 // Reduce step size
466 α *= α_reduction_factor;
467
468 // If step size hit a minimum, check if the KKT error was reduced. If it
469 // wasn't, invoke feasibility restoration.
470 if (α < α_min) {
471 Scalar current_kkt_error =
472 kkt_error<Scalar, KKTErrorType::ONE_NORM>(g, A_e, c_e, y);
473
474 trial_x = x + α_max * step.p_x;
475 trial_y = y + α_max * step.p_y;
476
477 trial_f = matrices.f(trial_x);
478 trial_c_e = matrices.c_e(trial_x);
479
480 Scalar next_kkt_error = kkt_error<Scalar, KKTErrorType::ONE_NORM>(
481 matrices.g(trial_x), matrices.A_e(trial_x), trial_c_e, trial_y);
482
483 // If the step using αᵐᵃˣ reduced the KKT error, accept it anyway
484 if (next_kkt_error <= Scalar(0.999) * current_kkt_error) {
485 // Accept step
486 break;
487 }
488
489 call_feasibility_restoration = true;
490 break;
491 }
492 }
493
494 line_search_profiler.stop();
495
496 if (call_feasibility_restoration) {
497 ScopedProfiler feasibility_restoration_profiler{
498 feasibility_restoration_prof};
499
500 FilterEntry initial_entry{matrices.f(x), c_e};
501
502 // Feasibility restoration phase
503 gch::small_vector<std::function<bool(const IterationInfo<Scalar>& info)>>
504 callbacks;
505 for (auto& callback : iteration_callbacks) {
506 callbacks.emplace_back(callback);
507 }
508 callbacks.emplace_back([&](const IterationInfo<Scalar>& info) {
509 DenseVector trial_x =
510 info.x.segment(0, matrices.num_decision_variables);
511
512 DenseVector trial_c_e = matrices.c_e(trial_x);
513
514 FilterEntry trial_entry{matrices.f(trial_x), trial_c_e};
515
516 // If the current iterate sufficiently reduces constraint violation and
517 // is accepted by the normal filter, stop feasibility restoration
518 return trial_entry.constraint_violation <
519 Scalar(0.9) * initial_entry.constraint_violation &&
520 filter.try_add(initial_entry, trial_entry, trial_x - x, g, α);
521 });
522 auto status =
523 feasibility_restoration<Scalar>(matrices, callbacks, options, x, y);
524
525 if (status != ExitStatus::SUCCESS) {
526 // Report failure
527 return status;
528 }
529
530 f = matrices.f(x);
531 c_e = matrices.c_e(x);
532 } else {
533 // If full step was accepted, reset full-step rejected counter
534 if (α == α_max) {
535 full_step_rejected_counter = 0;
536 }
537
538 // Update iterates
539 x = trial_x;
540 y = trial_y;
541
542 f = trial_f;
543 c_e = trial_c_e;
544 }
545
546 // Update autodiff for Jacobians and Hessian
547 A_e = matrices.A_e(x);
548 g = matrices.g(x);
549 H = matrices.H(x, y);
550
551 // Update the error
552 E_0 = kkt_error<Scalar, KKTErrorType::INF_NORM_SCALED>(g, A_e, c_e, y);
553
554 inner_iter_profiler.stop();
555
556 if (options.diagnostics) {
557 print_iteration_diagnostics(iterations, IterationType::NORMAL,
558 inner_iter_profiler.current_duration(), E_0,
559 f, c_e.template lpNorm<1>(), Scalar(0),
560 Scalar(0), solver.hessian_regularization(), α,
561 α_max, α_reduction_factor, α);
562 }
563
564 ++iterations;
565
566 // Check for max iterations
567 if (iterations >= options.max_iterations) {
568 return ExitStatus::MAX_ITERATIONS_EXCEEDED;
569 }
570
571 // Check for max wall clock time
572 if (std::chrono::steady_clock::now() - solve_start_time > options.timeout) {
573 return ExitStatus::TIMEOUT;
574 }
575 }
576
577 return ExitStatus::SUCCESS;
578}
579
580extern template SLEIPNIR_DLLEXPORT ExitStatus
581sqp(const SQPMatrixCallbacks<double>& matrix_callbacks,
582 std::span<std::function<bool(const IterationInfo<double>& info)>>
583 iteration_callbacks,
584 const Options& options, Eigen::Vector<double, Eigen::Dynamic>& x);
585
586} // namespace slp