QPALM main
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
examples/c/qpalm_demo.c
1#include <qpalm.h>
2#include <stdio.h>
3
4#define N 2
5#define M 3
6#define ANZMAX 4
7#define QNZMAX 2
8
9#define TRUE 1
10#define FALSE 0
11
12c_float *random_vector(c_int n) {
13 c_float *X = (c_float *)qpalm_calloc(n, sizeof(c_float));
14 for (int i = 0; i < n; i++)
15 X[i] = (c_float)10 * rand() / (c_float)RAND_MAX;
16 return X;
17}
18
19c_float *constant_vector(c_float c, c_int n) {
20 c_float *X = (c_float *)qpalm_calloc(n, sizeof(c_float));
21 for (int i = 0; i < n; i++)
22 X[i] = c;
23 return X;
24}
25int main() {
26
27 // Load problem data
28 c_int n = N;
29 c_int m = M;
30
31 // Problem settings
32 QPALMSettings *settings =
34
35 // Structures
36 QPALMWorkspace *work; // Solver workspace
37 QPALMData *data; // Problem matrices
38
39 // Populate data
40 srand(12345);
41 data = (QPALMData *)qpalm_malloc(sizeof(QPALMData));
42 data->n = n;
43 data->m = m;
44 data->q = random_vector(data->n);
45 data->c = 0;
46 data->bmin = constant_vector(-2, data->m);
47 data->bmax = constant_vector(2, data->m);
48
49 solver_sparse *A, *Q;
50 A = ladel_sparse_alloc(M, N, ANZMAX, UNSYMMETRIC, TRUE, FALSE);
51 Q = ladel_sparse_alloc(N, N, QNZMAX, UPPER, TRUE, FALSE);
52
53 // clang-format off
54 A->x[0] = 1; A->x[1] = 1; A->x[2] = 1; A->x[3] = 1;
55 A->p[0] = 0; A->p[1] = 2; A->p[2] = 4;
56 A->i[0] = 0; A->i[1] = 2; A->i[2] = 1; A->i[3] = 2;
57 Q->x[0] = 1.0; Q->x[1] = 1.5;
58 Q->p[0] = 0; Q->p[1] = 1; Q->p[2] = 2;
59 Q->i[0] = 0; Q->i[1] = 1;
60 // clang-format on
61 data->A = A;
62 data->Q = Q;
63
64 // Define Solver settings as default
66
67 // Setup workspace
68 work = qpalm_setup(data, settings);
69
70 // Solve Problem
71 qpalm_solve(work);
72
73 printf("Solver status: ");
74 puts(work->info->status);
75 printf("Iter: %d\n", (int)work->info->iter);
76 printf("Iter Out: %d\n", (int)work->info->iter_out);
77 printf("Solution:");
78 for (c_int i = 0; i < n; i++)
79 printf(" %.10f", work->solution->x[i]);
80 printf("\n");
81#ifdef QPALM_TIMING
82 printf("Setup time: %f\n", work->info->setup_time);
83 printf("Solve time: %f\n", work->info->solve_time);
84 printf("Run time: %f\n", work->info->run_time);
85#endif
86
87 // Clean workspace
88 ladel_sparse_free(data->Q);
89 ladel_sparse_free(data->A);
90
91 qpalm_cleanup(work);
92 qpalm_free(data->q);
93 qpalm_free(data->bmin);
94 qpalm_free(data->bmax);
95 qpalm_free(data);
96 qpalm_free(settings);
97
98 return 0;
99}
#define TRUE
Definition constants.h:18
#define FALSE
Definition constants.h:19
void qpalm_free(void *ptr)
Definition global_opts.c:16
void * qpalm_malloc(size_t size)
Definition global_opts.c:7
void * qpalm_calloc(size_t num, size_t size)
Definition global_opts.c:3
ladel_int c_int
type for integer numbers
Definition global_opts.h:42
ladel_double c_float
type for floating point numbers
Definition global_opts.h:41
void qpalm_solve(QPALMWorkspace *work)
Solve the quadratic program.
Definition qpalm.c:483
QPALMWorkspace * qpalm_setup(const QPALMData *data, const QPALMSettings *settings)
Initialize QPALM solver allocating memory.
Definition qpalm.c:68
void qpalm_set_default_settings(QPALMSettings *settings)
Set default settings from constants.h file.
Definition qpalm.c:33
void qpalm_cleanup(QPALMWorkspace *work)
Cleanup the workspace by deallocating memory.
Definition qpalm.c:735
QPALM main solver API.
Data structure.
Definition types.h:109
size_t m
number of constraints m
Definition types.h:111
c_float * bmin
dense array for lower bounds (size m)
Definition types.h:116
c_float c
constant part of cost
Definition types.h:115
size_t n
number of variables n
Definition types.h:110
c_float * q
dense array for linear part of cost function (size n)
Definition types.h:114
solver_sparse * A
sparse linear constraints matrix A (size m x n)
Definition types.h:113
c_float * bmax
dense array for upper bounds (size m)
Definition types.h:117
solver_sparse * Q
sparse quadratic part of the cost Q (size n x n)
Definition types.h:112
c_float run_time
total time (seconds)
Definition types.h:97
c_int iter_out
number of outer iterations (i.e. dual updates)
Definition types.h:83
c_int iter
number of iterations taken
Definition types.h:82
c_float solve_time
time taken for solve phase (seconds)
Definition types.h:96
char status[32]
status string, e.g. 'solved'
Definition types.h:84
c_float setup_time
time taken for setup phase (seconds)
Definition types.h:95
Settings struct.
Definition types.h:124
c_float * x
primal solution
Definition types.h:51
QPALM Workspace.
Definition types.h:204
QPALMInfo * info
solver information
Definition types.h:315
QPALMSolution * solution
problem solution
Definition types.h:314
ladel_sparse_matrix solver_sparse
Definition types.h:26