alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.cpp
Go to the documentation of this file.
2
7#include <fstream>
8#include <stdexcept>
9#include <variant>
10
12
25#include <alpaqa/outer/alm.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 <>
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 = util::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 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
75 "': " + std::make_error_code(res.ec).message());
76 if (res.ptr != val_end)
77 throw std::invalid_argument(
78 "Invalid suffix '" + std::string(res.ptr, val_end) +
79 "' for type '" + demangled_typename(typeid(T)) + "' in '" +
80 std::string(s.full_key) + "'");
81}
82
83#ifdef ALPAQA_WITH_QUAD_PRECISION
84template <>
85void ALPAQA_EXPORT set_param(__float128 &f, ParamString s) {
86 long double ld;
87 set_param(ld, s);
88 f = static_cast<__float128>(ld);
89}
90#endif
91
92template <>
94 v.resize(std::count(s.value.begin(), s.value.end(), ',') + 1);
95 std::string_view value, remainder = s.value;
96 for (auto &e : v) {
97 std::tie(value, remainder) = split_key(remainder, ',');
98 set_param(e, {.full_key = s.full_key, .key = "", .value = value});
99 }
100}
101
102template <>
105 if (s.value.starts_with('@')) {
106 std::string fpath{s.value.substr(1)};
107 std::ifstream f(fpath);
108 if (!f)
109 throw std::invalid_argument("Unable to open file '" + fpath +
110 "' in '" + std::string(s.full_key) +
111 '\'');
112 try {
113 auto r = alpaqa::csv::read_row_std_vector<real_t<config_t>>(f);
114 auto r_size = static_cast<length_t<config_t>>(r.size());
115 if (v.expected_size >= 0 && r_size != v.expected_size)
116 throw std::invalid_argument(
117 "Incorrect size in '" + std::string(s.full_key) +
118 "' (expected " + std::to_string(v.expected_size) +
119 ", but got " + std::to_string(r.size()) + ')');
120 v.value.emplace(cmvec<config_t>{r.data(), r_size});
121 } catch (alpaqa::csv::read_error &e) {
122 throw std::invalid_argument(
123 "Unable to read from file '" + fpath + "' in '" +
124 std::string(s.full_key) +
125 "': alpaqa::csv::read_error: " + e.what());
126 }
127 } else {
128 alpaqa::params::set_param(v.value.emplace(), s);
129 if (v.expected_size >= 0 && v.value->size() != v.expected_size)
130 throw std::invalid_argument(
131 "Incorrect size in '" + std::string(s.full_key) +
132 "' (expected " + std::to_string(v.expected_size) +
133 ", but got " + std::to_string(v.value->size()) + ')');
134 }
135}
136
137template <class T>
138inline constexpr bool is_duration = false;
139template <class Rep, class Period>
140inline constexpr bool is_duration<std::chrono::duration<Rep, Period>> = true;
141
142template <class Duration>
143 requires is_duration<Duration>
146 try {
147 util::parse_duration(t = {}, s.value);
148 } catch (util::invalid_duration_value &e) {
149 throw invalid_param(
150 "Invalid value '" + std::string(s.value) + "' for type '" +
151 demangled_typename(typeid(Duration)) + "': error at '" +
152 std::string(std::string_view(s.value.data(), e.result.ptr)));
153 } catch (util::invalid_duration_units &e) {
154 throw invalid_param("Invalid units '" + std::string(e.units) +
155 "' for type '" +
156 demangled_typename(typeid(Duration)) + "' in '" +
157 std::string(s.value) + "'");
158 }
159}
160
162
163// Because of the new name mangling for concepts (https://reviews.llvm.org/D147655)
164// we need this to be an unconstrained function template. The actual constraints
165// are in set_param_default, which is not exported. Fully specialized
166// instantiations of set_param are still allowed.
167template <class T>
169 set_param_default(t, s);
170}
171
172template <class... Ts>
174
175#define ALPAQA_SET_PARAM_INST(...) \
176 template void ALPAQA_EXPORT set_param( \
177 util::possible_alias_t<__VA_ARGS__> &, ParamString)
178
181ALPAQA_SET_PARAM_INST(long double, double, float);
182
191
192// Here, we would like to instantiate alpaqa::params::set_param for all standard
193// integer types, but the issue is that they might not be distinct types:
194// For example, on some platforms, int32_t might be a weak alias to int, whereas
195// on other platforms, it could be a distinct type.
196// To resolve this issue, we use some metaprogramming to ensure distinct
197// instantiations with unique dummy types.
198#define ALPAQA_SET_PARAM_INST_INT(...) \
199 ALPAQA_SET_PARAM_INST(__VA_ARGS__, int8_t, uint8_t, int16_t, uint16_t, \
200 int32_t, int64_t, uint32_t, uint64_t)
201
205ALPAQA_SET_PARAM_INST_INT(long long, long, int, short);
206ALPAQA_SET_PARAM_INST_INT(ptrdiff_t, long long, long, int, short);
208ALPAQA_SET_PARAM_INST_INT(unsigned int, unsigned short);
209ALPAQA_SET_PARAM_INST_INT(unsigned long, unsigned int, unsigned short);
210ALPAQA_SET_PARAM_INST_INT(unsigned long long, unsigned long, unsigned int,
211 unsigned short);
212ALPAQA_SET_PARAM_INST_INT(size_t, unsigned long long, unsigned long,
213 unsigned int, unsigned short);
214
215ALPAQA_SET_PARAM_INST(std::chrono::nanoseconds);
216ALPAQA_SET_PARAM_INST(std::chrono::microseconds);
217ALPAQA_SET_PARAM_INST(std::chrono::milliseconds);
218ALPAQA_SET_PARAM_INST(std::chrono::seconds);
219ALPAQA_SET_PARAM_INST(std::chrono::minutes);
220ALPAQA_SET_PARAM_INST(std::chrono::hours);
221
240#if ALPAQA_WITH_OCP
242#endif
243
244} // namespace alpaqa::params
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
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:113
constexpr bool is_duration
Definition json.cpp:35
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:248
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
Unused unique type tag for template specializations that were rejected because some types were not di...
void parse_duration(std::chrono::duration< Rep, Period > &t, std::string_view s)
Adds the sum of the durations in the string s to the duration t.
std::from_chars_result from_chars(const char *first, const char *last, T &value, std::chars_format fmt=std::chars_format::general)
typename Conf::length_t length_t
Definition config.hpp:103
typename Conf::cmvec cmvec
Definition config.hpp:90
constexpr const auto inf
Definition config.hpp:112
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:175
#define ALPAQA_SET_PARAM_INST_INT(...)
Definition params.cpp:198
Parameters for the StructuredLBFGSDirection class.
Custom parameter parsing exception.
Definition params.hpp:26