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