9#include <casadi/core/external.hpp>
21namespace fs = std::filesystem;
23namespace casadi_loader {
31 std::optional<CasADiFunctionEvaluator<Conf, 2, 1>>
g = std::nullopt;
32 std::optional<CasADiFunctionEvaluator<Conf, 3, 1>>
grad_g_prod =
34 std::optional<CasADiFunctionEvaluator<Conf, 2, 1>>
jac_g = std::nullopt;
35 std::optional<CasADiFunctionEvaluator<Conf, 3, 1>>
grad_L = std::nullopt;
36 std::optional<CasADiFunctionEvaluator<Conf, 5, 1>>
hess_L_prod =
38 std::optional<CasADiFunctionEvaluator<Conf, 4, 1>>
hess_L = std::nullopt;
39 std::optional<CasADiFunctionEvaluator<Conf, 6, 2>>
ψ = std::nullopt;
40 std::optional<CasADiFunctionEvaluator<Conf, 6, 2>>
ψ_grad_ψ = std::nullopt;
41 std::optional<CasADiFunctionEvaluator<Conf, 8, 1>>
hess_ψ_prod =
43 std::optional<CasADiFunctionEvaluator<Conf, 7, 1>>
hess_ψ = std::nullopt;
45 template <
class Loader>
47 {
loader(name) } -> std::same_as<casadi::Function>;
48 {
loader.format_name(name) } -> std::same_as<std::string>;
55 using namespace std::literals::string_literals;
58 "Invalid number of input arguments: got "s +
59 std::to_string(
gfun.n_in()) +
", should be 2.");
62 "Invalid number of output arguments: got "s +
63 std::to_string(
gfun.n_in()) +
", should be 0 or 1.");
64 if (
gfun.size2_in(0) != 1)
66 "First input argument should be a column vector.");
67 if (
gfun.size2_in(1) != 1)
69 "Second input argument should be a column vector.");
70 if (
gfun.n_out() == 1 &&
gfun.size2_out(0) != 1)
72 "First output argument should be a column vector.");
74 if (
gfun.n_out() == 1)
77 if (
gfun.n_out() == 0) {
80 "Function g has no outputs but m != 0");
85 return std::make_optional(std::move(
g));
90 return std::make_unique<CasADiFunctionsWithParam>(
111 loader,
"psi",
dims(
n,
p,
m,
m,
m,
m),
dims(1,
m)),
113 loader,
"psi_grad_psi",
dims(
n,
p,
m,
m,
m,
m),
dims(1,
n)),
115 loader,
"hess_psi_prod",
dims(
n,
p,
m,
m, 1,
m,
m,
n),
128template <Config Conf>
134template <Config Conf>
140 auto operator()(
const std::string &name)
const {
141 return casadi::external(name,
filename);
152 this->C = Box<config_t>{impl->n};
153 this->D = Box<config_t>{impl->m};
160template <Config Conf>
166 auto operator()(
const std::string &name)
const {
167 return casadi::Function::deserialize(functions.
functions.at(name));
170 return "SerializedCasADiFunctions['" + name +
"']";
178 this->C = Box<config_t>{impl->n};
179 this->D = Box<config_t>{impl->m};
182template <Config Conf>
188 auto operator()(
const std::string &name)
const {
192 return "CasADiFunctions['" + name +
"']";
200 this->C = Box<config_t>{impl->n};
201 this->D = Box<config_t>{impl->m};
204template <Config Conf>
206 const std::filesystem::path &
filepath,
char sep) {
210 throw std::runtime_error(
"Unable to open data file \"" +
219 return static_cast<void>(
data_file.get());
223 auto s = csv::read_row_std_vector<real_t>(
data_file, sep);
228 throw std::runtime_error(
"Unable to read " + std::string(name) +
229 " from data file \"" +
filepath.string() +
230 ':' + std::to_string(
line) +
235 auto read_single = [&](std::string_view name,
auto &
v) {
238 throw std::runtime_error(
"Unable to read " + std::string(name) +
239 " from data file \"" +
filepath.string() +
240 ':' + std::to_string(
line) +
'"');
250 read_single(
"penalty_alm_split", this->penalty_alm_split);
255template <Config Conf>
257template <Config Conf>
260template <Config Conf>
272 impl->f({x.data(), param.data()}, {&f});
276template <Config Conf>
279 impl->f_grad_f({x.data(), param.data()}, {&f,
grad_fx.data()});
282template <Config Conf>
285 impl->f_grad_f({x.data(), param.data()}, {&f,
grad_fx.data()});
289template <Config Conf>
294 (*impl->g)({x.data(), param.data()}, {g.data()});
299template <Config Conf>
305 if (impl->grad_g_prod)
306 (*impl->grad_g_prod)({x.data(), param.data(), y.data()}, {
gxy.data()});
311template <Config Conf>
315 impl->grad_ψ({x.data(), param.data(), y.data(), Σ.data(),
316 this->D.lowerbound.data(),
this->D.upperbound.data()},
321 eval_ψ_grad_ψ(x, y, Σ, grad_ψ, work_n, work_m);
325template <Config Conf>
330 throw std::logic_error(
"CasADiProblem::eval_ψ_grad_ψ");
332 (*impl->ψ_grad_ψ)({x.data(), param.data(), y.data(), Σ.data(),
333 this->D.lowerbound.data(), this->D.upperbound.data()},
334 {&ψ, grad_ψ.data()});
338template <Config Conf>
342 throw std::logic_error(
"CasADiProblem::eval_grad_L");
343 (*impl->grad_L)({x.data(), param.data(), y.data()}, {grad_L.data()});
346template <Config Conf>
350 throw std::logic_error(
"CasADiProblem::eval_ψ");
352 (*impl->ψ)({x.data(), param.data(), y.data(), Σ.data(),
353 this->D.lowerbound.data(), this->D.upperbound.data()},
358template <Config Conf>
363template <Config Conf>
367 using map_t =
typename SparseCSC::index_vector_map_t;
370 .cols =
static_cast<index_t>(
sp.size2()),
371 .symmetry = symmetry,
374 .order = SparseCSC::SortedRows,
378template <Config Conf>
385 if (!impl->jac_g.has_value())
387 const auto &
sp = impl->jac_g->fun.sparsity_out(0);
393template <Config Conf>
396 throw std::logic_error(
"CasADiProblem::eval_jac_g");
397 (*impl->jac_g)({x.data(), param.data()}, {
J_values.data()});
400template <Config Conf>
403 if (!impl->hess_L_prod)
404 throw std::logic_error(
"CasADiProblem::eval_ψ");
405 (*impl->hess_L_prod)({x.data(), param.data(), y.data(), &
scale,
v.data()},
409template <Config Conf>
416 if (!impl->hess_L.has_value())
418 const auto &
sp = impl->hess_L->fun.sparsity_out(0);
423template <Config Conf>
427 throw std::logic_error(
"CasADiProblem::eval_hess_L");
428 (*impl->hess_L)({x.data(), param.data(), y.data(), &
scale},
432template <Config Conf>
436 if (!impl->hess_ψ_prod)
437 throw std::logic_error(
"CasADiProblem::eval_hess_ψ_prod");
438 (*impl->hess_ψ_prod)({x.data(), param.data(), y.data(), Σ.data(), &
scale,
439 this->D.lowerbound.data(), this->D.upperbound.data(),
444template <Config Conf>
451 if (!impl->hess_ψ.has_value())
453 const auto &
sp = impl->hess_ψ->fun.sparsity_out(0);
458template <Config Conf>
462 throw std::logic_error(
"CasADiProblem::eval_hess_ψ");
463 (*impl->hess_ψ)({x.data(), param.data(), y.data(), Σ.data(), &
scale,
464 this->D.lowerbound.data(), this->D.upperbound.data()},
468template <Config Conf>
472template <Config Conf>
474 return impl->ψ.has_value();
476template <Config Conf>
478 return impl->ψ_grad_ψ.has_value();
480template <Config Conf>
482 return impl->ψ_grad_ψ.has_value();
484template <Config Conf>
486 return impl->grad_L.has_value();
488template <Config Conf>
490 return impl->jac_g.has_value();
492template <Config Conf>
494 return impl->hess_L_prod.has_value();
496template <Config Conf>
498 return impl->hess_L.has_value();
500template <Config Conf>
502 return impl->hess_ψ_prod.has_value();
504template <Config Conf>
506 return impl->hess_ψ.has_value();
509template <Config Conf>
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(const std::string &filename)
Load a problem generated by CasADi (with parameters).
bool provides_eval_hess_L_prod() const
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
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
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
static void validate_dimensions(const casadi::Function &fun, const std::array< casadi_dim, N_in > &dim_in={}, const std::array< casadi_dim, N_out > &dim_out={})
#define USING_ALPAQA_CONFIG(Conf)
auto wrapped_load(Loader &&loader, const char *name, Args &&...args)
std::optional< T > try_load(Loader &&loader, const char *name, Args &&...args)
constexpr auto dims(auto... a)
std::pair< casadi_int, casadi_int > dim
auto wrap_load(Loader &&loader, const char *name, F f)
void read_row(std::istream &is, Eigen::Ref< Eigen::VectorX< Eigen::Index > > v, char sep)
auto casadi_to_index(casadi_int i) -> index_t< Conf >
Symmetry
Describes the symmetry of matrices.
@ Unsymmetric
No symmetry.
@ Upper
Symmetric, upper-triangular part is stored.
typename Conf::real_t real_t
typename Conf::index_t index_t
Sparsity< Conf > convert_csc(const auto &sp, sparsity::Symmetry symmetry)
typename Conf::length_t length_t
typename Conf::cmvec cmvec
typename Conf::crvec crvec
std::map< std::string, std::string > functions
std::map< std::string, casadi::Function > functions
std::optional< CasADiFunctionEvaluator< Conf, 5, 1 > > hess_L_prod
std::optional< CasADiFunctionEvaluator< Conf, 7, 1 > > hess_ψ
static std::unique_ptr< CasADiFunctionsWithParam > load(Loader &&loader)
std::optional< CasADiFunctionEvaluator< Conf, 6, 2 > > ψ_grad_ψ
std::optional< CasADiFunctionEvaluator< Conf, 4, 1 > > hess_L
CasADiFunctionEvaluator< Conf, 2, 1 > f
std::optional< CasADiFunctionEvaluator< Conf, 8, 1 > > hess_ψ_prod
std::optional< CasADiFunctionEvaluator< Conf, 3, 1 > > grad_L
std::optional< CasADiFunctionEvaluator< Conf, 2, 1 > > jac_g
std::optional< CasADiFunctionEvaluator< Conf, 3, 1 > > grad_g_prod
std::optional< CasADiFunctionEvaluator< Conf, 2, 1 > > g
std::optional< CasADiFunctionEvaluator< Conf, 6, 2 > > ψ
CasADiFunctionEvaluator< Conf, 2, 2 > f_grad_f
Sparse compressed-column structure (CCS or CSC).
Stores any of the supported sparsity patterns.