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