casadi_sqpmethod.hpp
1 //
2 // MIT No Attribution
3 //
4 // Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
7 // software and associated documentation files (the "Software"), to deal in the Software
8 // without restriction, including without limitation the rights to use, copy, modify,
9 // merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 //
19 
20 
21 // C-REPLACE "casadi_nlpsol_prob<T1>" "struct casadi_nlpsol_prob"
22 
23 // SYMBOL "sqpmethod_prob"
24 template<typename T1>
27  // Sparsity patterns
28  const casadi_int *sp_h, *sp_a, *sp_hr;
29  casadi_int merit_memsize;
30  casadi_int max_iter_ls;
31  // Solver configuration (immutable after class init)
33  int so_corr;
34 };
35 // C-REPLACE "casadi_sqpmethod_prob<T1>" "struct casadi_sqpmethod_prob"
36 
37 
38 // SYMBOL "sqpmethod_data"
39 template<typename T1>
41  // Problem structure
43 
44  T1* z_cand;
45  // Lagrange gradient in the next iterate
46  T1 *gLag, *gLag_old;
47  // Gradient of the objective
48  T1 *gf;
49  // Bounds of the QP
50  T1 *lbdz, *ubdz;
51  // QP solution
52  T1 *dx, *dlam;
53  // Hessian approximation
54  T1 *Bk;
55  // Jacobian
56  T1* Jk;
57  // merit_mem
58  T1* merit_mem;
59  // temp_mem
60  T1* temp_mem;
61  // temp_sol
62  T1* temp_sol;
63 
64  const T1** arg;
65  T1** res;
66  casadi_int* iw;
67  T1* w;
68 };
69 // C-REPLACE "casadi_sqpmethod_data<T1>" "struct casadi_sqpmethod_data"
70 
71 
72 // SYMBOL "sqpmethod_work"
73 template<typename T1>
74 void casadi_sqpmethod_work(const casadi_sqpmethod_prob<T1>* p,
75  casadi_int* sz_iw, casadi_int* sz_w) {
76  // Local variables
77  casadi_int nnz_h, nnz_a, nx, ng;
78  int elastic_mode = p->elastic_mode;
79  int so_corr = p->so_corr;
80  nnz_h = p->sp_h[2+p->sp_h[1]];
81  nnz_a = p->sp_a[2+p->sp_a[1]];
82  nx = p->nlp->nx;
83  ng = p->nlp->ng;
84 
85  // Reset sz_w, sz_iw
86  *sz_w = *sz_iw = 0;
87  if (p->max_iter_ls>0 || so_corr) *sz_w += nx + ng; // z_cand
88  // Lagrange gradient in the next iterate
89  *sz_w += nx; // gLag
90  *sz_w += nx; // gLag_old
91  // Gradient of the objective
92  *sz_w += nx; // gf
93  // Bounds of the QP
94  *sz_w += nx + ng; // lbdz
95  *sz_w += nx + ng; // ubdz
96  // QP solution
97  *sz_w += nx; // dx
98  *sz_w += nx + ng; // dlam
99  // Hessian approximation
100  *sz_w += nnz_h; // Bk
101  // Jacobian
102  *sz_w += nnz_a; // Jk
103  // merit_mem
104  if (p->max_iter_ls>0 || so_corr) *sz_w += p->merit_memsize;
105 
106  if (elastic_mode) {
107  // Additional work for larger objective gradient
108  *sz_w += 2*ng; // gf
109  // Additional work for the larger bounds
110  *sz_w += 2*ng; // lbdz
111  *sz_w += 2*ng; // ubdz
112  // Additional work for larger solution
113  *sz_w += 2*ng; // dx
114  *sz_w += 2*ng; // dlam
115  // Additional work for larger jacobian
116  *sz_w += 2*ng; // Jk
117  // Additional work for temp memory
118  *sz_w += ng;
119  }
120 
121  if (so_corr) *sz_w += nx+nx+ng; // Temp memory for failing soc
122 }
123 
124 // SYMBOL "sqpmethod_set_work"
125 template<typename T1>
126 void casadi_sqpmethod_set_work(casadi_sqpmethod_data<T1>* d,
127  const T1*** arg, T1*** res, casadi_int** iw, T1** w) {
128  // Local variables
129  casadi_int nnz_h, nnz_a, nx, ng;
130  const casadi_sqpmethod_prob<T1>* p = d->prob;
131  int elastic_mode = p->elastic_mode;
132  int so_corr = p->so_corr;
133  // Get matrix number of nonzeros
134  nnz_h = p->sp_h[2+p->sp_h[1]];
135  nnz_a = p->sp_a[2+p->sp_a[1]];
136  nx = p->nlp->nx;
137  ng = p->nlp->ng;
138  if (p->max_iter_ls>0 || so_corr) {
139  d->z_cand = *w; *w += nx + ng;
140  }
141  // Lagrange gradient in the next iterate
142  d->gLag = *w; *w += nx;
143  d->gLag_old = *w; *w += nx;
144  // Hessian approximation
145  d->Bk = *w; *w += nnz_h;
146  // merit_mem
147  if (p->max_iter_ls>0 || so_corr) {
148  d->merit_mem = *w; *w += p->merit_memsize;
149  }
150 
151  if (so_corr) {
152  d->temp_sol = *w; *w += nx+nx+ng;
153  }
154 
155  if (elastic_mode) {
156  // Gradient of the objective
157  d->gf = *w; *w += nx + 2*ng;
158  // Bounds of the QP
159  d->lbdz = *w; *w += nx + 3*ng;
160  d->ubdz = *w; *w += nx + 3*ng;
161  // QP solution
162  d->dx = *w; *w += nx + 2*ng;
163  d->dlam = *w; *w += nx + 3*ng;
164  // Jacobian
165  d->Jk = *w; *w += nnz_a + 2*ng;
166  // temp mem
167  d->temp_mem = *w; *w += ng;
168  } else {
169  // Gradient of the objective
170  d->gf = *w; *w += nx;
171  // Bounds of the QP
172  d->lbdz = *w; *w += nx + ng;
173  d->ubdz = *w; *w += nx + ng;
174  // QP solution
175  d->dx = *w; *w += nx;
176  d->dlam = *w; *w += nx + ng;
177  // Jacobian
178  d->Jk = *w; *w += nnz_a;
179  }
180  d->arg = *arg;
181  d->res = *res;
182  d->iw = *iw;
183  d->w = *w;
184 }
const casadi_sqpmethod_prob< T1 > * prob
const casadi_nlpsol_prob< T1 > * nlp
const casadi_int * sp_hr
const casadi_int * sp_h
const casadi_int * sp_a