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