QPALM 1.1.1
Proximal Augmented Lagrangian method for Quadratic Programs
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 = 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 }
17 return X;
18}
19
20c_float* constant_vector(c_float c, c_int n) {
21 c_float* X = qpalm_calloc(n, sizeof(c_float));
22 for (int i = 0; i < n; i++) {
23 X[i] = c;
24 }
25 return X;
26}
27int main() {
28
29// Load problem data
30
31 c_int n = N;
32 c_int m = M;
33
34 // Problem settings
36
37 // Structures
38 QPALMWorkspace *work; // Workspace
39 QPALMData *data; // QPALMData
40
41 // Populate data
42 data = (QPALMData *)qpalm_malloc(sizeof(QPALMData));
43 data->n = n;
44 data->m = m;
45 data->q = random_vector(data->n);
46 data->c = 0;
47 data->bmin = constant_vector(-2, data->m);
48 data->bmax = constant_vector(2, data->m);
49
50 solver_sparse *A, *Q;
51 A = ladel_sparse_alloc(M, N, ANZMAX, UNSYMMETRIC, TRUE, FALSE);
52 Q = ladel_sparse_alloc(N, N, QNZMAX, UPPER, TRUE, FALSE);
53
54 c_float *Ax;
55 c_int *Ai, *Ap;
56 Ax = A->x;
57 Ap = A->p;
58 Ai = A->i;
59 Ax[0] = 1.0; Ax[1] = 1.0; Ax[2] = 1.0; Ax[3] = 1.0;
60 Ap[0] = 0; Ap[1] = 2; Ap[2] = 4;
61 Ai[0] = 0; Ai[1] = 2; Ai[2] = 1; Ai[3] = 2;
62 c_float *Qx;
63 c_int *Qi, *Qp;
64 Qx = Q->x;
65 Qp = Q->p;
66 Qi = Q->i;
67 Qx[0] = 1.0; Qx[1] = 1.5;
68 Qp[0] = 0; Qp[1] = 1; Qp[2] = 2;
69 Qi[0] = 0; Qi[1] = 1;
70
71 data->A = A;
72 data->Q = Q;
73
74 // Define Solver settings as default
76
77 // Setup workspace
78 work = qpalm_setup(data, settings);
79
80 // Solve Problem
81 qpalm_solve(work);
82
83 printf("Solver status: ");
84 puts(work->info->status);
85 printf("Iter: %d\n", (int) work->info->iter);
86 printf("Iter Out: %d\n", (int) work->info->iter_out);
87 // for (c_int i = 0; i < n; i++) {
88 // printf("Solution variable %.10f \n",work->x[i]);
89 // }
90 #ifdef QPALM_TIMING
91 printf("Setup time: %f\n", work->info->setup_time);
92 printf("Solve time: %f\n", work->info->solve_time);
93 printf("Run time: %f\n", work->info->run_time);
94 #endif
95
96 // Clean workspace
97 data->Q = ladel_sparse_free(data->Q);
98 data->A = ladel_sparse_free(data->A);
99
100 qpalm_cleanup(work);
101 qpalm_free(data->q);
102 qpalm_free(data->bmin);
103 qpalm_free(data->bmax);
104 qpalm_free(data);
105 qpalm_free(settings);
106
107 return 0;
108
109}
#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_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:723
void qpalm_solve(QPALMWorkspace *work)
Solve the quadratic program.
Definition: qpalm.c:476
QPALMWorkspace * qpalm_setup(const QPALMData *data, const QPALMSettings *settings)
Initialize QPALM solver allocating memory.
Definition: qpalm.c:68
QPALM main solver API.
Data structure.
Definition: types.h:102
size_t m
number of constraints m
Definition: types.h:104
c_float * bmin
dense array for lower bounds (size m)
Definition: types.h:109
c_float c
constant part of cost
Definition: types.h:108
size_t n
number of variables n
Definition: types.h:103
c_float * q
dense array for linear part of cost function (size n)
Definition: types.h:107
solver_sparse * A
sparse linear constraints matrix A (size m x n)
Definition: types.h:106
c_float * bmax
dense array for upper bounds (size m)
Definition: types.h:110
solver_sparse * Q
sparse quadratic part of the cost Q (size n x n)
Definition: types.h:105
c_float run_time
total time (seconds)
Definition: types.h:90
c_int iter_out
number of outer iterations (i.e. dual updates)
Definition: types.h:76
c_int iter
number of iterations taken
Definition: types.h:75
c_float solve_time
time taken for solve phase (seconds)
Definition: types.h:89
char status[32]
status string, e.g. 'solved'
Definition: types.h:77
c_float setup_time
time taken for setup phase (seconds)
Definition: types.h:88
Settings struct.
Definition: types.h:117
QPALM Workspace.
Definition: types.h:197
QPALMInfo * info
solver information
Definition: types.h:308
ladel_sparse_matrix solver_sparse
Definition: types.h:19