QPALM 1.1.2
Proximal Augmented Lagrangian method for Quadratic Programs
qpalm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <sparse.hpp>
4
5#include <types.h> // ::QPALMData, ::QPALMSettings, ::QPALMSolution, ::QPALMInfo
6
7#include <optional>
8
9/**
10 * @defgroup qpalm-cxx-grp C++ Interface
11 *
12 * This is a C++ interface of the QPALM solver that provides a solver class to
13 * help with resource management, and with interoperability with Eigen matrices
14 * and vectors.
15 */
16
17/// @see @ref qpalm-cxx-grp
18namespace qpalm {
19/// RAII-based wrappers for the allocation and deallocation functions of the C
20/// API.
21namespace alloc {
22/// Callable that cleans up the given workspace.
25};
26} // namespace alloc
27
28/**
29 * Stores the matrices and vectors that define the problem.
30 * @f{align*}{
31 * & \operatorname*{\mathrm{minimize}}_x
32 * & & \tfrac{1}{2}x^\top Q x + q^\top x + c \\
33 * & \text{subject to}
34 * & & b_\mathrm{min} \leq Ax \leq b_\mathrm{max}
35 * @f}
36 * @ingroup qpalm-cxx-grp
37 */
38class Data {
39 public:
40 /// Problem dimension
41 /// (size of x and q, number rows and columns of Q, number of columns of A).
43 /// Number of constraints
44 /// (size of bmin and bmax, number of rows of A).
49 vec_t q = vec_t::Zero(n);
50 vec_t bmin = vec_t::Zero(m);
51 vec_t bmax = vec_t::Zero(m);
52
53 public:
54 /// Construct a problem of dimension n with m constraints.
55 Data(index_t n, index_t m) : n{n}, m{m} {}
56
57 /// Set the sparse Q matrix. Creates a copy.
58 void set_Q(const sparse_mat_t &Q) {
59 assert(Q.rows() == n);
60 assert(Q.cols() == n);
61 this->Q = eigen_to_ladel_copy(Q);
62 }
63 /// Set the sparse A matrix. Creates a copy.
64 void set_A(const sparse_mat_t &A) {
65 assert(A.rows() == m);
66 assert(A.cols() == n);
67 this->A = eigen_to_ladel_copy(A);
68 }
69 /// Get a pointer to the underlying C data structure.
70 /// @see @ref ::QPALMData
71 QPALM_CXX_EXPORT const ::QPALMData *get_c_data_ptr() const;
72
73 /// Get a view on the Q matrix of the problem.
75 return {Q->nrow, Q->ncol, Q->nzmax, Q->p, Q->i, Q->x, Q->nz};
76 }
77 /// Get a view on the A matrix of the problem.
79 return {A->nrow, A->ncol, A->nzmax, A->p, A->i, A->x, A->nz};
80 }
81
82 private:
83 // Underlying C data structure that is passed to the solver.
84 mutable ::QPALMData data{};
85};
86
87/**
88 * Settings and parameters for the QPALM solver.
89 * @ingroup qpalm-cxx-grp
90 */
92 /// Construct with default settings.
93 /// @see @ref qpalm_set_default_settings
95};
96
97/**
98 * Information returned by the solver.
99 * @ingroup qpalm-cxx-grp
100 */
101
103
104/**
105 * View on the solution returned by the solver.
106 * @note This is just a view of the solution, which is invalidated when the
107 * solver object is destroyed. Create a copy of @c x and @c y as type
108 * @c vec_t if you need the solution after the solver is gone.
109 */
113};
114
115/**
116 * Main QPALM solver.
117 *
118 * @see @ref ::qpalm_solve
119 * @ingroup qpalm-cxx-grp
120 */
121class Solver {
122 public:
123 /// Create a new solver for the problem defined by @p data and with the
124 /// parameters defined by @p settings.
125 QPALM_CXX_EXPORT Solver(const Data &data, const Settings &settings);
126
127 /// @see @ref ::qpalm_update_settings
128 QPALM_CXX_EXPORT void update_settings(const Settings &settings);
129 /// @see @ref ::qpalm_update_bounds
130 QPALM_CXX_EXPORT void update_bounds(std::optional<const_ref_vec_t> bmin,
131 std::optional<const_ref_vec_t> bmax);
132 /// @see @ref ::qpalm_update_q
134 /// @see @ref ::qpalm_update_Q_A
135 /// @note Updates only the values, sparsity pattern should remain the
136 /// same.
138 const_ref_vec_t A_vals);
139
140 /// @see @ref ::qpalm_warm_start
141 QPALM_CXX_EXPORT void warm_start(std::optional<const_ref_vec_t> x,
142 std::optional<const_ref_vec_t> y);
143
144 /// Solve the problem. The solution will be available through
145 /// @ref get_solution() and the solver information and statistics through
146 /// @ref get_info().
147 /// @see @ref ::qpalm_solve
148 QPALM_CXX_EXPORT void solve();
149
150 /// Get the solution computed by @ref solve().
151 /// @note Returns a view that is only valid as long as the solver is not
152 /// destroyed.
153 /// @see @ref QPALMWorkspace::solution
155 /// Get the solver information from the last call to @ref solve().
156 /// @note Returns a reference that is only valid as long as the solver is
157 /// not destroyed.
158 QPALM_CXX_EXPORT const QPALMInfo &get_info() const;
159
160 /// Get the certificate of primal infeasibility of the problem.
162 /// Get the certificate of dual infeasibility of the problem.
164
165 /// Get the problem dimension @f$ n @f$ (size of @f$ x @f$).
166 /// @see @ref QPALMData::n
167 index_t get_n() const { return work->data->n; }
168 /// Get the number of constraints @f$ m @f$.
169 /// @see @ref QPALMData::m
170 index_t get_m() const { return work->data->m; }
171
172 /// Get a pointer to the underlying C workspace data structure.
173 /// @see @ref ::QPALMWorkspace
174 QPALM_CXX_EXPORT const ::QPALMWorkspace *get_c_work_ptr() const {
175 return work.get();
176 }
177
178 private:
179 using workspace_ptr =
180 std::unique_ptr<::QPALMWorkspace, alloc::qpalm_workspace_cleaner>;
181 workspace_ptr work;
182};
183
184} // namespace qpalm
Stores the matrices and vectors that define the problem.
Definition: qpalm.hpp:38
index_t m
Number of constraints (size of bmin and bmax, number of rows of A).
Definition: qpalm.hpp:45
Data(index_t n, index_t m)
Construct a problem of dimension n with m constraints.
Definition: qpalm.hpp:55
index_t n
Problem dimension (size of x and q, number rows and columns of Q, number of columns of A).
Definition: qpalm.hpp:42
ladel_sparse_matrix_ptr Q
Definition: qpalm.hpp:46
void set_A(const sparse_mat_t &A)
Set the sparse A matrix. Creates a copy.
Definition: qpalm.hpp:64
c_float c
Definition: qpalm.hpp:48
vec_t bmax
Definition: qpalm.hpp:51
ladel_sparse_matrix_ptr A
Definition: qpalm.hpp:47
QPALM_CXX_EXPORTconst ::QPALMData * get_c_data_ptr() const
Get a pointer to the underlying C data structure.
Definition: qpalm.cpp:7
sparse_mat_view_t get_Q() const
Get a view on the Q matrix of the problem.
Definition: qpalm.hpp:74
vec_t q
Definition: qpalm.hpp:49
vec_t bmin
Definition: qpalm.hpp:50
sparse_mat_view_t get_A() const
Get a view on the A matrix of the problem.
Definition: qpalm.hpp:78
void set_Q(const sparse_mat_t &Q)
Set the sparse Q matrix. Creates a copy.
Definition: qpalm.hpp:58
Main QPALM solver.
Definition: qpalm.hpp:121
QPALM_CXX_EXPORT SolutionView get_solution() const
Get the solution computed by solve().
Definition: qpalm.cpp:54
QPALM_CXX_EXPORT void update_Q_A(const_ref_vec_t Q_vals, const_ref_vec_t A_vals)
Definition: qpalm.cpp:42
QPALM_CXX_EXPORT const QPALMInfo & get_info() const
Get the solver information from the last call to solve().
Definition: qpalm.cpp:66
QPALM_CXX_EXPORT void update_q(const_ref_vec_t q)
Definition: qpalm.cpp:38
QPALM_CXX_EXPORT Solver(const Data &data, const Settings &settings)
Create a new solver for the problem defined by data and with the parameters defined by settings.
Definition: qpalm.cpp:25
QPALM_CXX_EXPORT void warm_start(std::optional< const_ref_vec_t > x, std::optional< const_ref_vec_t > y)
Definition: qpalm.cpp:46
QPALM_CXX_EXPORT void update_bounds(std::optional< const_ref_vec_t > bmin, std::optional< const_ref_vec_t > bmax)
Definition: qpalm.cpp:32
QPALM_CXX_EXPORT void solve()
Solve the problem.
Definition: qpalm.cpp:52
QPALM_CXX_EXPORT void update_settings(const Settings &settings)
Definition: qpalm.cpp:28
index_t get_m() const
Get the number of constraints .
Definition: qpalm.hpp:170
QPALM_CXX_EXPORT const_borrowed_vec_t get_dual_inf_certificate() const
Get the certificate of dual infeasibility of the problem.
Definition: qpalm.cpp:76
QPALM_CXX_EXPORTconst ::QPALMWorkspace * get_c_work_ptr() const
Get a pointer to the underlying C workspace data structure.
Definition: qpalm.hpp:174
index_t get_n() const
Get the problem dimension (size of ).
Definition: qpalm.hpp:167
QPALM_CXX_EXPORT const_borrowed_vec_t get_prim_inf_certificate() const
Get the certificate of primal infeasibility of the problem.
Definition: qpalm.cpp:71
ladel_double c_float
type for floating point numbers
Definition: global_opts.h:41
::QPALMInfo Info
Information returned by the solver.
Definition: qpalm.hpp:102
Definition: qpalm.hpp:18
Eigen::Matrix< c_float, Eigen::Dynamic, 1 > vec_t
Owning dense vector type.
Definition: sparse.hpp:28
Eigen::Ref< const vec_t > const_ref_vec_t
Read-only reference to a dense vector (vector view).
Definition: sparse.hpp:36
Eigen::Map< const sparse_mat_t > sparse_mat_view_t
Read-only view on a sparse matrix.
Definition: sparse.hpp:24
std::unique_ptr< ladel_sparse_matrix, alloc::ladel_sparse_matrix_deleter > ladel_sparse_matrix_ptr
Smart pointer that automatically cleans up an owning ladel_sparse_matrix object.
Definition: sparse.hpp:47
Eigen::Index index_t
Index types for vectors and matrices.
Definition: sparse.hpp:18
Eigen::SparseMatrix< c_float, Eigen::ColMajor, sp_index_t > sparse_mat_t
Owning sparse matrix type.
Definition: sparse.hpp:22
Eigen::Map< const vec_t > const_borrowed_vec_t
Read-only borrowed dense vector type (vector view).
Definition: sparse.hpp:32
QPALM_CXX_EXPORT ladel_sparse_matrix_ptr eigen_to_ladel_copy(const sparse_mat_t &mat)
Similar to eigen_to_ladel, but creates a copy of all data, in such a way that the returned matrix is ...
Definition: sparse.cpp:29
::QPALMInfo QPALMInfo
Definition: qpalm.cpp:23
#define QPALM_CXX_EXPORT
Solver return information.
Definition: types.h:74
Settings struct.
Definition: types.h:117
QPALM Workspace.
Definition: types.h:197
Settings and parameters for the QPALM solver.
Definition: qpalm.hpp:91
QPALM_CXX_EXPORT Settings()
Construct with default settings.
Definition: qpalm.cpp:21
View on the solution returned by the solver.
Definition: qpalm.hpp:110
const_borrowed_vec_t y
Definition: qpalm.hpp:112
const_borrowed_vec_t x
Definition: qpalm.hpp:111
Callable that cleans up the given workspace.
Definition: qpalm.hpp:23
QPALM_CXX_EXPORT void operator()(::QPALMWorkspace *) const
Definition: qpalm.cpp:81
Internal data structures used in QPALM.