alpaqa 1.0.0a17
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiProblem.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <filesystem>
9#include <map>
10#include <string>
11
12// Forward declaration
13namespace casadi {
14class Function;
15} // namespace casadi
16
17namespace alpaqa {
18namespace casadi_loader {
19template <Config>
20struct CasADiFunctionsWithParam;
21} // namespace casadi_loader
22
24 std::map<std::string, std::string> functions;
25};
26
28 std::map<std::string, casadi::Function> functions;
30};
31
32/// Problem definition for a CasADi problem, loaded from a DLL.
33/// @ingroup grp_Problems
34template <Config Conf = EigenConfigd>
35class CasADiProblem : public BoxConstrProblem<Conf> {
36 public:
40 std::string name = "CasADiProblem";
41
42 /// Load a problem generated by CasADi (with parameters).
43 ///
44 /// @param filename
45 /// Filename of the shared library to load the functions from.
46 ///
47 /// The file should contain functions with the names `f`, `grad_f`, `g` and
48 /// `grad_g`. These functions evaluate the objective function, its gradient,
49 /// the constraints, and the constraint gradient times a vector respectively.
50 /// For second order solvers, additional functions `hess_L`, `hess_ψ`,
51 /// `hess_L_prod` and `hess_ψ_prod` can be provided to evaluate the
52 /// Hessian of the (augmented) Lagrangian and Hessian-vector products.
53 ///
54 /// @throws std::invalid_argument
55 /// The dimensions of the loaded functions do not match.
56 CasADiProblem(const std::string &filename);
57 /// Create a problem from a collection of serialized CasADi functions.
59 /// Create a problem from a collection of CasADi functions.
60 CasADiProblem(const CasADiFunctions &functions);
62
67
68 /// Load the numerical problem data (bounds and parameters) from a CSV file.
69 /// The file should contain 7 rows, with the following contents:
70 /// 1. @ref C lower bound [n]
71 /// 2. @ref C upper bound [n]
72 /// 3. @ref D lower bound [m]
73 /// 4. @ref D upper bound [m]
74 /// 5. @ref param [p]
75 /// 6. @ref l1_reg [0, 1 or n]
76 /// 7. @ref penalty_alm_split [1]
77 ///
78 /// Line endings are encoded using a single line feed (`\n`), and the column
79 /// separator can be specified using the @p sep argument.
81 char sep = ',');
82
83 // clang-format off
86 real_t eval_f_grad_f(crvec x, rvec grad_fx) const; // NOLINT(*nodiscard)
87 void eval_g(crvec x, rvec g) const;
89 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
90 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const; // NOLINT(*nodiscard)
91 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
102 // clang-format on
103
104 /// @see @ref TypeErasedProblem::provides_eval_grad_L
106 /// @see @ref TypeErasedProblem::provides_eval_ψ
108 /// @see @ref TypeErasedProblem::provides_eval_grad_ψ
110 /// @see @ref TypeErasedProblem::provides_eval_ψ_grad_ψ
112 /// @see @ref TypeErasedProblem::provides_eval_grad_gi
114 /// @see @ref TypeErasedProblem::provides_eval_jac_g
116 /// @see @ref TypeErasedProblem::provides_eval_hess_L_prod
118 /// @see @ref TypeErasedProblem::provides_eval_hess_L
120 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ_prod
122 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ
124
125 /// @see @ref TypeErasedProblem::get_name
126 [[nodiscard]] std::string get_name() const;
127
128 private:
129 using Functions = casadi_loader::CasADiFunctionsWithParam<Conf>;
130 util::copyable_unique_ptr<Functions> impl;
131};
132
134
135} // namespace alpaqa
#define CASADI_LOADER_EXPORT_EXTERN_TEMPLATE(strcls, name,...)
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