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