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
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
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
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