Sleipnir C++ API
Loading...
Searching...
No Matches
kkt_error.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <cmath>
7#include <limits>
8
9#include <Eigen/Core>
10#include <Eigen/SparseCore>
11
12#include "sleipnir/optimization/solver/util/first_derivatives.hpp"
13#include "sleipnir/optimization/solver/util/function_values.hpp"
14#include "sleipnir/optimization/solver/util/iterates.hpp"
15#include "sleipnir/optimization/solver/util/problem_scaling.hpp"
16
17// See docs/algorithms.md#Works_cited for citation definitions
18
19namespace slp {
20
25template <typename Scalar>
26Scalar kkt_error_inf_norm(const Eigen::SparseVector<Scalar>& g) {
27 // The KKT conditions from docs/algorithms.md:
28 //
29 // ∇f = 0
30 return g.coeffs().template lpNorm<Eigen::Infinity>();
31}
32
37template <typename Scalar>
38Scalar kkt_error_one_norm(const Eigen::SparseVector<Scalar>& g) {
39 // The KKT conditions from docs/algorithms.md:
40 //
41 // ∇f = 0
42 return g.coeffs().template lpNorm<1>();
43}
44
54template <typename Scalar>
55Scalar kkt_error_inf_norm(const SQPIterates<Scalar>& iterates,
56 const SQPFunctionValues<Scalar>& function_values,
57 const SQPFirstDerivatives<Scalar>& first_derivatives,
58 Scalar tolerance) {
59 using std::pow;
60
61 const auto& y = iterates.y;
62 const auto& c_e = function_values.c_e;
63 const auto& [g, A_e] = first_derivatives;
64
65 // The KKT conditions from docs/algorithms.md:
66 //
67 // ∇f − Aₑᵀy = 0
68 // cₑ = 0
69
70 constexpr Scalar ε = std::numeric_limits<Scalar>::epsilon();
71
72 // The stationarity residual must not go below its rounding error floor κεω,
73 // where κ is how much the problem amplifies rounding error and ω is the size
74 // of the largest stationarity terms. If κεω > tol, the solver stalls instead
75 // of terminating.
76 //
77 // κ is unknown, so assume κ ≤ ε^(-3/8) (≈ 7.4e5 for double, 395 for float).
78 // Then the floor is at most
79 //
80 // κεω ≤ ε^(-3/8) εω = ε^(5/8) ω
81 //
82 // and dividing the residual by ε^(5/8) ω/tol makes it meet tol at the floor.
83 // Dividing by less than 1 would tighten tol instead, so
84 //
85 // s_d = max(1, ε^(5/8) ω/tol)
86
87 // ω = ‖|∇f| + |Aₑ|ᵀ|y|‖_∞
88 const Scalar ω = (g.cwiseAbs() + A_e.cwiseAbs().transpose() * y.cwiseAbs())
89 .toDense()
90 .template lpNorm<Eigen::Infinity>();
91
92 Scalar s_d = std::max(Scalar(1), pow(ε, Scalar(0.625)) * ω / tolerance);
93
94 // ‖∇f − Aₑᵀy‖_∞ / s_d
95 // ‖cₑ‖_∞
96 return std::max(
97 {(g - A_e.transpose() * y).toDense().template lpNorm<Eigen::Infinity>() /
98 s_d,
99 c_e.template lpNorm<Eigen::Infinity>()});
100}
101
110template <typename Scalar>
111Scalar kkt_error_one_norm(
112 const SQPIterates<Scalar>& iterates,
113 const SQPFunctionValues<Scalar>& function_values,
114 const SQPFirstDerivatives<Scalar>& first_derivatives) {
115 const auto& y = iterates.y;
116 const auto& c_e = function_values.c_e;
117 const auto& [g, A_e] = first_derivatives;
118
119 // The KKT conditions from docs/algorithms.md:
120 //
121 // ∇f − Aₑᵀy = 0
122 // cₑ = 0
123 return (g - A_e.transpose() * y).toDense().template lpNorm<1>() +
124 c_e.template lpNorm<1>();
125}
126
136template <typename Scalar>
137Scalar kkt_error_inf_norm(const IPMIterates<Scalar>& iterates,
138 const IPMFunctionValues<Scalar>& function_values,
139 const IPMFirstDerivatives<Scalar>& first_derivatives,
140 Scalar μ, Scalar tolerance) {
141 using std::pow;
142
143 const auto& [x, s, y, z] = iterates;
144 const auto& [f, c_e, c_i] = function_values;
145 const auto& [g, A_e, A_i] = first_derivatives;
146
147 // The KKT conditions from docs/algorithms.md:
148 //
149 // ∇f − Aₑᵀy − Aᵢᵀz = 0
150 // Sz − μe = 0
151 // cₑ = 0
152 // cᵢ − s = 0
153
154 constexpr Scalar ε = std::numeric_limits<Scalar>::epsilon();
155
156 // The stationarity residual must not go below its rounding error floor κεω,
157 // where κ is how much the problem amplifies rounding error and ω is the size
158 // of the largest stationarity terms. If κεω > tol, the solver stalls instead
159 // of terminating.
160 //
161 // κ is unknown, so assume κ ≤ ε^(-3/8) (≈ 7.4e5 for double, 395 for float).
162 // Then the floor is at most
163 //
164 // κεω ≤ ε^(-3/8) εω = ε^(5/8) ω
165 //
166 // and dividing the residual by ε^(5/8) ω/tol makes it meet tol at the floor.
167 // Dividing by less than 1 would tighten tol instead, so
168 //
169 // s_d = max(1, ε^(5/8) ω/tol)
170
171 // ω = ‖|∇f| + |Aₑ|ᵀ|y| + |Aᵢ|ᵀ|z|‖_∞
172 const Scalar ω = (g.cwiseAbs() + A_e.cwiseAbs().transpose() * y.cwiseAbs() +
173 A_i.cwiseAbs().transpose() * z.cwiseAbs())
174 .toDense()
175 .template lpNorm<Eigen::Infinity>();
176
177 Scalar s_d = std::max(Scalar(1), pow(ε, Scalar(0.625)) * ω / tolerance);
178
179 // s_c = max(sₘₐₓ, ‖z‖₁ / n) / sₘₐₓ
180 //
181 // See equation (5) of [2].
182 constexpr Scalar s_max(100);
183 Scalar s_c =
184 std::max(s_max, z.template lpNorm<1>() / Scalar(z.rows())) / s_max;
185
186 const auto S = s.asDiagonal();
187 const Eigen::VectorX<Scalar> μe =
188 Eigen::VectorX<Scalar>::Constant(s.rows(), μ);
189
190 // ‖∇f − Aₑᵀy − Aᵢᵀz‖_∞ / s_d
191 // ‖Sz − μe‖_∞ / s_c
192 // ‖cₑ‖_∞
193 // ‖cᵢ − s‖_∞
194 return std::max({(g - A_e.transpose() * y - A_i.transpose() * z)
195 .toDense()
196 .template lpNorm<Eigen::Infinity>() /
197 s_d,
198 (S * z - μe).template lpNorm<Eigen::Infinity>() / s_c,
199 c_e.template lpNorm<Eigen::Infinity>(),
200 (c_i - s).template lpNorm<Eigen::Infinity>()});
201}
202
211template <typename Scalar>
212Scalar kkt_error_one_norm(const IPMIterates<Scalar>& iterates,
213 const IPMFunctionValues<Scalar>& function_values,
214 const IPMFirstDerivatives<Scalar>& first_derivatives,
215 Scalar μ) {
216 const auto& [x, s, y, z] = iterates;
217 const auto& [f, c_e, c_i] = function_values;
218 const auto& [g, A_e, A_i] = first_derivatives;
219
220 // The KKT conditions from docs/algorithms.md:
221 //
222 // ∇f − Aₑᵀy − Aᵢᵀz = 0
223 // Sz − μe = 0
224 // cₑ = 0
225 // cᵢ − s = 0
226
227 const auto S = s.asDiagonal();
228 const Eigen::VectorX<Scalar> μe =
229 Eigen::VectorX<Scalar>::Constant(s.rows(), μ);
230
231 return (g - A_e.transpose() * y - A_i.transpose() * z)
232 .toDense()
233 .template lpNorm<1>() +
234 (S * z - μe).template lpNorm<1>() + c_e.template lpNorm<1>() +
235 (c_i - s).template lpNorm<1>();
236}
237
243template <typename Scalar>
244Scalar unscaled_kkt_error_inf_norm(const ProblemScaling<Scalar>& scaling,
245 const Eigen::SparseVector<Scalar>& g) {
246 using SparseVector = Eigen::SparseVector<Scalar>;
247
248 if (scaling.is_identity()) {
249 return kkt_error_inf_norm<Scalar>(g);
250 }
251
252 const SparseVector g_unscaled = (Scalar(1) / scaling.f) * g;
253
254 return kkt_error_inf_norm<Scalar>(g_unscaled);
255}
256
267template <typename Scalar>
268Scalar unscaled_kkt_error_inf_norm(
269 const ProblemScaling<Scalar>& scaling, const SQPIterates<Scalar>& iterates,
270 const SQPFunctionValues<Scalar>& function_values,
271 const SQPFirstDerivatives<Scalar>& first_derivatives, Scalar tolerance) {
272 using DenseVector = Eigen::VectorX<Scalar>;
273
274 if (scaling.is_identity()) {
275 return kkt_error_inf_norm<Scalar>(iterates, function_values,
276 first_derivatives, tolerance);
277 }
278
279 const Scalar inv_d_f = Scalar(1) / scaling.f;
280 const DenseVector inv_d_c_e = scaling.c_e.cwiseInverse();
281
282 const SQPFunctionValues<Scalar> function_values_unscaled{
283 inv_d_f * function_values.f, inv_d_c_e.cwiseProduct(function_values.c_e)};
284 const SQPFirstDerivatives<Scalar> first_derivatives_unscaled{
285 inv_d_f * first_derivatives.g,
286 inv_d_c_e.asDiagonal() * first_derivatives.A_e};
287 const SQPIterates<Scalar> iterates_unscaled{
288 iterates.x, scaling.c_e.cwiseProduct(iterates.y) * inv_d_f};
289
290 return kkt_error_inf_norm<Scalar>(iterates_unscaled, function_values_unscaled,
291 first_derivatives_unscaled, tolerance);
292}
293
305template <typename Scalar>
306Scalar unscaled_kkt_error_inf_norm(
307 const ProblemScaling<Scalar>& scaling, const IPMIterates<Scalar>& iterates,
308 const IPMFunctionValues<Scalar>& function_values,
309 const IPMFirstDerivatives<Scalar>& first_derivatives, Scalar μ,
310 Scalar tolerance) {
311 using DenseVector = Eigen::VectorX<Scalar>;
312
313 if (scaling.is_identity()) {
314 return kkt_error_inf_norm<Scalar>(iterates, function_values,
315 first_derivatives, μ, tolerance);
316 }
317
318 const Scalar inv_d_f = Scalar(1) / scaling.f;
319 const DenseVector inv_d_c_e = scaling.c_e.cwiseInverse();
320 const DenseVector inv_d_c_i = scaling.c_i.cwiseInverse();
321
322 const IPMFunctionValues<Scalar> function_values_unscaled{
323 inv_d_f * function_values.f, inv_d_c_e.cwiseProduct(function_values.c_e),
324 inv_d_c_i.cwiseProduct(function_values.c_i)};
325 const IPMFirstDerivatives<Scalar> first_derivatives_unscaled{
326 inv_d_f * first_derivatives.g,
327 inv_d_c_e.asDiagonal() * first_derivatives.A_e,
328 inv_d_c_i.asDiagonal() * first_derivatives.A_i};
329 const IPMIterates<Scalar> iterates_unscaled{
330 iterates.x, inv_d_c_i.cwiseProduct(iterates.s),
331 scaling.c_e.cwiseProduct(iterates.y) * inv_d_f,
332 scaling.c_i.cwiseProduct(iterates.z) * inv_d_f};
333 const Scalar μ_unscaled = inv_d_f * μ;
334
335 return kkt_error_inf_norm<Scalar>(iterates_unscaled, function_values_unscaled,
336 first_derivatives_unscaled, μ_unscaled,
337 tolerance);
338}
339
340} // namespace slp