alpaqa no-casadi-dep
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiProblem.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <filesystem>
10#include <map>
11#include <string>
12
13#if ALPAQA_WITH_EXTERNAL_CASADI
14// Forward declaration
15namespace casadi {
16class Function;
17} // namespace casadi
18#else
20#endif
21
23namespace casadi_loader {
24template <Config>
25struct CasADiFunctionsWithParam;
26} // namespace casadi_loader
27
29 std::map<std::string, std::string> functions;
30};
31
33 std::map<std::string, casadi::Function> functions;
35};
36
37/// Problem definition for a CasADi problem, loaded from a DLL.
38/// @ingroup grp_Problems
39template <Config Conf = EigenConfigd>
40class CasADiProblem : public BoxConstrProblem<Conf> {
41 public:
44 vec param;
45 std::string name = "CasADiProblem";
46
47 /// Load a problem generated by CasADi (with parameters).
48 ///
49 /// @param filename
50 /// Filename of the shared library to load the functions from.
51 ///
52 /// The file should contain functions with the names `f`, `grad_f`, `g` and
53 /// `grad_g`. These functions evaluate the objective function, its gradient,
54 /// the constraints, and the constraint gradient times a vector respectively.
55 /// For second order solvers, additional functions `hess_L`, `hess_ψ`,
56 /// `hess_L_prod` and `hess_ψ_prod` can be provided to evaluate the
57 /// Hessian of the (augmented) Lagrangian and Hessian-vector products.
58 ///
59 /// @throws std::invalid_argument
60 /// The dimensions of the loaded functions do not match.
61 CasADiProblem(const std::string &filename);
62 /// Create a problem from a collection of serialized CasADi functions.
64 /// Create a problem from a collection of CasADi functions.
65 CasADiProblem(const CasADiFunctions &functions);
67
71 CasADiProblem &operator=(CasADiProblem &&) noexcept;
72
73 /// Load the numerical problem data (bounds and parameters) from a CSV file.
74 /// The file should contain 7 rows, with the following contents:
75 /// 1. @ref C lower bound [n]
76 /// 2. @ref C upper bound [n]
77 /// 3. @ref D lower bound [m]
78 /// 4. @ref D upper bound [m]
79 /// 5. @ref param [p]
80 /// 6. @ref l1_reg [0, 1 or n]
81 /// 7. @ref penalty_alm_split [1]
82 ///
83 /// Line endings are encoded using a single line feed (`\n`), and the column
84 /// separator can be specified using the @p sep argument.
85 void load_numerical_data(const std::filesystem::path &filepath,
86 char sep = ',');
87
88 // clang-format off
89 [[nodiscard]] real_t eval_f(crvec x) const;
90 void eval_grad_f(crvec x, rvec grad_fx) const;
91 real_t eval_f_grad_f(crvec x, rvec grad_fx) const; // NOLINT(*nodiscard)
92 void eval_g(crvec x, rvec g) const;
93 void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const;
94 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
95 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const; // NOLINT(*nodiscard)
96 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
97 [[nodiscard]] real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const;
98 void eval_grad_gi(crvec x, index_t i, rvec grad_i) const;
99 [[nodiscard]] Sparsity get_jac_g_sparsity() const;
100 void eval_jac_g(crvec x, rvec J_values) const;
101 void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const;
102 [[nodiscard]] Sparsity get_hess_L_sparsity() const;
103 void eval_hess_L(crvec x, crvec y, real_t scale, rvec H_values) const;
104 void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const;
105 [[nodiscard]] Sparsity get_hess_ψ_sparsity() const;
106 void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rvec H_values) const;
107 // clang-format on
108
109 /// @see @ref TypeErasedProblem::provides_eval_grad_L
110 [[nodiscard]] bool provides_eval_grad_L() const;
111 /// @see @ref TypeErasedProblem::provides_eval_ψ
112 [[nodiscard]] bool provides_eval_ψ() const;
113 /// @see @ref TypeErasedProblem::provides_eval_grad_ψ
114 [[nodiscard]] bool provides_eval_grad_ψ() const;
115 /// @see @ref TypeErasedProblem::provides_eval_ψ_grad_ψ
116 [[nodiscard]] bool provides_eval_ψ_grad_ψ() const;
117 /// @see @ref TypeErasedProblem::provides_eval_grad_gi
118 [[nodiscard]] bool provides_eval_grad_gi() const;
119 /// @see @ref TypeErasedProblem::provides_eval_jac_g
120 [[nodiscard]] bool provides_eval_jac_g() const;
121 /// @see @ref TypeErasedProblem::provides_eval_hess_L_prod
122 [[nodiscard]] bool provides_eval_hess_L_prod() const;
123 /// @see @ref TypeErasedProblem::provides_eval_hess_L
124 [[nodiscard]] bool provides_eval_hess_L() const;
125 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ_prod
126 [[nodiscard]] bool provides_eval_hess_ψ_prod() const;
127 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ
128 [[nodiscard]] bool provides_eval_hess_ψ() const;
129
130 /// @see @ref TypeErasedProblem::get_name
131 [[nodiscard]] std::string get_name() const;
132
133 private:
134 using Functions = casadi_loader::CasADiFunctionsWithParam<Conf>;
135 util::copyable_unique_ptr<Functions> impl;
136};
137
139
140} // namespace alpaqa::inline ALPAQA_CASADI_LOADER_NAMESPACE
#define CASADI_LOADER_EXPORT_EXTERN_TEMPLATE(strcls, name,...)
#define ALPAQA_CASADI_LOADER_NAMESPACE
Problem definition for a CasADi problem, loaded from a DLL.
CasADiProblem & operator=(const CasADiProblem &)
CasADiProblem(CasADiProblem &&) noexcept
CasADiProblem(const CasADiProblem &)
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
constexpr const auto inf
Definition config.hpp:112
std::map< std::string, std::string > functions
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106
std::map< std::string, casadi::Function > functions