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 <limits>
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/iteration_info.hpp"
17#include "sleipnir/optimization/solver/options.hpp"
18#include "sleipnir/optimization/solver/sqp_matrix_callbacks.hpp"
19#include "sleipnir/optimization/solver/util/error_estimate.hpp"
20#include "sleipnir/optimization/solver/util/filter.hpp"
21#include "sleipnir/optimization/solver/util/is_locally_infeasible.hpp"
22#include "sleipnir/optimization/solver/util/kkt_error.hpp"
23#include "sleipnir/optimization/solver/util/regularized_ldlt.hpp"
24#include "sleipnir/util/assert.hpp"
25#include "sleipnir/util/print_diagnostics.hpp"
26#include "sleipnir/util/scope_exit.hpp"
27#include "sleipnir/util/scoped_profiler.hpp"
28#include "sleipnir/util/solve_profiler.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 using SparseMatrix = Eigen::SparseMatrix<Scalar>;
63 using SparseVector = Eigen::SparseVector<Scalar>;
64
66 struct Step {
68 DenseVector p_x;
70 DenseVector p_y;
71 };
72
73 using std::isfinite;
74
75 const auto solve_start_time = std::chrono::steady_clock::now();
76
77 gch::small_vector<SolveProfiler> solve_profilers;
78 solve_profilers.emplace_back("solver");
79 solve_profilers.emplace_back(" ↳ setup");
80 solve_profilers.emplace_back(" ↳ iteration");
81 solve_profilers.emplace_back(" ↳ feasibility ✓");
82 solve_profilers.emplace_back(" ↳ iter callbacks");
83 solve_profilers.emplace_back(" ↳ KKT matrix build");
84 solve_profilers.emplace_back(" ↳ KKT matrix decomp");
85 solve_profilers.emplace_back(" ↳ KKT system solve");
86 solve_profilers.emplace_back(" ↳ line search");
87 solve_profilers.emplace_back(" ↳ SOC");
88 solve_profilers.emplace_back(" ↳ next iter prep");
89 solve_profilers.emplace_back(" ↳ f(x)");
90 solve_profilers.emplace_back(" ↳ ∇f(x)");
91 solve_profilers.emplace_back(" ↳ ∇²ₓₓL");
92 solve_profilers.emplace_back(" ↳ cₑ(x)");
93 solve_profilers.emplace_back(" ↳ ∂cₑ/∂x");
94
95 auto& solver_prof = solve_profilers[0];
96 auto& setup_prof = solve_profilers[1];
97 auto& inner_iter_prof = solve_profilers[2];
98 auto& feasibility_check_prof = solve_profilers[3];
99 auto& iter_callbacks_prof = solve_profilers[4];
100 auto& kkt_matrix_build_prof = solve_profilers[5];
101 auto& kkt_matrix_decomp_prof = solve_profilers[6];
102 auto& kkt_system_solve_prof = solve_profilers[7];
103 auto& line_search_prof = solve_profilers[8];
104 auto& soc_prof = solve_profilers[9];
105 auto& next_iter_prep_prof = solve_profilers[10];
106
107 // Set up profiled matrix callbacks
108#ifndef SLEIPNIR_DISABLE_DIAGNOSTICS
109 auto& f_prof = solve_profilers[11];
110 auto& g_prof = solve_profilers[12];
111 auto& H_prof = solve_profilers[13];
112 auto& c_e_prof = solve_profilers[14];
113 auto& A_e_prof = solve_profilers[15];
114
115 SQPMatrixCallbacks<Scalar> matrices{
116 [&](const DenseVector& x) -> Scalar {
117 ScopedProfiler prof{f_prof};
118 return matrix_callbacks.f(x);
119 },
120 [&](const DenseVector& x) -> SparseVector {
121 ScopedProfiler prof{g_prof};
122 return matrix_callbacks.g(x);
123 },
124 [&](const DenseVector& x, const DenseVector& y) -> SparseMatrix {
125 ScopedProfiler prof{H_prof};
126 return matrix_callbacks.H(x, y);
127 },
128 [&](const DenseVector& x) -> DenseVector {
129 ScopedProfiler prof{c_e_prof};
130 return matrix_callbacks.c_e(x);
131 },
132 [&](const DenseVector& x) -> SparseMatrix {
133 ScopedProfiler prof{A_e_prof};
134 return matrix_callbacks.A_e(x);
135 }};
136#else
137 const auto& matrices = matrix_callbacks;
138#endif
139
140 solver_prof.start();
141 setup_prof.start();
142
143 Scalar f = matrices.f(x);
144 DenseVector c_e = matrices.c_e(x);
145
146 int num_decision_variables = x.rows();
147 int num_equality_constraints = c_e.rows();
148
149 // Check for overconstrained problem
150 if (num_equality_constraints > num_decision_variables) {
151 if (options.diagnostics) {
152 print_too_few_dofs_error(c_e);
153 }
154
155 return ExitStatus::TOO_FEW_DOFS;
156 }
157
158 SparseVector g = matrices.g(x);
159 SparseMatrix A_e = matrices.A_e(x);
160
161 DenseVector y = DenseVector::Zero(num_equality_constraints);
162
163 SparseMatrix H = matrices.H(x, y);
164
165 // Ensure matrix callback dimensions are consistent
166 slp_assert(g.rows() == num_decision_variables);
167 slp_assert(A_e.rows() == num_equality_constraints);
168 slp_assert(A_e.cols() == num_decision_variables);
169 slp_assert(H.rows() == num_decision_variables);
170 slp_assert(H.cols() == num_decision_variables);
171
172 // Check whether initial guess has finite f(xₖ) and cₑ(xₖ)
173 if (!isfinite(f) || !c_e.allFinite()) {
174 return ExitStatus::NONFINITE_INITIAL_COST_OR_CONSTRAINTS;
175 }
176
177 int iterations = 0;
178
179 Filter<Scalar> filter;
180
181 // Kept outside the loop so its storage can be reused
182 gch::small_vector<Eigen::Triplet<Scalar>> triplets;
183
184 RegularizedLDLT<Scalar> solver{num_decision_variables,
185 num_equality_constraints};
186
187 // Variables for determining when a step is acceptable
188 constexpr Scalar α_reduction_factor(0.5);
189 constexpr Scalar α_min(1e-7);
190
191 int full_step_rejected_counter = 0;
192
193 // Error estimate
194 Scalar E_0 = std::numeric_limits<Scalar>::infinity();
195
196 setup_prof.stop();
197
198 // Prints final solver diagnostics when the solver exits
199 scope_exit exit{[&] {
200 if (options.diagnostics) {
201 solver_prof.stop();
202 if (iterations > 0) {
203 print_bottom_iteration_diagnostics();
204 }
205 print_solver_diagnostics(solve_profilers);
206 }
207 }};
208
209 while (E_0 > Scalar(options.tolerance)) {
210 ScopedProfiler inner_iter_profiler{inner_iter_prof};
211 ScopedProfiler feasibility_check_profiler{feasibility_check_prof};
212
213 // Check for local equality constraint infeasibility
214 if (is_equality_locally_infeasible(A_e, c_e)) {
215 if (options.diagnostics) {
216 print_c_e_local_infeasibility_error(c_e);
217 }
218
219 return ExitStatus::LOCALLY_INFEASIBLE;
220 }
221
222 // Check for diverging iterates
223 if (x.template lpNorm<Eigen::Infinity>() > Scalar(1e10) || !x.allFinite()) {
224 return ExitStatus::DIVERGING_ITERATES;
225 }
226
227 feasibility_check_profiler.stop();
228 ScopedProfiler iter_callbacks_profiler{iter_callbacks_prof};
229
230 // Call iteration callbacks
231 for (const auto& callback : iteration_callbacks) {
232 if (callback({iterations, x, g, H, A_e, SparseMatrix{}})) {
233 return ExitStatus::CALLBACK_REQUESTED_STOP;
234 }
235 }
236
237 iter_callbacks_profiler.stop();
238 ScopedProfiler kkt_matrix_build_profiler{kkt_matrix_build_prof};
239
240 // lhs = [H Aₑᵀ]
241 // [Aₑ 0 ]
242 //
243 // Don't assign upper triangle because solver only uses lower triangle.
244 triplets.clear();
245 triplets.reserve(H.nonZeros() + A_e.nonZeros());
246 for (int col = 0; col < H.cols(); ++col) {
247 // Append column of H lower triangle in top-left quadrant
248 for (typename SparseMatrix::InnerIterator it{H, col}; it; ++it) {
249 triplets.emplace_back(it.row(), it.col(), it.value());
250 }
251 // Append column of Aₑ in bottom-left quadrant
252 for (typename SparseMatrix::InnerIterator it{A_e, col}; it; ++it) {
253 triplets.emplace_back(H.rows() + it.row(), it.col(), it.value());
254 }
255 }
256 SparseMatrix lhs(num_decision_variables + num_equality_constraints,
257 num_decision_variables + num_equality_constraints);
258 lhs.setFromSortedTriplets(triplets.begin(), triplets.end());
259
260 // rhs = −[∇f − Aₑᵀy]
261 // [ cₑ ]
262 DenseVector rhs{x.rows() + y.rows()};
263 rhs.segment(0, x.rows()) = -g + A_e.transpose() * y;
264 rhs.segment(x.rows(), y.rows()) = -c_e;
265
266 kkt_matrix_build_profiler.stop();
267 ScopedProfiler kkt_matrix_decomp_profiler{kkt_matrix_decomp_prof};
268
269 Step step;
270 constexpr Scalar α_max(1);
271 Scalar α(1);
272
273 // Solve the Newton-KKT system
274 //
275 // [H Aₑᵀ][ pˣ] = −[∇f − Aₑᵀy]
276 // [Aₑ 0 ][−pʸ] [ cₑ ]
277 if (solver.compute(lhs).info() != Eigen::Success) [[unlikely]] {
278 return ExitStatus::FACTORIZATION_FAILED;
279 }
280
281 kkt_matrix_decomp_profiler.stop();
282 ScopedProfiler kkt_system_solve_profiler{kkt_system_solve_prof};
283
284 auto compute_step = [&](Step& step) {
285 // p = [ pˣ]
286 // [−pʸ]
287 DenseVector p = solver.solve(rhs);
288 step.p_x = p.segment(0, x.rows());
289 step.p_y = -p.segment(x.rows(), y.rows());
290 };
291 compute_step(step);
292
293 kkt_system_solve_profiler.stop();
294 ScopedProfiler line_search_profiler{line_search_prof};
295
296 α = α_max;
297
298 // Loop until a step is accepted
299 while (1) {
300 DenseVector trial_x = x + α * step.p_x;
301 DenseVector trial_y = y + α * step.p_y;
302
303 Scalar trial_f = matrices.f(trial_x);
304 DenseVector trial_c_e = matrices.c_e(trial_x);
305
306 // If f(xₖ + αpₖˣ) or cₑ(xₖ + αpₖˣ) aren't finite, reduce step size
307 // immediately
308 if (!isfinite(trial_f) || !trial_c_e.allFinite()) {
309 // Reduce step size
310 α *= α_reduction_factor;
311
312 if (α < α_min) {
313 return ExitStatus::LINE_SEARCH_FAILED;
314 }
315 continue;
316 }
317
318 // Check whether filter accepts trial iterate
319 if (filter.try_add(FilterEntry{trial_f, trial_c_e}, α)) {
320 // Accept step
321 break;
322 }
323
324 Scalar prev_constraint_violation = c_e.template lpNorm<1>();
325 Scalar next_constraint_violation = trial_c_e.template lpNorm<1>();
326
327 // Second-order corrections
328 //
329 // If first trial point was rejected and constraint violation stayed the
330 // same or went up, apply second-order corrections
331 if (α == α_max &&
332 next_constraint_violation >= prev_constraint_violation) {
333 // Apply second-order corrections. See section 2.4 of [2].
334 auto soc_step = step;
335
336 Scalar α_soc = α;
337 DenseVector c_e_soc = c_e;
338
339 bool step_acceptable = false;
340 for (int soc_iteration = 0; soc_iteration < 5 && !step_acceptable;
341 ++soc_iteration) {
342 ScopedProfiler soc_profiler{soc_prof};
343
344 scope_exit soc_exit{[&] {
345 soc_profiler.stop();
346
347 if (options.diagnostics) {
348 print_iteration_diagnostics(
349 iterations,
350 step_acceptable ? IterationType::ACCEPTED_SOC
351 : IterationType::REJECTED_SOC,
352 soc_profiler.current_duration(),
353 error_estimate<Scalar>(g, A_e, trial_c_e, trial_y), trial_f,
354 trial_c_e.template lpNorm<1>(), Scalar(0), Scalar(0),
355 solver.hessian_regularization(), α_soc, Scalar(1),
356 α_reduction_factor, Scalar(1));
357 }
358 }};
359
360 // Rebuild Newton-KKT rhs with updated constraint values.
361 //
362 // rhs = −[∇f − Aₑᵀy]
363 // [ cₑˢᵒᶜ ]
364 //
365 // where cₑˢᵒᶜ = αc(xₖ) + c(xₖ + αpₖˣ)
366 c_e_soc = α_soc * c_e_soc + trial_c_e;
367 rhs.bottomRows(y.rows()) = -c_e_soc;
368
369 // Solve the Newton-KKT system
370 compute_step(soc_step);
371
372 trial_x = x + α_soc * soc_step.p_x;
373 trial_y = y + α_soc * soc_step.p_y;
374
375 trial_f = matrices.f(trial_x);
376 trial_c_e = matrices.c_e(trial_x);
377
378 // Constraint violation scale factor for second-order corrections
379 constexpr Scalar κ_soc(0.99);
380
381 // If constraint violation hasn't been sufficiently reduced, stop
382 // making second-order corrections
383 next_constraint_violation = trial_c_e.template lpNorm<1>();
384 if (next_constraint_violation > κ_soc * prev_constraint_violation) {
385 break;
386 }
387
388 // Check whether filter accepts trial iterate
389 if (filter.try_add(FilterEntry{trial_f, trial_c_e}, α)) {
390 step = soc_step;
391 α = α_soc;
392 step_acceptable = true;
393 }
394 }
395
396 if (step_acceptable) {
397 // Accept step
398 break;
399 }
400 }
401
402 // If we got here and α is the full step, the full step was rejected.
403 // Increment the full-step rejected counter to keep track of how many full
404 // steps have been rejected in a row.
405 if (α == α_max) {
406 ++full_step_rejected_counter;
407 }
408
409 // If the full step was rejected enough times in a row, reset the filter
410 // because it may be impeding progress.
411 //
412 // See section 3.2 case I of [2].
413 if (full_step_rejected_counter >= 4 &&
414 filter.max_constraint_violation >
415 filter.back().constraint_violation / Scalar(10)) {
416 filter.max_constraint_violation *= Scalar(0.1);
417 filter.reset();
418 continue;
419 }
420
421 // Reduce step size
422 α *= α_reduction_factor;
423
424 // If step size hit a minimum, check if the KKT error was reduced. If it
425 // wasn't, report line search failure.
426 if (α < α_min) {
427 Scalar current_kkt_error = kkt_error<Scalar>(g, A_e, c_e, y);
428
429 trial_x = x + α_max * step.p_x;
430 trial_y = y + α_max * step.p_y;
431
432 trial_c_e = matrices.c_e(trial_x);
433
434 Scalar next_kkt_error = kkt_error<Scalar>(
435 matrices.g(trial_x), matrices.A_e(trial_x), trial_c_e, trial_y);
436
437 // If the step using αᵐᵃˣ reduced the KKT error, accept it anyway
438 if (next_kkt_error <= Scalar(0.999) * current_kkt_error) {
439 α = α_max;
440
441 // Accept step
442 break;
443 }
444
445 return ExitStatus::LINE_SEARCH_FAILED;
446 }
447 }
448
449 line_search_profiler.stop();
450
451 // If full step was accepted, reset full-step rejected counter
452 if (α == α_max) {
453 full_step_rejected_counter = 0;
454 }
455
456 // xₖ₊₁ = xₖ + αₖpₖˣ
457 // yₖ₊₁ = yₖ + αₖpₖʸ
458 x += α * step.p_x;
459 y += α * step.p_y;
460
461 // Update autodiff for Jacobians and Hessian
462 f = matrices.f(x);
463 A_e = matrices.A_e(x);
464 g = matrices.g(x);
465 H = matrices.H(x, y);
466
467 ScopedProfiler next_iter_prep_profiler{next_iter_prep_prof};
468
469 c_e = matrices.c_e(x);
470
471 // Update the error estimate
472 E_0 = error_estimate<Scalar>(g, A_e, c_e, y);
473
474 next_iter_prep_profiler.stop();
475 inner_iter_profiler.stop();
476
477 if (options.diagnostics) {
478 print_iteration_diagnostics(iterations, IterationType::NORMAL,
479 inner_iter_profiler.current_duration(), E_0,
480 f, c_e.template lpNorm<1>(), Scalar(0),
481 Scalar(0), solver.hessian_regularization(), α,
482 α_max, α_reduction_factor, α);
483 }
484
485 ++iterations;
486
487 // Check for max iterations
488 if (iterations >= options.max_iterations) {
489 return ExitStatus::MAX_ITERATIONS_EXCEEDED;
490 }
491
492 // Check for max wall clock time
493 if (std::chrono::steady_clock::now() - solve_start_time > options.timeout) {
494 return ExitStatus::TIMEOUT;
495 }
496 }
497
498 return ExitStatus::SUCCESS;
499}
500
501extern template SLEIPNIR_DLLEXPORT ExitStatus
502sqp(const SQPMatrixCallbacks<double>& matrix_callbacks,
503 std::span<std::function<bool(const IterationInfo<double>& info)>>
504 iteration_callbacks,
505 const Options& options, Eigen::Vector<double, Eigen::Dynamic>& x);
506
507} // namespace slp