21namespace fs = std::filesystem;
27std::string get_prefix_option(std::span<const std::string_view> prob_opts) {
28 std::string prefix =
"benchmark_problem";
29 std::string_view prefix_key =
"prefix=";
30 auto prefix_it = std::find_if(
31 prob_opts.rbegin(), prob_opts.rend(),
32 [&](std::string_view opt) { return opt.starts_with(prefix_key); });
33 if (prefix_it != prob_opts.rend())
34 prefix = prefix_it->substr(prefix_key.size());
40 std::optional<vec> value = std::nullopt;
44 if (s.
value.starts_with(
'@')) {
45 fs::path fpath = s.
value.substr(1);
46 std::ifstream f(fpath);
48 throw std::invalid_argument(
"Unable to open file '" +
49 fpath.string() +
"' in '" +
54 throw std::invalid_argument(
55 "Unable to read from file '" + fpath.string() +
"' in '" +
56 std::string(s.
full_key) +
"': " + e.what());
60 if (v.value->size() != v.expected_size) {
61 throw std::invalid_argument(
62 "Incorrect size in '" + std::string(s.
full_key) +
"' (got " +
63 std::to_string(v.value->size()) +
", expected " +
64 std::to_string(v.expected_size) +
')');
77 load_file_vec_opt(v, s);
84 FileVec x0{n}, y0{m}, w0{2 * n};
98 const fs::path &file,
Options &opts) {
101 std::vector<std::string_view> prob_opts;
102 std::string_view prob_prefix =
"problem.";
104 auto used = opts.
used();
105 for (
auto opt = options.begin(); opt != options.end(); ++opt) {
106 if (opt->starts_with(prob_prefix)) {
107 prob_opts.push_back(opt->substr(prob_prefix.size()));
108 used.begin()[opt - options.begin()] =
true;
112 auto full_path = dir / file;
113 if (type ==
"dl" || type.empty()) {
117 auto prefix = get_prefix_option(prob_opts);
118 std::any dl_opt = std::span{prob_opts};
120 .
problem = TEProblem::make<CntProblem>(
121 std::in_place, full_path.c_str(), prefix, &dl_opt),
122 .abs_path = fs::absolute(full_path),
125 auto &cnt_problem = problem.
problem.
as<CntProblem>();
127 load_initial_guess(opts, problem);
129 }
else if (type ==
"cs") {
130#if ALPAQA_HAVE_CASADI
131 static std::mutex mtx;
132 std::unique_lock lck{mtx};
138 TEProblem::make<CntProblem>(std::in_place, full_path.c_str()),
139 .abs_path = fs::absolute(full_path),
143 auto &cnt_problem = problem.
problem.
as<CntProblem>();
144 auto &cs_problem = cnt_problem.problem;
146 auto param_size = cs_problem.param.size();
148 if (cs_problem.param.size() != param_size)
150 "Incorrect problem parameter size: got " +
151 std::to_string(cs_problem.param.size()) +
", should be " +
152 std::to_string(param_size));
153 load_initial_guess(opts, problem);
156 throw std::logic_error(
157 "This version of alpaqa was compiled without CasADi support");
160 throw std::invalid_argument(
"Unknown problem type '" + std::string(type) +
std::span< const std::string_view > options() const
Problem definition for a CasADi problem, loaded from a DLL.
length_t get_n() const
[Required] Number of decision variables.
length_t get_m() const
[Required] Number of constraints.
Class that loads a problem using dlopen.
T & as() &
Convert the type-erased object to the given type.
#define USING_ALPAQA_CONFIG(Conf)
void read_row(std::istream &is, Eigen::Ref< Eigen::VectorX< float > > v, char sep)
void set_param(bool &b, ParamString s)
void set_params(T &t, std::string_view prefix, std::span< const std::string_view > options, std::optional< std::span< bool > > used=std::nullopt)
Overwrites t based on the options that start with prefix.
std::string_view key
The subkey to resolve next.
std::string_view full_key
Full key string, used for diagnostics.
std::string_view value
The value of the parameter to store.
Represents a parameter value encoded as a string in the format abc.def.key=value.
typename Conf::length_t length_t
decltype(auto) 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
Unknowns.
vec initial_guess_w
Multipliers g.
alpaqa::TypeErasedProblem< config_t > problem
std::shared_ptr< alpaqa::EvalCounter > evaluations
Problem wrapper that keeps track of the number of evaluations and the run time of each function.
Custom parameter parsing exception.