4#include <casadi/core/external.hpp>
13std::function<alpaqa::Problem::f_sig>
14load_CasADi_objective(
const std::string &so_name,
const std::string &fun_name) {
17std::function<alpaqa::Problem::grad_f_sig>
18load_CasADi_gradient_objective(
const std::string &so_name,
19 const std::string &fun_name) {
22std::function<alpaqa::Problem::g_sig>
23load_CasADi_constraints(
const std::string &so_name,
24 const std::string &fun_name) {
27std::function<alpaqa::Problem::grad_g_prod_sig>
28load_CasADi_gradient_constraints_prod(
const std::string &so_name,
29 const std::string &fun_name) {
38std::function<alpaqa::Problem::hess_L_sig>
39load_CasADi_hessian_lagrangian(
const std::string &so_name,
40 const std::string &fun_name) {
44 if (
x.rows() !=
H.rows()) {
45 for (
auto c =
x.rows();
c-- > 1;)
46 for (
auto r =
x.rows(); r-- > 0;)
47 std::swap(
H(r,
c),
H.data()[r +
x.rows() *
c]);
51 if (
x.rows() !=
H.rows()) {
52 for (
auto c =
x.rows();
c-- > 1;)
53 for (
auto r =
x.rows(); r-- > 0;)
54 std::swap(
H(r,
c),
H.data()[r +
x.rows() *
c]);
58std::function<alpaqa::Problem::hess_L_prod_sig>
59load_CasADi_hessian_lagrangian_prod(
const std::string &so_name,
60 const std::string &fun_name) {
69 }
catch (
const std::invalid_argument &e) {
70 throw std::invalid_argument(
"Unable to load function '" + so_name +
71 ":" +
name +
"': " + e.what());
75template <
class T,
class... Args>
79 return T(casadi::external(
name, so_name), std::forward<Args>(args)...);
83constexpr static auto dims = [](
auto...
a) {
84 return std::array<casadi_int,
sizeof...(a)>{
a...};
86using dim = std::pair<casadi_int, casadi_int>;
89 unsigned m,
bool second_order) {
91 auto load_g_unknown_dims = [&] {
93 if (
g.fun.size2_in(0) != 1)
94 throw std::invalid_argument(
95 "First input argument should be a column vector.");
96 if (
g.fun.size2_out(0) != 1)
97 throw std::invalid_argument(
98 "First output argument should be a column vector.");
100 n =
g.fun.size1_in(0);
102 m =
g.fun.size1_out(0);
103 g.validate_dimensions({
dim(
n, 1)}, {
dim(
m, 1)});
107 auto load_g_known_dims = [&] {
109 casadi::external(
"g", so_name), {
dim(
n, 1)}, {
dim(
m, 1)}};
117 ?
wrap_load(so_name,
"g", load_g_unknown_dims)
120 :
wrap_load(so_name,
"g", load_g_known_dims);
124 prob.f = wrapped_load<CasADiFun_1Vi1So>(so_name,
"f",
n);
125 prob.grad_f = wrapped_load<CasADiFun_1Vi1Vo>(so_name,
"grad_f",
n,
n);
128 wrapped_load<CasADiFun_2Vi1Vo>(so_name,
"grad_g",
dims(
n,
m),
n);
129 prob.grad_g_prod = grad_g;
139 wrapped_load<CasADiFun_2Vi1Mo>(so_name,
"hess_L",
142 wrapped_load<CasADiFun_3Vi1Vo>(so_name,
"hess_L_prod",
150 public std::enable_shared_from_this<CasADiParamWrapper> {
167 static std::shared_ptr<CasADiParamWrapper>
create(
unsigned p,
171 std::move(functions),
176 auto param = this->shared_from_this();
195 if (
param->cs.hess_L) {
200 if (
param->cs.hess_L_prod) {
207 std::shared_ptr<ParamWrapper>
clone()
const override {
208 return std::make_shared<CasADiParamWrapper>(*
this);
213 unsigned n,
unsigned m,
214 unsigned p,
bool second_order) {
216 auto load_g_unknown_dims = [&] {
218 if (
g.fun.size2_in(0) != 1)
219 throw std::invalid_argument(
220 "First input argument should be a column vector.");
221 if (
g.fun.size2_in(1) != 1)
222 throw std::invalid_argument(
223 "Second input argument should be a column vector.");
224 if (
g.fun.size2_out(0) != 1)
225 throw std::invalid_argument(
226 "First output argument should be a column vector.");
228 n =
g.fun.size1_in(0);
230 m =
g.fun.size1_out(0);
232 p =
g.fun.size1_in(1);
237 auto load_g_known_dims = [&] {
245 (
n == 0 ||
m == 0 ||
p == 0)
248 ?
wrap_load(so_name,
"g", load_g_unknown_dims)
251 :
wrap_load(so_name,
"g", load_g_known_dims);
258 wrapped_load<CasADiFun_2Vi1So>(so_name,
"f",
dims(
n,
p)),
259 wrapped_load<CasADiFun_2Vi1Vo>(so_name,
"grad_f",
dims(
n,
p),
n),
261 wrapped_load<CasADiFun_3Vi1Vo>(so_name,
"grad_g",
dims(
n,
p,
m),
n),
262 second_order ? std::make_optional(wrapped_load<CasADiFun_3Vi1Mo>(
265 second_order ? std::make_optional(wrapped_load<CasADiFun_4Vi1Vo>(
266 so_name,
"hess_L_prod",
dims(
n,
p,
m,
n),
n))
Wrapper for CasADiFunctionEvaluator with 1 vector input, scalar output.
Wrapper for CasADiFunctionEvaluator with 1 vector input, 1 vector output.
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 matrix output.
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, scalar output.
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 vector output.
Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 vector output.
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
CasADiFun_3Vi1Vo grad_g_prod
struct alpaqa::CasADiParamWrapper::Functions cs
std::shared_ptr< ParamWrapper > clone() const override
std::optional< CasADiFun_4Vi1Vo > hess_L_prod
std::optional< CasADiFun_3Vi1Mo > hess_L
void wrap(Problem &prob) override
static std::shared_ptr< CasADiParamWrapper > create(unsigned p, Functions &&functions)
CasADiParamWrapper(unsigned p, Functions &&functions)
ProblemWithParam load_CasADi_problem_with_param(const std::string &filename, unsigned n=0, unsigned m=0, unsigned p=0, bool second_order=false)
Load a problem generated by CasADi (with parameters).
alpaqa::Problem load_CasADi_problem(const std::string &filename, unsigned n=0, unsigned m=0, bool second_order=false)
Load a problem generated by CasADi (without parameters).
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
std::pair< casadi_int, casadi_int > dim
realvec vec
Default type for vectors.
auto wrapped_load(const std::string &so_name, const char *name, Args &&...args)
constexpr static auto dims
Eigen::Ref< mat > rmat
Default type for mutable references to matrices.
double real_t
Default floating point type.
auto wrap_load(const std::string &so_name, const char *name, F f)
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Problem description for minimization problems.