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 guanaqo::DynamicLoadFlags flags;
45 set_params(flags,
"dl_flags", opts);
53 set_params(x0,
"x0", opts);
56 set_params(y0,
"mul_g0", opts);
59 set_params(w0,
"mul_x0", opts);
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,
129 alpaqa::Options &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,
164 alpaqa::Options &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();
203 std::vector<std::string_view> prob_opts;
204 std::string_view prob_prefix =
"problem.";
206 auto used = opts.
used();
207 for (
auto opt = options.begin(); opt != options.end(); ++opt) {
208 if (opt->starts_with(prob_prefix)) {
209 prob_opts.push_back(opt->substr(prob_prefix.size()));
210 ++used.begin()[opt - options.begin()];
214 if (type ==
"dl" || type.empty()) {
216 return load_dl_problem(file, prob_opts, opts);
218 throw std::logic_error(
"This version of alpaqa was compiled without "
219 "support for dynamic problem loading");
221 }
else if (type ==
"cs") {
222#if ALPAQA_WITH_CASADI
223 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
224 return load_cs_problem(file, prob_opts, opts);
226 throw std::logic_error(
"CasADi only supports double precision.");
228 throw std::logic_error(
229 "This version of alpaqa was compiled without CasADi support");
231 }
else if (type ==
"cu") {
232#ifdef ALPAQA_WITH_CUTEST
233 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
234 return load_cu_problem(file, prob_opts, opts);
236 throw std::logic_error(
"CUTEst only supports double precision.");
238 throw std::logic_error(
239 "This version of alpaqa was compiled without CUTEst support");
242 throw std::invalid_argument(
"Unknown problem type '" + std::string(type) +
248 os <<
"Loaded problem \"" << problem.
name <<
"\"\n"
254 os <<
"Nonzeros in Jg: " << *problem.
nnz_jac_g <<
"\n";
256 os <<
"Nonzeros in ∇²L: " << *problem.
nnz_hess_L <<
"\n";
258 os <<
"Nonzeros in ∇²ψ: " << *problem.
nnz_hess_ψ <<
"\n";
260 os <<
"Box constraints:"
267 os <<
"General constraints:"
274 os <<
"Provided functions:\n";
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 print_provided_functions(std::ostream &os, const TypeErasedProblem< Conf > &problem)
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
vec initial_guess_y
Multipliers g.
PROBLEM_LOADER_EXPORT LoadedProblem load_problem(std::string_view type, const fs::path &file, Options &opts)
vec initial_guess_x
Unknowns.
vec initial_guess_w
Multipliers bounds.
std::optional< length_t > nnz_hess_L
std::optional< ConstrCount > general_constr_count
EigenConfigd DefaultConfig
std::optional< length_t > nnz_hess_ψ
alpaqa::TypeErasedProblem< config_t > problem
std::optional< ConstrCount > box_constr_count
std::optional< length_t > nnz_jac_g
PROBLEM_LOADER_EXPORT void print_problem_description(std::ostream &os, LoadedProblem &problem, bool show_funcs=true)
guanaqo::DynamicLoadFlags get_dl_flags(alpaqa::Options &opts)
vec initial_guess_y
Multipliers g.
vec initial_guess_x
Unknowns.
std::string get_reg_name_option(std::span< const std::string_view > prob_opts)
void count_problem(LoadedProblem &p)
void load_initial_guess(alpaqa::Options &opts, LoadedProblem &problem)
alpaqa::TypeErasedProblem< config_t > problem
void count_constr(alpaqa::ConstrCount &cnt, const alpaqa::Box< config_t > &C)
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.