8#include <guanaqo/io/csv.hpp>
9#include <guanaqo/not-implemented.hpp>
12#if ALPAQA_WITH_EXTERNAL_CASADI
13#include <casadi/core/external.hpp>
26namespace fs = std::filesystem;
36 std::optional<CasADiFunctionEvaluator<Conf, 2, 1>>
g = std::nullopt;
37 std::optional<CasADiFunctionEvaluator<Conf, 3, 1>>
grad_g_prod =
39 std::optional<CasADiFunctionEvaluator<Conf, 2, 1>>
jac_g = std::nullopt;
40 std::optional<CasADiFunctionEvaluator<Conf, 3, 1>>
grad_L = std::nullopt;
41 std::optional<CasADiFunctionEvaluator<Conf, 5, 1>>
hess_L_prod =
43 std::optional<CasADiFunctionEvaluator<Conf, 4, 1>>
hess_L = std::nullopt;
44 std::optional<CasADiFunctionEvaluator<Conf, 6, 2>>
ψ = std::nullopt;
45 std::optional<CasADiFunctionEvaluator<Conf, 6, 2>>
ψ_grad_ψ = std::nullopt;
46 std::optional<CasADiFunctionEvaluator<Conf, 8, 1>>
hess_ψ_prod =
48 std::optional<CasADiFunctionEvaluator<Conf, 7, 1>>
hess_ψ = std::nullopt;
50 template <
class Loader>
51 requires requires(Loader &&loader,
const char *name) {
52 { loader(name) } -> std::same_as<casadi::Function>;
53 { loader.format_name(name) } -> std::same_as<std::string>;
55 static std::unique_ptr<CasADiFunctionsWithParam>
load(Loader &&loader) {
59 auto gfun = loader(
"g");
60 using namespace std::literals::string_literals;
63 "Invalid number of input arguments: got "s +
64 std::to_string(gfun.n_in()) +
", should be 2.");
67 "Invalid number of output arguments: got "s +
68 std::to_string(gfun.n_in()) +
", should be 0 or 1.");
69 if (gfun.size2_in(0) != 1)
71 "First input argument should be a column vector.");
72 if (gfun.size2_in(1) != 1)
74 "Second input argument should be a column vector.");
75 if (gfun.n_out() == 1 && gfun.size2_out(0) != 1)
77 "First output argument should be a column vector.");
78 n =
static_cast<length_t>(gfun.size1_in(0));
79 if (gfun.n_out() == 1)
80 m =
static_cast<length_t>(gfun.size1_out(0));
81 p =
static_cast<length_t>(gfun.size1_in(1));
82 if (gfun.n_out() == 0) {
85 "Function g has no outputs but m != 0");
90 return std::make_optional(std::move(
g));
95 return std::make_unique<CasADiFunctionsWithParam>(
116 loader,
"psi",
dims(
n,
p,
m,
m,
m,
m),
dims(1,
m)),
118 loader,
"psi_grad_psi",
dims(
n,
p,
m,
m,
m,
m),
dims(1,
n)),
120 loader,
"hess_psi_prod",
dims(
n,
p,
m,
m, 1,
m,
m,
n),
123 loader,
"hess_psi",
dims(
n,
p,
m,
m, 1,
m,
m),
133template <Config Conf>
139template <Config Conf>
141 DynamicLoadFlags dl_flags)
145 const std::string &filename;
146 DynamicLoadFlags dl_flags;
147 auto operator()(
const std::string &name)
const {
148#if ALPAQA_WITH_EXTERNAL_CASADI
154 auto format_name(
const std::string &name)
const {
155 return filename +
':' + name;
157 } loader{filename, dl_flags};
160 this->num_variables = impl->n;
161 this->num_constraints = impl->m;
166 auto data_filepath = fs::path{filename}.replace_extension(
"csv");
167 if (fs::exists(data_filepath))
168 load_numerical_data(data_filepath);
171template <Config Conf>
174#if ALPAQA_WITH_EXTERNAL_CASADI
177 auto operator()(
const std::string &name)
const {
178 return casadi::Function::deserialize(
functions.functions.at(name));
180 auto format_name(
const std::string &name)
const {
181 return "SerializedCasADiFunctions['" + name +
"']";
186 this->num_variables = impl->n;
187 this->num_constraints = impl->m;
193 throw std::runtime_error(
194 "This version of alpaqa was compiled without the CasADi C++ library");
198template <Config Conf>
204 auto operator()(
const std::string &name)
const {
207 auto format_name(
const std::string &name)
const {
208 return "CasADiFunctions['" + name +
"']";
213 this->num_variables = impl->n;
214 this->num_constraints = impl->m;
220template <Config Conf>
222 const std::filesystem::path &filepath,
char sep) {
224 std::ifstream data_file{filepath};
226 throw std::runtime_error(
"Unable to open data file \"" +
227 filepath.string() +
'"');
231 auto wrap_data_load = [&](std::string_view
name,
auto &v,
bool fixed_size) {
232 using namespace guanaqo::io;
235 if (data_file.peek() ==
'\n')
236 return static_cast<void>(data_file.get());
238 csv_read_row(data_file,
as_span(v), sep);
240 auto s = csv_read_row_std_vector<real_t>(data_file, sep);
243 }
catch (csv_read_error &e) {
245 throw std::runtime_error(
"Unable to read " + std::string(
name) +
246 " from data file \"" + filepath.string() +
247 ':' + std::to_string(line) +
252 auto read_single = [&](std::string_view
name,
auto &v) {
255 throw std::runtime_error(
"Unable to read " + std::string(
name) +
256 " from data file \"" + filepath.string() +
257 ':' + std::to_string(line) +
'"');
260 wrap_data_load(
"variable_bounds.lower", this->
variable_bounds.lower,
true);
261 wrap_data_load(
"variable_bounds.upper", this->
variable_bounds.upper,
true);
262 wrap_data_load(
"general_bounds.lower", this->
general_bounds.lower,
true);
263 wrap_data_load(
"general_bounds.upper", this->
general_bounds.upper,
true);
264 wrap_data_load(
"param", this->
param,
true);
265 wrap_data_load(
"l1_reg", this->
l1_reg,
false);
272template <Config Conf>
274template <Config Conf>
277template <Config Conf>
293template <Config Conf>
296 impl->f_grad_f({x.data(),
param.data()}, {&f, grad_fx.data()});
299template <Config Conf>
303 impl->f_grad_f({x.data(),
param.data()}, {&f, grad_fx.data()});
307template <Config Conf>
312 (*
impl->g)({x.data(),
param.data()}, {g.data()});
314 throw not_implemented_error(
"CasADiProblem::eval_constraints");
317template <Config Conf>
324 if (
impl->grad_g_prod)
325 (*
impl->grad_g_prod)({x.data(),
param.data(), y.data()}, {gxy.data()});
327 throw not_implemented_error(
328 "CasADiProblem::eval_constraints_gradient_product");
331template <Config Conf>
335 impl->grad_ψ({x.data(),
param.data(), y.data(), Σ.data(),
336 this->D.lower.data(), this->D.upper.data()},
345template <Config Conf>
352 throw std::logic_error(
353 "CasADiProblem::eval_augmented_lagrangian_and_gradient");
355 (*
impl->ψ_grad_ψ)({x.data(),
param.data(), y.data(), Σ.data(),
358 {&ψ, grad_ψ.data()});
362template <Config Conf>
366 throw std::logic_error(
"CasADiProblem::eval_lagrangian_gradient");
367 (*
impl->grad_L)({x.data(),
param.data(), y.data()}, {grad_L.data()});
370template <Config Conf>
375 throw std::logic_error(
"CasADiProblem::eval_augmented_lagrangian");
377 (*
impl->ψ)({x.data(),
param.data(), y.data(), Σ.data(),
384template <Config Conf>
386 throw not_implemented_error(
"CasADiProblem::eval_grad_gi");
389template <Config Conf>
390Sparsity
convert_csc(
const auto &sp, sparsity::Symmetry symmetry) {
392 using SparseCSC = sparsity::SparseCSC<casadi_int, casadi_int>;
395 .rows =
static_cast<index_t>(sp.size1()),
396 .cols =
static_cast<index_t>(sp.size2()),
397 .symmetry = symmetry,
398 .inner_idx = span{sp.row(),
static_cast<size_t>(sp.nnz())},
399 .outer_ptr = span{sp.colind(),
static_cast<size_t>(sp.size2()) + 1},
400 .order = SparseCSC::SortedRows,
404template <Config Conf>
407 sparsity::Dense dense{
410 .symmetry = sparsity::Symmetry::Unsymmetric,
412 if (!
impl->jac_g.has_value())
414 const auto &sp =
impl->jac_g->fun.sparsity_out(0);
420template <Config Conf>
422 rvec J_values)
const {
424 throw std::logic_error(
"CasADiProblem::eval_constraints_jacobian");
425 (*
impl->jac_g)({x.data(),
param.data()}, {J_values.data()});
428template <Config Conf>
432 if (!
impl->hess_L_prod)
433 throw std::logic_error(
"CasADiProblem::eval_augmented_lagrangian");
434 (*
impl->hess_L_prod)({x.data(),
param.data(), y.data(), &scale, v.data()},
438template <Config Conf>
440 sparsity::Dense dense{
443 .symmetry = sparsity::Symmetry::Upper,
445 if (!
impl->hess_L.has_value())
447 const auto &sp =
impl->hess_L->fun.sparsity_out(0);
448 return sp.is_dense() ? Sparsity{dense}
452template <Config Conf>
455 rvec H_values)
const {
457 throw std::logic_error(
"CasADiProblem::eval_lagrangian_hessian");
458 (*
impl->hess_L)({x.data(),
param.data(), y.data(), &scale},
462template <Config Conf>
465 if (!
impl->hess_ψ_prod)
466 throw std::logic_error(
467 "CasADiProblem::eval_augmented_lagrangian_hessian_product");
468 (*
impl->hess_ψ_prod)({x.data(),
param.data(), y.data(), Σ.data(), &scale,
474template <Config Conf>
477 sparsity::Dense dense{
480 .symmetry = sparsity::Symmetry::Upper,
482 if (!
impl->hess_ψ.has_value())
484 const auto &sp =
impl->hess_ψ->fun.sparsity_out(0);
485 return sp.is_dense() ? Sparsity{dense}
489template <Config Conf>
493 throw std::logic_error(
494 "CasADiProblem::eval_augmented_lagrangian_hessian");
495 (*
impl->hess_ψ)({x.data(),
param.data(), y.data(), Σ.data(), &scale,
501template <Config Conf>
505template <Config Conf>
507 return impl->ψ.has_value();
509template <Config Conf>
511 return impl->ψ_grad_ψ.has_value();
513template <Config Conf>
516 return impl->ψ_grad_ψ.has_value();
518template <Config Conf>
520 return impl->grad_L.has_value();
522template <Config Conf>
524 return impl->jac_g.has_value();
526template <Config Conf>
528 return impl->hess_L_prod.has_value();
530template <Config Conf>
532 return impl->hess_L.has_value();
534template <Config Conf>
537 return impl->hess_ψ_prod.has_value();
539template <Config Conf>
541 return impl->hess_ψ.has_value();
544template <Config Conf>
#define BEGIN_ALPAQA_CASADI_LOADER_NAMESPACE
#define END_ALPAQA_CASADI_LOADER_NAMESPACE
BoxConstrProblem(length_t num_variables, length_t num_constraints)
index_t penalty_alm_split
Problem definition for a CasADi problem, loaded from a DLL.
std::string get_name() const
bool provides_eval_constraints_jacobian() const
Sparsity get_lagrangian_hessian_sparsity() const
void eval_lagrangian_hessian_product(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
guanaqo::copyable_unique_ptr< Functions > impl
void eval_lagrangian_hessian(crvec x, crvec y, real_t scale, rvec H_values) const
bool provides_eval_augmented_lagrangian() const
real_t eval_objective_and_gradient(crvec x, rvec grad_fx) const
real_t eval_augmented_lagrangian(crvec x, crvec y, crvec Σ, rvec ŷ) const
bool provides_eval_augmented_lagrangian_and_gradient() const
void eval_constraints_jacobian(crvec x, rvec J_values) 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 &)
real_t eval_objective(crvec x) const
void eval_augmented_lagrangian_hessian_product(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
bool provides_eval_lagrangian_hessian_product() const
void eval_lagrangian_gradient(crvec x, crvec y, rvec grad_L, rvec work_n) const
CasADiProblem(const std::string &filename, DynamicLoadFlags dl_flags={})
Load a problem generated by CasADi (with parameters).
real_t eval_augmented_lagrangian_and_gradient(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
Sparsity get_constraints_jacobian_sparsity() const
void eval_augmented_lagrangian_gradient(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
void eval_augmented_lagrangian_hessian(crvec x, crvec y, crvec Σ, real_t scale, rvec H_values) const
bool provides_eval_augmented_lagrangian_hessian() const
bool provides_eval_augmented_lagrangian_gradient() const
void eval_constraints(crvec x, rvec g) const
bool provides_eval_grad_gi() const
void eval_objective_gradient(crvec x, rvec grad_fx) const
bool provides_eval_augmented_lagrangian_hessian_product() const
void eval_grad_gi(crvec x, index_t i, rvec grad_i) const
bool provides_eval_lagrangian_hessian() const
void eval_constraints_gradient_product(crvec x, crvec y, rvec grad_gxy) const
Sparsity get_augmented_lagrangian_hessian_sparsity() const
bool provides_eval_lagrangian_gradient() const
Class for evaluating CasADi functions, allocating the necessary workspace storage in advance for allo...
#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)
Function external(const std::string &name, const std::string &bin_name, DynamicLoadFlags dl_flags)
Load the given CasADi function from the given DLL/SO file.
auto casadi_to_index(casadi_int i) -> index_t< Conf >
auto as_span(Eigen::DenseBase< Derived > &v)
Convert an Eigen vector view to a std::span.
Sparsity convert_csc(const auto &sp, sparsity::Symmetry symmetry)
typename Conf::real_t real_t
typename Conf::index_t index_t
auto as_vec(std::span< T, E > s)
Convert a std::span to an Eigen::Vector view.
typename Conf::length_t length_t
typename Conf::crvec crvec
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