|
| 1 | +// To use float instead of double: |
| 2 | +// #define CLARABEL_USE_FLOAT |
| 3 | + |
| 4 | +#include "utils.h" |
| 5 | +#include <stdio.h> |
| 6 | +#include <Clarabel.h> |
| 7 | + |
| 8 | +int main(void) |
| 9 | +{ |
| 10 | + |
| 11 | + #ifndef FEATURE_PARDISO_MKL |
| 12 | + printf("This example requires MKL Pardiso support.\n"); |
| 13 | + return 1; |
| 14 | +#else |
| 15 | + |
| 16 | + // QP Example |
| 17 | + |
| 18 | + /* From dense matrix: |
| 19 | + * [[6., 0.], |
| 20 | + * [0., 4.]] |
| 21 | + */ |
| 22 | + ClarabelCscMatrix P; |
| 23 | + clarabel_CscMatrix_init( |
| 24 | + &P, |
| 25 | + 2, // row |
| 26 | + 2, // col |
| 27 | + (uintptr_t[]){ 0, 1, 2 }, // colptr |
| 28 | + (uintptr_t[]){ 0, 1 }, // rowval |
| 29 | + (ClarabelFloat[]){ 6., 4. } // nzval |
| 30 | + ); |
| 31 | + |
| 32 | + ClarabelFloat q[2] = { -1., -4. }; |
| 33 | + |
| 34 | + /* From dense matrix: |
| 35 | + * [[ 1., -2.], // <-- LHS of equality constraint (lower bound) |
| 36 | + * [ 1., 0.], // <-- LHS of inequality constraint (upper bound) |
| 37 | + * [ 0., 1.], // <-- LHS of inequality constraint (upper bound) |
| 38 | + * [-1., 0.], // <-- LHS of inequality constraint (lower bound) |
| 39 | + * [ 0., -1.]] // <-- LHS of inequality constraint (lower bound) |
| 40 | + */ |
| 41 | + ClarabelCscMatrix A; |
| 42 | + clarabel_CscMatrix_init( |
| 43 | + &A, |
| 44 | + 5, // row |
| 45 | + 2, // col |
| 46 | + (uintptr_t[]){ 0, 3, 6 }, // colptr |
| 47 | + (uintptr_t[]){ 0, 1, 3, 0, 2, 4 }, // rowval |
| 48 | + (ClarabelFloat[]){ 1., 1., -1., -2., 1., -1. } // nzval |
| 49 | + ); |
| 50 | + |
| 51 | + ClarabelFloat b[5] = { 0., 1., 1., 1., 1. }; |
| 52 | + |
| 53 | + ClarabelSupportedConeT cones[2] = { ClarabelZeroConeT(1), ClarabelNonnegativeConeT(4) }; |
| 54 | + |
| 55 | + // Settings |
| 56 | + ClarabelDefaultSettings settings = clarabel_DefaultSettings_default(); |
| 57 | + //settings.direct_solve_method = PARDISO_PANUA; |
| 58 | + settings.direct_solve_method = PARDISO_MKL; |
| 59 | + |
| 60 | + // turn on verbose output and change some settings |
| 61 | + settings.pardiso_verbose = true; |
| 62 | + settings.pardiso_iparm[1] = 0; // tries minimum degree ordering instead of default |
| 63 | + |
| 64 | + // Build solver |
| 65 | + ClarabelDefaultSolver *solver = clarabel_DefaultSolver_new( |
| 66 | + &P, // P |
| 67 | + q, // q |
| 68 | + &A, // A |
| 69 | + b, // b |
| 70 | + 2, // n_cones |
| 71 | + cones, &settings |
| 72 | + ); |
| 73 | + |
| 74 | + // Solve |
| 75 | + clarabel_DefaultSolver_solve(solver); |
| 76 | + |
| 77 | + // Get solution |
| 78 | + ClarabelDefaultSolution solution = clarabel_DefaultSolver_solution(solver); |
| 79 | + print_solution(&solution); |
| 80 | + |
| 81 | + // Get some detailed solve information |
| 82 | + ClarabelDefaultInfo_f64 info = clarabel_DefaultSolver_info(solver); |
| 83 | + printf("primal residual = %e\n", info.res_primal); |
| 84 | + printf("dual residual = %e\n", info.res_dual); |
| 85 | + printf("# of threads = %d\n", info.linsolver.threads); |
| 86 | + printf("KKT nonzeros = %d\n", info.linsolver.nnzA); |
| 87 | + printf("factor nonzeros = %d\n", info.linsolver.nnzL); |
| 88 | + |
| 89 | + // Free the matrices and the solver |
| 90 | + clarabel_DefaultSolver_free(solver); |
| 91 | + |
| 92 | + return 0; |
| 93 | + |
| 94 | +#endif |
| 95 | +} |
0 commit comments