4#include <guanaqo/demangled-typename.hpp>
5#include <guanaqo/duration-parse.hpp>
6#include <guanaqo/io/csv.hpp>
7#include <guanaqo/possible-alias.hpp>
28#include <guanaqo/dl-flags.hpp>
35using guanaqo::demangled_typename;
39template <
class Rep,
class Period>
42template <
class Duration>
47 "Invalid value " + to_string(j) +
" for type '" +
48 demangled_typename(
typeid(Duration)) +
"' (expected a string)");
49 std::string value = j;
51 guanaqo::parse_duration(t = {}, value);
52 }
catch (guanaqo::invalid_duration_value &e) {
54 "Invalid value '" + value +
"' for type '" +
55 demangled_typename(
typeid(Duration)) +
"': error at '" +
56 std::string(std::string_view(value.data(), e.result.ptr)));
57 }
catch (guanaqo::invalid_duration_units &e) {
59 "Invalid units '" + std::string(e.units) +
"' for type '" +
60 demangled_typename(
typeid(Duration)) +
"' in '" + value +
"'");
64template <
class Duration>
67 namespace chr = std::chrono;
70 if (dur.count() == 0) {
73 if (
auto d = duration_cast<chr::hours>(dur); d.count() != 0) {
74 result += std::to_string(d.count()) +
"h";
75 dur -= duration_cast<Duration>(d);
77 if (
auto d = duration_cast<chr::minutes>(dur); d.count() != 0) {
78 result += std::to_string(d.count()) +
"min";
79 dur -= duration_cast<Duration>(d);
81 if (
auto d = duration_cast<chr::seconds>(dur); d.count() != 0) {
82 result += std::to_string(d.count()) +
"s";
83 dur -= duration_cast<Duration>(d);
85 if (
auto d = duration_cast<chr::milliseconds>(dur); d.count() != 0) {
86 result += std::to_string(d.count()) +
"ms";
87 dur -= duration_cast<Duration>(d);
89 if (
auto d = duration_cast<chr::microseconds>(dur); d.count() != 0) {
90 result += std::to_string(d.count()) +
"µs";
91 dur -= duration_cast<Duration>(d);
93 if (
auto d = duration_cast<chr::nanoseconds>(dur); d.count() != 0) {
94 result += std::to_string(d.count()) +
"ns";
95 dur -= duration_cast<Duration>(d);
98 s = std::move(result);
105 " for type '" + demangled_typename(
typeid(v)) +
106 "' (expected an array, but got " +
107 j.type_name() +
')');
112 }
catch (json::exception &e) {
114 " (expected a number, but got " +
115 j.type_name() +
"): " + e.what());
118 std::ranges::transform(j, v.begin(), convert);
123 using namespace guanaqo::io;
125 std::string fpath{j};
126 std::ifstream f(fpath);
130 demangled_typename(
typeid(v)));
132 auto r = csv_read_row_std_vector<real_t<config_t>>(f);
136 "Incorrect size in '" + fpath +
"' (expected " +
138 std::to_string(r.size()) +
")");
140 }
catch (csv_read_error &e) {
142 "': alpaqa::csv::read_error: " + e.what());
144 }
else if (j.is_array()) {
148 "Incorrect size in " + to_string(j) +
"' (expected " +
150 std::to_string(v.
value->size()) +
')');
153 " for type '" + demangled_typename(
typeid(v)) +
154 "' (expected string or array, but got " +
155 j.type_name() +
')');
160void ALPAQA_EXPORT
set_param(std::monostate &,
const nlohmann::json &) {
165void ALPAQA_EXPORT
set_param(
bool &t,
const nlohmann::json &j) {
168 " for type '" + demangled_typename(
typeid(t)) +
169 "' (expected boolean, but got " +
170 j.type_name() +
')');
171 t =
static_cast<bool>(j);
175void ALPAQA_EXPORT
set_param(std::string &t,
const nlohmann::json &j) {
178 " for type '" + demangled_typename(
typeid(t)) +
179 "' (expected string, but got " +
180 j.type_name() +
')');
181 t =
static_cast<std::string
>(j);
184template <std::
integral T>
185 requires(!std::same_as<T, bool>)
187 if (std::unsigned_integral<T> && !j.is_number_unsigned())
189 " for type '" + demangled_typename(
typeid(T)) +
190 "' (expected unsigned integer, but got " +
191 j.type_name() +
')');
192 if (!j.is_number_integer())
194 " for type '" + demangled_typename(
typeid(T)) +
195 "' (expected integer, but got " +
196 j.type_name() +
')');
197 t =
static_cast<T
>(j);
200template <std::
floating_po
int T>
204 t = std::numeric_limits<T>::quiet_NaN();
205 }
else if (j ==
"inf" || j ==
"+inf") {
206 t = std::numeric_limits<T>::infinity();
207 }
else if (j ==
"-inf") {
208 t = -std::numeric_limits<T>::infinity();
212 demangled_typename(
typeid(T)) +
213 "' (expected number or any of "
214 "\"nan\", \"inf\", \"+inf\", \"-inf\")");
216 }
else if (j.is_number()) {
217 t =
static_cast<T
>(j);
220 " for type '" + demangled_typename(
typeid(T)) +
221 "' (expected number, but got " +
222 j.type_name() +
')');
227 requires(std::integral<T> || std::same_as<T, bool> ||
228 std::same_as<T, std::string>)
233template <std::
floating_po
int T>
237 else if (t == +std::numeric_limits<T>::infinity())
239 else if (t == -std::numeric_limits<T>::infinity())
260template <
class... Ts>
261void set_param(guanaqo::detail::dummy<Ts...> &,
const json &) {}
262template <
class... Ts>
263void get_param(
const guanaqo::detail::dummy<Ts...> &, json &) {}
265#define ALPAQA_GET_PARAM_INST(...) \
266 template void ALPAQA_EXPORT get_param( \
267 const guanaqo::possible_alias_t<__VA_ARGS__> &, json &)
268#define ALPAQA_GETSET_PARAM_INST(...) \
269 template void ALPAQA_EXPORT set_param( \
270 guanaqo::possible_alias_t<__VA_ARGS__> &, const json &); \
271 template void ALPAQA_EXPORT get_param( \
272 const guanaqo::possible_alias_t<__VA_ARGS__> &, json &)
297#define ALPAQA_GETSET_PARAM_INST_INT(...) \
298 ALPAQA_GETSET_PARAM_INST(__VA_ARGS__, int8_t, uint8_t, int16_t, uint16_t, \
299 int32_t, int64_t, uint32_t, uint64_t)
312 unsigned int,
unsigned short);
Parameters for the Augmented Lagrangian solver.
Parameters for the AndersonAccel class.
Parameters for the AndersonDirection class.
Parameters for the ConvexNewtonDirection class.
Parameters for the ConvexNewtonDirection class.
Tuning parameters for the FISTA algorithm.
Parameters for the LBFGSDirection class.
Parameters for the LBFGS class.
Parameters for the estimation of the Lipschitz constant of the gradient of the smooth term of the cos...
Parameters for the NewtonTRDirection class.
Tuning parameters for the PANOC algorithm.
Tuning parameters for the PANOC algorithm.
Tuning parameters for the PANTR algorithm.
Parameters for SteihaugCG.
Parameters for the StructuredNewtonDirection class.
Parameters for the StructuredNewtonDirection class.
Tuning parameters for the ZeroFPR algorithm.
#define ALPAQA_GETSET_PARAM_INST(...)
#define ALPAQA_GETSET_PARAM_INST_INT(...)
#define ALPAQA_GET_PARAM_INST(...)
void get_param(const T &, json &j)
Get the first argument as a JSON object j.
void get_param_default(const T &t, json &j)
void set_param_default(T &t, const json &j)
constexpr bool is_duration
void set_param(T &, const json &j)
Update/overwrite the first argument based on the JSON object j.
std::optional< vec > value
typename Conf::real_t real_t
typename Conf::length_t length_t
typename Conf::cmvec cmvec
LBFGSStepSize
Which method to use to select the L-BFGS step size.
Parameters for the StructuredLBFGSDirection class.
Custom parameter parsing exception.