Skip to content

Commit 5021061

Browse files
committed
proxqp/sparse: replicate change for duality gap
1 parent 9e023a0 commit 5021061

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

include/proxsuite/proxqp/sparse/utils.hpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#ifndef PROXSUITE_PROXQP_SPARSE_UTILS_HPP
77
#define PROXSUITE_PROXQP_SPARSE_UTILS_HPP
88

9+
#include <iostream>
10+
#include <Eigen/IterativeLinearSolvers>
11+
#include <unsupported/Eigen/IterativeSolvers>
12+
13+
#include "proxsuite/helpers/common.hpp"
914
#include <proxsuite/linalg/dense/core.hpp>
1015
#include <proxsuite/linalg/sparse/core.hpp>
1116
#include <proxsuite/linalg/sparse/factorize.hpp>
@@ -21,10 +26,6 @@
2126
#include "proxsuite/proxqp/sparse/preconditioner/ruiz.hpp"
2227
#include "proxsuite/proxqp/sparse/preconditioner/identity.hpp"
2328

24-
#include <iostream>
25-
#include <Eigen/IterativeLinearSolvers>
26-
#include <unsupported/Eigen/IterativeSolvers>
27-
2829
namespace proxsuite {
2930
namespace proxqp {
3031
namespace sparse {
@@ -98,11 +99,28 @@ print_setup_header(const Settings<T>& settings,
9899
<< std::endl;
99100
}
100101
}
102+
101103
namespace detail {
104+
105+
/// @brief \brief Returns the part of the expression which is lower than value
106+
template<typename T, typename Scalar>
107+
auto
108+
lower_than(T const& expr, const Scalar value)
109+
VEG_DEDUCE_RET((expr.array() < value).select(expr, T::Zero(expr.rows())));
110+
111+
/// @brief \brief Returns the part of the expression which is greater than value
112+
template<typename T, typename Scalar>
113+
auto
114+
greater_than(T const& expr, const Scalar value)
115+
VEG_DEDUCE_RET((expr.array() > value).select(expr, T::Zero(expr.rows())));
116+
117+
/// @brief \brief Returns the positive part of an expression
102118
template<typename T>
103119
auto
104120
positive_part(T const& expr)
105121
VEG_DEDUCE_RET((expr.array() > 0).select(expr, T::Zero(expr.rows())));
122+
123+
/// @brief \brief Returns the negative part of an expression
106124
template<typename T>
107125
auto
108126
negative_part(T const& expr)
@@ -657,27 +675,36 @@ unscaled_primal_dual_residual(
657675
precond.unscale_primal_in_place({ proxqp::from_eigen, x_e });
658676
results.info.duality_gap = x_e.dot(data.g); // contains gTx
659677
rhs_duality_gap = std::abs(results.info.duality_gap);
660-
T xHx = (tmp).dot(x_e);
678+
679+
const T xHx = (tmp).dot(x_e);
661680
results.info.duality_gap += xHx;
662681
rhs_duality_gap = std::max(rhs_duality_gap, std::abs(xHx));
663682
tmp += data.g; // contains now Hx+g
664683
precond.scale_primal_in_place({ proxqp::from_eigen, x_e });
665684

666685
precond.unscale_dual_in_place_eq({ proxsuite::proxqp::from_eigen, y_e });
667-
T by = (data.b).dot(y_e);
686+
const T by = (data.b).dot(y_e);
668687
results.info.duality_gap += by;
669688
rhs_duality_gap = std::max(rhs_duality_gap, std::abs(by));
670689
precond.scale_dual_in_place_eq({ proxsuite::proxqp::from_eigen, y_e });
690+
671691
precond.unscale_dual_in_place_in({ proxsuite::proxqp::from_eigen, z_e });
672-
T zl = (data.l).dot(detail::negative_part(z_e));
692+
693+
const T zl = negative_part(z_e).dot(
694+
greater_than(data.l, -helpers::infinite_bound<T>::value()));
673695
results.info.duality_gap += zl;
674696
rhs_duality_gap = std::max(rhs_duality_gap, std::abs(zl));
675-
T zu = (data.u).dot(detail::positive_part(z_e));
697+
698+
const T zu = positive_part(z_e).dot(
699+
lower_than(data.u, helpers::infinite_bound<T>::value()));
676700
results.info.duality_gap += zu;
677701
rhs_duality_gap = std::max(rhs_duality_gap, std::abs(zu));
702+
678703
precond.scale_dual_in_place_in({ proxsuite::proxqp::from_eigen, z_e });
704+
679705
results.info.duality_gap /=
680706
sqrt_max_dim; // in order to get an a-dimensional duality gap
707+
rhs_duality_gap /= sqrt_max_dim;
681708
}
682709

683710
{

0 commit comments

Comments
 (0)