alpaqa 1.0.0a18
Nonconvex constrained optimization
Loading...
Searching...
No Matches
problem.cpp
Go to the documentation of this file.
1#include <alpaqa/export.h>
7#if ALPAQA_WITH_DL
9#endif
10#if ALPAQA_WITH_CASADI
12#endif
13#ifdef ALPAQA_WITH_CUTEST
15#endif
16
17#include <filesystem>
18#include <mutex>
19#include <optional>
20#include <span>
21#include <stdexcept>
22#include <string>
23namespace fs = std::filesystem;
24
25#include "options.hpp"
26#include "problem.hpp"
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 LoadedProblem problem{
100 .problem = TEProblem::make<CntProblem>(std::in_place, full_path,
101 register_name, prob_opts),
102 .abs_path = fs::absolute(full_path),
103 .path = full_path,
104 };
105 auto &cnt_problem = problem.problem.as<CntProblem>();
106 problem.name = cnt_problem.problem.get_name();
107 problem.evaluations = cnt_problem.evaluations;
108 load_initial_guess(opts, problem);
109 count_problem(problem);
110 return problem;
111}
112#endif
113
114#if ALPAQA_WITH_CASADI
115template <bool = true>
116LoadedProblem load_cs_problem(const fs::path &full_path,
117 std::span<std::string_view> prob_opts,
118 Options &opts) {
119 static std::mutex mtx;
120 std::unique_lock lck{mtx};
121 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
122 using CsProblem = alpaqa::CasADiProblem<config_t>;
124 LoadedProblem problem{
125 .problem = TEProblem::make<CntProblem>(std::in_place,
126 full_path.string().c_str()),
127 .abs_path = fs::absolute(full_path),
128 .path = full_path,
129 };
130 lck.unlock();
131 auto &cnt_problem = problem.problem.as<CntProblem>();
132 auto &cs_problem = cnt_problem.problem;
133 problem.name = cs_problem.get_name();
134 problem.evaluations = cnt_problem.evaluations;
135 auto param_size = cs_problem.param.size();
136 alpaqa::params::set_params(cs_problem.param, "param", prob_opts);
137 if (cs_problem.param.size() != param_size)
139 "Incorrect problem parameter size (expected " +
140 std::to_string(param_size) + ", but got " +
141 std::to_string(cs_problem.param.size()) + ")");
142 load_initial_guess(opts, problem);
143 count_problem(problem);
144 return problem;
145}
146#endif
147
148#ifdef ALPAQA_WITH_CUTEST
149template <bool = true>
150LoadedProblem load_cu_problem(const fs::path &full_path,
151 std::span<std::string_view> prob_opts,
152 Options &opts) {
153 std::string outsdif_path;
154 alpaqa::params::set_params(outsdif_path, "outsdif", prob_opts);
155 bool sparse = false;
156 alpaqa::params::set_params(sparse, "sparse", prob_opts);
157 static std::mutex mtx;
158 std::unique_lock lck{mtx};
159 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
160 using CuProblem = alpaqa::CUTEstProblem;
162 LoadedProblem problem{
163 .problem = TEProblem::make<CntProblem>(std::in_place, full_path.c_str(),
164 outsdif_path.c_str(), sparse),
165 .abs_path = fs::absolute(full_path),
166 .path = full_path,
167 };
168 lck.unlock();
169 auto &cnt_problem = problem.problem.as<CntProblem>();
170 auto &cu_problem = cnt_problem.problem;
171 problem.name = cu_problem.get_name();
172 problem.evaluations = cnt_problem.evaluations;
173 problem.initial_guess_x = std::move(cu_problem.x0);
174 problem.initial_guess_y = std::move(cu_problem.y0);
175 load_initial_guess(opts, problem);
176 count_problem(problem);
177 return problem;
178}
179#endif
180
181} // namespace
182
183LoadedProblem load_problem(std::string_view type, const fs::path &dir,
184 const fs::path &file, Options &opts) {
186 // Isolate problem-specific options
187 std::vector<std::string_view> prob_opts;
188 std::string_view prob_prefix = "problem.";
189 auto options = opts.options();
190 auto used = opts.used();
191 for (auto opt = options.begin(); opt != options.end(); ++opt) {
192 if (opt->starts_with(prob_prefix)) {
193 prob_opts.push_back(opt->substr(prob_prefix.size()));
194 ++used.begin()[opt - options.begin()];
195 }
196 }
197 // Load problem
198 auto full_path = dir / file;
199 if (type == "dl" || type.empty()) {
200#if ALPAQA_WITH_DL
201 return load_dl_problem(full_path, prob_opts, opts);
202#else
203 throw std::logic_error("This version of alpaqa was compiled without "
204 "support for dynamic problem loading");
205#endif
206 } else if (type == "cs") {
207#if ALPAQA_WITH_CASADI
208 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
209 return load_cs_problem(full_path, prob_opts, opts);
210 else
211 throw std::logic_error("CasADi only supports double precision.");
212#else
213 throw std::logic_error(
214 "This version of alpaqa was compiled without CasADi support");
215#endif
216 } else if (type == "cu") {
217#ifdef ALPAQA_WITH_CUTEST
218 if constexpr (std::is_same_v<config_t, alpaqa::EigenConfigd>)
219 return load_cu_problem(full_path, prob_opts, opts);
220 else
221 throw std::logic_error("CUTEst only supports double precision.");
222#else
223 throw std::logic_error(
224 "This version of alpaqa was compiled without CUTEst support");
225#endif
226 }
227 throw std::invalid_argument("Unknown problem type '" + std::string(type) +
228 "'");
229}
std::span< unsigned > used()
Definition options.hpp:63
std::span< const std::string_view > options() const
Definition options.hpp:60
Wrapper for CUTEst problems loaded from an external shared library.
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:77
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:112
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
void set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:128
LoadedProblem load_problem(std::string_view type, const fs::path &dir, const fs::path &file, Options &opts)
Definition problem.cpp:183
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:174
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