alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
qpalm-adapter.cpp
Go to the documentation of this file.
5
6#include <qpalm/sparse.hpp>
7
8#include <cmath>
9#include <stdexcept>
10
11namespace alpaqa {
12
13namespace {
14
16
18 switch (symmetry) {
22 default: throw std::invalid_argument("Invalid symmetry");
23 }
24}
25
26} // namespace
27
28OwningQPALMData
31
32 // Get the dimensions of the problem matrices
33 const auto n = problem.get_n(), m = problem.get_m();
34
35 // Dummy data to evaluate Hessian and Jacobian
36 vec x = vec::Zero(n), y = vec::Zero(m), g(m);
37
38 // Construct QPALM problem
40
41 using std::span;
42 using qp_idx_t = qpalm::sp_index_t;
47 { // Evaluate cost Hessian
49 SparsityConv sp_Q{sp_Q_orig, {.order = SparseCSC::SortedRows}};
50 auto nnz_Q = static_cast<qp_idx_t>(sp_Q.get_sparsity().nnz());
51 auto symm = convert_symmetry(sp_Q.get_sparsity().symmetry);
52 qp.sto->Q = qpalm::ladel_sparse_create(n, n, nnz_Q, symm);
53 qp.Q = qp.sto->Q.get();
54 // Copy sparsity pattern
55 std::ranges::copy(sp_Q.get_sparsity().inner_idx, qp.Q->i);
56 std::ranges::copy(sp_Q.get_sparsity().outer_ptr, qp.Q->p);
57 // Get actual values
58 mvec H_values{qp.Q->x, nnz_Q};
59 auto eval_h = [&](rvec v) { problem.eval_hess_L(x, y, 1, v); };
60 sp_Q.convert_values(eval_h, H_values);
61 }
62 { // Evaluate constraints Jacobian
64 SparsityConv sp_A{sp_A_orig, {.order = SparseCSC::SortedRows}};
65 auto nnz_A = static_cast<qp_idx_t>(sp_A.get_sparsity().nnz());
66 auto symm = convert_symmetry(sp_A.get_sparsity().symmetry);
67 qp.sto->A = qpalm::ladel_sparse_create(m, n, nnz_A + n, symm);
68 qp.A = qp.sto->A.get();
69 // Copy sparsity pattern
70 std::ranges::copy(sp_A.get_sparsity().inner_idx, qp.A->i);
71 std::ranges::copy(sp_A.get_sparsity().outer_ptr, qp.A->p);
72 // Get actual values
73 mvec J_values{qp.A->x, nnz_A};
74 auto eval_j = [&](rvec v) { problem.eval_jac_g(x, v); };
75 sp_A.convert_values(eval_j, J_values);
76 // Add the bound constraints
77 ConstrConv::SparseView A{
78 .nrow = qp.A->nrow,
79 .ncol = qp.A->ncol,
80 .inner_idx = span{qp.A->i, static_cast<size_t>(qp.A->nzmax)},
81 .outer_ptr = span{qp.A->p, static_cast<size_t>(qp.A->ncol) + 1},
82 .values = span{qp.A->x, static_cast<size_t>(qp.A->nzmax)},
83 };
84 ConstrConv::add_box_constr_to_constr_matrix(A, problem.get_box_C());
85 qp.A->nrow = A.nrow;
86 }
87 { // Evaluate constraints
88 problem.eval_g(x, g);
89 }
90 { // Evaluate cost and cost gradient
91 qp.sto->q.resize(n);
92 qp.q = qp.sto->q.data();
93 qp.c = problem.eval_f_grad_f(x, qp.sto->q);
94 }
95 { // Combine bound constraints
96 qp.sto->b.lowerbound.resize(qp.A->nrow);
97 qp.sto->b.upperbound.resize(qp.A->nrow);
98 qp.bmin = qp.sto->b.lowerbound.data();
99 qp.bmax = qp.sto->b.upperbound.data();
100 // Combine bound constraints and linear constraints
101 auto &&C = problem.get_box_C(), &&D = problem.get_box_D();
102 ConstrConv::combine_bound_constr(C, D, qp.sto->b, g);
103 }
104 qp.m = static_cast<size_t>(qp.A->nrow);
105 qp.n = static_cast<size_t>(qp.Q->nrow);
106 return qp;
107}
108
109} // namespace alpaqa
The main polymorphic minimization problem interface.
const Box & get_box_D() const
[Optional] Get the rectangular constraint set of the general constraint function, .
void eval_jac_g(crvec x, rvec J_values) const
[Optional] Function that evaluates the nonzero values of the Jacobian matrix of the constraints,
Sparsity get_jac_g_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Jacobian of the constraints.
length_t get_n() const
[Required] Number of decision variables.
Sparsity get_hess_L_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Hessian of the Lagrangian.
length_t get_m() const
[Required] Number of constraints.
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
[Optional] Evaluate both and its gradient, .
void eval_g(crvec x, rvec gx) const
[Required] Function that evaluates the constraints,
void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const
[Optional] Function that evaluates the nonzero values of the Hessian of the Lagrangian,
const Box & get_box_C() const
[Optional] Get the rectangular constraint set of the decision variables, .
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
int convert_symmetry(sparsity::Symmetry symmetry)
Symmetry
Describes the symmetry of matrices.
Definition sparsity.hpp:12
@ Upper
Symmetric, upper-triangular part is stored.
@ Lower
Symmetric, lower-triangular part is stored.
Converts one matrix storage format to another.
typename Conf::mvec mvec
Definition config.hpp:89
OwningQPALMData build_qpalm_problem(const TypeErasedProblem< EigenConfigd > &problem)
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::vec vec
Definition config.hpp:88
Double-precision double configuration.
Definition config.hpp:176
Sparse compressed-column structure (CCS or CSC).
Definition sparsity.hpp:29
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106