QPALM 1.1.5a1
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 <sparse.hpp>
4
5#include <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.
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 n with m constraints.
56 Data(index_t n, index_t m) : n{n}, m{m} {}
57
58 /// Set the sparse Q matrix. Creates a copy.
59 void set_Q(const sparse_mat_t &Q) {
60 assert(Q.rows() == n);
61 assert(Q.cols() == n);
62 this->Q = eigen_to_ladel_copy(Q);
63 }
64 /// Set the sparse A matrix. Creates a copy.
65 void set_A(const sparse_mat_t &A) {
66 assert(A.rows() == m);
67 assert(A.cols() == n);
68 this->A = eigen_to_ladel_copy(A);
69 }
70 /// Get a pointer to the underlying C data structure.
71 /// @see @ref ::QPALMData
72 QPALM_CXX_EXPORT const ::QPALMData *get_c_data_ptr() const;
73
74 /// Get a view on the Q matrix of the problem.
76 return {static_cast<index_t>(Q->nrow),
77 static_cast<index_t>(Q->ncol),
78 static_cast<index_t>(Q->nzmax),
79 Q->p,
80 Q->i,
81 Q->x,
82 Q->nz};
83 }
84 /// Get a view on the A matrix of the problem.
86 return {static_cast<index_t>(A->nrow),
87 static_cast<index_t>(A->ncol),
88 static_cast<index_t>(A->nzmax),
89 A->p,
90 A->i,
91 A->x,
92 A->nz};
93 }
94
95 private:
96 // Underlying C data structure that is passed to the solver.
97 mutable ::QPALMData data{};
98};
99
100/**
101 * Settings and parameters for the QPALM solver.
102 * @ingroup qpalm-cxx-grp
103 */
105 /// Construct with default settings.
106 /// @see @ref qpalm_set_default_settings
108};
109
110/**
111 * Information returned by the solver.
112 * @ingroup qpalm-cxx-grp
113 */
114
116
117/**
118 * View on the solution returned by the solver.
119 * @note This is just a view of the solution, which is invalidated when the
120 * solver object is destroyed. Create a copy of @c x and @c y as type
121 * @c vec_t if you need the solution after the solver is gone.
122 */
126};
127
128/**
129 * Main QPALM solver.
130 *
131 * @see @ref ::qpalm_solve
132 * @ingroup qpalm-cxx-grp
133 */
134class Solver {
135 public:
136 /// Create a new solver for the problem defined by @p data and with the
137 /// parameters defined by @p settings.
138 QPALM_CXX_EXPORT Solver(const Data &data, const Settings &settings);
139
140 /// @see @ref ::qpalm_update_settings
141 QPALM_CXX_EXPORT void update_settings(const Settings &settings);
142 /// @see @ref ::qpalm_update_bounds
143 QPALM_CXX_EXPORT void update_bounds(std::optional<const_ref_vec_t> bmin,
144 std::optional<const_ref_vec_t> bmax);
145 /// @see @ref ::qpalm_update_q
147 /// @see @ref ::qpalm_update_Q_A
148 /// @note Updates only the values, sparsity pattern should remain the
149 /// same.
151 const_ref_vec_t A_vals);
152
153 /// @see @ref ::qpalm_warm_start
154 QPALM_CXX_EXPORT void warm_start(std::optional<const_ref_vec_t> x,
155 std::optional<const_ref_vec_t> y);
156
157 /// Solve the problem. The solution will be available through
158 /// @ref get_solution() and the solver information and statistics through
159 /// @ref get_info().
160 /// @see @ref ::qpalm_solve
161 QPALM_CXX_EXPORT void solve();
162
163 /// Get the solution computed by @ref solve().
164 /// @note Returns a view that is only valid as long as the solver is not
165 /// destroyed.
166 /// @see @ref QPALMWorkspace::solution
168 /// Get the solver information from the last call to @ref solve().
169 /// @note Returns a reference that is only valid as long as the solver is
170 /// not destroyed.
171 QPALM_CXX_EXPORT const QPALMInfo &get_info() const;
172
173 /// Get the certificate of primal infeasibility of the problem.
175 /// Get the certificate of dual infeasibility of the problem.
177
178 /// Get the problem dimension @f$ n @f$ (size of @f$ x @f$).
179 /// @see @ref QPALMData::n
180 index_t get_n() const { return work->data->n; }
181 /// Get the number of constraints @f$ m @f$.
182 /// @see @ref QPALMData::m
183 index_t get_m() const { return work->data->m; }
184
185 /// Get a pointer to the underlying C workspace data structure.
186 /// @see @ref ::QPALMWorkspace
187 QPALM_CXX_EXPORT const ::QPALMWorkspace *get_c_work_ptr() const {
188 return work.get();
189 }
190
191 private:
192 using workspace_ptr =
193 std::unique_ptr<::QPALMWorkspace, alloc::qpalm_workspace_cleaner>;
194 workspace_ptr work;
195};
196
197} // 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
ladel_sparse_matrix_ptr Q
Definition qpalm.hpp:47
void set_A(const sparse_mat_t &A)
Set the sparse A matrix. Creates a copy.
Definition qpalm.hpp:65
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:7
sparse_mat_view_t get_Q() const
Get a view on the Q matrix of the problem.
Definition qpalm.hpp:75
vec_t q
Definition qpalm.hpp:50
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:85
void set_Q(const sparse_mat_t &Q)
Set the sparse Q matrix. Creates a copy.
Definition qpalm.hpp:59
Main QPALM solver.
Definition qpalm.hpp:134
SolutionView get_solution() const
Get the solution computed by solve().
Definition qpalm.cpp:54
void update_Q_A(const_ref_vec_t Q_vals, const_ref_vec_t A_vals)
Definition qpalm.cpp:42
const QPALMInfo & get_info() const
Get the solver information from the last call to solve().
Definition qpalm.cpp:66
const ::QPALMWorkspace * get_c_work_ptr() const
Get a pointer to the underlying C workspace data structure.
Definition qpalm.hpp:187
void update_q(const_ref_vec_t q)
Definition qpalm.cpp:38
void warm_start(std::optional< const_ref_vec_t > x, std::optional< const_ref_vec_t > y)
Definition qpalm.cpp:46
void update_bounds(std::optional< const_ref_vec_t > bmin, std::optional< const_ref_vec_t > bmax)
Definition qpalm.cpp:32
void solve()
Solve the problem.
Definition qpalm.cpp:52
void update_settings(const Settings &settings)
Definition qpalm.cpp:28
index_t get_m() const
Get the number of constraints .
Definition qpalm.hpp:183
const_borrowed_vec_t get_dual_inf_certificate() const
Get the certificate of dual infeasibility of the problem.
Definition qpalm.cpp:76
index_t get_n() const
Get the problem dimension (size of ).
Definition qpalm.hpp:180
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:115
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
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:30
::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:104
Settings()
Construct with default settings.
Definition qpalm.cpp:21
View on the solution returned by the solver.
Definition qpalm.hpp:123
const_borrowed_vec_t y
Definition qpalm.hpp:125
const_borrowed_vec_t x
Definition qpalm.hpp:124
Callable that cleans up the given workspace.
Definition qpalm.hpp:24
void operator()(::QPALMWorkspace *) const
Definition qpalm.cpp:81
Internal data structures used in QPALM.