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 };
32 // C-REPLACE "casadi_sqpmethod_prob<T1>" "struct casadi_sqpmethod_prob"
33 
34 
35 // SYMBOL "sqpmethod_data"
36 template<typename T1>
38  // Problem structure
40 
41  T1* z_cand;
42  // Lagrange gradient in the next iterate
43  T1 *gLag, *gLag_old;
44  // Gradient of the objective
45  T1 *gf;
46  // Bounds of the QP
47  T1 *lbdz, *ubdz;
48  // QP solution
49  T1 *dx, *dlam;
50  // Hessian approximation
51  T1 *Bk;
52  // Jacobian
53  T1* Jk;
54  // merit_mem
55  T1* merit_mem;
56  // temp_mem
57  T1* temp_mem;
58  // temp_sol
59  T1* temp_sol;
60 
61  const T1** arg;
62  T1** res;
63  casadi_int* iw;
64  T1* w;
65 };
66 // C-REPLACE "casadi_sqpmethod_data<T1>" "struct casadi_sqpmethod_data"
67 
68 
69 // SYMBOL "sqpmethod_work"
70 template<typename T1>
71 void casadi_sqpmethod_work(const casadi_sqpmethod_prob<T1>* p,
72  casadi_int* sz_iw, casadi_int* sz_w, int elastic_mode, int so_corr) {
73  // Local variables
74  casadi_int nnz_h, nnz_a, nx, ng;
75  nnz_h = p->sp_h[2+p->sp_h[1]];
76  nnz_a = p->sp_a[2+p->sp_a[1]];
77  nx = p->nlp->nx;
78  ng = p->nlp->ng;
79 
80  // Reset sz_w, sz_iw
81  *sz_w = *sz_iw = 0;
82  if (p->max_iter_ls>0 || so_corr) *sz_w += nx + ng; // z_cand
83  // Lagrange gradient in the next iterate
84  *sz_w += nx; // gLag
85  *sz_w += nx; // gLag_old
86  // Gradient of the objective
87  *sz_w += nx; // gf
88  // Bounds of the QP
89  *sz_w += nx + ng; // lbdz
90  *sz_w += nx + ng; // ubdz
91  // QP solution
92  *sz_w += nx; // dx
93  *sz_w += nx + ng; // dlam
94  // Hessian approximation
95  *sz_w += nnz_h; // Bk
96  // Jacobian
97  *sz_w += nnz_a; // Jk
98  // merit_mem
99  if (p->max_iter_ls>0 || so_corr) *sz_w += p->merit_memsize;
100 
101  if (elastic_mode) {
102  // Additional work for larger objective gradient
103  *sz_w += 2*ng; // gf
104  // Additional work for the larger bounds
105  *sz_w += 2*ng; // lbdz
106  *sz_w += 2*ng; // ubdz
107  // Additional work for larger solution
108  *sz_w += 2*ng; // dx
109  *sz_w += 2*ng; // dlam
110  // Additional work for larger jacobian
111  *sz_w += 2*ng; // Jk
112  // Additional work for temp memory
113  *sz_w += ng;
114  }
115 
116  if (so_corr) *sz_w += nx+nx+ng; // Temp memory for failing soc
117 }
118 
119 // SYMBOL "sqpmethod_init"
120 template<typename T1>
121 void casadi_sqpmethod_init(casadi_sqpmethod_data<T1>* d,
122  const T1*** arg, T1*** res, casadi_int** iw, T1** w,
123  int elastic_mode, int so_corr) {
124  // Local variables
125  casadi_int nnz_h, nnz_a, nx, ng;
126  const casadi_sqpmethod_prob<T1>* p = d->prob;
127  // Get matrix number of nonzeros
128  nnz_h = p->sp_h[2+p->sp_h[1]];
129  nnz_a = p->sp_a[2+p->sp_a[1]];
130  nx = p->nlp->nx;
131  ng = p->nlp->ng;
132  if (p->max_iter_ls>0 || so_corr) {
133  d->z_cand = *w; *w += nx + ng;
134  }
135  // Lagrange gradient in the next iterate
136  d->gLag = *w; *w += nx;
137  d->gLag_old = *w; *w += nx;
138  // Hessian approximation
139  d->Bk = *w; *w += nnz_h;
140  // merit_mem
141  if (p->max_iter_ls>0 || so_corr) {
142  d->merit_mem = *w; *w += p->merit_memsize;
143  }
144 
145  if (so_corr) {
146  d->temp_sol = *w; *w += nx+nx+ng;
147  }
148 
149  if (elastic_mode) {
150  // Gradient of the objective
151  d->gf = *w; *w += nx + 2*ng;
152  // Bounds of the QP
153  d->lbdz = *w; *w += nx + 3*ng;
154  d->ubdz = *w; *w += nx + 3*ng;
155  // QP solution
156  d->dx = *w; *w += nx + 2*ng;
157  d->dlam = *w; *w += nx + 3*ng;
158  // Jacobian
159  d->Jk = *w; *w += nnz_a + 2*ng;
160  // temp mem
161  d->temp_mem = *w; *w += ng;
162  } else {
163  // Gradient of the objective
164  d->gf = *w; *w += nx;
165  // Bounds of the QP
166  d->lbdz = *w; *w += nx + ng;
167  d->ubdz = *w; *w += nx + ng;
168  // QP solution
169  d->dx = *w; *w += nx;
170  d->dlam = *w; *w += nx + ng;
171  // Jacobian
172  d->Jk = *w; *w += nnz_a;
173  }
174  d->arg = *arg;
175  d->res = *res;
176  d->iw = *iw;
177  d->w = *w;
178 }
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