8#include <Eigen/SparseCore>
15enum class KKTErrorType {
27template <
typename Scalar, KKTErrorType T>
28Scalar kkt_error(
const Eigen::Vector<Scalar, Eigen::Dynamic>& g) {
33 if constexpr (T == KKTErrorType::INF_NORM_SCALED) {
34 return g.template lpNorm<Eigen::Infinity>();
35 }
else if constexpr (T == KKTErrorType::ONE_NORM) {
36 return g.template lpNorm<1>();
50template <
typename Scalar, KKTErrorType T>
51Scalar kkt_error(
const Eigen::Vector<Scalar, Eigen::Dynamic>& g,
52 const Eigen::SparseMatrix<Scalar>& A_e,
53 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e,
54 const Eigen::Vector<Scalar, Eigen::Dynamic>& y) {
60 if constexpr (T == KKTErrorType::INF_NORM_SCALED) {
64 constexpr Scalar s_max(100);
66 std::max(s_max, y.template lpNorm<1>() / Scalar(y.rows())) / s_max;
71 {(g - A_e.transpose() * y).template lpNorm<Eigen::Infinity>() / s_d,
72 c_e.template lpNorm<Eigen::Infinity>()});
73 }
else if constexpr (T == KKTErrorType::ONE_NORM) {
74 return (g - A_e.transpose() * y).
template lpNorm<1>() +
75 c_e.template lpNorm<1>();
96template <
typename Scalar, KKTErrorType T>
97Scalar kkt_error(
const Eigen::Vector<Scalar, Eigen::Dynamic>& g,
98 const Eigen::SparseMatrix<Scalar>& A_e,
99 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_e,
100 const Eigen::SparseMatrix<Scalar>& A_i,
101 const Eigen::Vector<Scalar, Eigen::Dynamic>& c_i,
102 const Eigen::Vector<Scalar, Eigen::Dynamic>& s,
103 const Eigen::Vector<Scalar, Eigen::Dynamic>& y,
104 const Eigen::Vector<Scalar, Eigen::Dynamic>& z, Scalar μ) {
112 if constexpr (T == KKTErrorType::INF_NORM_SCALED) {
116 constexpr Scalar s_max(100);
118 std::max(s_max, (y.template lpNorm<1>() + z.template lpNorm<1>()) /
119 Scalar(y.rows() + z.rows())) /
124 std::max(s_max, z.template lpNorm<1>() / Scalar(z.rows())) / s_max;
126 const auto S = s.asDiagonal();
127 const Eigen::Vector<Scalar, Eigen::Dynamic> μe =
128 Eigen::Vector<Scalar, Eigen::Dynamic>::Constant(s.rows(), μ);
134 return std::max({(g - A_e.transpose() * y - A_i.transpose() * z)
135 .template lpNorm<Eigen::Infinity>() /
137 (S * z - μe).
template lpNorm<Eigen::Infinity>() / s_c,
138 c_e.template lpNorm<Eigen::Infinity>(),
139 (c_i - s).
template lpNorm<Eigen::Infinity>()});
140 }
else if constexpr (T == KKTErrorType::ONE_NORM) {
141 const auto S = s.asDiagonal();
142 const Eigen::Vector<Scalar, Eigen::Dynamic> μe =
143 Eigen::Vector<Scalar, Eigen::Dynamic>::Constant(s.rows(), μ);
145 return (g - A_e.transpose() * y - A_i.transpose() * z)
146 .template lpNorm<1>() +
147 (S * z - μe).
template lpNorm<1>() + c_e.template lpNorm<1>() +
148 (c_i - s).
template lpNorm<1>();