alpaqa 1.0.0a10
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiProblem.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <filesystem>
8
9namespace alpaqa {
10namespace casadi_loader {
11template <Config>
12struct CasADiFunctionsWithParam;
13} // namespace casadi_loader
14
15/// Problem definition for a CasADi problem, loaded from a DLL.
16/// @ingroup grp_Problems
17template <Config Conf = EigenConfigd>
18class CasADiProblem : public BoxConstrProblem<Conf> {
19 public:
22
23 /// Load a problem generated by CasADi (with parameters).
24 ///
25 /// @param filename
26 /// Filename of the shared library to load the functions from.
27 ///
28 /// The file should contain functions with the names `f`, `grad_f`, `g` and
29 /// `grad_g`. These functions evaluate the objective function, its gradient,
30 /// the constraints, and the constraint gradient times a vector respectively.
31 /// For second order solvers, additional functions `hess_L`, `hess_ψ`,
32 /// `hess_L_prod` and `hess_ψ_prod` can be provided to evaluate the
33 /// Hessian of the (augmented) Lagrangian and Hessian-vector products.
34 ///
35 /// @throws std::invalid_argument
36 /// The dimensions of the loaded functions do not match.
37 CasADiProblem(const std::string &filename);
39
43 CasADiProblem &operator=(CasADiProblem &&) noexcept;
44
45 /// Load the numerical problem data (bounds and parameters) from a CSV file.
46 /// The file should contain 7 rows, with the following contents:
47 /// 1. @ref C lower bound [n]
48 /// 2. @ref C upper bound [n]
49 /// 3. @ref D lower bound [m]
50 /// 4. @ref D upper bound [m]
51 /// 5. @ref param [p]
52 /// 6. @ref l1_reg [0, 1 or n]
53 /// 7. @ref penalty_alm_split [1]
54 ///
55 /// Line endings are encoded using a single line feed (`\n`), and the column
56 /// separator can be specified using the @p sep argument.
57 void load_numerical_data(const std::filesystem::path &filepath,
58 char sep = ',');
59
60 // clang-format off
61 [[nodiscard]] real_t eval_f(crvec x) const;
62 void eval_grad_f(crvec x, rvec grad_fx) const;
63 real_t eval_f_grad_f(crvec x, rvec grad_fx) const; // NOLINT(*nodiscard)
64 void eval_g(crvec x, rvec g) const;
65 void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const;
66 void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const;
67 real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const; // NOLINT(*nodiscard)
68 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
69 [[nodiscard]] real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const;
70 void eval_grad_gi(crvec x, index_t i, rvec grad_i) const;
71 [[nodiscard]] length_t get_jac_g_num_nonzeros() const;
72 void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const;
73 void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const;
74 [[nodiscard]] length_t get_hess_L_num_nonzeros() const;
75 void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const;
76 void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const;
77 [[nodiscard]] length_t get_hess_ψ_num_nonzeros() const;
78 void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const;
79 // clang-format on
80
81 /// @see @ref TypeErasedProblem::provides_eval_grad_gi
82 [[nodiscard]] bool provides_eval_grad_gi() const;
83 /// @see @ref TypeErasedProblem::provides_eval_jac_g
84 [[nodiscard]] bool provides_eval_jac_g() const;
85 /// @see @ref TypeErasedProblem::provides_eval_hess_L_prod
86 [[nodiscard]] bool provides_eval_hess_L_prod() const;
87 /// @see @ref TypeErasedProblem::provides_eval_hess_L
88 [[nodiscard]] bool provides_eval_hess_L() const;
89 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ_prod
90 [[nodiscard]] bool provides_eval_hess_ψ_prod() const;
91 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ
92 [[nodiscard]] bool provides_eval_hess_ψ() const;
93
94 private:
95 using Functions = casadi_loader::CasADiFunctionsWithParam<Conf>;
96 util::copyable_unique_ptr<Functions> impl;
97};
98
100
101} // 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
void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
bool provides_eval_hess_ψ_prod() const
bool provides_eval_jac_g() 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
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) 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 &)
void eval_grad_gi(crvec x, index_t i, rvec grad_i) const
length_t get_jac_g_num_nonzeros() const
bool provides_eval_hess_ψ() const
void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const
void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
length_t get_hess_ψ_num_nonzeros() const
length_t get_hess_L_num_nonzeros() const
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:54
typename Conf::real_t real_t
Definition: config.hpp:63
typename Conf::rindexvec rindexvec
Definition: config.hpp:77
typename Conf::index_t index_t
Definition: config.hpp:75
typename Conf::length_t length_t
Definition: config.hpp:74
typename Conf::rvec rvec
Definition: config.hpp:67
typename Conf::crvec crvec
Definition: config.hpp:68
typename Conf::vec vec
Definition: config.hpp:64
Double-precision double configuration.
Definition: config.hpp:127