-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexample_qp.cpp
More file actions
68 lines (54 loc) · 1.85 KB
/
example_qp.cpp
File metadata and controls
68 lines (54 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "utils.h"
#include <iostream>
#include <Clarabel>
#include <Eigen/Eigen>
#include <vector>
using namespace clarabel;
using namespace std;
using namespace Eigen;
int main(void)
{
/* From dense matrix:
* [[6., 0.],
* [0., 4.]]
*/
MatrixXd P_dense(2, 2);
P_dense <<
6., 0.,
0., 4.;
SparseMatrix<double> P = P_dense.sparseView();
P.makeCompressed();
Vector<double, 2> q = { -1., -4. };
MatrixXd A_dense(5, 2);
A_dense <<
1., -2., // <-- LHS of equality constraint (lower bound)
1., 0., // <-- LHS of inequality constraint (upper bound)
0., 1., // <-- LHS of inequality constraint (upper bound)
-1., 0., // <-- LHS of inequality constraint (lower bound)
0., -1.; // <-- LHS of inequality constraint (lower bound)
SparseMatrix<double> A = A_dense.sparseView();
A.makeCompressed();
Vector<double, 5> b = { 0., 1., 1., 1., 1. };
vector<SupportedConeT<double>> cones
{
ZeroConeT<double>(1),
NonnegativeConeT<double>(4),
};
// Settings
DefaultSettings<double> settings = DefaultSettings<double>::default_settings();
// Build solver
DefaultSolver<double> solver(P, q, A, b, cones, settings);
// Solve
solver.solve();
// Get solution
DefaultSolution<double> solution = solver.solution();
utils::print_solution(solution);
// Get some detailed solve information
DefaultInfo<double> info = solver.info();
std::cout << "primal residual = " << info.res_primal << std::endl;
std::cout << "dual residual = " << info.res_dual << std::endl;
std::cout << "# of threads = " << info.linsolver.threads << std::endl;
std::cout << "KKT nonzeros = " << info.linsolver.nnzA << std::endl;
std::cout << "factor nonzeros = " << info.linsolver.nnzL << std::endl;
return 0;
}