casadi_feasiblesqpmethod.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 "feasiblesqpmethod_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 };
34 // C-REPLACE "casadi_feasiblesqpmethod_prob<T1>" "struct casadi_feasiblesqpmethod_prob"
35 
36 
37 // SYMBOL "feasiblesqpmethod_data"
38 template<typename T1>
40  // Problem structure
42 
43  // Lagrange gradient in the next iterate
44  T1 *gLag, *gLag_old;
45  // Gradient of the objective
46  T1 *gf;
47  // Bounds of the QP
48  T1 *lbdz, *ubdz;
49  // QP solution
50  T1 *dx, *dlam;
51  // Feasibility QP solution
52  T1 *dx_feas;
53  T1 *dlam_feas;
54  // Feasibility iterate
55  T1 *z_feas;
56  T1 *gf_feas;
58  T1* z_tmp;
60  casadi_int *tr_mask;
61  // Hessian approximation
62  T1 *Bk;
63  // Jacobian
64  T1* Jk;
65  // Anderson vectors
68  T1* gamma;
69 
70  // Function value of feasibility iterate
71  T1 f_feas;
72  // merit_mem
73  // T1* merit_mem;
74  // temp_mem
75  // T1* temp_mem;
76  // temp_sol
77  // T1* temp_sol;
78 };
79 // C-REPLACE "casadi_feasiblesqpmethod_data<T1>" "struct casadi_feasiblesqpmethod_data"
80 
81 
82 // SYMBOL "feasiblesqpmethod_work"
83 template<typename T1>
84 void casadi_feasiblesqpmethod_work(const casadi_feasiblesqpmethod_prob<T1>* p,
85  casadi_int* sz_iw, casadi_int* sz_w) {
86  // Local variables
87  casadi_int nnz_h, nnz_a, nx, ng;
88  int sz_anderson_memory = p->sz_anderson_memory;
89  nnz_h = p->sp_h[2+p->sp_h[1]];
90  nnz_a = p->sp_a[2+p->sp_a[1]];
91  nx = p->nlp->nx;
92  ng = p->nlp->ng;
93 
94  // Reset sz_w, sz_iw
95  *sz_w = *sz_iw = 0;
96  // if (p->max_iter_ls>0) *sz_w += nx + ng; // z_cand
97  // Lagrange gradient in the next iterate
98  *sz_w += nx; // gLag
99  *sz_w += nx; // gLag_old
100  // Gradient of the objective
101  *sz_w += nx; // gf
102  // Bounds of the QP
103  *sz_w += nx + ng; // lbdz
104  *sz_w += nx + ng; // ubdz
105  // QP solution
106  *sz_w += nx; // dx
107  *sz_w += nx + ng; // dlam
108  // Feasibility QP solution
109  *sz_w += nx; // dx_feas
110  *sz_w += nx + ng; // dlam_feas
111  // Feasibility iterate
112  *sz_w += nx + ng; // x_feas + g_feas
113  *sz_w += nx; // gf_feas
114  *sz_w += nx + ng; // lower bounds feasibile QP
115  *sz_w += nx + ng; // upper bounds feasible QP
116  *sz_w += nx+ng; // x tmp feasible QP
117  *sz_w += nx; // tr_scale_vector
118  *sz_iw += nx; // tr_mask
119  // Hessian approximation
120  *sz_w += nnz_h; // Bk
121  // Jacobian
122  *sz_w += nnz_a; // Jk
123  // merit_mem
124  // if (p->max_iter_ls>0) *sz_w += p->merit_memsize;
125 
126  if (sz_anderson_memory > 0) {
127  // for step (dx)
128  *sz_w += sz_anderson_memory*nx;
129  // for x
130  *sz_w += sz_anderson_memory*nx;
131  // for gamma
132  *sz_w += sz_anderson_memory;
133  }
134 
135  // if (elastic_mode) {
136  // // Additional work for larger objective gradient
137  // *sz_w += 2*ng; // gf
138  // // Additional work for the larger bounds
139  // *sz_w += 2*ng; // lbdz
140  // *sz_w += 2*ng; // ubdz
141  // // Additional work for larger solution
142  // *sz_w += 2*ng; // dx
143  // *sz_w += 2*ng; // dlam
144  // // Additional work for larger jacobian
145  // *sz_w += 2*ng; // Jk
146  // // Additional work for temp memory
147  // *sz_w += ng;
148  // }
149 
150  // if (so_corr) *sz_w += nx+nx+ng; // Temp memory for failing soc
151 }
152 
153 // SYMBOL "feasiblesqpmethod_set_work"
154 template<typename T1>
155 void casadi_feasiblesqpmethod_set_work(casadi_feasiblesqpmethod_data<T1>* d,
156  const T1*** arg, T1*** res, casadi_int** iw, T1** w) {
157  (void)arg; (void)res;
158  // Local variables
159  casadi_int nnz_h, nnz_a, nx, ng;
161  int sz_anderson_memory = p->sz_anderson_memory;
162  // Get matrix number of nonzeros
163  nnz_h = p->sp_h[2+p->sp_h[1]];
164  nnz_a = p->sp_a[2+p->sp_a[1]];
165  nx = p->nlp->nx;
166  ng = p->nlp->ng;
167  // if (p->max_iter_ls>0) {
168  // d->z_cand = *w; *w += nx + ng;
169  // }
170  // Lagrange gradient in the next iterate
171  d->gLag = *w; *w += nx;
172  d->gLag_old = *w; *w += nx;
173  // Hessian approximation
174  d->Bk = *w; *w += nnz_h;
175  // merit_mem
176  // if (p->max_iter_ls>0) {
177  // d->merit_mem = *w; *w += p->merit_memsize;
178  // }
179 
180  // if (so_corr) {
181  // d->temp_sol = *w; *w += nx+nx+ng;
182  // }
183 
184  // Gradient of the objective
185  d->gf = *w; *w += nx;
186  // Bounds of the QP
187  d->lbdz = *w; *w += nx + ng;
188  d->ubdz = *w; *w += nx + ng;
189  // QP solution
190  d->dx = *w; *w += nx;
191  d->dlam = *w; *w += nx + ng;
192  // Feasible QP solution
193  d->dx_feas = *w; *w += nx;
194  d->dlam_feas = *w; *w += nx + ng;
195  // feasibility iterate
196  d->z_feas = *w; *w += nx + ng;
197  d->gf_feas = *w; *w += nx;
198  // Bounds of the feasibility QPs
199  d->lbdz_feas = *w; *w += nx + ng;
200  d->ubdz_feas = *w; *w += nx + ng;
201  // x tmp for QPs
202  d->z_tmp = *w; *w += sz_anderson_memory*nx+ng;
203  // trust-region scale vector
204  d->tr_scale_vector = *w; *w += nx;
205  d->tr_mask = *iw; *iw += nx;
206  // Jacobian
207  d->Jk = *w; *w += nnz_a;
208  // Anderson vector
209  d->anderson_memory_step = *w; *w += sz_anderson_memory*nx;
210  d->anderson_memory_iterate = *w; *w += sz_anderson_memory*nx;
211  d->gamma = *w; *w += sz_anderson_memory;
212 
213 }
const casadi_feasiblesqpmethod_prob< T1 > * prob
const casadi_nlpsol_prob< T1 > * nlp