alpaqa 1.0.0a13
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
14struct invalid_argument_dimensions : std::invalid_argument {
15 using std::invalid_argument::invalid_argument;
16};
17
18/// Class for evaluating CasADi functions, allocating the necessary workspace
19/// storage in advance for allocation-free evaluations.
20template <Config Conf, size_t N_in, size_t N_out>
22 public:
24 static_assert(std::is_same_v<real_t, casadi_real>);
25
26 using casadi_dim = std::pair<casadi_int, casadi_int>;
27
28 /// @throws invalid_argument_dimensions
29 CasADiFunctionEvaluator(casadi::Function &&f)
30 : fun(std::move(f)), iwork(fun.sz_iw()), dwork(fun.sz_w()),
32 using namespace std::literals::string_literals;
33 if (N_in != fun.n_in())
35 "Invalid number of input arguments: got "s +
36 std::to_string(fun.n_in()) + ", should be " +
37 std::to_string(N_in) + ".");
38 if (N_out != fun.n_out())
40 "Invalid number of output arguments: got "s +
41 std::to_string(fun.n_out()) + ", should be " +
42 std::to_string(N_out) + ".");
43 }
44
45 /// @throws invalid_argument_dimensions
46 CasADiFunctionEvaluator(casadi::Function &&f,
47 const std::array<casadi_dim, N_in> &dim_in,
48 const std::array<casadi_dim, N_out> &dim_out)
49 : CasADiFunctionEvaluator{std::move(f)} {
51 }
52
53 /// @throws invalid_argument_dimensions
54 void
55 validate_dimensions(const std::array<casadi_dim, N_in> &dim_in = {},
56 const std::array<casadi_dim, N_out> &dim_out = {}) {
57 using namespace std::literals::string_literals;
58 static constexpr std::array count{"first", "second", "third",
59 "fourth", "fifth", "sixth",
60 "seventh", "eighth"};
61 static_assert(N_in <= count.size());
62 static_assert(N_out <= count.size());
63 auto to_string = [](casadi_dim d) {
64 return "(" + std::to_string(d.first) + ", " +
65 std::to_string(d.second) + ")";
66 };
67 for (size_t n = 0; n < N_in; ++n) {
68 auto cs_n = static_cast<casadi_int>(n);
69 if (dim_in[n].first != 0 && dim_in[n] != fun.size_in(cs_n))
70 throw invalid_argument_dimensions(
71 "Invalid dimension of "s + count[n] +
72 " input argument: got " + to_string(fun.size_in(cs_n)) +
73 ", should be " + to_string(dim_in[n]) + ".");
74 }
75 for (size_t n = 0; n < N_out; ++n) {
76 auto cs_n = static_cast<casadi_int>(n);
77 if (dim_out[n].first != 0 && dim_out[n] != fun.size_out(cs_n))
78 throw invalid_argument_dimensions(
79 "Invalid dimension of "s + count[n] +
80 " output argument: got " + to_string(fun.size_out(cs_n)) +
81 ", should be " + to_string(dim_out[n]) + ".");
82 }
83 }
84
85 protected:
86 void operator()(const double *const *in, double *const *out) const {
87 std::copy_n(in, N_in, arg_work.begin());
88 std::copy_n(out, N_out, res_work.begin());
89 fun(arg_work.data(), res_work.data(), iwork.data(), dwork.data(), 0);
90 }
91
92 public:
93 void operator()(const double *const (&in)[N_in],
94 double *const (&out)[N_out]) const {
95 this->operator()(&in[0], &out[0]);
96 }
97
98 public:
99 casadi::Function fun;
100
101 private:
102 mutable std::vector<casadi_int> iwork;
103 mutable std::vector<double> dwork;
104 mutable std::vector<const double *> arg_work;
105 mutable std::vector<double *> res_work;
106};
107
108} // 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
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:56
constexpr const auto inf
Definition config.hpp:85