fatrop_test.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include <ocp/OCPAbstract.hpp>
27 #include <ocp/StageOCPApplication.hpp>
28 #include <casadi/core/casadi_misc.hpp>
29 
140 namespace casadi {
141 
142 class CasadiStructuredQP : public fatrop::OCPAbstract {
145  fatrop_int get_nxk(const fatrop_int k) const override {
146  std::vector<fatrop_int> res = {2,3,2,1};
147  return res[k];
148  }
151  fatrop_int get_nuk(const fatrop_int k) const override {
152  std::vector<fatrop_int> res = {1, 2,1};
153  return res[k];
154  };
157  fatrop_int get_ngk(const fatrop_int k) const override {
158  std::vector<fatrop_int> res = {2, 0,0,0};
159  return res[k]; // 2 from lbx
160 
161  };
164  fatrop_int get_n_stage_params_k(const fatrop_int k) const override { return 0;}
166  fatrop_int get_n_global_params() const override { return 0;}
170  fatrop_int get_default_stage_paramsk(double *stage_params, const fatrop_int k) const override { return 0;}
173  fatrop_int get_default_global_params(double *global_params) const override{ return 0; }
176  virtual fatrop_int get_ng_ineq_k(const fatrop_int k) const {
177  std::vector<fatrop_int> res_lbg = {2, 1, 0, 0};
178  std::vector<fatrop_int> res_lbx = {1, 5, 3, 1};
179  return res_lbg[k]+res_lbx[k];
180  }
182  fatrop_int get_horizon_length() const override { return 4; }
193  fatrop_int eval_BAbtk(
194  const double *states_kp1,
195  const double *inputs_k,
196  const double *states_k,
197  const double *stage_params_k,
198  const double *global_params,
199  MAT *res,
200  const fatrop_int k) override {
201  printf("eval_BAbtk k=%d\n", k);
202  if (k==0) {
203  std::vector<double> r = {1, 0.2, 1, 0,
204  -0.1, 0.4,0 , 0,
205  0.3, 0.2, 0, 0};
206  int out_m = 4; // rows
207  int out_n = 3; // cols
208  PACKMAT(out_m, out_n, get_ptr(r), out_m, res, 0, 0);
209  blasfeo_print_dmat(out_m, out_n, res, 0, 0);
210 
211  std::vector<double> r2 = {1, -0.1, 0.3, 0.2, 0.4, 0.2, 1, 0, 0, 0, 0, 0};
212 
213  blasfeo_pack_tran_dmat(out_n, out_m, get_ptr(r2), out_n, res, 0, 0);
214 
215  blasfeo_print_dmat(out_m, out_n, res, 0, 0);
216 
217  //casadi_error("foo");
218 
219  } else if (k==1) {
220  std::vector<double> r = {1, 4, 2, 1, 0.3, 0,
221  3, 1, 00, 1, 0.2, 0};
222  int out_m = 6; // rows
223  int out_n = 2; // cols
224  PACKMAT(out_m, out_n, get_ptr(r), out_m, res, 0, 0);
225  blasfeo_print_dmat(out_m, out_n, res, 0, 0);
226 
227 
228  std::vector<double> r2 = {1,3,4,1,2,0,1,1,0.3,0.2,0,0,0,0,0};
229 
230  //blasfeo_pack_tran_dmat(out_n, out_m, get_ptr(r2), out_n, res, 0, 0);
231 
232  //blasfeo_print_dmat(out_m, out_n, res, 0, 0);
233 
234  } else if (k==2) {
235  std::vector<double> r = {2, 4, 0, 0};
236  int out_m = 4; // rows
237  int out_n = 1; // cols
238  PACKMAT(out_m, out_n, get_ptr(r), out_m, res, 0, 0);
239  blasfeo_print_dmat(out_m, out_n, res, 0, 0);
240  }
241 
242 
243  };
258  fatrop_int eval_RSQrqtk(
259  const double *objective_scale,
260  const double *inputs_k,
261  const double *states_k,
262  const double *lam_dyn_k,
263  const double *lam_eq_k,
264  const double *lam_eq_ineq_k,
265  const double *stage_params_k,
266  const double *global_params,
267  MAT *res,
268  const fatrop_int k) override {
269 
270  }
281  fatrop_int eval_Ggtk(
282  const double *inputs_k,
283  const double *states_k,
284  const double *stage_params_k,
285  const double *global_params,
286  MAT *res,
287  const fatrop_int k) override {
288  printf("eval_Ggtk k=%d\n", k);
289  if (k==0) {
290  std::vector<double> r = {2, 0, 0.3, 0,
291  1, 1, 0.4, 0};
292  int out_m = 4; // rows
293  int out_n = 2; // cols
294  PACKMAT(out_m, out_n, get_ptr(r), out_m, res, 0, 0);
295  blasfeo_print_dmat(out_m, out_n, res, 0, 0);
296  }
297  }
308  fatrop_int eval_Ggt_ineqk(
309  const double *inputs_k,
310  const double *states_k,
311  const double *stage_params_k,
312  const double *global_params,
313  MAT *res,
314  const fatrop_int k) {
315  printf("eval_Ggt_ineqk k=%d\n", k);
316  }
326  fatrop_int eval_bk(
327  const double *states_kp1,
328  const double *inputs_k,
329  const double *states_k,
330  const double *stage_params_k,
331  const double *global_params,
332  double *res,
333  const fatrop_int k) override {
334  printf("eval_bk k=%d\n", k);
335  }
343  fatrop_int eval_gk(
344  const double *states_k,
345  const double *inputs_k,
346  const double *stage_params_k,
347  const double *global_params,
348  double *res,
349  const fatrop_int k) override {
350  printf("eval_gk k=%d\n", k);
351  }
359  fatrop_int eval_gineqk(
360  const double *states_k,
361  const double *inputs_k,
362  const double *stage_params_k,
363  const double *global_params,
364  double *res,
365  const fatrop_int k) override {
366  printf("eval_gineqk k=%d\n", k);
367  }
376  fatrop_int eval_rqk(
377  const double *objective_scale,
378  const double *inputs_k,
379  const double *states_k,
380  const double *stage_params_k,
381  const double *global_params,
382  double *res,
383  const fatrop_int k) override {
384  printf("eval_rqk k=%d\n", k);
385  }
394  fatrop_int eval_Lk(
395  const double *objective_scale,
396  const double *inputs_k,
397  const double *states_k,
398  const double *stage_params_k,
399  const double *global_params,
400  double *res,
401  const fatrop_int k) override {
402  printf("eval_Lk k=%d\n", k);
403  }
408  fatrop_int get_boundsk(double *lower, double *upper, const fatrop_int k) const override {
409  printf("get_boundsk k=%d\n", k);
410  }
414  fatrop_int get_initial_xk(double *xk, const fatrop_int k) const override {
415  printf("get_initial_xk k=%d\n", k);
416  }
420  fatrop_int get_initial_uk(double *uk, const fatrop_int k) const override {
421  printf("get_initial_uk k=%d\n", k);
422  }
423 
424 };
425 
426 } // namespace casadi
427 
428 int main() {
429 
430  int m = 5;
431  int n = 7;
432 
433 
434  size_t s = blasfeo_memsize_dmat(m, n);
435  std::vector<double> mem(s, 0);
436 
437  blasfeo_dmat res;
438  blasfeo_create_dmat(m, n, &res, &mem.front());
439 
440  blasfeo_print_dmat(m, n, &res, 0, 0);
441 
442 
443  std::vector<double> a(100);
444  for (int i=0;i<100;++i) a[i] = i+1;
445 
446 
447  //blasfeo_pack_dmat(2, 3, &a.front(), 10, &res, 1, 3);
448 
449  //blasfeo_pack_tran_dmat(2, 3, &a.front(), 10, &res, 1, 3);
450  blasfeo_pack_tran_dmat(1, 3, &a.front(), 2, &res, 1, 3);
451 
452  blasfeo_print_dmat(m, n, &res, 0, 0);
453  return 0;
454 
455  /*blasfeo_pack_dmat(int m, int n, double *A, int lda, struct blasfeo_dmat *sB, int bi, int bj);
456 
457  blasfeo_pack_tran_dmat(1, n, d->CD+p->CD_offsets[k]+(i-start), ng_ineq, res, 0, column++);
458 
459  blasfeo_print_dmat(m, n, res, 0, 0);*/
460 
461 
462 
463  casadi::CasadiStructuredQP qp;
464 
465  fatrop::OCPApplication app(std::make_shared<casadi::CasadiStructuredQP>(qp));
466  app.build();
467 
468  app.optimize();
469 
470 }
The casadi namespace.
Definition: archiver.cpp:28
fatrop_int get_initial_uk(double *uk, const fatrop_int k, void *user_data)
fatrop_int get_horizon_length(void *user_data)
fatrop_int get_n_global_params(void *user_data)
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
fatrop_int get_default_global_params(double *global_params, void *user_data)
fatrop_int get_initial_xk(double *xk, const fatrop_int k, void *user_data)