Nonconvex constrained optimization
Loading...
Searching...
No Matches
problem.cpp
Go to the documentation of this file.
1#include <alpaqa/export.h>
6#include <guanaqo/dl-flags.hpp>
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
43alpaqa::DynamicLoadFlags get_dl_flags(Options &opts) {
44 alpaqa::DynamicLoadFlags flags;
45 set_params(flags, "dl_flags", opts);
46 return flags;
47}
48
50 const auto n = problem.problem.get_num_variables(),
51 m = problem.problem.get_num_constraints();
53 set_params(x0, "x0", opts);
54 if (x0.value)
55 problem.initial_guess_x = std::move(*x0.value);
56 set_params(y0, "mul_g0", opts);
57 if (y0.value)
58 problem.initial_guess_y = std::move(*y0.value);
59 set_params(w0, "mul_x0", opts);
60 if (w0.value)
61 problem.initial_guess_w = std::move(*w0.value);
62}
63
65 const auto n = C.lower.size();
66 cnt.lb = 0;
67 cnt.ub = 0;
68 cnt.lbub = 0;
69 cnt.eq = 0;
70 for (index_t i = 0; i < n; ++i) {
71 bool lb = C.lower(i) > -alpaqa::inf<config_t>;
72 bool ub = C.upper(i) < +alpaqa::inf<config_t>;
73 bool eq = C.lower(i) == C.upper(i);
74 if (eq)
75 ++cnt.eq;
76 else if (lb && ub)
77 ++cnt.lbub;
78 else if (lb)
79 ++cnt.lb;
80 else if (ub)
81 ++cnt.ub;
82 }
83}
84
100
101#if ALPAQA_WITH_DL
102LoadedProblem load_dl_problem(const fs::path &full_path,
103 std::span<std::string_view> prob_opts,
104 Options &opts) {
105 using TEProblem = alpaqa::TypeErasedProblem<config_t>;
106 using DLProblem = alpaqa::dl::DLProblem;
108 auto register_name = get_reg_name_option(prob_opts);
109 auto flags = get_dl_flags(opts);
110 LoadedProblem problem{
111 .problem = TEProblem::make<CntProblem>(std::in_place, full_path,
112 register_name, prob_opts, flags),
113 .abs_path = fs::absolute(full_path),
114 .path = full_path,
115 };
116 auto &cnt_problem = problem.problem.as<CntProblem>();
117 problem.name = cnt_problem.problem.get_name();
118 problem.evaluations = cnt_problem.evaluations;
119 load_initial_guess(opts, problem);
120 count_problem(problem);
121 return problem;
122}
123#endif
124
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 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>;
135 auto flags = get_dl_flags(opts);
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),
140 .path = full_path,
141 };
142 lck.unlock();
143 auto &cnt_problem = problem.problem.as<CntProblem>();
144 auto &cs_problem = cnt_problem.problem;
145 problem.name = cs_problem.get_name();
146 problem.evaluations = cnt_problem.evaluations;
147 auto param_size = cs_problem.param.size();
148 alpaqa::params::set_params(cs_problem.param, "param", prob_opts);
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()) + ")");
154 load_initial_guess(opts, problem);
155 count_problem(problem);
156 return problem;
157}
158#endif
159
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 Options &opts) {
165 std::string outsdif_path;
166 alpaqa::params::set_params(outsdif_path, "outsdif", prob_opts);
167 bool sparse = false;
168 alpaqa::params::set_params(sparse, "sparse", prob_opts);
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>;
174 auto flags = get_dl_flags(opts);
175 LoadedProblem problem{
176 .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),
180 .path = full_path,
181 };
182 lck.unlock();
183 auto &cnt_problem = problem.problem.as<CntProblem>();
184 auto &cu_problem = cnt_problem.problem;
185 problem.name = cu_problem.get_name();
186 problem.evaluations = cnt_problem.evaluations;
187 problem.initial_guess_x = std::move(cu_problem.x0);
188 problem.initial_guess_y = std::move(cu_problem.y0);
189 load_initial_guess(opts, problem);
190 count_problem(problem);
191 return problem;
192}
193#endif
194
195} // namespace
196
197LoadedProblem load_problem(std::string_view type, const fs::path &dir,
198 const fs::path &file, Options &opts) {
200 // Isolate problem-specific options
201 std::vector<std::string_view> prob_opts;
202 std::string_view prob_prefix = "problem.";
203 auto options = opts.options();
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()];
209 }
210 }
211 // Load problem
212 auto full_path = dir / file;
213 if (type == "dl" || type.empty()) {
214#if ALPAQA_WITH_DL
215 return load_dl_problem(full_path, prob_opts, opts);
216#else
217 throw std::logic_error("This version of alpaqa was compiled without "
218 "support for dynamic problem loading");
219#endif
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);
224 else
225 throw std::logic_error("CasADi only supports double precision.");
226#else
227 throw std::logic_error(
228 "This version of alpaqa was compiled without CasADi support");
229#endif
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);
234 else
235 throw std::logic_error("CUTEst only supports double precision.");
236#else
237 throw std::logic_error(
238 "This version of alpaqa was compiled without CUTEst support");
239#endif
240 }
241 throw std::invalid_argument("Unknown problem type '" + std::string(type) +
242 "'");
243}
std::span< unsigned > used()
Definition options.hpp:61
std::span< const std::string_view > options() const
Definition options.hpp:58
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)
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
EigenConfigd DefaultConfig
Definition config.hpp:31
constexpr const auto inf
Definition config.hpp:112
void count_constr(ConstrCount &cnt, const alpaqa::Box< config_t > &C)
Definition problem.cpp:64
alpaqa::DynamicLoadFlags get_dl_flags(Options &opts)
Definition problem.cpp:43
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:85
void load_initial_guess(Options &opts, LoadedProblem &problem)
Definition problem.cpp:49
void set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:126
LoadedProblem load_problem(std::string_view type, const fs::path &dir, const fs::path &file, Options &opts)
Definition problem.cpp:197
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:33
std::optional< length_t > nnz_hess_L
Definition problem.hpp:36
std::optional< ConstrCount > general_constr_count
Definition problem.hpp:35
std::optional< length_t > nnz_hess_ψ
Definition problem.hpp:37
alpaqa::TypeErasedProblem< config_t > problem
Definition problem.hpp:25
std::string name
Definition problem.hpp:28
std::optional< ConstrCount > box_constr_count
Definition problem.hpp:34
std::optional< length_t > nnz_jac_g
Definition problem.hpp:36
std::shared_ptr< alpaqa::EvalCounter > evaluations
Definition problem.hpp:29
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
Problem wrapper that keeps track of the number of evaluations and the run time of each function.