alpaqa guanaqo
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.cpp
Go to the documentation of this file.
2
4#include <guanaqo/duration-parse.hpp>
5#include <guanaqo/from_chars-wrapper.hpp>
6#include <guanaqo/io/csv.hpp>
7#include <guanaqo/possible-alias.hpp>
8#include <fstream>
9#include <stdexcept>
10#include <variant>
24#include <alpaqa/outer/alm.hpp>
25#include <guanaqo/dl-flags.hpp>
26#if ALPAQA_WITH_OCP
28#endif
29
30namespace alpaqa::params {
31
32template <>
33void ALPAQA_EXPORT set_param(std::monostate &, ParamString) {
34 throw std::invalid_argument("Cannot set value of std::monostate");
35}
36
37template <>
38void ALPAQA_EXPORT set_param(bool &b, ParamString s) {
40 if (s.value == "0" || s.value == "false")
41 b = false;
42 else if (s.value == "1" || s.value == "true")
43 b = true;
44 else
45 throw std::invalid_argument(
46 "Invalid value '" + std::string(s.value) +
47 "' for type 'bool' in '" + std::string(s.full_key) +
48 "',\n "
49 "possible values are: '0', '1', 'true', 'false'");
50}
51
52template <>
53void ALPAQA_EXPORT set_param(std::string_view &v, ParamString s) {
55 v = s.value;
56}
57
58template <>
59void ALPAQA_EXPORT set_param(std::string &v, ParamString s) {
61 v = s.value;
62}
63
64template <class T>
65 requires((std::floating_point<T> || std::integral<T>) &&
66 !std::is_enum_v<T> && !std::is_same_v<T, bool>)
69 const auto *val_end = s.value.data() + s.value.size();
70 auto res = guanaqo::from_chars(s.value.data(), val_end, f);
71 if (res.ec != std::errc())
72 throw std::invalid_argument(
73 "Invalid value '" + std::string(s.value) + "' for type '" +
74 guanaqo::demangled_typename(typeid(T)) + "' in '" +
75 std::string(s.full_key) +
76 "': " + std::make_error_code(res.ec).message());
77 if (res.ptr != val_end)
78 throw std::invalid_argument(
79 "Invalid suffix '" + std::string(res.ptr, val_end) +
80 "' for type '" + guanaqo::demangled_typename(typeid(T)) + "' in '" +
81 std::string(s.full_key) + "'");
82}
83
84#ifdef ALPAQA_WITH_QUAD_PRECISION
85template <>
86void ALPAQA_EXPORT set_param(__float128 &f, ParamString s) {
87 long double ld;
88 set_param(ld, s);
89 f = static_cast<__float128>(ld);
90}
91#endif
92
93template <>
94void ALPAQA_EXPORT set_param(alpaqa::vec<config_t> &v, ParamString s) {
95 v.resize(std::count(s.value.begin(), s.value.end(), ',') + 1);
96 std::string_view value, remainder = s.value;
97 for (auto &e : v) {
98 std::tie(value, remainder) = split_key(remainder, ',');
99 set_param(e, {.full_key = s.full_key, .key = "", .value = value});
100 }
101}
102
103template <>
106 if (s.value.starts_with('@')) {
107 std::string fpath{s.value.substr(1)};
108 std::ifstream f(fpath);
109 if (!f)
110 throw std::invalid_argument("Unable to open file '" + fpath +
111 "' in '" + std::string(s.full_key) +
112 '\'');
113 try {
114 auto r = guanaqo::io::csv_read_row_std_vector<real_t<config_t>>(f);
115 auto r_size = static_cast<length_t<config_t>>(r.size());
116 if (v.expected_size >= 0 && r_size != v.expected_size)
117 throw std::invalid_argument(
118 "Incorrect size in '" + std::string(s.full_key) +
119 "' (expected " + std::to_string(v.expected_size) +
120 ", but got " + std::to_string(r.size()) + ')');
121 v.value.emplace(cmvec<config_t>{r.data(), r_size});
122 } catch (guanaqo::io::csv_read_error &e) {
123 throw std::invalid_argument(
124 "Unable to read from file '" + fpath + "' in '" +
125 std::string(s.full_key) +
126 "': alpaqa::csv::read_error: " + e.what());
127 }
128 } else {
129 alpaqa::params::set_param(v.value.emplace(), s);
130 if (v.expected_size >= 0 && v.value->size() != v.expected_size)
131 throw std::invalid_argument(
132 "Incorrect size in '" + std::string(s.full_key) +
133 "' (expected " + std::to_string(v.expected_size) +
134 ", but got " + std::to_string(v.value->size()) + ')');
135 }
136}
137
138template <class T>
139inline constexpr bool is_duration = false;
140template <class Rep, class Period>
141inline constexpr bool is_duration<std::chrono::duration<Rep, Period>> = true;
142
143template <class Duration>
144 requires is_duration<Duration>
145void set_param_default(Duration &t, ParamString s) {
147 try {
148 guanaqo::parse_duration(t = {}, s.value);
149 } catch (guanaqo::invalid_duration_value &e) {
150 throw invalid_param(
151 "Invalid value '" + std::string(s.value) + "' for type '" +
152 guanaqo::demangled_typename(typeid(Duration)) + "': error at '" +
153 std::string(std::string_view(s.value.data(), e.result.ptr)));
154 } catch (guanaqo::invalid_duration_units &e) {
155 throw invalid_param("Invalid units '" + std::string(e.units) +
156 "' for type '" +
157 guanaqo::demangled_typename(typeid(Duration)) +
158 "' in '" + std::string(s.value) + "'");
159 }
160}
161
164// Because of the new name mangling for concepts (https://reviews.llvm.org/D147655)
165// we need this to be an unconstrained function template. The actual constraints
166// are in set_param_default, which is not exported. Fully specialized
167// instantiations of set_param are still allowed.
168template <class T>
169void set_param(T &t, ParamString s) {
171}
172
173template <class... Ts>
174void set_param(guanaqo::detail::dummy<Ts...> &, ParamString) {}
176#define ALPAQA_SET_PARAM_INST(...) \
177 template void ALPAQA_EXPORT set_param( \
178 guanaqo::possible_alias_t<__VA_ARGS__> &, ParamString)
182ALPAQA_SET_PARAM_INST(long double, double, float);
183
192
193// Here, we would like to instantiate alpaqa::params::set_param for all standard
194// integer types, but the issue is that they might not be distinct types:
195// For example, on some platforms, int32_t might be a weak alias to int, whereas
196// on other platforms, it could be a distinct type.
197// To resolve this issue, we use some metaprogramming to ensure distinct
198// instantiations with unique dummy types.
199#define ALPAQA_SET_PARAM_INST_INT(...) \
200 ALPAQA_SET_PARAM_INST(__VA_ARGS__, int8_t, uint8_t, int16_t, uint16_t, \
201 int32_t, int64_t, uint32_t, uint64_t)
202
206ALPAQA_SET_PARAM_INST_INT(long long, long, int, short);
207ALPAQA_SET_PARAM_INST_INT(ptrdiff_t, long long, long, int, short);
209ALPAQA_SET_PARAM_INST_INT(unsigned int, unsigned short);
210ALPAQA_SET_PARAM_INST_INT(unsigned long, unsigned int, unsigned short);
211ALPAQA_SET_PARAM_INST_INT(unsigned long long, unsigned long, unsigned int,
212 unsigned short);
213ALPAQA_SET_PARAM_INST_INT(size_t, unsigned long long, unsigned long,
214 unsigned int, unsigned short);
215
216ALPAQA_SET_PARAM_INST(std::chrono::nanoseconds);
217ALPAQA_SET_PARAM_INST(std::chrono::microseconds);
218ALPAQA_SET_PARAM_INST(std::chrono::milliseconds);
219ALPAQA_SET_PARAM_INST(std::chrono::seconds);
220ALPAQA_SET_PARAM_INST(std::chrono::minutes);
221ALPAQA_SET_PARAM_INST(std::chrono::hours);
222
225ALPAQA_SET_PARAM_INST(guanaqo::DynamicLoadFlags);
242#if ALPAQA_WITH_OCP
244#endif
245
246} // namespace alpaqa::params
Parameters for the Augmented Lagrangian solver.
Definition alm.hpp:21
Parameters for the AndersonAccel class.
Definition anderson.hpp:15
Parameters for the AndersonDirection class.
Definition anderson.hpp:12
Parameters for the ConvexNewtonDirection class.
Parameters for the ConvexNewtonDirection class.
Tuning parameters for the FISTA algorithm.
Definition fista.hpp:25
Parameters for the LBFGSDirection class.
Definition lbfgs.hpp:12
Parameters for the LBFGS class.
Definition lbfgs.hpp:42
Parameters for the NewtonTRDirection class.
Definition newton-tr.hpp:17
Tuning parameters for the PANOC algorithm.
Definition panoc-ocp.hpp:17
Tuning parameters for the PANOC algorithm.
Definition panoc.hpp:25
Tuning parameters for the PANTR algorithm.
Definition pantr.hpp:23
Parameters for SteihaugCG.
Parameters for the StructuredNewtonDirection class.
Parameters for the StructuredNewtonDirection class.
Tuning parameters for the ZeroFPR algorithm.
Definition zerofpr.hpp:24
void set_param_default(T &t, const json &j)
Definition json.tpp:114
constexpr bool is_duration
Definition json.cpp:38
void assert_key_empty(ParamString s)
Throw a meaningful error when s.key is not empty, to indicate that the given type T is not of struct ...
Definition params.tpp:15
std::string_view full_key
Full key string, used for diagnostics.
Definition params.hpp:18
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
Definition params.hpp:32
void set_param(T &, const json &j)
Update/overwrite the first argument based on the JSON object j.
Definition json.cpp:252
std::string_view value
The value of the parameter to store.
Definition params.hpp:22
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition params.hpp:16
typename Conf::length_t length_t
Definition config.hpp:103
typename Conf::cmvec cmvec
Definition config.hpp:90
typename Conf::vec vec
Definition config.hpp:88
LBFGSStepSize
Which method to use to select the L-BFGS step size.
Definition lbfgs.hpp:26
#define ALPAQA_SET_PARAM_INST(...)
Definition params.cpp:176
#define ALPAQA_SET_PARAM_INST_INT(...)
Definition params.cpp:199
Parameters for the StructuredLBFGSDirection class.
Custom parameter parsing exception.
Definition params.hpp:26