alpaqa 1.0.0a15
Nonconvex constrained optimization
Loading...
Searching...
No Matches
problem.cpp
Go to the documentation of this file.
6#if ALPAQA_WITH_DL
8#endif
9#if ALPAQA_WITH_CASADI
11#endif
12#ifdef ALPAQA_WITH_CUTEST
14#endif
15
16#include <filesystem>
17#include <mutex>
18#include <optional>
19#include <span>
20#include <stdexcept>
21#include <string>
22
23#include "options.hpp"
24#include "problem.hpp"
25
26namespace fs = std::filesystem;
27
28namespace {
29
31
32std::string get_reg_name_option(std::span<const std::string_view> prob_opts) {
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());
40 return name;
41}
42
44 const auto n = problem.problem.get_n(), m = problem.problem.get_m();
45 alpaqa::params::vec_from_file<config_t> x0{n}, y0{m}, w0{2 * n};
46 set_params(x0, "x0", opts);
47 if (x0.value)
48 problem.initial_guess_x = std::move(*x0.value);
49 set_params(y0, "mul_g0", opts);
50 if (y0.value)
51 problem.initial_guess_y = std::move(*y0.value);
52 set_params(w0, "mul_x0", opts);
53 if (w0.value)
54 problem.initial_guess_w = std::move(*w0.value);
55}
56
58 const auto n = C.lowerbound.size();
59 cnt.lb = 0;
60 cnt.ub = 0;
61 cnt.lbub = 0;
62 cnt.eq = 0;
63 for (index_t i = 0; i < n; ++i) {
64 bool lb = C.lowerbound(i) > -alpaqa::inf<config_t>;
65 bool ub = C.upperbound(i) < +alpaqa::inf<config_t>;
66 bool eq = C.lowerbound(i) == C.upperbound(i);
67 if (eq)
68 ++cnt.eq;
69 else if (lb && ub)
70 ++cnt.lbub;
71 else if (lb)
72 ++cnt.lb;
73 else if (ub)
74 ++cnt.ub;
75 }
76}
77
90
91#if ALPAQA_WITH_DL
92LoadedProblem load_dl_problem(const fs::path &full_path,
93 std::span<std::string_view> prob_opts,
94 Options &opts) {
96 using DLProblem = alpaqa::dl::DLProblem;
98 auto register_name = get_reg_name_option(prob_opts);
99 std::any dl_opt = prob_opts;
100 LoadedProblem problem{
101 .problem = TEProblem::make<CntProblem>(std::in_place, full_path.c_str(),
102 register_name, &dl_opt),
103 .abs_path = fs::absolute(full_path),
104 .path = full_path,
105 };
106 auto &cnt_problem = problem.problem.as<CntProblem>();
107 try {
108 using sig_t = std::string(const DLProblem::instance_t *);
109 problem.name = cnt_problem.problem.call_extra_func<sig_t>("get_name");
110 } catch (std::out_of_range &) {
111 problem.name = problem.path.filename();
112 }
113 problem.evaluations = cnt_problem.evaluations;
114 load_initial_guess(opts, problem);
115 count_problem(problem);
116 return problem;
117}
118#endif
119
120#if ALPAQA_WITH_CASADI
121template <bool = true>
122LoadedProblem load_cs_problem(const fs::path &full_path,
123 std::span<std::string_view> prob_opts,
124 Options &opts) {
125 static std::mutex mtx;
126 std::unique_lock lck{mtx};
127 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
128 using CsProblem = alpaqa::CasADiProblem<config_t>;
130 LoadedProblem problem{
131 .problem = TEProblem::make<CntProblem>(std::in_place,
132 full_path.string().c_str()),
133 .abs_path = fs::absolute(full_path),
134 .path = full_path,
135 };
136 lck.unlock();
137 auto &cnt_problem = problem.problem.as<CntProblem>();
138 auto &cs_problem = cnt_problem.problem;
139 problem.name = problem.path.filename().string();
140 problem.evaluations = cnt_problem.evaluations;
141 auto param_size = cs_problem.param.size();
142 alpaqa::params::set_params(cs_problem.param, "param", prob_opts);
143 if (cs_problem.param.size() != param_size)
145 "Incorrect problem parameter size (expected " +
146 std::to_string(param_size) + ", but got " +
147 std::to_string(cs_problem.param.size()) + ")");
148 load_initial_guess(opts, problem);
149 count_problem(problem);
150 return problem;
151}
152#endif
153
154#ifdef ALPAQA_WITH_CUTEST
155template <bool = true>
156LoadedProblem load_cu_problem(const fs::path &full_path,
157 std::span<std::string_view> prob_opts,
158 Options &opts) {
159 std::string outsdif_path;
160 alpaqa::params::set_params(outsdif_path, "outsdif", prob_opts);
161 bool sparse = false;
162 alpaqa::params::set_params(sparse, "sparse", prob_opts);
163 static std::mutex mtx;
164 std::unique_lock lck{mtx};
165 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
166 using CuProblem = alpaqa::CUTEstProblem;
168 LoadedProblem problem{
169 .problem = TEProblem::make<CntProblem>(std::in_place, full_path.c_str(),
170 outsdif_path.c_str(), sparse),
171 .abs_path = fs::absolute(full_path),
172 .path = full_path,
173 };
174 lck.unlock();
175 auto &cnt_problem = problem.problem.as<CntProblem>();
176 auto &cu_problem = cnt_problem.problem;
177 problem.name = cu_problem.name;
178 problem.evaluations = cnt_problem.evaluations;
179 problem.initial_guess_x = std::move(cu_problem.x0);
180 problem.initial_guess_y = std::move(cu_problem.y0);
181 load_initial_guess(opts, problem);
182 count_problem(problem);
183 return problem;
184}
185#endif
186
187} // namespace
188
189LoadedProblem load_problem(std::string_view type, const fs::path &dir,
190 const fs::path &file, Options &opts) {
192 // Isolate problem-specific options
193 std::vector<std::string_view> prob_opts;
194 std::string_view prob_prefix = "problem.";
195 auto options = opts.options();
196 auto used = opts.used();
197 for (auto opt = options.begin(); opt != options.end(); ++opt) {
198 if (opt->starts_with(prob_prefix)) {
199 prob_opts.push_back(opt->substr(prob_prefix.size()));
200 ++used.begin()[opt - options.begin()];
201 }
202 }
203 // Load problem
204 auto full_path = dir / file;
205 if (type == "dl" || type.empty()) {
206#if ALPAQA_WITH_DL
207 return load_dl_problem(full_path, prob_opts, opts);
208#else
209 throw std::logic_error("This version of alpaqa was compiled without "
210 "support for dynamic problem loading");
211#endif
212 } else if (type == "cs") {
213#if ALPAQA_WITH_CASADI
214 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
215 return load_cs_problem(full_path, prob_opts, opts);
216 else
217 throw std::logic_error("CasADi only supports double precision.");
218#else
219 throw std::logic_error(
220 "This version of alpaqa was compiled without CasADi support");
221#endif
222 } else if (type == "cu") {
223#ifdef ALPAQA_WITH_CUTEST
224 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
225 return load_cu_problem(full_path, prob_opts, opts);
226 else
227 throw std::logic_error("CUTEst only supports double precision.");
228#else
229 throw std::logic_error(
230 "This version of alpaqa was compiled without CUTEst support");
231#endif
232 }
233 throw std::invalid_argument("Unknown problem type '" + std::string(type) +
234 "'");
235}
std::span< unsigned > used()
Definition options.hpp:61
std::span< const std::string_view > options() const
Definition options.hpp:58
Wrapper for CUTEst problems loaded from an external shared library.
Problem definition for a CasADi problem, loaded from a DLL.
The main polymorphic minimization problem interface.
bool provides_get_hess_L_sparsity() const
Returns true if the problem provides an implementation of get_hess_L_sparsity.
const Box & get_box_D() const
[Optional] Get the rectangular constraint set of the general constraint function, .
bool provides_get_box_C() const
Returns true if the problem provides an implementation of get_box_C.
Sparsity get_jac_g_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Jacobian of the constraints.
Sparsity get_hess_ψ_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Hessian of the augmented Lag...
length_t get_n() const
[Required] Number of decision variables.
Sparsity get_hess_L_sparsity() const
[Optional] Function that returns (a view of) the sparsity pattern of the Hessian of the Lagrangian.
length_t get_m() const
[Required] Number of constraints.
bool provides_get_hess_ψ_sparsity() const
Returns true if the problem provides an implementation of get_hess_ψ_sparsity.
bool provides_get_jac_g_sparsity() const
Returns true if the problem provides an implementation of get_jac_g_sparsity.
bool provides_get_box_D() const
Returns true if the problem provides an implementation of get_box_D.
const Box & get_box_C() const
[Optional] Get the rectangular constraint set of the decision variables, .
Class that loads a problem using dlopen.
T & as() &
Convert the type-erased object to the given type.
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:56
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.
Definition params.hpp:49
constexpr const auto inf
Definition config.hpp:85
void count_constr(ConstrCount &cnt, const alpaqa::Box< config_t > &C)
Definition problem.cpp:57
std::string get_reg_name_option(std::span< const std::string_view > prob_opts)
Definition problem.cpp:32
void count_problem(LoadedProblem &p)
Definition problem.cpp:78
void load_initial_guess(Options &opts, LoadedProblem &problem)
Definition problem.cpp:43
decltype(auto) set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:86
LoadedProblem load_problem(std::string_view type, const fs::path &dir, const fs::path &file, Options &opts)
Definition problem.cpp:189
vec initial_guess_y
Multipliers g.
Definition problem.hpp:31
vec initial_guess_x
Unknowns.
Definition problem.hpp:30
vec initial_guess_w
Multipliers bounds.
Definition problem.hpp:32
std::optional< length_t > nnz_hess_L
Definition problem.hpp:35
std::optional< ConstrCount > general_constr_count
Definition problem.hpp:34
std::optional< length_t > nnz_hess_ψ
Definition problem.hpp:36
alpaqa::TypeErasedProblem< config_t > problem
Definition problem.hpp:25
std::optional< ConstrCount > box_constr_count
Definition problem.hpp:33
std::optional< length_t > nnz_jac_g
Definition problem.hpp:35
length_t ub
Number of variables with only upper bound.
Definition problem.hpp:18
length_t eq
Number of variables with equal bounds.
Definition problem.hpp:20
length_t lb
Number of variables with only lower bound.
Definition problem.hpp:17
length_t lbub
Number of variables with both bounds.
Definition problem.hpp:19
Double-precision double configuration.
Definition config.hpp:135
Problem wrapper that keeps track of the number of evaluations and the run time of each function.
Custom parameter parsing exception.
Definition params.hpp:26