alpaqa 1.0.0a10
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiFunctionWrapper.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <stdexcept>
6#include <string>
7#include <vector>
8
9#include <casadi/core/function.hpp>
10#include <casadi/mem.h>
11
12namespace alpaqa::casadi_loader {
13
14/// Class for evaluating CasADi functions, allocating the necessary workspace
15/// storage in advance for allocation-free evaluations.
16template <Config Conf, size_t N_in, size_t N_out>
18 public:
20 static_assert(std::is_same_v<real_t, casadi_real>);
21
22 using casadi_dim = std::pair<casadi_int, casadi_int>;
23
24 /// @throws std::invalid_argument
25 CasADiFunctionEvaluator(casadi::Function &&f)
26 : fun(std::move(f)), iwork(fun.sz_iw()), dwork(fun.sz_w()),
27 arg_work(fun.sz_arg()), res_work(fun.sz_res()) {
28 using namespace std::literals::string_literals;
29 if (N_in != fun.n_in())
30 throw std::invalid_argument(
31 "Invalid number of input arguments: got "s +
32 std::to_string(fun.n_in()) + ", should be " +
33 std::to_string(N_in) + ".");
34 if (N_out != fun.n_out())
35 throw std::invalid_argument(
36 "Invalid number of output arguments: got "s +
37 std::to_string(fun.n_out()) + ", should be " +
38 std::to_string(N_out) + ".");
39 }
40
41 /// @throws std::invalid_argument
42 CasADiFunctionEvaluator(casadi::Function &&f,
43 const std::array<casadi_dim, N_in> &dim_in,
44 const std::array<casadi_dim, N_out> &dim_out)
45 : CasADiFunctionEvaluator{std::move(f)} {
46 validate_dimensions(dim_in, dim_out);
47 }
48
49 /// @throws std::invalid_argument
50 void
51 validate_dimensions(const std::array<casadi_dim, N_in> &dim_in = {},
52 const std::array<casadi_dim, N_out> &dim_out = {}) {
53 using namespace std::literals::string_literals;
54 static constexpr std::array count{"first", "second", "third",
55 "fourth", "fifth", "sixth",
56 "seventh", "eighth"};
57 static_assert(N_in <= count.size());
58 static_assert(N_out <= count.size());
59 auto to_string = [](casadi_dim d) {
60 return "(" + std::to_string(d.first) + ", " +
61 std::to_string(d.second) + ")";
62 };
63 for (size_t n = 0; n < N_in; ++n) {
64 auto cs_n = static_cast<casadi_int>(n);
65 if (dim_in[n].first != 0 && dim_in[n] != fun.size_in(cs_n))
66 throw std::invalid_argument(
67 "Invalid dimension of "s + count[n] +
68 " input argument: got " + to_string(fun.size_in(cs_n)) +
69 ", should be " + to_string(dim_in[n]) + ".");
70 }
71 for (size_t n = 0; n < N_out; ++n) {
72 auto cs_n = static_cast<casadi_int>(n);
73 if (dim_out[n].first != 0 && dim_out[n] != fun.size_out(cs_n))
74 throw std::invalid_argument(
75 "Invalid dimension of "s + count[n] +
76 " output argument: got " + to_string(fun.size_out(cs_n)) +
77 ", should be " + to_string(dim_out[n]) + ".");
78 }
79 }
80
81 protected:
82 void operator()(const double *const *in, double *const *out) const {
83 std::copy_n(in, N_in, arg_work.begin());
84 std::copy_n(out, N_out, res_work.begin());
85 fun(arg_work.data(), res_work.data(), iwork.data(), dwork.data(), 0);
86 }
87
88 public:
89 void operator()(const double *const (&in)[N_in],
90 double *const (&out)[N_out]) const {
91 this->operator()(&in[0], &out[0]);
92 }
93
94 public:
95 casadi::Function fun;
96
97 private:
98 mutable std::vector<casadi_int> iwork;
99 mutable std::vector<double> dwork;
100 mutable std::vector<const double *> arg_work;
101 mutable std::vector<double *> res_work;
102};
103
104} // namespace alpaqa::casadi_loader
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
CasADiFunctionEvaluator(casadi::Function &&f, const std::array< casadi_dim, N_in > &dim_in, const std::array< casadi_dim, N_out > &dim_out)
void validate_dimensions(const std::array< casadi_dim, N_in > &dim_in={}, const std::array< casadi_dim, N_out > &dim_out={})
void operator()(const double *const *in, double *const *out) const
void operator()(const double *const (&in)[N_in], double *const (&out)[N_out]) const
std::pair< casadi_int, casadi_int > casadi_dim
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:54