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