QPALM 1.1.5a1
Proximal Augmented Lagrangian method for Quadratic Programs
Loading...
Searching...
No Matches
qpalm_fortran.c
Go to the documentation of this file.
1// THIS VERSION: 25/04/2022 AT 13:45 GMT
2// Nick Gould (nick.gould@stfc.ac.uk)
3
4#include "qpalm_fortran.h"
5
6#define TRUE 1
7#define FALSE 0
8
10 f_int m,
11 f_int h_ne,
12 f_int H_ptr[],
13 f_int H_row[],
14 f_float H_val[],
15 f_float g[n],
16 f_float f,
17 f_int a_ne,
18 f_int A_ptr[],
19 f_int A_row[],
20 f_float A_val[],
21 f_float c_l[],
22 f_float c_u[],
23 QPALMSettings settings_c,
24 f_float x[],
25 f_float y[],
26 QPALMInfo *info_c ) {
27
28 // Structures
29 QPALMWorkspace *work; // Workspace
30 QPALMData *data; // QPALMData
31
32 #ifdef QPALM_FORTRAN_DEBUG_PRINT
33 qpalm_print("m = %li \n",m);
34 qpalm_print("n = %li \n",n);
35 #endif
36
37 // Populate QP data
38 data = (QPALMData *)qpalm_malloc(sizeof(QPALMData));
39 data->n = n;
40 data->m = m;
41 data->c = f;
42 data->q = (c_float *)qpalm_malloc(n * sizeof(c_float));
43 data->bmin = (c_float *)qpalm_malloc(m * sizeof(c_float));
44 data->bmax = (c_float *)qpalm_malloc(m * sizeof(c_float));
45
46 for (int i = 0; i < n; i++) {
47 data->q[i] = g[i];
48 }
49 for (int i = 0; i < m; i++) {
50 data->bmin[i] = c_l[i];
51 data->bmax[i] = c_u[i];
52 }
53
54 // Populate A data
55 ladel_int a_nrow;
56 ladel_int a_ncol;
57 ladel_int a_nzmax;
58 ladel_int a_symmetry;
59
60 a_nrow = m;
61 a_ncol = n;
62 a_nzmax = a_ne;
63 a_symmetry = 0;
64
65 #ifdef QPALM_FORTRAN_DEBUG_PRINT
66 qpalm_print("nrow = %li \n",a_nrow);
67 qpalm_print("ncol = %li \n",a_ncol);
68 qpalm_print("nzmax = %li \n",a_nzmax);
69 qpalm_print("\nset A\n");
70 #endif
71
72 ladel_sparse_matrix *A;
73
74 A = ladel_sparse_alloc( a_nrow, a_ncol, a_nzmax, a_symmetry, TRUE, FALSE );
75
76 #ifdef QPALM_FORTRAN_DEBUG_PRINT
77 qpalm_print("nrow = %li \n",A->nrow);
78 qpalm_print("ncol = %li \n",A->ncol);
79 qpalm_print("nzmax = %li \n",A->nzmax);
80 qpalm_print("symmetry = %li \n",A->symmetry);
81 qpalm_print("values = %li \n",A->values);
82 #endif
83
84 c_float *Ax;
85 c_int *Ai, *Ap;
86
87 Ax = A->x;
88 Ap = A->p;
89 Ai = A->i;
90
91 // N.B. convert to 0-based indices
92 for (int i = 0; i < n+1; i++) {
93 Ap[i] = A_ptr[i]-1;
94 #ifdef QPALM_FORTRAN_DEBUG_PRINT
95 printf("A column pointer %i = %i \n",i, A->p[i]);
96 #endif
97 }
98 for (int i = 0; i < a_ne; i++) {
99 Ai[i] = A_row[i]-1;
100 #ifdef QPALM_FORTRAN_DEBUG_PRINT
101 printf("A row index %i = %i \n",i, A->i[i]);
102 #endif
103 }
104 for (int i = 0; i < a_ne; i++) {
105 Ax[i] = A_val[i];
106 #ifdef QPALM_FORTRAN_DEBUG_PRINT
107 printf("A value %i = %.10f \n",i, A->x[i]);
108 #endif
109 }
110
111 data->A = A;
112
113 // Populate Q data
114 ladel_int q_nrow;
115 ladel_int q_ncol;
116 ladel_int q_nzmax;
117 ladel_int q_symmetry;
118
119 q_nrow = n;
120 q_ncol = n;
121 q_nzmax = h_ne;
122 q_symmetry = 1;
123
124 #ifdef QPALM_FORTRAN_DEBUG_PRINT
125 printf("nrow = %li \n",q_nrow);
126 printf("ncol = %li \n",q_ncol);
127 printf("nzmax = %li \n",q_nzmax);
128 printf("\nset Q\n");
129 #endif
130
131 ladel_sparse_matrix *Q;
132
133 Q = ladel_sparse_alloc( q_nrow, q_ncol, q_nzmax, q_symmetry, TRUE, FALSE );
134
135 #ifdef QPALM_FORTRAN_DEBUG_PRINT
136 printf("nrow = %li \n",Q->nrow);
137 printf("ncol = %li \n",Q->ncol);
138 printf("nzmax = %li \n",Q->nzmax);
139 printf("symmetry = %li \n",Q->symmetry);
140 printf("values = %li \n",Q->values);
141 #endif
142
143 c_float *Qx;
144 c_int *Qi, *Qp;
145
146 Qx = Q->x;
147 Qp = Q->p;
148 Qi = Q->i;
149
150 // N.B. convert to 0-based indices
151 for (int i = 0; i < n+1; i++) {
152 Qp[i] = H_ptr[i]-1;
153 #ifdef QPALM_FORTRAN_DEBUG_PRINT
154 printf("Q column pointer %i = %i \n",i, Q->p[i]);
155 #endif
156 }
157 for (int i = 0; i < h_ne; i++) {
158 Qi[i] = H_row[i]-1;
159 #ifdef QPALM_FORTRAN_DEBUG_PRINT
160 printf("Q row index %i = %i \n",i, Q->i[i]);
161 #endif
162 }
163 for (int i = 0; i < h_ne; i++) {
164 Qx[i] = H_val[i];
165 #ifdef QPALM_FORTRAN_DEBUG_PRINT
166 printf("Q value %i = %.10f \n",i, Q->x[i]);
167 #endif
168 }
169
170 data->Q = Q;
171
172 // setup workspace
173 work = qpalm_setup(data, &settings_c);
174
175 // solve Problem
176 qpalm_solve(work);
177
178 // print details of solution if required
179 if ( settings_c.verbose == 1 ) {
180 qpalm_print("Solver status: %s\n", work->info->status);
181 qpalm_print("Iter: %d\n", (int) work->info->iter);
182 qpalm_print("Iter Out: %d\n", (int) work->info->iter_out);
183 #ifdef QPALM_TIMING
184 qpalm_print("Setup time: %f\n", work->info->setup_time);
185 qpalm_print("Solve time: %f\n", work->info->solve_time);
186 qpalm_print("Run time: %f\n", work->info->run_time);
187 #endif
188 }
189
190 // recover solution
191 for (int i = 0; i < n; i++) {
192 x[i] = work->x[i];
193 // printf("Solution variable %.10f \n",x[i]);
194 }
195 for (int i = 0; i < m; i++) {
196 y[i] = work->y[i];
197 // printf("Multiplier variable %.10f \n",y[i]);
198 }
199
200 // return info
201 *info_c = *work->info;
202
203 // Clean workspace
204 data->Q = ladel_sparse_free(data->Q);
205 data->A = ladel_sparse_free(data->A);
206 qpalm_free(data->q);
207 qpalm_free(data->bmin);
208 qpalm_free(data->bmax);
209
210 qpalm_cleanup(work);
211 qpalm_free(data);
212}
void qpalm_free(void *ptr)
Definition global_opts.c:16
void * qpalm_malloc(size_t size)
Definition global_opts.c:7
ladel_int c_int
type for integer numbers
Definition global_opts.h:42
#define qpalm_print
Definition global_opts.h:75
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:476
QPALMWorkspace * qpalm_setup(const QPALMData *data, const QPALMSettings *settings)
Initialize QPALM solver allocating memory.
Definition qpalm.c:68
void qpalm_cleanup(QPALMWorkspace *work)
Cleanup the workspace by deallocating memory.
Definition qpalm.c:723
#define TRUE
#define FALSE
void qpalm_fortran_c(f_int n, f_int m, f_int h_ne, f_int H_ptr[], f_int H_row[], f_float H_val[], f_float g[n], f_float f, f_int a_ne, f_int A_ptr[], f_int A_row[], f_float A_val[], f_float c_l[], f_float c_u[], QPALMSettings settings_c, f_float x[], f_float y[], QPALMInfo *info_c)
double f_float
int f_int
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
Solver return information.
Definition types.h:74
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
c_int verbose
boolean, write out progress
Definition types.h:137
QPALM Workspace.
Definition types.h:197
c_float * x
primal iterate
Definition types.h:204
c_float * y
dual iterate
Definition types.h:205
QPALMInfo * info
solver information
Definition types.h:308