qpSWIFT
A Sparse Quadratic Programming Solver
Auxilary.h
Go to the documentation of this file.
1 #ifndef __QP_AUXILARY_H__
2 #define __QP_AUXILARY_H__
3 
4 #ifdef __cplusplus
5 extern "C"
6 {
7 #endif
8 
9 #include "timer.h"
10 #include "amd.h"
11 #include "amd_internal.h"
12 #include "ldl.h"
18 typedef struct smat
19 {
20  qp_int *jc;
21  qp_int *ir;
22  qp_real *pr;
23  qp_int n;
24  qp_int m;
25  qp_int nnz;
26 } smat;
27 
31 typedef struct kkt
32 {
34  qp_real *b;
35  qp_int *Parent;
36  qp_int *Flag;
37  qp_int *Lnz;
38  qp_int *Li;
39  qp_int *Lp;
40  qp_int *Lti;
41  qp_int *Ltp;
42  qp_int *Pattern;
43  qp_int *UPattern;
44  qp_real *Y;
45  qp_real *Lx;
46  qp_real *D;
47  qp_int *P;
48  qp_int *Pinv;
50 } kkt;
51 
55 typedef struct stats
56 {
57 
59  qp_real tsetup;
60  qp_real tsolve;
61  qp_real kkt_time;
62  qp_real ldl_numeric;
66  qp_int IterationCount;
67  qp_real n_rx;
68  qp_real n_ry;
69  qp_real n_rz;
70  qp_real n_mu;
72  qp_real alpha_p;
73  qp_real alpha_d;
76  qp_real fval;
77  qp_int Flag;
78  qp_int AMD_RESULT;
82  qp_int resolve_kkt;
84 } stats;
85 
89 typedef struct settings
90 {
91 
92  qp_int maxit;
93  qp_real reltol;
94  qp_real abstol;
95  qp_real sigma;
96  qp_int verbose;
100 } settings;
101 
106  typedef struct QP
107  {
108 
109  qp_int n;
110  qp_int m;
111  qp_int p;
113  qp_real sigma_d;
114  qp_real mu;
115  qp_real rho;
117  qp_real *x;
118  qp_real *y;
119  qp_real *z;
120  qp_real *s;
122  qp_real *rx;
123  qp_real *ry;
124  qp_real *rz;
126  qp_real *delta;
127  qp_real *delta_x;
128  qp_real *delta_y;
129  qp_real *delta_z;
130  qp_real *delta_s;
132  qp_real *ds;
133  qp_real *lambda;
135  qp_real *temp;
137  smat *P;
138  qp_real *c;
139  smat *G;
140  qp_real *h;
141  smat *A;
142  qp_real *b;
144  smat *At;
145  smat *Gt;
147  kkt *kkt;
151 } QP;
152 
153 
154 qp_int kkt_initialize(QP *myQP);
155 
156 qp_int kktsolve_1(QP *myQP);
157 
158 void kktsolve_2(QP *myQP);
159 
160 void SparseMatrixSetup(qp_int m, qp_int n, qp_int nnz, qp_int *jc, qp_int *ir, qp_real *pr, smat *sparse);
161 
162 void Transpose_Row_Count(qp_int m, qp_int n, qp_int *Li, qp_int *Lp, qp_int *Lti, qp_int *Ltp);
163 
164 void computeresiduals(QP *myQP);
165 
166 void SparseMatrixMultiply(smat *A, qp_real *x, qp_real *y, qp_int start);
167 
168 void SparseMatrixTransMultiply(smat *A, qp_real *x, qp_real *y, qp_int start);
169 
170 void form_ds(qp_real *ds, qp_real *lambda, qp_real *delta_s, qp_real *delta_z, qp_real sigma, qp_real mu, qp_int m, qp_int selector);
171 
172 void formkktmatrix_U(smat *P, smat *G, smat *Gt, smat *kkt);
173 
174 void formkktmatrix_full(smat *P, smat *G, smat *A, smat *Gt, smat *At, smat *kktmatrix);
175 
176 void SparseMatrixTranspose(smat *A, smat *At);
177 
178 void updatekktmatrix(smat *kkt, qp_real *s, qp_real *z, qp_real *delta_s, qp_real *delta_z, qp_real alpha_p, qp_real alpha_d, qp_int m, qp_int n, qp_int p, qp_int indicator);
179 
180 void updatekktmatrix_b(qp_real *b, qp_real *rx, qp_real *ry, qp_real *rz, qp_real *ds, qp_real *z, qp_int n, qp_int m, qp_int p);
181 
182 qp_int checksign(qp_real *s, qp_real *delta_s, qp_real alpha, qp_int count);
183 
184 void updatevariables(qp_real *x, qp_real *delta_x, qp_real alpha, qp_int count);
185 
186 void formlambda(qp_real *lambda, qp_real *s, qp_real *z, qp_int n);
187 
188 qp_real formrho(qp_real *s, qp_real *delta_s, qp_real *z, qp_real *delta_z, qp_real alpha_p, qp_real alpha_d, qp_int n);
189 
190 qp_int ldlinitialsolve(kkt *mykkt, qp_real *delta);
191 
192 void test_reach(qp_int *Parent, qp_int *Pinv, qp_int *UPattern, qp_int n, qp_int m, qp_int p);
193 
194 void findsteplength(qp_real *s, qp_real *delta_s, qp_real *z, qp_real *delta_z, qp_int m, qp_real *alpha_p, qp_real *alpha_d);
195 
196 qp_real obj_value(smat *P, qp_real *c, qp_real *x, qp_real *temp);
197 
198 qp_real innerproduct(qp_real *x, qp_real *y, qp_int n);
199 
200 void densetosparse(qp_int m, qp_int n, qp_real *pr, smat *A);
201 
202 void densetosparse_ROWMAJOR(qp_int m, qp_int n, qp_real *pr, smat *A);
203 
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif
210 
smat::pr
qp_real * pr
Definition: Auxilary.h:22
QP::stats
stats * stats
Definition: Auxilary.h:149
QP::h
qp_real * h
Definition: Auxilary.h:140
SparseMatrixSetup
void SparseMatrixSetup(qp_int m, qp_int n, qp_int nnz, qp_int *jc, qp_int *ir, qp_real *pr, smat *sparse)
Sets up the Sparse Matrix in Column Compressed Storage Format based on inputs.
Definition: Auxilary.c:660
QP::temp
qp_real * temp
Definition: Auxilary.h:135
kkt::Pinv
qp_int * Pinv
Definition: Auxilary.h:48
formkktmatrix_U
void formkktmatrix_U(smat *P, smat *G, smat *Gt, smat *kkt)
Definition: Auxilary.c:14
kkt::UPattern
qp_int * UPattern
Definition: Auxilary.h:43
kkt::kktmatrix
smat * kktmatrix
Definition: Auxilary.h:33
kkt::P
qp_int * P
Definition: Auxilary.h:47
settings::sigma
qp_real sigma
Definition: Auxilary.h:95
settings
struct settings settings
stats::tsolve
qp_real tsolve
Definition: Auxilary.h:60
formkktmatrix_full
void formkktmatrix_full(smat *P, smat *G, smat *A, smat *Gt, smat *At, smat *kktmatrix)
Assembles the KKT matrix.
Definition: Auxilary.c:71
kkt
struct kkt kkt
stats::n_rx
qp_real n_rx
Definition: Auxilary.h:67
stats::Flag
qp_int Flag
Definition: Auxilary.h:77
smat
Definition: Auxilary.h:18
QP::delta
qp_real * delta
Definition: Auxilary.h:126
QP::delta_y
qp_real * delta_y
Definition: Auxilary.h:128
updatekktmatrix_b
void updatekktmatrix_b(qp_real *b, qp_real *rx, qp_real *ry, qp_real *rz, qp_real *ds, qp_real *z, qp_int n, qp_int m, qp_int p)
Updates the right hand side of the KKT linear system of equations.
Definition: Auxilary.c:274
kkt::Parent
qp_int * Parent
Definition: Auxilary.h:35
obj_value
qp_real obj_value(smat *P, qp_real *c, qp_real *x, qp_real *temp)
Computes the objective function value f = x'Px + c'x.
Definition: Auxilary.c:1133
QP::z
qp_real * z
Definition: Auxilary.h:119
QP::m
qp_int m
Definition: Auxilary.h:110
stats::n_rz
qp_real n_rz
Definition: Auxilary.h:69
stats::tsetup
qp_real tsetup
Definition: Auxilary.h:59
form_ds
void form_ds(qp_real *ds, qp_real *lambda, qp_real *delta_s, qp_real *delta_z, qp_real sigma, qp_real mu, qp_int m, qp_int selector)
Updates the ds vector based on the selector.
Definition: Auxilary.c:315
QP
struct QP QP
kkt::Pattern
qp_int * Pattern
Definition: Auxilary.h:42
stats::kkt_time
qp_real kkt_time
Definition: Auxilary.h:61
SparseMatrixTranspose
void SparseMatrixTranspose(smat *A, smat *At)
Computes the sparse matrix transpose.
Definition: Auxilary.c:901
SparseMatrixTransMultiply
void SparseMatrixTransMultiply(smat *A, qp_real *x, qp_real *y, qp_int start)
Performs Sparse Matrix Transpose Vector Multiplication as.
Definition: Auxilary.c:802
stats::fval
qp_real fval
Definition: Auxilary.h:76
QP::mu
qp_real mu
Definition: Auxilary.h:114
checksign
qp_int checksign(qp_real *s, qp_real *delta_s, qp_real alpha, qp_int count)
Checks if x + alpha*delta_x < 0.
Definition: Auxilary.c:404
Transpose_Row_Count
void Transpose_Row_Count(qp_int m, qp_int n, qp_int *Li, qp_int *Lp, qp_int *Lti, qp_int *Ltp)
Computes the ir and jc of transpose of a Matrix.
Definition: Auxilary.c:683
innerproduct
qp_real innerproduct(qp_real *x, qp_real *y, qp_int n)
Calculates the inner product of two vectors.
Definition: Auxilary.c:451
QP::b
qp_real * b
Definition: Auxilary.h:142
QP::At
smat * At
Definition: Auxilary.h:144
QP::s
qp_real * s
Definition: Auxilary.h:120
updatevariables
void updatevariables(qp_real *x, qp_real *delta_x, qp_real alpha, qp_int count)
Performs scalar vector addition.
Definition: Auxilary.c:246
stats::resolve_kkt
qp_int resolve_kkt
Definition: Auxilary.h:82
stats::ldl_numeric
qp_real ldl_numeric
Definition: Auxilary.h:62
QP::delta_x
qp_real * delta_x
Definition: Auxilary.h:127
smat::n
qp_int n
Definition: Auxilary.h:23
settings::maxit
qp_int maxit
Definition: Auxilary.h:92
smat::nnz
qp_int nnz
Definition: Auxilary.h:25
QP::G
smat * G
Definition: Auxilary.h:139
QP::x
qp_real * x
Definition: Auxilary.h:117
QP::ds
qp_real * ds
Definition: Auxilary.h:132
computeresiduals
void computeresiduals(QP *myQP)
Computes the residuals rx, ry and rz.
Definition: Auxilary.c:745
QP::rho
qp_real rho
Definition: Auxilary.h:115
QP::n
qp_int n
Definition: Auxilary.h:109
timer.h
test_reach
void test_reach(qp_int *Parent, qp_int *Pinv, qp_int *UPattern, qp_int n, qp_int m, qp_int p)
Computes the nodes to be updated at each iteration.
Definition: Auxilary.c:1104
stats::alpha_d
qp_real alpha_d
Definition: Auxilary.h:73
QP::sigma_d
qp_real sigma_d
Definition: Auxilary.h:113
smat::jc
qp_int * jc
Definition: Auxilary.h:20
kkt::D
qp_real * D
Definition: Auxilary.h:46
QP::delta_s
qp_real * delta_s
Definition: Auxilary.h:130
stats::IterationCount
qp_int IterationCount
Definition: Auxilary.h:66
densetosparse_ROWMAJOR
void densetosparse_ROWMAJOR(qp_int m, qp_int n, qp_real *pr, smat *A)
Converts dense matrix in row major format to CCS format.
Definition: Auxilary.c:1219
kkt::Y
qp_real * Y
Definition: Auxilary.h:44
QP::y
qp_real * y
Definition: Auxilary.h:118
formlambda
void formlambda(qp_real *lambda, qp_real *s, qp_real *z, qp_int n)
Computes the scaling point lambda.
Definition: Auxilary.c:638
stats::AMD_RESULT
qp_int AMD_RESULT
Definition: Auxilary.h:78
settings
Definition: Auxilary.h:89
QP::kkt
kkt * kkt
Definition: Auxilary.h:147
SparseMatrixMultiply
void SparseMatrixMultiply(smat *A, qp_real *x, qp_real *y, qp_int start)
Performs Sparse Matrix Vector Multiplication as.
Definition: Auxilary.c:839
stats::alpha_p
qp_real alpha_p
Definition: Auxilary.h:72
updatekktmatrix
void updatekktmatrix(smat *kkt, qp_real *s, qp_real *z, qp_real *delta_s, qp_real *delta_z, qp_real alpha_p, qp_real alpha_d, qp_int m, qp_int n, qp_int p, qp_int indicator)
Updates the lower diagonal part of the kkt Matrix,.
Definition: Auxilary.c:205
smat::m
qp_int m
Definition: Auxilary.h:24
stats
Definition: Auxilary.h:55
kkt::Lti
qp_int * Lti
Definition: Auxilary.h:40
QP::lambda
qp_real * lambda
Definition: Auxilary.h:133
kkt::Li
qp_int * Li
Definition: Auxilary.h:38
kktsolve_2
void kktsolve_2(QP *myQP)
Solves the kktlinear system from results of kktsolve_1 and updates delta_x, delta_y,...
Definition: Auxilary.c:524
smat::ir
qp_int * ir
Definition: Auxilary.h:21
QP::rz
qp_real * rz
Definition: Auxilary.h:124
ldlinitialsolve
qp_int ldlinitialsolve(kkt *mykkt, qp_real *delta)
Solves the kkt linear system to find initial conditions.
Definition: Auxilary.c:578
densetosparse
void densetosparse(qp_int m, qp_int n, qp_real *pr, smat *A)
Converts dense matrix in column major format to CCS format.
Definition: Auxilary.c:1154
QP
Definition: Auxilary.h:106
settings::reltol
qp_real reltol
Definition: Auxilary.h:93
kkt::Lp
qp_int * Lp
Definition: Auxilary.h:39
stats::n_ry
qp_real n_ry
Definition: Auxilary.h:68
QP::Gt
smat * Gt
Definition: Auxilary.h:145
findsteplength
void findsteplength(qp_real *s, qp_real *delta_s, qp_real *z, qp_real *delta_z, qp_int m, qp_real *alpha_p, qp_real *alpha_d)
Calculates the step length.
Definition: Auxilary.c:359
settings::verbose
qp_int verbose
Definition: Auxilary.h:96
kkt_initialize
qp_int kkt_initialize(QP *myQP)
Computes the initial condition for the QP problem.
Definition: Auxilary.c:992
QP::P
smat * P
Definition: Auxilary.h:137
QP::delta_z
qp_real * delta_z
Definition: Auxilary.h:129
QP::p
qp_int p
Definition: Auxilary.h:111
QP::rx
qp_real * rx
Definition: Auxilary.h:122
kkt::Lx
qp_real * Lx
Definition: Auxilary.h:45
kkt::Ltp
qp_int * Ltp
Definition: Auxilary.h:41
kktsolve_1
qp_int kktsolve_1(QP *myQP)
Solves the KKT linear systems and updates delta_z and delta_s.
Definition: Auxilary.c:471
QP::ry
qp_real * ry
Definition: Auxilary.h:123
kkt::Flag
qp_int * Flag
Definition: Auxilary.h:36
QP::options
settings * options
Definition: Auxilary.h:148
kkt
Definition: Auxilary.h:31
QP::c
qp_real * c
Definition: Auxilary.h:138
stats
struct stats stats
stats::n_mu
qp_real n_mu
Definition: Auxilary.h:70
kkt::b
qp_real * b
Definition: Auxilary.h:34
formrho
qp_real formrho(qp_real *s, qp_real *delta_s, qp_real *z, qp_real *delta_z, qp_real alpha_p, qp_real alpha_d, qp_int n)
Computes the scalar rho as.
Definition: Auxilary.c:879
kkt::Lnz
qp_int * Lnz
Definition: Auxilary.h:37
settings::abstol
qp_real abstol
Definition: Auxilary.h:94
QP::A
smat * A
Definition: Auxilary.h:141
smat
struct smat smat