Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dense LP interface #231

Merged
merged 4 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bindings/python/src/expose-settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ exposeSettings(pybind11::module_ m)
.value("GPDAL", MeritFunctionType::GPDAL)
.value("PDAL", MeritFunctionType::PDAL)
.export_values();
::pybind11::enum_<ProblemType>(m, "problem_type", pybind11::module_local())
.value("QP", ProblemType::QP)
.value("LP", ProblemType::LP)
.export_values();

::pybind11::enum_<SparseBackend>(m, "SparseBackend", pybind11::module_local())
.value("Automatic", SparseBackend::Automatic)
Expand Down Expand Up @@ -83,6 +87,7 @@ exposeSettings(pybind11::module_ m)
.def_readwrite("bcl_update", &Settings<T>::bcl_update)
.def_readwrite("merit_function_type", &Settings<T>::merit_function_type)
.def_readwrite("alpha_gpdal", &Settings<T>::alpha_gpdal)
.def_readwrite("problem_type", &Settings<T>::problem_type)
.def(pybind11::self == pybind11::self)
.def(pybind11::self != pybind11::self)
.def(pybind11::pickle(
Expand Down
1 change: 1 addition & 0 deletions doc/2-PROXQP_API/2-ProxQP_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ In this table you have on the three columns from left to right: the name of the
| safe_guard | 1.E4 | Safeguard parameter ensuring global convergence of the scheme. More precisely, if the total number of iteration is superior to safe_guard, the BCL scheme accept always the multipliers (hence the scheme is a pure proximal point algorithm).
| preconditioner_max_iter | 10 | Maximal number of authorized iterations for the preconditioner.
| preconditioner_accuracy | 1.E-3 | Accuracy level of the preconditioner.
| problem_type | QP | Defines the type of problem solved (QP or LP). In case LP option is used, the solver does not perform internally linear algebra operations involving the Hessian H.

\subsection OverviewInitialGuess The different initial guesses

Expand Down
20 changes: 16 additions & 4 deletions include/proxsuite/proxqp/dense/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,21 @@ template<typename T>
void
setup_factorization(Workspace<T>& qpwork,
const Model<T>& qpmodel,
const Settings<T>& qpsettings,
Results<T>& qpresults)
{

proxsuite::linalg::veg::dynstack::DynStackMut stack{
proxsuite::linalg::veg::from_slice_mut,
qpwork.ldl_stack.as_mut(),
};

qpwork.kkt.topLeftCorner(qpmodel.dim, qpmodel.dim) = qpwork.H_scaled;
switch (qpsettings.problem_type) {
case ProblemType::QP:
qpwork.kkt.topLeftCorner(qpmodel.dim, qpmodel.dim) = qpwork.H_scaled;
break;
case ProblemType::LP:
break;
}
qpwork.kkt.topLeftCorner(qpmodel.dim, qpmodel.dim).diagonal().array() +=
qpresults.info.rho;
qpwork.kkt.block(0, qpmodel.dim, qpmodel.dim, qpmodel.n_eq) =
Expand Down Expand Up @@ -124,6 +130,7 @@ setup_equilibration(Workspace<T>& qpwork,
execute_preconditioner,
qpsettings.preconditioner_max_iter,
qpsettings.preconditioner_accuracy,
qpsettings.problem_type,
stack);
qpwork.correction_guess_rhs_g = infty_norm(qpwork.g_scaled);
}
Expand Down Expand Up @@ -385,8 +392,13 @@ setup( //
} // else qpmodel.l remains initialized to a matrix with zero elements or zero
// shape
assert(qpmodel.is_valid());

qpwork.H_scaled = qpmodel.H;
switch (qpsettings.problem_type) {
case ProblemType::LP:
break;
case ProblemType::QP:
qpwork.H_scaled = qpmodel.H;
break;
}
qpwork.g_scaled = qpmodel.g;
qpwork.A_scaled = qpmodel.A;
qpwork.b_scaled = qpmodel.b;
Expand Down
238 changes: 131 additions & 107 deletions include/proxsuite/proxqp/dense/preconditioner/ruiz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "proxsuite/proxqp/dense/views.hpp"
#include "proxsuite/proxqp/dense/fwd.hpp"
#include <proxsuite/linalg/dense/core.hpp>
#include <proxsuite/proxqp/settings.hpp>
#include <ostream>

#include <Eigen/Core>
Expand All @@ -34,12 +35,12 @@ ruiz_scale_qp_in_place( //
T epsilon,
isize max_iter,
Symmetry sym,
ProblemType problem_type,
proxsuite::linalg::veg::dynstack::DynStackMut stack) -> T
{

T c(1);
auto S = delta_.to_eigen();

auto H = qp.H.to_eigen();
auto g = qp.g.to_eigen();
auto A = qp.A.to_eigen();
Expand All @@ -65,7 +66,13 @@ ruiz_scale_qp_in_place( //
LDLT_TEMP_VEC(T, delta, n + n_eq + n_in, stack);

i64 iter = 1;

switch (problem_type) {
case ProblemType::LP:
delta.head(n).setOnes();
break;
case ProblemType::QP:
break;
}
while (infty_norm((1 - delta.array()).matrix()) > epsilon) {
if (logger_ptr != nullptr) {
*logger_ptr //
Expand All @@ -83,54 +90,59 @@ ruiz_scale_qp_in_place( //

// normalization vector
{
for (isize k = 0; k < n; ++k) {
switch (sym) {
case Symmetry::upper: { // upper triangular part
T aux = sqrt(std::max({
infty_norm(H.col(k).head(k)),
infty_norm(H.row(k).tail(n - k)),
n_eq > 0 ? infty_norm(A.col(k)) : T(0),
n_in > 0 ? infty_norm(C.col(k)) : T(0),
}));
if (aux == T(0)) {
delta(k) = T(1);
} else {
delta(k) = T(1) / (aux + machine_eps);
}
break;
}
case Symmetry::lower: { // lower triangular part

T aux = sqrt(std::max({
infty_norm(H.col(k).head(k)),
infty_norm(H.col(k).tail(n - k)),
n_eq > 0 ? infty_norm(A.col(k)) : T(0),
n_in > 0 ? infty_norm(C.col(k)) : T(0),
}));
if (aux == T(0)) {
delta(k) = T(1);
} else {
delta(k) = T(1) / (aux + machine_eps);
}
break;
}
case Symmetry::general: {

T aux = sqrt(std::max({
infty_norm(H.col(k)),
n_eq > 0 ? infty_norm(A.col(k)) : T(0),
n_in > 0 ? infty_norm(C.col(k)) : T(0),
}));
if (aux == T(0)) {
delta(k) = T(1);
} else {
delta(k) = T(1) / (aux + machine_eps);
switch (problem_type) {
case ProblemType::LP:
break;
case ProblemType::QP:
for (isize k = 0; k < n; ++k) {
switch (sym) {
case Symmetry::upper: { // upper triangular part
T aux = sqrt(std::max({
infty_norm(H.col(k).head(k)),
infty_norm(H.row(k).tail(n - k)),
n_eq > 0 ? infty_norm(A.col(k)) : T(0),
n_in > 0 ? infty_norm(C.col(k)) : T(0),
}));
if (aux == T(0)) {
delta(k) = T(1);
} else {
delta(k) = T(1) / (aux + machine_eps);
}
break;
}
case Symmetry::lower: { // lower triangular part

T aux = sqrt(std::max({
infty_norm(H.col(k).head(k)),
infty_norm(H.col(k).tail(n - k)),
n_eq > 0 ? infty_norm(A.col(k)) : T(0),
n_in > 0 ? infty_norm(C.col(k)) : T(0),
}));
if (aux == T(0)) {
delta(k) = T(1);
} else {
delta(k) = T(1) / (aux + machine_eps);
}
break;
}
case Symmetry::general: {

T aux = sqrt(std::max({
infty_norm(H.col(k)),
n_eq > 0 ? infty_norm(A.col(k)) : T(0),
n_in > 0 ? infty_norm(C.col(k)) : T(0),
}));
if (aux == T(0)) {
delta(k) = T(1);
} else {
delta(k) = T(1) / (aux + machine_eps);
}
break;
}
}
break;
}
}
break;
}

for (isize k = 0; k < n_eq; ++k) {
T aux = sqrt(infty_norm(A.row(k)));
if (aux == T(0)) {
Expand Down Expand Up @@ -160,72 +172,77 @@ ruiz_scale_qp_in_place( //
l.array() *= delta.tail(n_in).array();

// normalize H
switch (sym) {
case Symmetry::upper: {
// upper triangular part
for (isize j = 0; j < n; ++j) {
H.col(j).head(j + 1) *= delta(j);
}
// normalisation des lignes
for (isize i = 0; i < n; ++i) {
H.row(i).tail(n - i) *= delta(i);
}
switch (problem_type) {
case ProblemType::LP:
break;
}
case Symmetry::lower: {
// lower triangular part
for (isize j = 0; j < n; ++j) {
H.col(j).tail(n - j) *= delta(j);
}
// normalisation des lignes
for (isize i = 0; i < n; ++i) {
H.row(i).head(i + 1) *= delta(i);
case ProblemType::QP:
switch (sym) {
case Symmetry::upper: {
// upper triangular part
for (isize j = 0; j < n; ++j) {
H.col(j).head(j + 1) *= delta(j);
}
// normalisation des lignes
for (isize i = 0; i < n; ++i) {
H.row(i).tail(n - i) *= delta(i);
}
break;
}
case Symmetry::lower: {
// lower triangular part
for (isize j = 0; j < n; ++j) {
H.col(j).tail(n - j) *= delta(j);
}
// normalisation des lignes
for (isize i = 0; i < n; ++i) {
H.row(i).head(i + 1) *= delta(i);
}
break;
}
case Symmetry::general: {
// all matrix
H = delta.head(n).asDiagonal() * H * delta.head(n).asDiagonal();
break;
}
default:
break;
}
break;
}
case Symmetry::general: {
// all matrix
H = delta.head(n).asDiagonal() * H * delta.head(n).asDiagonal();
break;
}
default:
break;
}

// additional normalization for the cost function
switch (sym) {
case Symmetry::upper: {
// upper triangular part
T tmp = T(0);
for (isize j = 0; j < n; ++j) {
tmp += proxqp::dense::infty_norm(H.row(j).tail(n - j));
}
gamma = 1 / std::max(tmp / T(n), T(1));
break;
}
case Symmetry::lower: {
// lower triangular part
T tmp = T(0);
for (isize j = 0; j < n; ++j) {
tmp += proxqp::dense::infty_norm(H.col(j).tail(n - j));
// additional normalization for the cost function
switch (sym) {
case Symmetry::upper: {
// upper triangular part
T tmp = T(0);
for (isize j = 0; j < n; ++j) {
tmp += proxqp::dense::infty_norm(H.row(j).tail(n - j));
}
gamma = 1 / std::max(tmp / T(n), T(1));
break;
}
case Symmetry::lower: {
// lower triangular part
T tmp = T(0);
for (isize j = 0; j < n; ++j) {
tmp += proxqp::dense::infty_norm(H.col(j).tail(n - j));
}
gamma = 1 / std::max(tmp / T(n), T(1));
break;
}
case Symmetry::general: {
// all matrix
gamma =
1 / std::max(
T(1),
(H.colwise().template lpNorm<Eigen::Infinity>()).mean());
break;
}
default:
break;
}
gamma = 1 / std::max(tmp / T(n), T(1));
break;
}
case Symmetry::general: {
// all matrix
gamma =
1 /
std::max(T(1),
(H.colwise().template lpNorm<Eigen::Infinity>()).mean());
break;
}
default:
H *= gamma;
break;
}

g *= gamma;
H *= gamma;

S.array() *= delta.array(); // coefficientwise product
c *= gamma;
Expand Down Expand Up @@ -316,6 +333,7 @@ struct RuizEquilibration
bool execute_preconditioner,
const isize max_iter,
const T epsilon,
const ProblemType problem_type,
proxsuite::linalg::veg::dynstack::DynStackMut stack)
{
if (execute_preconditioner) {
Expand All @@ -326,6 +344,7 @@ struct RuizEquilibration
epsilon,
max_iter,
sym,
problem_type,
stack);
} else {

Expand Down Expand Up @@ -397,6 +416,7 @@ struct RuizEquilibration
*/
void scale_qp(const QpViewBox<T> qp,
QpViewBoxMut<T> scaled_qp,
bool execute_preconditioner,
VectorViewMut<T> tmp_delta_preallocated) const
{

Expand All @@ -413,7 +433,11 @@ struct RuizEquilibration
scaled_qp.b.to_eigen() = qp.b.to_eigen();
scaled_qp.d.to_eigen() = qp.d.to_eigen();

scale_qp_in_place(scaled_qp, tmp_delta_preallocated, epsilon, max_iter);
scale_qp_in_place(scaled_qp,
execute_preconditioner,
tmp_delta_preallocated,
epsilon,
max_iter);
}
// modifies variables in place
/*!
Expand Down
Loading