alpaqa matlab
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
41 /// Load a problem generated by CasADi (with parameters).
42 ///
43 /// @param filename
44 /// Filename of the shared library to load the functions from.
45 ///
46 /// The file should contain functions with the names `f`, `grad_f`, `g` and
47 /// `grad_g`. These functions evaluate the objective function, its gradient,
48 /// the constraints, and the constraint gradient times a vector respectively.
49 /// For second order solvers, additional functions `hess_L`, `hess_ψ`,
50 /// `hess_L_prod` and `hess_ψ_prod` can be provided to evaluate the
51 /// Hessian of the (augmented) Lagrangian and Hessian-vector products.
52 ///
53 /// @throws std::invalid_argument
54 /// The dimensions of the loaded functions do not match.
55 CasADiProblem(const std::string &filename);
56 /// Create a problem from a collection of serialized CasADi functions.
58 /// Create a problem from a collection of CasADi functions.
59 CasADiProblem(const CasADiFunctions &functions);
61
66
67 /// Load the numerical problem data (bounds and parameters) from a CSV file.
68 /// The file should contain 7 rows, with the following contents:
69 /// 1. @ref C lower bound [n]
70 /// 2. @ref C upper bound [n]
71 /// 3. @ref D lower bound [m]
72 /// 4. @ref D upper bound [m]
73 /// 5. @ref param [p]
74 /// 6. @ref l1_reg [0, 1 or n]
75 /// 7. @ref penalty_alm_split [1]
76 ///
77 /// Line endings are encoded using a single line feed (`\n`), and the column
78 /// separator can be specified using the @p sep argument.
80 char sep = ',');
81
82 // clang-format off
85 real_t eval_f_grad_f(crvec x, rvec grad_fx) const; // NOLINT(*nodiscard)
86 void eval_g(crvec x, rvec g) const;
88 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
89 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const; // NOLINT(*nodiscard)
90 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
101 // clang-format on
102
103 /// @see @ref TypeErasedProblem::provides_eval_grad_L
105 /// @see @ref TypeErasedProblem::provides_eval_ψ
107 /// @see @ref TypeErasedProblem::provides_eval_grad_ψ
109 /// @see @ref TypeErasedProblem::provides_eval_ψ_grad_ψ
111 /// @see @ref TypeErasedProblem::provides_eval_grad_gi
113 /// @see @ref TypeErasedProblem::provides_eval_jac_g
115 /// @see @ref TypeErasedProblem::provides_eval_hess_L_prod
117 /// @see @ref TypeErasedProblem::provides_eval_hess_L
119 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ_prod
121 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ
123
124 private:
125 using Functions = casadi_loader::CasADiFunctionsWithParam<Conf>;
126 util::copyable_unique_ptr<Functions> impl;
127};
128
130
131} // 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
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:56
typename Conf::real_t real_t
Definition config.hpp:65
typename Conf::index_t index_t
Definition config.hpp:77
constexpr const auto inf
Definition config.hpp:85
typename Conf::rvec rvec
Definition config.hpp:69
typename Conf::crvec crvec
Definition config.hpp:70
typename Conf::vec vec
Definition config.hpp:66
std::map< std::string, std::string > functions
std::map< std::string, casadi::Function > functions
Double-precision double configuration.
Definition config.hpp:135
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:106