1#include <alpaqa/export.h>
6#include <guanaqo/dl-flags.hpp>
13#ifdef ALPAQA_WITH_CUTEST
23namespace fs = std::filesystem;
33 std::string name =
"register_alpaqa_problem";
34 std::string_view name_key =
"register=";
35 auto name_it = std::find_if(
36 prob_opts.rbegin(), prob_opts.rend(),
37 [&](std::string_view opt) { return opt.starts_with(name_key); });
38 if (name_it != prob_opts.rend())
39 name = name_it->substr(name_key.size());
44 alpaqa::DynamicLoadFlags flags;
65 const auto n = C.
lower.size();
70 for (index_t i = 0; i < n; ++i) {
103 std::span<std::string_view> prob_opts,
108 auto register_name = get_reg_name_option(prob_opts);
109 auto flags = get_dl_flags(opts);
111 .problem = TEProblem::make<CntProblem>(std::in_place, full_path,
112 register_name, prob_opts, flags),
113 .abs_path = fs::absolute(full_path),
116 auto &cnt_problem = problem.
problem.as<CntProblem>();
117 problem.
name = cnt_problem.problem.get_name();
125#if ALPAQA_WITH_CASADI
126template <
bool = true>
127LoadedProblem load_cs_problem(
const fs::path &full_path,
128 std::span<std::string_view> prob_opts,
130 static std::mutex mtx;
131 std::unique_lock lck{mtx};
132 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
133 using CsProblem = alpaqa::CasADiProblem<config_t>;
134 using CntProblem = alpaqa::ProblemWithCounters<CsProblem>;
136 LoadedProblem problem{
137 .problem = TEProblem::make<CntProblem>(
138 std::in_place, full_path.string().c_str(), flags),
139 .abs_path = fs::absolute(full_path),
143 auto &cnt_problem = problem.
problem.as<CntProblem>();
144 auto &cs_problem = cnt_problem.problem;
145 problem.
name = cs_problem.get_name();
147 auto param_size = cs_problem.param.size();
149 if (cs_problem.param.size() != param_size)
150 throw alpaqa::params::invalid_param(
151 "Incorrect problem parameter size (expected " +
152 std::to_string(param_size) +
", but got " +
153 std::to_string(cs_problem.param.size()) +
")");
160#ifdef ALPAQA_WITH_CUTEST
161template <
bool = true>
162LoadedProblem load_cu_problem(
const fs::path &full_path,
163 std::span<std::string_view> prob_opts,
165 std::string outsdif_path;
169 static std::mutex mtx;
170 std::unique_lock lck{mtx};
171 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
172 using CuProblem = alpaqa::CUTEstProblem;
173 using CntProblem = alpaqa::ProblemWithCounters<CuProblem>;
175 LoadedProblem problem{
177 TEProblem::make<CntProblem>(std::in_place, full_path.c_str(),
178 outsdif_path.c_str(), sparse, flags),
179 .abs_path = fs::absolute(full_path),
183 auto &cnt_problem = problem.
problem.as<CntProblem>();
184 auto &cu_problem = cnt_problem.problem;
185 problem.
name = cu_problem.get_name();
198 const fs::path &file,
Options &opts) {
201 std::vector<std::string_view> prob_opts;
202 std::string_view prob_prefix =
"problem.";
204 auto used = opts.
used();
205 for (
auto opt = options.begin(); opt != options.end(); ++opt) {
206 if (opt->starts_with(prob_prefix)) {
207 prob_opts.push_back(opt->substr(prob_prefix.size()));
208 ++used.begin()[opt - options.begin()];
212 auto full_path = dir / file;
213 if (type ==
"dl" || type.empty()) {
215 return load_dl_problem(full_path, prob_opts, opts);
217 throw std::logic_error(
"This version of alpaqa was compiled without "
218 "support for dynamic problem loading");
220 }
else if (type ==
"cs") {
221#if ALPAQA_WITH_CASADI
222 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
223 return load_cs_problem(full_path, prob_opts, opts);
225 throw std::logic_error(
"CasADi only supports double precision.");
227 throw std::logic_error(
228 "This version of alpaqa was compiled without CasADi support");
230 }
else if (type ==
"cu") {
231#ifdef ALPAQA_WITH_CUTEST
232 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
233 return load_cu_problem(full_path, prob_opts, opts);
235 throw std::logic_error(
"CUTEst only supports double precision.");
237 throw std::logic_error(
238 "This version of alpaqa was compiled without CUTEst support");
241 throw std::invalid_argument(
"Unknown problem type '" + std::string(type) +
std::span< unsigned > used()
std::span< const std::string_view > options() const
The main polymorphic minimization problem interface.
Sparsity get_lagrangian_hessian_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Hessian of the Lagrangian.
const Box & get_variable_bounds() const
[Optional] Get the rectangular constraint set of the decision variables, .
length_t get_num_constraints() const
[Required] Number of constraints.
bool provides_get_lagrangian_hessian_sparsity() const
Returns true if the problem provides an implementation of get_lagrangian_hessian_sparsity.
bool provides_get_constraints_jacobian_sparsity() const
Returns true if the problem provides an implementation of get_constraints_jacobian_sparsity.
length_t get_num_variables() const
[Required] Number of decision variables.
Sparsity get_constraints_jacobian_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Jacobian of the constraints.
const Box & get_general_bounds() const
[Optional] Get the rectangular constraint set of the general constraint function, .
bool provides_get_variable_bounds() const
Returns true if the problem provides an implementation of get_variable_bounds.
bool provides_get_general_bounds() const
Returns true if the problem provides an implementation of get_general_bounds.
bool provides_get_augmented_lagrangian_hessian_sparsity() const
Returns true if the problem provides an implementation of get_augmented_lagrangian_hessian_sparsity.
Sparsity get_augmented_lagrangian_hessian_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Hessian of the augmented Lag...
Class that loads a problem using dlopen.
#define USING_ALPAQA_CONFIG(Conf)
void set_params(T &t, std::string_view prefix, std::span< const std::string_view > options, std::optional< std::span< unsigned > > used=std::nullopt)
Overwrites t based on the options that start with prefix.
std::optional< vec > value
EigenConfigd DefaultConfig
void count_constr(ConstrCount &cnt, const alpaqa::Box< config_t > &C)
alpaqa::DynamicLoadFlags get_dl_flags(Options &opts)
std::string get_reg_name_option(std::span< const std::string_view > prob_opts)
void count_problem(LoadedProblem &p)
void load_initial_guess(Options &opts, LoadedProblem &problem)
void set_params(T &t, std::string_view prefix, Options &opts)
LoadedProblem load_problem(std::string_view type, const fs::path &dir, const fs::path &file, Options &opts)
vec initial_guess_y
Multipliers g.
vec initial_guess_x
Unknowns.
vec initial_guess_w
Multipliers bounds.
std::optional< length_t > nnz_hess_L
std::optional< ConstrCount > general_constr_count
std::optional< length_t > nnz_hess_ψ
alpaqa::TypeErasedProblem< config_t > problem
std::optional< ConstrCount > box_constr_count
std::optional< length_t > nnz_jac_g
std::shared_ptr< alpaqa::EvalCounter > evaluations
length_t ub
Number of variables with only upper bound.
length_t eq
Number of variables with equal bounds.
length_t lb
Number of variables with only lower bound.
length_t lbub
Number of variables with both bounds.
Problem wrapper that keeps track of the number of evaluations and the run time of each function.