fatrop_conic_runtime.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 // C-REPLACE "casadi_qp_prob<T1>" "struct casadi_qp_prob"
21 // C-REPLACE "casadi_qp_data<T1>" "struct casadi_qp_data"
22 
23 // C-REPLACE "reinterpret_cast<int**>" "(int**) "
24 // C-REPLACE "reinterpret_cast<int*>" "(int*) "
25 // C-REPLACE "const_cast<int*>" "(int*) "
26 
27 
28 // SYMBOL "fatrop_mproject"
29 template<typename T1>
30 void casadi_fatrop_conic_mproject(T1 factor, const T1* x, const casadi_int* sp_x,
31  T1* y, const casadi_int* sp_y, T1* w) {
32  casadi_int ncol_y;
33  const casadi_int* colind_y;
34  ncol_y = sp_y[1];
35  colind_y = sp_y+2;
36  casadi_project(x, sp_x, y, sp_y, w);
37  casadi_scal(colind_y[ncol_y], factor, y);
38 }
39 
40 // SYMBOL "fatrop_dense_transfer"
41 template<typename T1>
42 void casadi_fatrop_conic_dense_transfer(double factor, const T1* x,
43  const casadi_int* sp_x, T1* y,
44  const casadi_int* sp_y, T1* w) {
45  casadi_sparsify(x, w, sp_x, 0);
46  casadi_int nrow_y = sp_y[0];
47  casadi_int ncol_y = sp_y[1];
48  const casadi_int *colind_y = sp_y+2, *row_y = sp_y + 2 + ncol_y+1;
49  /* Loop over columns of y */
50  casadi_int i, el;
51  for (i=0; i<ncol_y; ++i) {
52  for (el=colind_y[i]; el<colind_y[i+1]; ++el) y[nrow_y*i + row_y[el]] += factor*(*w++);
53  }
54 }
55 
56 template<typename T1>
59  const int *nx, *nu, *ng;
60 
61  // Sparsities
62  const casadi_int *ABsp;
63  const casadi_int *AB_offsets;
64  const casadi_int *CDsp;
65  const casadi_int *CD_offsets;
66  const casadi_int *RSQsp;
67  const casadi_int *RSQ_offsets;
68 
69  casadi_int N;
71 
73  T1 inf;
74 
75 };
76 // C-REPLACE "casadi_fatrop_conic_prob<T1>" "struct casadi_fatrop_conic_prob"
77 
78 // SYMBOL "fatrop_data"
79 template<typename T1>
81  // Problem structure
83  // Problem structure
85 
86  T1 *AB, *CD, *RSQ;
87 
88  casadi_int *a_eq, *a_ineq, *a_eq_idx, *a_ineq_idx;
89  casadi_int *x_eq, *x_ineq, *x_eq_idx, *x_ineq_idx;
90 
92  const char* return_status;
94  T1 res_eq;
97 
98  T1 *pv;
99 };
100 // C-REPLACE "casadi_fatrop_conic_data<T1>" "struct casadi_fatrop_conic_data"
101 
102 
103 // SYMBOL "fatrop_setup"
104 template<typename T1>
105 void casadi_fatrop_conic_setup(casadi_fatrop_conic_prob<T1>* p) {
106 
107 
108 }
109 
110 // SYMBOL "fatrop_ocp_c_solve"
111 template<typename T1>
112 int casadi_fatrop_conic_solve(casadi_fatrop_conic_data<T1>* d, const double** arg, double** res, casadi_int* iw, double* w) {
113  casadi_int k, i, start, stop;
114  const casadi_fatrop_conic_prob<T1>* p = d->prob;
115  const casadi_qp_prob<T1>* p_qp = p->qp;
116  casadi_qp_data<T1>* d_qp = d->qp;
117 
118  for (int i=0;i<casadi_sp_nnz(p_qp->sp_a);++i) {
119  printf("a %d: %e\n", i, d_qp->a[i]);
120  }
121 
122  casadi_fatrop_conic_mproject(-1.0, d_qp->a, p_qp->sp_a, d->AB, p->ABsp, d->pv);
123  casadi_project(d_qp->a, p_qp->sp_a, d->CD, p->CDsp, d->pv);
124 
125  casadi_project(d_qp->h, p_qp->sp_h, d->RSQ, p->RSQsp, d->pv);
126 
127  d->a_eq_idx[0] = 0;
128  d->a_ineq_idx[0] = 0;
129  d->x_eq_idx[0] = 0;
130  d->x_ineq_idx[0] = 0;
131 
132  // Loop over CD blocks
133  for (k=0;k<p->N+1;++k) {
134  d->a_eq_idx[k+1] = d->a_eq_idx[k];
135  d->a_ineq_idx[k+1] = d->a_ineq_idx[k];
136  start = p->CD[k].offset_r;
137  stop = p->CD[k].offset_r+p->CD[k].rows;
138  for (i=start;i<stop;++i) {
139  if (d_qp->lba[i]==d_qp->uba[i]) {
140  d->a_eq[d->a_eq_idx[k+1]++] = i;
141  } else {
142  if (d_qp->lba[i]==-inf && d_qp->uba[i]==inf) continue;
143  d->a_ineq[d->a_ineq_idx[k+1]++] = i;
144  }
145  }
146  d->x_eq_idx[k+1] = d->x_eq_idx[k];
147  d->x_ineq_idx[k+1] = d->x_ineq_idx[k];
148  start = p->CD[k].offset_c;
149  stop = p->CD[k].offset_c+p->CD[k].cols;
150  //uout() << "start" << start << "->" << stop << std::endl;
151  for (i=start;i<stop;++i) {
152  if (d_qp->lbx[i]==d_qp->ubx[i]) {
153  d->x_eq[d->x_eq_idx[k+1]++] = i;
154  } else {
155  if (d_qp->lbx[i]==-inf && d_qp->ubx[i]==inf) continue;
156  d->x_ineq[d->x_ineq_idx[k+1]++] = i;
157  }
158  }
159  //uout() << "k=" << k << std::endl;
160  //uout() << "a_eq" << std::vector<double>(d->a_eq+d->a_eq_idx[k], d->a_eq+d->a_eq_idx[k+1]) << std::endl;
161  //uout() << "a_ineq" << std::vector<double>(d->a_ineq+d->a_ineq_idx[k], d->a_ineq+d->a_ineq_idx[k+1]) << std::endl;
162  //uout() << "x_eq" << std::vector<double>(d->x_eq+d->x_eq_idx[k], d->x_eq+d->x_eq_idx[k+1]) << std::endl;
163  //uout() << "x_ineq" << std::vector<double>(d->x_ineq+d->x_ineq_idx[k], d->x_ineq+d->x_ineq_idx[k+1]) << std::endl;
164 
165  //uout() << "AB=" << std::vector<double>(d->AB,d->AB+100) << std::endl;
166 
167  //uout() << "CD=" << std::vector<double>(d->CD,d->CD+100) << std::endl;
168 
169  //uout() << "RSQ=" << std::vector<double>(d->RSQ,d->RSQ+100) << std::endl;
170  }
171 
172 
173  return 0;
174 }
175 
176 // SYMBOL "fatrop_work"
177 template<typename T1>
178 void casadi_fatrop_conic_work(const casadi_fatrop_conic_prob<T1>* p, casadi_int* sz_arg, casadi_int* sz_res, casadi_int* sz_iw, casadi_int* sz_w) {
179  casadi_qp_work(p->qp, sz_arg, sz_res, sz_iw, sz_w);
180 
181  // Temporary work vectors
182  *sz_w = casadi_max(*sz_w, 2*(p->qp->nx+p->qp->na)); // pv
183  // Persistent work vectors
184  *sz_w += casadi_sp_nnz(p->ABsp); // AB
185  *sz_w += casadi_sp_nnz(p->CDsp); // CD
186  *sz_w += casadi_sp_nnz(p->RSQsp); // RSQ
187 
188  *sz_iw += p->N+2; // a_eq_idx
189  *sz_iw += p->N+2; // a_ineq_idx
190  *sz_iw += p->N+2; // x_eq_idx
191  *sz_iw += p->N+2; // x_ineq_idx
192  *sz_iw += p->qp->na; // a_eq
193  *sz_iw += p->qp->na; // a_ineq
194  *sz_iw += p->qp->nx; // x_eq
195  *sz_iw += p->qp->nx; // x_ineq
196 
197 }
198 
199 
200 // SYMBOL "fatrop_set_work"
201 template<typename T1>
202 void casadi_fatrop_conic_set_work(casadi_fatrop_conic_data<T1>* d, const T1*** arg, T1*** res, casadi_int** iw, T1** w) {
203  // Local variables
204 
205  const casadi_fatrop_conic_prob<T1>* p = d->prob;
206 
207  d->AB = *w; *w += casadi_sp_nnz(p->ABsp);
208  d->CD = *w; *w += casadi_sp_nnz(p->CDsp);
209  d->RSQ = *w; *w += casadi_sp_nnz(p->RSQsp);
210 
211  d->a_eq_idx = *iw; *iw += p->N+2;
212  d->a_ineq_idx = *iw; *iw += p->N+2;
213  d->x_eq_idx = *iw; *iw += p->N+2;
214  d->x_ineq_idx = *iw; *iw += p->N+2;
215 
216  d->a_eq = *iw; *iw += p->qp->na;
217  d->a_ineq = *iw; *iw += p->qp->na;
218  d->x_eq = *iw; *iw += p->qp->nx;
219  d->x_ineq = *iw; *iw += p->qp->nx;
220 
221 
222  d->pv = *w;
223 }
casadi_qp_data< T1 > * qp
const casadi_fatrop_conic_prob< T1 > * prob
const casadi_qp_prob< T1 > * qp
const T1 * h
Definition: casadi_qp.hpp:64
const T1 * lba
Definition: casadi_qp.hpp:64
const T1 * lbx
Definition: casadi_qp.hpp:64
const T1 * uba
Definition: casadi_qp.hpp:64
const T1 * ubx
Definition: casadi_qp.hpp:64
const T1 * a
Definition: casadi_qp.hpp:64
const casadi_int * sp_h
Definition: casadi_qp.hpp:31
const casadi_int * sp_a
Definition: casadi_qp.hpp:31