alpaqa 1.0.0a19
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
22namespace alpaqa {
24namespace casadi_loader {
25template <Config>
26struct CasADiFunctionsWithParam;
27} // namespace casadi_loader
28
30 std::map<std::string, std::string> functions;
31};
32
34 std::map<std::string, casadi::Function> functions;
36};
37
38/// Problem definition for a CasADi problem, loaded from a DLL.
39/// @ingroup grp_Problems
40template <Config Conf = EigenConfigd>
41class CasADiProblem : public BoxConstrProblem<Conf> {
42 public:
46 std::string name = "CasADiProblem";
47
48 /// Load a problem generated by CasADi (with parameters).
49 ///
50 /// @param filename
51 /// Filename of the shared library to load the functions from.
52 ///
53 /// The file should contain functions with the names `f`, `grad_f`, `g` and
54 /// `grad_g`. These functions evaluate the objective function, its gradient,
55 /// the constraints, and the constraint gradient times a vector respectively.
56 /// For second order solvers, additional functions `hess_L`, `hess_ψ`,
57 /// `hess_L_prod` and `hess_ψ_prod` can be provided to evaluate the
58 /// Hessian of the (augmented) Lagrangian and Hessian-vector products.
59 ///
60 /// @throws std::invalid_argument
61 /// The dimensions of the loaded functions do not match.
62 CasADiProblem(const std::string &filename);
63 /// Create a problem from a collection of serialized CasADi functions.
65 /// Create a problem from a collection of CasADi functions.
66 CasADiProblem(const CasADiFunctions &functions);
68
73
74 /// Load the numerical problem data (bounds and parameters) from a CSV file.
75 /// The file should contain 7 rows, with the following contents:
76 /// 1. @ref BoxConstrProblem::C lower bound [n]
77 /// 2. @ref BoxConstrProblem::C upper bound [n]
78 /// 3. @ref BoxConstrProblem::D lower bound [m]
79 /// 4. @ref BoxConstrProblem::D upper bound [m]
80 /// 5. @ref param [p]
81 /// 6. @ref BoxConstrProblem::l1_reg [0, 1 or n]
82 /// 7. @ref BoxConstrProblem::penalty_alm_split [1]
83 ///
84 /// Line endings are encoded using a single line feed (`\n`), and the column
85 /// separator can be specified using the @p sep argument.
87 char sep = ',');
88
89 // clang-format off
92 real_t eval_f_grad_f(crvec x, rvec grad_fx) const; // NOLINT(*nodiscard)
93 void eval_g(crvec x, rvec g) const;
95 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
96 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const; // NOLINT(*nodiscard)
97 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
108 // clang-format on
109
110 /// @see @ref TypeErasedProblem::provides_eval_grad_L
112 /// @see @ref TypeErasedProblem::provides_eval_ψ
114 /// @see @ref TypeErasedProblem::provides_eval_grad_ψ
116 /// @see @ref TypeErasedProblem::provides_eval_ψ_grad_ψ
118 /// @see @ref TypeErasedProblem::provides_eval_grad_gi
120 /// @see @ref TypeErasedProblem::provides_eval_jac_g
122 /// @see @ref TypeErasedProblem::provides_eval_hess_L_prod
124 /// @see @ref TypeErasedProblem::provides_eval_hess_L
126 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ_prod
128 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ
130
131 /// @see @ref TypeErasedProblem::get_name
132 [[nodiscard]] std::string get_name() const;
133
134 private:
135 using Functions = casadi_loader::CasADiFunctionsWithParam<Conf>;
136 util::copyable_unique_ptr<Functions> impl;
137};
138
140
142} // 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
Double-precision double configuration.
Definition config.hpp:174
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106