alpaqa pantr
Nonconvex constrained optimization
Loading...
Searching...
No Matches
params.cpp
Go to the documentation of this file.
11#include <alpaqa/outer/alm.hpp>
12#if ALPAQA_WITH_OCP
14#endif
15
17
18namespace alpaqa::params {
19
20template <>
21void ALPAQA_EXPORT set_param(bool &b, ParamString s) {
22 assert_key_empty<bool>(s);
23 if (s.value == "0" || s.value == "false")
24 b = false;
25 else if (s.value == "1" || s.value == "true")
26 b = true;
27 else
28 throw std::invalid_argument(
29 "Invalid value '" + std::string(s.value) +
30 "' for type 'bool' in '" + std::string(s.full_key) +
31 "',\n "
32 "possible values are: '0', '1', 'true', 'false'");
33}
34
35template <>
36void ALPAQA_EXPORT set_param(std::string_view &v, ParamString s) {
37 assert_key_empty<bool>(s);
38 v = s.value;
39}
40
41template <>
42void ALPAQA_EXPORT set_param(std::string &v, ParamString s) {
43 assert_key_empty<bool>(s);
44 v = s.value;
45}
46
47template <class T>
48 requires((std::floating_point<T> || std::integral<T>) && !std::is_enum_v<T>)
49void set_param(T &f, ParamString s) {
50 assert_key_empty<T>(s);
51 const auto *val_end = s.value.data() + s.value.size();
52 auto [ptr, ec] = std::from_chars(s.value.data(), val_end, f);
53 if (ec != std::errc())
54 throw std::invalid_argument(
55 "Invalid value '" + std::string(s.value) + "' for type '" +
56 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
57 "': " + std::make_error_code(ec).message());
58 if (ptr != val_end)
59 throw std::invalid_argument("Invalid suffix '" +
60 std::string(ptr, val_end) + "' for type '" +
61 demangled_typename(typeid(T)) + "' in '" +
62 std::string(s.full_key) + "'");
63}
64
65template <>
66void ALPAQA_EXPORT set_param(alpaqa::vec<config_t> &v, ParamString s) {
67 v.resize(std::count(s.value.begin(), s.value.end(), ',') + 1);
68 std::string_view value, remainder = s.value;
69 for (auto &e : v) {
70 std::tie(value, remainder) = split_key(remainder, ',');
71 set_param(e, {.full_key = s.full_key, .key = "", .value = value});
72 }
73}
74
75template <class Rep, class Period>
76void set_param(std::chrono::duration<Rep, Period> &t, ParamString s) {
77 using Duration = std::remove_cvref_t<decltype(t)>;
78 assert_key_empty<Duration>(s);
79 const auto *val_end = s.value.data() + s.value.size();
80 double value;
81 auto [ptr, ec] = std::from_chars(s.value.data(), val_end, value);
82 if (ec != std::errc())
83 throw std::invalid_argument("Invalid value '" +
84 std::string(ptr, val_end) + "' for type '" +
85 demangled_typename(typeid(Duration)) +
86 "' in '" + std::string(s.full_key) +
87 "': " + std::make_error_code(ec).message());
88 std::string_view units{ptr, val_end};
89 auto cast = [](auto t) { return std::chrono::duration_cast<Duration>(t); };
90 if (units == "s" || units.empty())
91 t = cast(std::chrono::duration<double, std::ratio<1, 1>>{value});
92 else if (units == "ms")
93 t = cast(std::chrono::duration<double, std::ratio<1, 1000>>{value});
94 else if (units == "us" || units == "µs")
95 t = cast(std::chrono::duration<double, std::ratio<1, 1000000>>{value});
96 else if (units == "ns")
97 t = cast(
98 std::chrono::duration<double, std::ratio<1, 1000000000>>{value});
99 else if (units == "min")
100 t = cast(std::chrono::duration<double, std::ratio<60, 1>>{value});
101 else
102 throw std::invalid_argument("Invalid units '" + std::string(units) +
103 "' in '" + std::string(s.full_key) + "'");
104}
105
106template <>
107void ALPAQA_EXPORT set_param(LBFGSStepSize &t, ParamString s) {
108 using enum LBFGSStepSize;
109 if (s.value == "BasedOnExternalStepSize")
111 else if (s.value == "BasedOnCurvature")
113 else
114 throw std::invalid_argument("Invalid value '" + std::string(s.value) +
115 "' for type 'LBFGSStepSize' in '" +
116 std::string(s.full_key) + "'");
117}
118
119template <>
120void ALPAQA_EXPORT set_param(PANOCStopCrit &t, ParamString s) {
121 using enum PANOCStopCrit;
122 if (s.value == "ApproxKKT")
123 t = ApproxKKT;
124 else if (s.value == "ApproxKKT2")
125 t = ApproxKKT2;
126 else if (s.value == "ProjGradNorm")
127 t = ProjGradNorm;
128 else if (s.value == "ProjGradNorm2")
129 t = ProjGradNorm2;
130 else if (s.value == "ProjGradUnitNorm")
132 else if (s.value == "ProjGradUnitNorm2")
134 else if (s.value == "FPRNorm")
135 t = FPRNorm;
136 else if (s.value == "FPRNorm2")
137 t = FPRNorm2;
138 else if (s.value == "Ipopt")
139 t = Ipopt;
140 else if (s.value == "LBFGSBpp")
141 t = LBFGSBpp;
142 else
143 throw std::invalid_argument("Invalid value '" + std::string(s.value) +
144 "' for type 'PANOCStopCrit' in '" +
145 std::string(s.full_key) + "'");
146}
147
149 PARAMS_MEMBER(memory), //
150 PARAMS_MEMBER(min_div_fac), //
151 PARAMS_MEMBER(min_abs_s), //
152 PARAMS_MEMBER(cbfgs), //
153 PARAMS_MEMBER(force_pos_def), //
154 PARAMS_MEMBER(stepsize), //
156
158 PARAMS_MEMBER(memory), //
159 PARAMS_MEMBER(min_div_fac), //
161
163 PARAMS_MEMBER(α), //
164 PARAMS_MEMBER(ϵ), //
166
168 PARAMS_MEMBER(L_0), //
169 PARAMS_MEMBER(δ), //
170 PARAMS_MEMBER(ε), //
171 PARAMS_MEMBER(Lγ_factor), //
173
175 PARAMS_MEMBER(Lipschitz), //
176 PARAMS_MEMBER(max_iter), //
177 PARAMS_MEMBER(max_time), //
178 PARAMS_MEMBER(L_min), //
179 PARAMS_MEMBER(L_max), //
180 PARAMS_MEMBER(stop_crit), //
181 PARAMS_MEMBER(max_no_progress), //
182 PARAMS_MEMBER(print_interval), //
183 PARAMS_MEMBER(print_precision), //
184 PARAMS_MEMBER(quadratic_upperbound_tolerance_factor), //
185 PARAMS_MEMBER(TR_tolerance_factor), //
186 PARAMS_MEMBER(ratio_threshold_acceptable), //
187 PARAMS_MEMBER(ratio_threshold_good), //
188 PARAMS_MEMBER(radius_factor_rejected), //
189 PARAMS_MEMBER(radius_factor_acceptable), //
190 PARAMS_MEMBER(radius_factor_good), //
191 PARAMS_MEMBER(initial_radius), //
192 PARAMS_MEMBER(min_radius), //
193 PARAMS_MEMBER(compute_ratio_using_new_stepsize), //
194 PARAMS_MEMBER(update_direction_on_prox_step), //
195 PARAMS_MEMBER(recompute_last_prox_step_after_direction_reset), //
196 PARAMS_MEMBER(disable_acceleration), //
198
200 PARAMS_MEMBER(Lipschitz), //
201 PARAMS_MEMBER(max_iter), //
202 PARAMS_MEMBER(max_time), //
203 PARAMS_MEMBER(min_linesearch_coefficient), //
204 PARAMS_MEMBER(force_linesearch), //
205 PARAMS_MEMBER(linesearch_strictness_factor), //
206 PARAMS_MEMBER(L_min), //
207 PARAMS_MEMBER(L_max), //
208 PARAMS_MEMBER(stop_crit), //
209 PARAMS_MEMBER(max_no_progress), //
210 PARAMS_MEMBER(print_interval), //
211 PARAMS_MEMBER(print_precision), //
212 PARAMS_MEMBER(quadratic_upperbound_tolerance_factor), //
213 PARAMS_MEMBER(linesearch_tolerance_factor), //
214 PARAMS_MEMBER(update_direction_in_candidate), //
215 PARAMS_MEMBER(recompute_last_prox_step_after_lbfgs_flush), //
217
219 PARAMS_MEMBER(Lipschitz), //
220 PARAMS_MEMBER(max_iter), //
221 PARAMS_MEMBER(max_time), //
222 PARAMS_MEMBER(min_linesearch_coefficient), //
223 PARAMS_MEMBER(force_linesearch), //
224 PARAMS_MEMBER(linesearch_strictness_factor), //
225 PARAMS_MEMBER(L_min), //
226 PARAMS_MEMBER(L_max), //
227 PARAMS_MEMBER(stop_crit), //
228 PARAMS_MEMBER(max_no_progress), //
229 PARAMS_MEMBER(print_interval), //
230 PARAMS_MEMBER(print_precision), //
231 PARAMS_MEMBER(quadratic_upperbound_tolerance_factor), //
232 PARAMS_MEMBER(linesearch_tolerance_factor), //
233 PARAMS_MEMBER(update_direction_in_candidate), //
234 PARAMS_MEMBER(recompute_last_prox_step_after_lbfgs_flush), //
235 PARAMS_MEMBER(update_direction_from_prox_step), //
237
239 PARAMS_MEMBER(rescale_on_step_size_changes), //
241
243 PARAMS_MEMBER(rescale_on_step_size_changes), //
245
247 PARAMS_MEMBER(hessian_vec), //
248 PARAMS_MEMBER(hessian_vec_finite_differences), //
249 PARAMS_MEMBER(full_augmented_hessian), //
251
253 PARAMS_MEMBER(rescale_on_step_size_changes), //
254 PARAMS_MEMBER(hessian_vec_factor), //
255 PARAMS_MEMBER(finite_diff), //
256 PARAMS_MEMBER(finite_diff_stepsize), //
258
260 PARAMS_MEMBER(tol_scale), //
261 PARAMS_MEMBER(tol_scale_root), //
262 PARAMS_MEMBER(tol_max), //
263 PARAMS_MEMBER(max_iter_factor), //
265
267 PARAMS_MEMBER(min_eig), //
268 PARAMS_MEMBER(print_eig), //
270
272 PARAMS_MEMBER(hessian_vec), //
274
276 PARAMS_MEMBER(tolerance), //
277 PARAMS_MEMBER(dual_tolerance), //
278 PARAMS_MEMBER(penalty_update_factor), //
279 PARAMS_MEMBER(penalty_update_factor_lower), //
280 PARAMS_MEMBER(min_penalty_update_factor), //
281 PARAMS_MEMBER(initial_penalty), //
282 PARAMS_MEMBER(initial_penalty_factor), //
283 PARAMS_MEMBER(initial_penalty_lower), //
284 PARAMS_MEMBER(initial_tolerance), //
285 PARAMS_MEMBER(initial_tolerance_increase), //
286 PARAMS_MEMBER(tolerance_update_factor), //
287 PARAMS_MEMBER(ρ_increase), //
288 PARAMS_MEMBER(ρ_max), //
289 PARAMS_MEMBER(rel_penalty_increase_threshold), //
290 PARAMS_MEMBER(max_multiplier), //
291 PARAMS_MEMBER(max_penalty), //
292 PARAMS_MEMBER(min_penalty), //
293 PARAMS_MEMBER(max_iter), //
294 PARAMS_MEMBER(max_time), //
295 PARAMS_MEMBER(max_num_initial_retries), //
296 PARAMS_MEMBER(max_num_retries), //
297 PARAMS_MEMBER(max_total_num_retries), //
298 PARAMS_MEMBER(print_interval), //
299 PARAMS_MEMBER(print_precision), //
300 PARAMS_MEMBER(single_penalty_factor), //
302
303#if ALPAQA_WITH_OCP
305 PARAMS_MEMBER(Lipschitz), //
306 PARAMS_MEMBER(max_iter), //
307 PARAMS_MEMBER(max_time), //
308 PARAMS_MEMBER(min_linesearch_coefficient), //
309 PARAMS_MEMBER(linesearch_strictness_factor), //
310 PARAMS_MEMBER(L_min), //
311 PARAMS_MEMBER(L_max), //
312 PARAMS_MEMBER(L_max_inc), //
313 PARAMS_MEMBER(stop_crit), //
314 PARAMS_MEMBER(max_no_progress), //
315 PARAMS_MEMBER(gn_interval), //
316 PARAMS_MEMBER(gn_sticky), //
317 PARAMS_MEMBER(reset_lbfgs_on_gn_step), //
318 PARAMS_MEMBER(lqr_factor_cholesky), //
319 PARAMS_MEMBER(lbfgs_params), //
320 PARAMS_MEMBER(print_interval), //
321 PARAMS_MEMBER(print_precision), //
322 PARAMS_MEMBER(quadratic_upperbound_tolerance_factor), //
323 PARAMS_MEMBER(linesearch_tolerance_factor), //
324 PARAMS_MEMBER(disable_acceleration), //
326#endif
327
328namespace detail {
329
330/// Check if @p A is equal to any of @p Bs.
331template <class A, class... Bs>
332constexpr bool any_is_same() {
333 return (std::is_same_v<A, Bs> || ...);
334}
335
336/// Unused unique type tag for template specializations that were rejected
337/// because some types were not distinct.
338template <class...>
339struct _dummy;
340
341/// If @p NewAlias is not the same type as any of @p PossibleAliases, the result
342/// is @p NewAlias. If @p NewAlias is not distinct from @p PossibleAliases, the
343/// result is a dummy type, uniquely determined by @p NewAlias and
344/// @p PossibleAliases.
345template <class NewAlias, class... PossibleAliases>
347 std::conditional_t<any_is_same<NewAlias, PossibleAliases...>(),
348 _dummy<NewAlias, PossibleAliases...>, NewAlias>;
349
350} // namespace detail
351
352template <class... Ts>
353void set_param(detail::_dummy<Ts...> &, ParamString) {}
354
355#define ALPAQA_SET_PARAM_INST(...) \
356 template void ALPAQA_EXPORT set_param( \
357 detail::possible_alias_t<__VA_ARGS__> &, ParamString)
358
361ALPAQA_SET_PARAM_INST(long double, double, float);
362
371
372// Here, we would like to instantiate alpaqa::params::set_param for all standard
373// integer types, but the issue is that they might not be distinct types:
374// For example, on some platforms, int32_t might be a weak alias to int, whereas
375// on other platforms, it could be a distinct type.
376// To resolve this issue, we use some metaprogramming to ensure distinct
377// instantiations with unique dummy types.
378#define ALPAQA_SET_PARAM_INST_INT(...) \
379 ALPAQA_SET_PARAM_INST(__VA_ARGS__, int8_t, uint8_t, int16_t, uint16_t, \
380 int32_t, int64_t, uint32_t, uint64_t)
381
385ALPAQA_SET_PARAM_INST_INT(long long, long, int, short);
386ALPAQA_SET_PARAM_INST_INT(ptrdiff_t, long long, long, int, short);
388ALPAQA_SET_PARAM_INST_INT(unsigned int, unsigned short);
389ALPAQA_SET_PARAM_INST_INT(unsigned long, unsigned int, unsigned short);
390ALPAQA_SET_PARAM_INST_INT(unsigned long long, unsigned long, unsigned int,
391 unsigned short);
392ALPAQA_SET_PARAM_INST_INT(size_t, unsigned long long, unsigned long,
393 unsigned int, unsigned short);
394
395ALPAQA_SET_PARAM_INST(std::chrono::nanoseconds);
396ALPAQA_SET_PARAM_INST(std::chrono::microseconds);
397ALPAQA_SET_PARAM_INST(std::chrono::milliseconds);
398ALPAQA_SET_PARAM_INST(std::chrono::seconds);
399ALPAQA_SET_PARAM_INST(std::chrono::minutes);
400ALPAQA_SET_PARAM_INST(std::chrono::hours);
401
415#if ALPAQA_WITH_OCP
417#endif
418
419} // namespace alpaqa::params
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
std::conditional_t< any_is_same< NewAlias, PossibleAliases... >(), _dummy< NewAlias, PossibleAliases... >, NewAlias > possible_alias_t
If NewAlias is not the same type as any of PossibleAliases, the result is NewAlias.
Definition: params.cpp:348
constexpr bool any_is_same()
Check if A is equal to any of Bs.
Definition: params.cpp:332
void set_param(bool &b, ParamString s)
Definition: params.cpp:21
std::string_view full_key
Full key string, used for diagnostics.
Definition: params.hpp:17
auto split_key(std::string_view full, char tok='.')
Split the string full on the first occurrence of tok.
Definition: params.hpp:31
std::string_view value
The value of the parameter to store.
Definition: params.hpp:21
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition: params.hpp:15
@ LBFGSBpp
The stopping criterion used by LBFGS++, see https://lbfgspp.statr.me/doc/classLBFGSpp_1_1LBFGSBParam....
@ ProjGradUnitNorm
∞-norm of the projected gradient with unit step size:
@ ProjGradNorm
∞-norm of the projected gradient with step size γ:
@ Ipopt
The stopping criterion used by Ipopt, see https://link.springer.com/article/10.1007/s10107-004-0559-y...
@ FPRNorm2
2-norm of fixed point residual:
@ ProjGradNorm2
2-norm of the projected gradient with step size γ:
@ ApproxKKT
Find an ε-approximate KKT point in the ∞-norm:
@ FPRNorm
∞-norm of fixed point residual:
@ ApproxKKT2
Find an ε-approximate KKT point in the 2-norm:
@ ProjGradUnitNorm2
2-norm of the projected gradient with unit step size:
typename Conf::vec vec
Definition: config.hpp:52
LBFGSStepSize
Which method to use to select the L-BFGS step size.
@ BasedOnCurvature
Initial inverse Hessian approximation is set to .
@ BasedOnExternalStepSize
Initial inverse Hessian approximation is set to .
Parameters for the Augmented Lagrangian solver.
Definition: alm.hpp:20
Parameters for the AndersonAccel class.
Parameters for the AndersonDirection class.
Parameters for the LBFGSDirection class.
Parameters for the LBFGS class.
Parameters for the NewtonTRDirection class.
Definition: newton-tr.hpp:16
Tuning parameters for the PANOC algorithm.
Definition: panoc-ocp.hpp:16
Tuning parameters for the PANOC algorithm.
Definition: panoc.hpp:24
Tuning parameters for the PANTR algorithm.
Definition: pantr.hpp:22
Parameters for the StructuredNewtonDirection class.
Parameters for the StructuredNewtonDirection class.
Tuning parameters for the ZeroFPR algorithm.
Definition: zerofpr.hpp:23
#define ALPAQA_SET_PARAM_INST(...)
Definition: params.cpp:355
#define ALPAQA_SET_PARAM_INST_INT(...)
Definition: params.cpp:378
#define PARAMS_MEMBER(name)
Helper macro to easily initialize a alpaqa::params::dict_to_struct_table_t.
Definition: params.tpp:129
#define PARAMS_TABLE(type_,...)
Helper macro to easily specialize alpaqa::params::dict_to_struct_table.
Definition: params.tpp:120
Cautious BFGS update.
Parameters for the StructuredLBFGSDirection class.