Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiFunctionWrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/casadi-loader-export.h>
6
7#include <stdexcept>
8#include <string>
9#include <vector>
10
11#if ALPAQA_WITH_EXTERNAL_CASADI
12#include <casadi/core/function.hpp>
13#include <casadi/mem.h>
14#else
16#endif
17
18namespace alpaqa {
20namespace casadi_loader {
21
22struct invalid_argument_dimensions : std::invalid_argument {
23 using std::invalid_argument::invalid_argument;
24};
25
26/// Class for evaluating CasADi functions, allocating the necessary workspace
27/// storage in advance for allocation-free evaluations.
28template <Config Conf, size_t N_in, size_t N_out>
30 public:
32 static_assert(std::is_same_v<real_t, casadi_real>);
33
34 using casadi_dim = std::pair<casadi_int, casadi_int>;
35
36 /// @throws invalid_argument_dimensions
38#if ALPAQA_WITH_EXTERNAL_CASADI
39 : fun(std::move(f)), iwork(fun.sz_iw()), dwork(fun.sz_w()),
40 arg_work(fun.sz_arg()), res_work(fun.sz_res())
41#else
42 : fun(std::move(f))
43#endif
44 {
46 }
47
48 /// @throws invalid_argument_dimensions
50 const std::array<casadi_dim, N_in> &dim_in,
51 const std::array<casadi_dim, N_out> &dim_out)
52 : CasADiFunctionEvaluator{std::move(f)} {
53 validate_dimensions(dim_in, dim_out);
54 }
55
56 /// @throws invalid_argument_dimensions
58 using namespace std::literals::string_literals;
59 if (N_in != fun.n_in())
61 "Invalid number of input arguments: got "s +
62 std::to_string(fun.n_in()) + ", should be " +
63 std::to_string(N_in) + ".");
64 if (N_out != fun.n_out())
66 "Invalid number of output arguments: got "s +
67 std::to_string(fun.n_out()) + ", should be " +
68 std::to_string(N_out) + ".");
69 }
70
71 /// @throws invalid_argument_dimensions
72 static void
74 const std::array<casadi_dim, N_in> &dim_in = {},
75 const std::array<casadi_dim, N_out> &dim_out = {}) {
76 using namespace std::literals::string_literals;
77 static constexpr std::array count{"first", "second", "third",
78 "fourth", "fifth", "sixth",
79 "seventh", "eighth"};
80 static_assert(N_in <= count.size());
81 static_assert(N_out <= count.size());
82 auto to_string = [](casadi_dim d) {
83 return "(" + std::to_string(d.first) + ", " +
84 std::to_string(d.second) + ")";
85 };
86 for (size_t n = 0; n < N_in; ++n) {
87 auto cs_n = static_cast<casadi_int>(n);
88 if (dim_in[n].first != 0 && dim_in[n] != fun.size_in(cs_n))
89 throw invalid_argument_dimensions(
90 "Invalid dimension of "s + count[n] +
91 " input argument: got " + to_string(fun.size_in(cs_n)) +
92 ", should be " + to_string(dim_in[n]) + ".");
93 }
94 for (size_t n = 0; n < N_out; ++n) {
95 auto cs_n = static_cast<casadi_int>(n);
96 if (dim_out[n].first != 0 && dim_out[n] != fun.size_out(cs_n))
97 throw invalid_argument_dimensions(
98 "Invalid dimension of "s + count[n] +
99 " output argument: got " + to_string(fun.size_out(cs_n)) +
100 ", should be " + to_string(dim_out[n]) + ".");
101 }
102 }
103
104 /// @throws invalid_argument_dimensions
105 void
106 validate_dimensions(const std::array<casadi_dim, N_in> &dim_in = {},
107 const std::array<casadi_dim, N_out> &dim_out = {}) {
108 validate_dimensions(fun, dim_in, dim_out);
109 }
110
111#if ALPAQA_WITH_EXTERNAL_CASADI
112 protected:
113 void operator()(const double *const *in, double *const *out) const {
114 std::copy_n(in, N_in, arg_work.begin());
115 std::copy_n(out, N_out, res_work.begin());
116 fun(arg_work.data(), res_work.data(), iwork.data(), dwork.data(), 0);
117 }
118
119 public:
120 void operator()(const double *const (&in)[N_in],
121 double *const (&out)[N_out]) const {
122 this->operator()(&in[0], &out[0]);
123 }
124#else
125 public:
126 void operator()(const double *const (&in)[N_in],
127 double *const (&out)[N_out]) {
128 fun(std::span{in}, std::span{out});
129 }
130#endif
131
132 public:
134
135#if ALPAQA_WITH_EXTERNAL_CASADI
136 private:
137 mutable std::vector<casadi_int> iwork;
138 mutable std::vector<double> dwork;
139 mutable std::vector<const double *> arg_work;
140 mutable std::vector<double *> res_work;
141#endif
142};
143
144} // namespace casadi_loader
146} // namespace alpaqa
#define BEGIN_ALPAQA_CASADI_LOADER_NAMESPACE
#define END_ALPAQA_CASADI_LOADER_NAMESPACE
Class that loads and calls pre-compiled CasADi functions in a DLL/SO file.
CasADiFunctionEvaluator(casadi::Function &&f, const std::array< casadi_dim, N_in > &dim_in, const std::array< casadi_dim, N_out > &dim_out)
static void validate_num_args(const casadi::Function &fun)
void operator()(const double *const (&in)[N_in], double *const (&out)[N_out])
void validate_dimensions(const std::array< casadi_dim, N_in > &dim_in={}, const std::array< casadi_dim, N_out > &dim_out={})
static void validate_dimensions(const casadi::Function &fun, const std::array< casadi_dim, N_in > &dim_in={}, const std::array< casadi_dim, N_out > &dim_out={})
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
long long int casadi_int