|
6 | 6 | #ifndef PROXSUITE_PROXQP_SPARSE_UTILS_HPP |
7 | 7 | #define PROXSUITE_PROXQP_SPARSE_UTILS_HPP |
8 | 8 |
|
| 9 | +#include <iostream> |
| 10 | +#include <Eigen/IterativeLinearSolvers> |
| 11 | +#include <unsupported/Eigen/IterativeSolvers> |
| 12 | + |
| 13 | +#include "proxsuite/helpers/common.hpp" |
9 | 14 | #include <proxsuite/linalg/dense/core.hpp> |
10 | 15 | #include <proxsuite/linalg/sparse/core.hpp> |
11 | 16 | #include <proxsuite/linalg/sparse/factorize.hpp> |
|
21 | 26 | #include "proxsuite/proxqp/sparse/preconditioner/ruiz.hpp" |
22 | 27 | #include "proxsuite/proxqp/sparse/preconditioner/identity.hpp" |
23 | 28 |
|
24 | | -#include <iostream> |
25 | | -#include <Eigen/IterativeLinearSolvers> |
26 | | -#include <unsupported/Eigen/IterativeSolvers> |
27 | | - |
28 | 29 | namespace proxsuite { |
29 | 30 | namespace proxqp { |
30 | 31 | namespace sparse { |
@@ -98,11 +99,28 @@ print_setup_header(const Settings<T>& settings, |
98 | 99 | << std::endl; |
99 | 100 | } |
100 | 101 | } |
| 102 | + |
101 | 103 | 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 |
102 | 118 | template<typename T> |
103 | 119 | auto |
104 | 120 | positive_part(T const& expr) |
105 | 121 | VEG_DEDUCE_RET((expr.array() > 0).select(expr, T::Zero(expr.rows()))); |
| 122 | + |
| 123 | +/// @brief \brief Returns the negative part of an expression |
106 | 124 | template<typename T> |
107 | 125 | auto |
108 | 126 | negative_part(T const& expr) |
@@ -657,27 +675,36 @@ unscaled_primal_dual_residual( |
657 | 675 | precond.unscale_primal_in_place({ proxqp::from_eigen, x_e }); |
658 | 676 | results.info.duality_gap = x_e.dot(data.g); // contains gTx |
659 | 677 | 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); |
661 | 680 | results.info.duality_gap += xHx; |
662 | 681 | rhs_duality_gap = std::max(rhs_duality_gap, std::abs(xHx)); |
663 | 682 | tmp += data.g; // contains now Hx+g |
664 | 683 | precond.scale_primal_in_place({ proxqp::from_eigen, x_e }); |
665 | 684 |
|
666 | 685 | 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); |
668 | 687 | results.info.duality_gap += by; |
669 | 688 | rhs_duality_gap = std::max(rhs_duality_gap, std::abs(by)); |
670 | 689 | precond.scale_dual_in_place_eq({ proxsuite::proxqp::from_eigen, y_e }); |
| 690 | + |
671 | 691 | 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())); |
673 | 695 | results.info.duality_gap += zl; |
674 | 696 | 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())); |
676 | 700 | results.info.duality_gap += zu; |
677 | 701 | rhs_duality_gap = std::max(rhs_duality_gap, std::abs(zu)); |
| 702 | + |
678 | 703 | precond.scale_dual_in_place_in({ proxsuite::proxqp::from_eigen, z_e }); |
| 704 | + |
679 | 705 | results.info.duality_gap /= |
680 | 706 | sqrt_max_dim; // in order to get an a-dimensional duality gap |
| 707 | + rhs_duality_gap /= sqrt_max_dim; |
681 | 708 | } |
682 | 709 |
|
683 | 710 | { |
|
0 commit comments