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