|
| 1 | +#include "utils.h" |
| 2 | + |
| 3 | +#include <Clarabel> |
| 4 | +#include <Eigen/Eigen> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +using namespace clarabel; |
| 8 | +using namespace std; |
| 9 | +using namespace Eigen; |
| 10 | + |
| 11 | +// NB: this example requires that the solver be built with -DCLARABEL_FEATURE_FAER_SPARSE |
| 12 | + |
| 13 | +int main(void) |
| 14 | +{ |
| 15 | + |
| 16 | +#ifndef FEATURE_PARDISO_MKL |
| 17 | + printf("This example requires MKL Pardiso support.\n"); |
| 18 | + return 1; |
| 19 | +#else |
| 20 | + |
| 21 | + |
| 22 | + /* From dense matrix: |
| 23 | + * [[6., 0.], |
| 24 | + * [0., 4.]] |
| 25 | + */ |
| 26 | + MatrixXd P_dense(2, 2); |
| 27 | + P_dense << |
| 28 | + 6., 0., |
| 29 | + 0., 4.; |
| 30 | + |
| 31 | + SparseMatrix<double> P = P_dense.sparseView(); |
| 32 | + P.makeCompressed(); |
| 33 | + |
| 34 | + Vector<double, 2> q = { -1., -4. }; |
| 35 | + |
| 36 | + MatrixXd A_dense(5, 2); |
| 37 | + A_dense << |
| 38 | + 1., -2., // <-- LHS of equality constraint (lower bound) |
| 39 | + 1., 0., // <-- LHS of inequality constraint (upper bound) |
| 40 | + 0., 1., // <-- LHS of inequality constraint (upper bound) |
| 41 | + -1., 0., // <-- LHS of inequality constraint (lower bound) |
| 42 | + 0., -1.; // <-- LHS of inequality constraint (lower bound) |
| 43 | + |
| 44 | + SparseMatrix<double> A = A_dense.sparseView(); |
| 45 | + A.makeCompressed(); |
| 46 | + |
| 47 | + Vector<double, 5> b = { 0., 1., 1., 1., 1. }; |
| 48 | + |
| 49 | + vector<SupportedConeT<double>> cones |
| 50 | + { |
| 51 | + ZeroConeT<double>(1), |
| 52 | + NonnegativeConeT<double>(4), |
| 53 | + }; |
| 54 | + |
| 55 | + // Settings |
| 56 | + DefaultSettings<double> settings = DefaultSettings<double>::default_settings(); |
| 57 | + settings.direct_solve_method = ClarabelDirectSolveMethods::PARDISO_MKL; |
| 58 | + //set the number of threads |
| 59 | + settings.max_threads = 16; |
| 60 | + |
| 61 | + // Build solver |
| 62 | + DefaultSolver<double> solver(P, q, A, b, cones, settings); |
| 63 | + |
| 64 | + // Solve |
| 65 | + solver.solve(); |
| 66 | + |
| 67 | + // Get solution |
| 68 | + DefaultSolution<double> solution = solver.solution(); |
| 69 | + utils::print_solution(solution); |
| 70 | + |
| 71 | + return 0; |
| 72 | + |
| 73 | +#endif // FEATURE_FAER_SPARSE |
| 74 | +} |
0 commit comments