QPALM main
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
examples/cxx/qpalm_demo.cpp
1#include <qpalm.hpp>
2
3#include <iostream>
4#include <vector>
5
6// Helper function to easily construct sparse matrices.
7static auto make_sparse_matrix(qpalm::index_t rows, qpalm::index_t cols,
8 const std::vector<qpalm::triplet_t> &triplets) {
9 qpalm::sparse_mat_t M{rows, cols};
10 M.setFromTriplets(begin(triplets), end(triplets));
11 return M;
12}
13
14int main() {
15
16 // Define the problem
17 // ------------------
18 qpalm::index_t n = 3; // Number of unknowns
19 qpalm::index_t m = 4; // Number of constraints
20 qpalm::Data data = {n, m};
21 data.set_Q(make_sparse_matrix(n, n,
22 {
23 {0, 0, 1.0}, // row, col, value
24 {1, 0, -1.0},
25 {0, 1, -1.0},
26 {1, 1, 2.0},
27 }));
28 data.q << -2, -6, 1;
29 data.c = 0;
30 data.set_A(make_sparse_matrix(m, n,
31 {
32 {0, 0, 1.0}, // row, col, value
33 {1, 0, 1.0},
34 {0, 1, 1.0},
35 {2, 1, 1.0},
36 {0, 2, 1.0},
37 {3, 2, 1.0},
38 }));
39 data.bmin = qpalm::vec_t::Constant(m, -5.0);
40 data.bmax = qpalm::vec_t::Constant(m, +8.0);
41
42 // Configure the solver
43 // --------------------
44 qpalm::Settings settings;
45 settings.eps_abs = 1e-10;
46 settings.eps_rel = 1e-10;
47 settings.max_iter = 100;
48 qpalm::Solver solver = {data, settings};
49
50 // Solve the roblem
51 // ----------------
52 solver.solve();
53 auto sol = solver.get_solution();
54 auto info = solver.get_info();
55
56 // Print the results
57 // -----------------
58 std::cout << "Solver status: " << info.status << "\n"
59 << "Iter: " << info.iter << "\n"
60 << "Iter Out: " << info.iter_out << "\n";
61
62#ifdef QPALM_TIMING
63 std::cout << "Setup time: " << info.setup_time << "\n"
64 << "Solve time: " << info.solve_time << "\n"
65 << "Run time: " << info.run_time << "\n\n";
66#endif
67
68 std::cout << "Solution: \n"
69 << sol.x.transpose() << "\n"
70 << "Multipliers:\n"
71 << sol.y.transpose() << "\n";
72}
Stores the matrices and vectors that define the problem.
Definition qpalm.hpp:39
void set_A(const sparse_mat_ref_t &A)
Set the sparse A matrix. Creates a copy.
Definition qpalm.hpp:71
c_float c
Definition qpalm.hpp:49
vec_t bmax
Definition qpalm.hpp:52
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
Main QPALM solver.
Definition qpalm.hpp:140
SolutionView get_solution() const
Get the solution computed by solve().
Definition qpalm.cpp:67
const QPALMInfo & get_info() const
Get the solver information from the last call to solve().
Definition qpalm.cpp:79
void solve()
Solve the problem.
Definition qpalm.cpp:63
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
char status[32]
status string, e.g. 'solved'
Definition types.h:84
c_float eps_rel
relative convergence tolerance
Definition types.h:128
c_int max_iter
maximum number of iterations
Definition types.h:125
c_float eps_abs
absolute convergence tolerance
Definition types.h:127
Settings and parameters for the QPALM solver.
Definition qpalm.hpp:110