alpaqa 1.0.0a17
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CompleteCasADiProblem.cpp
Go to the documentation of this file.
5#include <casadi/casadi.hpp>
6
7namespace alpaqa::casadi_loader {
8
9template <class F>
10auto wrap(const char *name, F f) {
11 try {
12 return f();
14 throw std::invalid_argument(
15 "Unable to load function '" + std::string(name) +
16 "': " + demangled_typename(typeid(e)) + ": " + e.what());
17 }
18}
19
22 CasADiFunctions result;
23 for (auto &&[k, v] : functions.functions)
24 result.functions.emplace(k, casadi::Function::deserialize(v));
25 return result;
26}
27
30
31 // Load and validate the objective
33 const auto &f = functions.functions.at("f");
34 wrap("f", [&] { f_eval::validate_num_args(f); });
35 auto n = f.size1_in(0);
36 auto p = f.size1_in(1);
37 wrap("f", [&] { f_eval::validate_dimensions(f, dims(n, p), dims(1)); });
38
39 // Add the gradient of the objective
40 auto x = casadi::SX::sym("x", n);
41 auto param = casadi::SX::sym("param", p);
42 auto fx = f(std::vector{x, param})[0];
43 auto grad_f = casadi::SX::gradient(fx, x);
44 functions.functions["f_grad_f"] =
45 casadi::Function("f_grad_f", {x, param}, {fx, grad_f});
46
47 auto complete_constraints = [&](const casadi::Function &g) {
48 // Validate the constraints
50 wrap("g", [&] { g_eval::validate_num_args(g); });
51 auto m = g.size1_out(0);
52 wrap("g", [&] { g_eval::validate_dimensions(g, dims(n, p), dims(m)); });
53 if (m == 0) // unconstrained case
54 return;
55
56 // Add the gradient-vector product of the constraints
57 auto y = casadi::SX::sym("y", m);
58 auto gx = g(std::vector{x, param})[0];
59 auto grad_g_prod = casadi::SX::jtimes(gx, x, y, true);
60 functions.functions["grad_g_prod"] =
61 casadi::Function("grad_g_prod", {x, param, y}, {grad_g_prod});
62
63 // Add the gradient of the Lagrangian
64 auto grad_L = grad_f + grad_g_prod;
65 functions.functions["grad_L"] =
66 casadi::Function("grad_L", {x, param, y}, {grad_L});
67 };
68 // Try loading the constrains
69 auto g = functions.functions.find("g");
70 if (g != functions.functions.end())
71 complete_constraints(g->second);
72}
73
74} // namespace alpaqa::casadi_loader
Problem definition for a CasADi problem, loaded from a DLL.
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
CasADiFunctions deserialize_problem(const SerializedCasADiFunctions &functions)
Convert the map of strings to a map of CasADi functions.
void complete_problem(CasADiFunctions &functions)
Complete the given problem that contains only functions f and g, adding the necessary functions and g...
constexpr auto dims(auto... a)
auto wrap(const char *name, F f)
constexpr const auto inf
Definition config.hpp:112
std::map< std::string, std::string > functions
std::map< std::string, casadi::Function > functions