WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
kkt_error.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <Eigen/Core>
6#include <Eigen/SparseCore>
7
8// See docs/algorithms.md#Works_cited for citation definitions
9
10namespace slp {
11
12/// Returns the KKT error for Newton's method.
13///
14/// @tparam Scalar Scalar type.
15/// @param g Gradient of the cost function ∇f.
16template <typename Scalar>
17Scalar kkt_error(const Eigen::Vector<Scalar, Eigen::Dynamic>& g) {
18 // Compute the KKT error as the 1-norm of the KKT conditions from equations
19 // (19.5a) through (19.5d) of [1].
20 //
21 // ∇f = 0
22
23 return g.template lpNorm<1>();
24}
25
26/// Returns the KKT error for Sequential Quadratic Programming.
27///
28/// @tparam Scalar Scalar type.
29/// @param g Gradient of the cost function ∇f.
30/// @param A_e The problem's equality constraint Jacobian Aₑ(x) evaluated at the
31/// current iterate.
32/// @param c_e The problem's equality constraints cₑ(x) evaluated at the current
33/// iterate.
34/// @param y Equality constraint dual variables.
35template <typename Scalar>
36Scalar kkt_error(const Eigen::Vector<Scalar, Eigen::Dynamic>& g,
37 const Eigen::SparseMatrix<Scalar>& A_e,
38 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e,
39 const Eigen::Vector<Scalar, Eigen::Dynamic>& y) {
40 // Compute the KKT error as the 1-norm of the KKT conditions from equations
41 // (19.5a) through (19.5d) of [1].
42 //
43 // ∇f − Aₑᵀy = 0
44 // cₑ = 0
45
46 return (g - A_e.transpose() * y).template lpNorm<1>() +
47 c_e.template lpNorm<1>();
48}
49
50/// Returns the KKT error for the interior-point method.
51///
52/// @tparam Scalar Scalar type.
53/// @param g Gradient of the cost function ∇f.
54/// @param A_e The problem's equality constraint Jacobian Aₑ(x) evaluated at the
55/// current iterate.
56/// @param c_e The problem's equality constraints cₑ(x) evaluated at the current
57/// iterate.
58/// @param A_i The problem's inequality constraint Jacobian Aᵢ(x) evaluated at
59/// the current iterate.
60/// @param c_i The problem's inequality constraints cᵢ(x) evaluated at the
61/// current iterate.
62/// @param s Inequality constraint slack variables.
63/// @param y Equality constraint dual variables.
64/// @param z Inequality constraint dual variables.
65/// @param μ Barrier parameter.
66template <typename Scalar>
67Scalar kkt_error(const Eigen::Vector<Scalar, Eigen::Dynamic>& g,
68 const Eigen::SparseMatrix<Scalar>& A_e,
69 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e,
70 const Eigen::SparseMatrix<Scalar>& A_i,
71 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i,
72 const Eigen::Vector<Scalar, Eigen::Dynamic>& s,
73 const Eigen::Vector<Scalar, Eigen::Dynamic>& y,
74 const Eigen::Vector<Scalar, Eigen::Dynamic>& z, Scalar μ) {
75 // Compute the KKT error as the 1-norm of the KKT conditions from equations
76 // (19.5a) through (19.5d) of [1].
77 //
78 // ∇f − Aₑᵀy − Aᵢᵀz = 0
79 // Sz − μe = 0
80 // cₑ = 0
81 // cᵢ − s = 0
82
83 const auto S = s.asDiagonal();
84 const Eigen::Vector<Scalar, Eigen::Dynamic> μe =
85 Eigen::Vector<Scalar, Eigen::Dynamic>::Constant(s.rows(), μ);
86
87 return (g - A_e.transpose() * y - A_i.transpose() * z).template lpNorm<1>() +
88 (S * z - μe).template lpNorm<1>() + c_e.template lpNorm<1>() +
89 (c_i - s).template lpNorm<1>();
90}
91
92} // namespace slp
#define S(label, offset, message)
Definition Errors.hpp:113
Definition expression_graph.hpp:11
Scalar kkt_error(const Eigen::Vector< Scalar, Eigen::Dynamic > &g)
Returns the KKT error for Newton's method.
Definition kkt_error.hpp:17