alpaqa pi-pico
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>
27#if ALPAQA_WITH_OCP
29#endif
30
31namespace alpaqa::params {
32
33template <>
34void ALPAQA_EXPORT set_param(std::monostate &, ParamString) {
35 throw std::invalid_argument("Cannot set value of std::monostate");
36}
37
38template <>
41 if (s.value == "0" || s.value == "false")
42 b = false;
43 else if (s.value == "1" || s.value == "true")
44 b = true;
45 else
46 throw std::invalid_argument(
47 "Invalid value '" + std::string(s.value) +
48 "' for type 'bool' in '" + std::string(s.full_key) +
49 "',\n "
50 "possible values are: '0', '1', 'true', 'false'");
51}
52
53template <>
54void ALPAQA_EXPORT set_param(std::string_view &v, ParamString s) {
56 v = s.value;
57}
58
59template <>
60void ALPAQA_EXPORT set_param(std::string &v, ParamString s) {
62 v = s.value;
63}
64
65template <class T>
66 requires((std::floating_point<T> || std::integral<T>) &&
67 !std::is_enum_v<T> && !std::is_same_v<T, bool>)
70 const auto *val_end = s.value.data() + s.value.size();
71 auto res = util::from_chars(s.value.data(), val_end, f);
72 if (res.ec != std::errc())
73 throw std::invalid_argument(
74 "Invalid value '" + std::string(s.value) + "' for type '" +
75 demangled_typename(typeid(T)) + "' in '" + 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 '" + 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 <>
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 = alpaqa::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 (alpaqa::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>
147 try {
148 util::parse_duration(t = {}, s.value);
149 } catch (util::invalid_duration_value &e) {
150 throw invalid_param(
151 "Invalid value '" + std::string(s.value) + "' for type '" +
152 demangled_typename(typeid(Duration)) + "': error at '" +
153 std::string(std::string_view(s.value.data(), e.result.ptr)));
154 } catch (util::invalid_duration_units &e) {
155 throw invalid_param("Invalid units '" + std::string(e.units) +
156 "' for type '" +
157 demangled_typename(typeid(Duration)) + "' in '" +
158 std::string(s.value) + "'");
159 }
160}
161
163
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>
170 set_param_default(t, s);
171}
172
173template <class... Ts>
175
176#define ALPAQA_SET_PARAM_INST(...) \
177 template void ALPAQA_EXPORT set_param( \
178 util::possible_alias_t<__VA_ARGS__> &, ParamString)
179
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
242#if ALPAQA_WITH_OCP
244#endif
245
246} // 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:114
constexpr bool is_duration
Definition json.cpp:36
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:249
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:176
#define ALPAQA_SET_PARAM_INST_INT(...)
Definition params.cpp:199
Flags to be passed to dlopen.
Definition dl-flags.hpp:8
Parameters for the StructuredLBFGSDirection class.
Custom parameter parsing exception.
Definition params.hpp:26