alpaqa 0.0.1
Nonconvex constrained optimization
CasADiFunctionWrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <casadi/core/function.hpp>
4
5#include <alpaqa/util/vec.hpp>
6
7#include <stdexcept>
8#include <string>
9#include <vector>
10
11/// @addtogroup grp_ExternalProblemLoaders
12/// @{
13
14/// Class for evaluating CasADi functions, allocating the necessary workspace
15/// storage in advance for allocation-free evaluations.
16template <size_t N_in, size_t N_out>
18 public:
19 using casadi_dim = std::pair<casadi_int, casadi_int>;
20
21 /// @throws std::invalid_argument
22 CasADiFunctionEvaluator(casadi::Function &&f,
23 const casadi_dim (&dim_in)[N_in] = {},
24 const casadi_dim (&dim_out)[N_out] = {})
25 : fun(std::move(f)), iwork(fun.sz_iw()), dwork(fun.sz_w()) {
26 if (N_in != fun.n_in())
27 throw std::invalid_argument("Invalid number of input arguments.");
28 if (N_out != fun.n_out())
29 throw std::invalid_argument("Invalid number of output arguments.");
30 validate_dimensions(dim_in, dim_out);
31 }
32
33 /// @throws std::invalid_argument
34 void validate_dimensions(const casadi_dim (&dim_in)[N_in],
35 const casadi_dim (&dim_out)[N_out]) {
36 using std::operator""s;
37 constexpr static const char *count[]{"first", "second", "third",
38 "fourth"};
39 static_assert(N_in <= 4);
40 static_assert(N_out <= 4);
41 auto to_string = [](casadi_dim d) {
42 return "(" + std::to_string(d.first) + ", " +
43 std::to_string(d.second) + ")";
44 };
45 for (size_t n = 0; n < N_in; ++n)
46 if (dim_in[n].first != 0 && dim_in[n] != fun.size_in(n))
47 throw std::invalid_argument(
48 "Invalid dimension of "s + count[n] +
49 " input argument: got " + to_string(fun.size_in(n)) +
50 ", should be " + to_string(dim_in[n]) + ".");
51 for (size_t n = 0; n < N_out; ++n)
52 if (dim_out[n].first != 0 && dim_out[n] != fun.size_out(n))
53 throw std::invalid_argument(
54 "Invalid dimension of "s + count[n] +
55 " output argument: got " + to_string(fun.size_out(n)) +
56 ", should be " + to_string(dim_out[n]) + ".");
57 }
58
59 protected:
60 void operator()(const double *const *in, double *const *out) const {
61 fun(const_cast<const double **>(in), const_cast<double **>(out),
62 iwork.data(), dwork.data(), 0);
63 }
64
65 public:
66 void operator()(const double *const (&in)[N_in],
67 double *const (&out)[N_out]) const {
68 this->operator()(&in[0], &out[0]);
69 }
70
71 public:
72 casadi::Function fun;
73
74 private:
75 mutable std::vector<casadi_int> iwork;
76 mutable std::vector<double> dwork;
77};
78
79/// Wrapper for CasADiFunctionEvaluator with 1 vector input, scalar output.
81 public:
82 CasADiFun_1Vi1So(casadi::Function &&f, casadi_int dim_in = 0)
83 : fun(std::move(f), {{dim_in, 1}}, {{1, 1}}) {}
84
85 double operator()(alpaqa::crvec x) const {
86 double out;
87 fun({x.data()}, {&out});
88 return out;
89 }
90
91 private:
93};
94
95/// Wrapper for CasADiFunctionEvaluator with 2 vector inputs, scalar output.
97 public:
98 CasADiFun_2Vi1So(casadi::Function &&f,
99 const std::array<casadi_int, 2> &dim_in = {})
100 : fun(std::move(f), {{dim_in[0], 1}, {dim_in[1], 1}}, {{1, 1}}) {}
101
103 double out;
104 fun({x.data(), p.data()}, {&out});
105 return out;
106 }
107
108 private:
110};
111
112/// Wrapper for CasADiFunctionEvaluator with 1 vector input, 1 vector output.
114 public:
116 : fun(std::move(fun)) {}
117 CasADiFun_1Vi1Vo(casadi::Function &&f, casadi_int dim_in = 0,
118 casadi_int dim_out = 0)
119 : fun(std::move(f), {{dim_in, 1}}, {{dim_out, 1}}) {}
120
122 fun({in.data()}, {out.data()});
123 }
124
125 private:
127};
128
129/// Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 vector output.
131 public:
133 : fun(std::move(fun)) {}
134 CasADiFun_2Vi1Vo(casadi::Function &&f,
135 const std::array<casadi_int, 2> &dim_in = {},
136 casadi_int dim_out = 0)
137 : fun(std::move(f), {{dim_in[0], 1}, {dim_in[1], 1}}, {{dim_out, 1}}) {}
138
140 fun({in1.data(), in2.data()}, {out.data()});
141 }
142
143 private:
145};
146
147/// Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 matrix output.
149 public:
150 CasADiFun_2Vi1Mo(casadi::Function &&f,
151 const std::array<casadi_int, 2> &dim_in = {},
153 : fun(std::move(f), {{dim_in[0], 1}, {dim_in[1], 1}}, {dim_out}) {}
154
156 fun({in1.data(), in2.data()}, {out.data()});
157 }
158
159 private:
161};
162
163/// Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 matrix output.
165 public:
166 CasADiFun_3Vi1Mo(casadi::Function &&f,
167 const std::array<casadi_int, 3> &dim_in = {},
169 : fun(std::move(f),
170 {
171 {dim_in[0], 1},
172 {dim_in[1], 1},
173 {dim_in[2], 1},
174 },
175 {dim_out}) {}
176
178 alpaqa::rmat out) const {
179 fun({in1.data(), in2.data(), in3.data()}, {out.data()});
180 }
181
182 private:
184};
185
186/// Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 vector output.
188 public:
189 CasADiFun_3Vi1Vo(casadi::Function &&f,
190 const std::array<casadi_int, 3> &dim_in = {},
191 casadi_int dim_out = 0)
192 : fun(std::move(f),
193 {
194 {dim_in[0], 1},
195 {dim_in[1], 1},
196 {dim_in[2], 1},
197 },
198 {{dim_out, 1}}) {}
199
201 alpaqa::rvec out) const {
202 fun({in1.data(), in2.data(), in3.data()}, {out.data()});
203 }
204
205 private:
207};
208
209/// Wrapper for CasADiFunctionEvaluator with 4 vector inputs, 1 vector output.
211 public:
212 CasADiFun_4Vi1Vo(casadi::Function &&f,
213 const std::array<casadi_int, 4> &dim_in = {},
214 casadi_int dim_out = 0)
215 : fun(std::move(f),
216 {
217 {dim_in[0], 1},
218 {dim_in[1], 1},
219 {dim_in[2], 1},
220 {dim_in[3], 1},
221 },
222 {{dim_out, 1}}) {}
223
225 alpaqa::rvec out) const {
226 fun({in1.data(), in2.data(), in3.data(), in4.data()}, {out.data()});
227 }
228
229 private:
231};
232
233/// @}
Wrapper for CasADiFunctionEvaluator with 1 vector input, scalar output.
CasADiFunctionEvaluator< 1, 1 > fun
CasADiFun_1Vi1So(casadi::Function &&f, casadi_int dim_in=0)
double operator()(alpaqa::crvec x) const
Wrapper for CasADiFunctionEvaluator with 1 vector input, 1 vector output.
CasADiFun_1Vi1Vo(CasADiFunctionEvaluator< 1, 1 > &&fun)
CasADiFun_1Vi1Vo(casadi::Function &&f, casadi_int dim_in=0, casadi_int dim_out=0)
void operator()(alpaqa::crvec in, alpaqa::rvec out) const
CasADiFunctionEvaluator< 1, 1 > fun
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 matrix output.
CasADiFun_2Vi1Mo(casadi::Function &&f, const std::array< casadi_int, 2 > &dim_in={}, CasADiFunctionEvaluator< 2, 1 >::casadi_dim dim_out={0, 0})
void operator()(alpaqa::crvec in1, alpaqa::crvec in2, alpaqa::rmat out) const
CasADiFunctionEvaluator< 2, 1 > fun
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, scalar output.
CasADiFun_2Vi1So(casadi::Function &&f, const std::array< casadi_int, 2 > &dim_in={})
double operator()(alpaqa::crvec x, alpaqa::crvec p) const
CasADiFunctionEvaluator< 2, 1 > fun
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 vector output.
CasADiFun_2Vi1Vo(CasADiFunctionEvaluator< 2, 1 > &&fun)
CasADiFun_2Vi1Vo(casadi::Function &&f, const std::array< casadi_int, 2 > &dim_in={}, casadi_int dim_out=0)
void operator()(alpaqa::crvec in1, alpaqa::crvec in2, alpaqa::rvec out) const
CasADiFunctionEvaluator< 2, 1 > fun
Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 matrix output.
void operator()(alpaqa::crvec in1, alpaqa::crvec in2, alpaqa::crvec in3, alpaqa::rmat out) const
CasADiFun_3Vi1Mo(casadi::Function &&f, const std::array< casadi_int, 3 > &dim_in={}, CasADiFunctionEvaluator< 3, 1 >::casadi_dim dim_out={0, 0})
CasADiFunctionEvaluator< 3, 1 > fun
Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 vector output.
void operator()(alpaqa::crvec in1, alpaqa::crvec in2, alpaqa::crvec in3, alpaqa::rvec out) const
CasADiFunctionEvaluator< 3, 1 > fun
CasADiFun_3Vi1Vo(casadi::Function &&f, const std::array< casadi_int, 3 > &dim_in={}, casadi_int dim_out=0)
Wrapper for CasADiFunctionEvaluator with 4 vector inputs, 1 vector output.
CasADiFunctionEvaluator< 4, 1 > fun
CasADiFun_4Vi1Vo(casadi::Function &&f, const std::array< casadi_int, 4 > &dim_in={}, casadi_int dim_out=0)
void operator()(alpaqa::crvec in1, alpaqa::crvec in2, alpaqa::crvec in3, alpaqa::crvec in4, alpaqa::rvec out) const
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
void operator()(const double *const *in, double *const *out) const
CasADiFunctionEvaluator(casadi::Function &&f, const casadi_dim(&dim_in)[N_in]={}, const casadi_dim(&dim_out)[N_out]={})
void operator()(const double *const (&in)[N_in], double *const (&out)[N_out]) const
void validate_dimensions(const casadi_dim(&dim_in)[N_in], const casadi_dim(&dim_out)[N_out])
std::pair< casadi_int, casadi_int > casadi_dim
std::vector< double > dwork
std::vector< casadi_int > iwork
int n
Definition: test.py:40
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
Eigen::Ref< mat > rmat
Default type for mutable references to matrices.
Definition: vec.hpp:22
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16