Sleipnir C++ API
Loading...
Searching...
No Matches
kkt_error.hpp
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
18template <typename Scalar>
19Scalar kkt_error(const Eigen::Vector<Scalar, Eigen::Dynamic>& g) {
20 // Compute the KKT error as the 1-norm of the KKT conditions from equations
21 // (19.5a) through (19.5d) of [1].
22 //
23 // ∇f = 0
24
25 return g.template lpNorm<1>();
26}
27
39template <typename Scalar>
40Scalar kkt_error(const Eigen::Vector<Scalar, Eigen::Dynamic>& g,
41 const Eigen::SparseMatrix<Scalar>& A_e,
42 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e,
43 const Eigen::Vector<Scalar, Eigen::Dynamic>& y) {
44 // Compute the KKT error as the 1-norm of the KKT conditions from equations
45 // (19.5a) through (19.5d) of [1].
46 //
47 // ∇f − Aₑᵀy = 0
48 // cₑ = 0
49
50 return (g - A_e.transpose() * y).template lpNorm<1>() +
51 c_e.template lpNorm<1>();
52}
53
72template <typename Scalar>
73Scalar kkt_error(const Eigen::Vector<Scalar, Eigen::Dynamic>& g,
74 const Eigen::SparseMatrix<Scalar>& A_e,
75 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e,
76 const Eigen::SparseMatrix<Scalar>& A_i,
77 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i,
78 const Eigen::Vector<Scalar, Eigen::Dynamic>& s,
79 const Eigen::Vector<Scalar, Eigen::Dynamic>& y,
80 const Eigen::Vector<Scalar, Eigen::Dynamic>& z, Scalar μ) {
81 // Compute the KKT error as the 1-norm of the KKT conditions from equations
82 // (19.5a) through (19.5d) of [1].
83 //
84 // ∇f − Aₑᵀy − Aᵢᵀz = 0
85 // Sz − μe = 0
86 // cₑ = 0
87 // cᵢ − s = 0
88
89 const auto S = s.asDiagonal();
90 const Eigen::Vector<Scalar, Eigen::Dynamic> μe =
91 Eigen::Vector<Scalar, Eigen::Dynamic>::Constant(s.rows(), μ);
92
93 return (g - A_e.transpose() * y - A_i.transpose() * z).template lpNorm<1>() +
94 (S * z - μe).template lpNorm<1>() + c_e.template lpNorm<1>() +
95 (c_i - s).template lpNorm<1>();
96}
97
98} // namespace slp