PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
kwargs-to-struct.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <functional>
10 #include <map>
11 
12 #include <pybind11/pybind11.h>
13 namespace py = pybind11;
14 
15 template <class T, class A>
16 auto attr_setter(A T::*attr) {
17  return [attr](T &t, const py::handle &h) { t.*attr = h.cast<A>(); };
18 }
19 template <class T, class A>
20 auto attr_getter(A T::*attr) {
21  return [attr](const T &t) { return py::cast(t.*attr); };
22 }
23 
24 template <class T>
26  public:
27  template <class A>
28  attr_setter_fun_t(A T::*attr)
29  : set(attr_setter(attr)), get(attr_getter(attr)) {}
30 
31  std::function<void(T &, const py::handle &)> set;
32  std::function<py::object(const T &)> get;
33 };
34 
35 template <class T>
36 using kwargs_to_struct_table_t = std::map<std::string, attr_setter_fun_t<T>>;
37 
38 template <class T>
40 
41 template <class T>
42 void kwargs_to_struct_helper(T &t, const py::kwargs &kwargs) {
43  const auto &m = kwargs_to_struct_table<T>;
44  for (auto &&[key, val] : kwargs) {
45  auto skey = key.template cast<std::string>();
46  auto it = m.find(skey);
47  if (it == m.end())
48  throw py::key_error("Unknown parameter " + skey);
49  it->second.set(t, val);
50  }
51 }
52 
53 template <class T>
54 py::dict struct_to_dict_helper(const T &t) {
55  const auto &m = kwargs_to_struct_table<T>;
56  py::dict d;
57  for (auto &&[key, val] : m) {
58  py::object o = val.get(t);
59  if (py::hasattr(o, "to_dict"))
60  o = o.attr("to_dict")();
61  d[key.c_str()] = std::move(o);
62  }
63  return d;
64 }
65 
66 template <class T>
67 T kwargs_to_struct(const py::kwargs &kwargs) {
68  T t{};
69  kwargs_to_struct_helper(t, kwargs);
70  return t;
71 }
72 
73 template <class T>
74 py::dict struct_to_dict(const T &t) {
75  return struct_to_dict_helper<T>(t);
76 }
77 
79 
80 template <>
82  kwargs_to_struct_table<pa::PANOCParams>{
83  {"Lipschitz", &pa::PANOCParams::Lipschitz},
84  {"max_iter", &pa::PANOCParams::max_iter},
85  {"max_time", &pa::PANOCParams::max_time},
86  {"τ_min", &pa::PANOCParams::τ_min},
87  {"L_min", &pa::PANOCParams::L_min},
88  {"L_max", &pa::PANOCParams::L_max},
89  {"stop_crit", &pa::PANOCParams::stop_crit},
90  {"max_no_progress", &pa::PANOCParams::max_no_progress},
91  {"print_interval", &pa::PANOCParams::print_interval},
92  {"quadratic_upperbound_tolerance_factor",
94  {"update_lipschitz_in_linesearch",
96  {"alternative_linesearch_cond",
98  {"lbfgs_stepsize", &pa::PANOCParams::lbfgs_stepsize},
99  };
100 
101 template <>
103  kwargs_to_struct_table<pa::LipschitzEstimateParams>{
108  };
109 
110 #include <panoc-alm/inner/pga.hpp>
111 
112 template <>
114  kwargs_to_struct_table<pa::PGAParams>{
115  {"Lipschitz", &pa::PGAParams::Lipschitz},
116  {"max_iter", &pa::PGAParams::max_iter},
117  {"max_time", &pa::PGAParams::max_time},
118  {"L_min", &pa::PGAParams::L_min},
119  {"L_max", &pa::PGAParams::L_max},
120  {"stop_crit", &pa::PGAParams::stop_crit},
121  {"print_interval", &pa::PGAParams::print_interval},
122  {"quadratic_upperbound_tolerance_factor",
124  };
125 
127 
128 template <>
130  kwargs_to_struct_table<pa::GAAPGAParams>{
131  {"Lipschitz", &pa::GAAPGAParams::Lipschitz},
132  {"limitedqr_mem", &pa::GAAPGAParams::limitedqr_mem},
133  {"max_iter", &pa::GAAPGAParams::max_iter},
134  {"max_time", &pa::GAAPGAParams::max_time},
135  {"L_min", &pa::GAAPGAParams::L_min},
136  {"L_max", &pa::GAAPGAParams::L_max},
137  {"stop_crit", &pa::GAAPGAParams::stop_crit},
138  {"print_interval", &pa::GAAPGAParams::print_interval},
139  {"quadratic_upperbound_tolerance_factor",
141  {"max_no_progress", &pa::GAAPGAParams::max_no_progress},
142  {"full_flush_on_γ_change", &pa::GAAPGAParams::full_flush_on_γ_change},
143  };
144 
146 
147 template <>
149  kwargs_to_struct_table<pa::StructuredPANOCLBFGSParams>{
156  {"nonmonotone_linesearch",
161  {"quadratic_upperbound_tolerance_factor",
162  &pa::StructuredPANOCLBFGSParams::
163  quadratic_upperbound_tolerance_factor},
164  {"update_lipschitz_in_linesearch",
166  {"alternative_linesearch_cond",
168  {"hessian_vec_finited_differences",
170  {"full_augmented_hessian",
173  };
174 
176 
177 template <>
179  kwargs_to_struct_table<pa::LBFGSParams>{
180  {"memory", &pa::LBFGSParams::memory},
181  {"cbfgs", &pa::LBFGSParams::cbfgs},
182  {"rescale_when_γ_changes", &pa::LBFGSParams::rescale_when_γ_changes},
183  };
184 
185 template <>
186 inline const kwargs_to_struct_table_t<decltype(pa::LBFGSParams::cbfgs)>
188  {"α", &decltype(pa::LBFGSParams::cbfgs)::α},
189  {"ϵ", &decltype(pa::LBFGSParams::cbfgs)::ϵ},
190  };
191 
192 #include <panoc-alm/decl/alm.hpp>
193 
194 template <>
196  kwargs_to_struct_table<pa::ALMParams>{
197  {"ε", &pa::ALMParams::ε},
198  {"δ", &pa::ALMParams::δ},
199  {"Δ", &pa::ALMParams::Δ},
200  {"Δ_lower", &pa::ALMParams::Δ_lower},
201  {"Σ_0", &pa::ALMParams::Σ₀},
202  {"σ_0", &pa::ALMParams::σ₀},
203  {"Σ_0_lower", &pa::ALMParams::Σ₀_lower},
204  {"ε_0", &pa::ALMParams::ε₀},
205  {"ε_0_increase", &pa::ALMParams::ε₀_increase},
206  {"ρ", &pa::ALMParams::ρ},
207  {"ρ_increase", &pa::ALMParams::ρ_increase},
208  {"θ", &pa::ALMParams::θ},
209  {"M", &pa::ALMParams::M},
210  {"Σ_max", &pa::ALMParams::Σ_max},
211  {"Σ_min", &pa::ALMParams::Σ_min},
212  {"max_iter", &pa::ALMParams::max_iter},
213  {"max_time", &pa::ALMParams::max_time},
214  {"max_num_initial_retries", &pa::ALMParams::max_num_initial_retries},
215  {"max_num_retries", &pa::ALMParams::max_num_retries},
216  {"max_total_num_retries", &pa::ALMParams::max_total_num_retries},
217  {"print_interval", &pa::ALMParams::print_interval},
218  {"preconditioning", &pa::ALMParams::preconditioning},
219  {"single_penalty_factor", &pa::ALMParams::single_penalty_factor},
220  };
pa::GAAPGAParams::limitedqr_mem
unsigned limitedqr_mem
Length of the history to keep in the limited-memory QR algorithm.
Definition: guarded-aa-pga.hpp:23
pa::ALMParams::Σ_min
real_t Σ_min
Minimum penalty factor (used during initialization).
Definition: decl/alm.hpp:50
bicycle-obstacle-avoidance-mpc.t
t
Definition: bicycle-obstacle-avoidance-mpc.py:109
pa::ALMParams::ε₀_increase
real_t ε₀_increase
Factor to increase the initial primal tolerance if convergence fails in the first iteration.
Definition: decl/alm.hpp:37
structured-panoc-lbfgs.hpp
kwargs_to_struct_helper
void kwargs_to_struct_helper(T &t, const py::kwargs &kwargs)
Definition: kwargs-to-struct.hpp:42
lbfgs.hpp
pga.hpp
pa::PANOCParams::L_min
real_t L_min
Minimum Lipschitz constant estimate.
Definition: inner/decl/panoc.hpp:30
pa::StructuredPANOCLBFGSParams::hessian_vec_finited_differences
bool hessian_vec_finited_differences
Definition: decl/structured-panoc-lbfgs.hpp:51
pa::ALMParams::θ
real_t θ
Error tolerance for penalty increase.
Definition: decl/alm.hpp:44
attr_setter_fun_t::set
std::function< void(T &, const py::handle &)> set
Definition: kwargs-to-struct.hpp:31
pa::ALMParams::max_iter
unsigned int max_iter
Maximum number of outer ALM iterations.
Definition: decl/alm.hpp:52
guarded-aa-pga.hpp
alm.hpp
attr_setter_fun_t::attr_setter_fun_t
attr_setter_fun_t(A T::*attr)
Definition: kwargs-to-struct.hpp:28
pa::StructuredPANOCLBFGSParams::full_augmented_hessian
bool full_augmented_hessian
Definition: decl/structured-panoc-lbfgs.hpp:52
pa::LipschitzEstimateParams::L₀
real_t L₀
Initial estimate of the Lipschitz constant of ∇ψ(x)
Definition: lipschitz.hpp:9
pa::StructuredPANOCLBFGSParams::nonmonotone_linesearch
real_t nonmonotone_linesearch
Factor used in update for exponentially weighted nonmonotone line search.
Definition: decl/structured-panoc-lbfgs.hpp:35
pa::PANOCParams::print_interval
unsigned print_interval
When to print progress.
Definition: inner/decl/panoc.hpp:40
pa::PANOCParams::quadratic_upperbound_tolerance_factor
real_t quadratic_upperbound_tolerance_factor
Definition: inner/decl/panoc.hpp:42
pa::LBFGSParams::cbfgs
struct pa::LBFGSParams::@0 cbfgs
Parameters in the cautious BFGS update condition.
pa::LBFGSParams::rescale_when_γ_changes
bool rescale_when_γ_changes
Definition: decl/lbfgs.hpp:24
pa::PANOCParams::τ_min
real_t τ_min
Minimum weight factor between Newton step and projected gradient step.
Definition: inner/decl/panoc.hpp:28
pa::PANOCParams::L_max
real_t L_max
Maximum Lipschitz constant estimate.
Definition: inner/decl/panoc.hpp:32
pa::PANOCParams::lbfgs_stepsize
LBFGSStepSize lbfgs_stepsize
Definition: inner/decl/panoc.hpp:48
pa::PANOCParams::max_time
std::chrono::microseconds max_time
Maximum duration.
Definition: inner/decl/panoc.hpp:26
pa::ALMParams::preconditioning
bool preconditioning
Apply preconditioning to the problem, based on the gradients in the starting point.
Definition: decl/alm.hpp:73
pa::ALMParams::max_total_num_retries
unsigned max_total_num_retries
Combined limit for ALMParams::max_num_initial_retries and ALMParams::max_num_retries.
Definition: decl/alm.hpp:65
pa::LipschitzEstimateParams::ε
real_t ε
Relative step size for initial finite difference Lipschitz estimate.
Definition: lipschitz.hpp:11
pa::ALMParams::single_penalty_factor
bool single_penalty_factor
Use one penalty factor for all m constraints.
Definition: decl/alm.hpp:75
pa::ALMParams::ρ_increase
real_t ρ_increase
Factor to increase the primal tolerance update factor by if convergence fails.
Definition: decl/alm.hpp:42
pa::ALMParams::Δ_lower
real_t Δ_lower
Factor to reduce ALMParams::Δ when inner convergence fails.
Definition: decl/alm.hpp:21
panoc.hpp
panocpy.test.α
α
Definition: test.py:31
pa::PANOCParams::Lipschitz
LipschitzEstimateParams Lipschitz
Parameters related to the Lipschitz constant estimate and step size.
Definition: inner/decl/panoc.hpp:22
pa::LipschitzEstimateParams::δ
real_t δ
Minimum step size for initial finite difference Lipschitz estimate.
Definition: lipschitz.hpp:13
pa::ALMParams::max_num_retries
unsigned max_num_retries
How many times can the penalty update factor ALMParams::Δ and the primal tolerance factor ALMParams::...
Definition: decl/alm.hpp:62
pa::ALMParams::max_num_initial_retries
unsigned max_num_initial_retries
How many times can the initial penalty ALMParams::Σ₀ or ALMParams::σ₀ and the initial primal toleranc...
Definition: decl/alm.hpp:59
kwargs_to_struct_table_t
std::map< std::string, attr_setter_fun_t< T > > kwargs_to_struct_table_t
Definition: kwargs-to-struct.hpp:36
panocpy.test.m
int m
Definition: test.py:39
pa::PANOCParams::stop_crit
PANOCStopCrit stop_crit
What stopping criterion to use.
Definition: inner/decl/panoc.hpp:34
attr_getter
auto attr_getter(A T::*attr)
Definition: kwargs-to-struct.hpp:20
pa::ALMParams::Σ_max
real_t Σ_max
Maximum penalty factor.
Definition: decl/alm.hpp:48
pa::PANOCParams::max_no_progress
unsigned max_no_progress
Maximum number of iterations without any progress before giving up.
Definition: inner/decl/panoc.hpp:36
pa::ALMParams::ε₀
real_t ε₀
Initial primal tolerance.
Definition: decl/alm.hpp:34
pa::LBFGSParams::memory
unsigned memory
Length of the history to keep.
Definition: decl/lbfgs.hpp:14
pa::ALMParams::ρ
real_t ρ
Update factor for primal tolerance.
Definition: decl/alm.hpp:39
attr_setter
auto attr_setter(A T::*attr)
Definition: kwargs-to-struct.hpp:16
struct_to_dict_helper
py::dict struct_to_dict_helper(const T &t)
Definition: kwargs-to-struct.hpp:54
pa::LipschitzEstimateParams::Lγ_factor
real_t Lγ_factor
Factor that relates step size γ and Lipschitz constant.
Definition: lipschitz.hpp:15
kwargs_to_struct
T kwargs_to_struct(const py::kwargs &kwargs)
Definition: kwargs-to-struct.hpp:67
attr_setter_fun_t
Definition: kwargs-to-struct.hpp:25
pa::ALMParams::M
real_t M
Lagrange multiplier bound.
Definition: decl/alm.hpp:46
kwargs_to_struct_table
kwargs_to_struct_table_t< T > kwargs_to_struct_table
Definition: kwargs-to-struct.hpp:39
pa::ALMParams::Δ
real_t Δ
Factor used in updating the penalty parameters.
Definition: decl/alm.hpp:19
main.A
A
Definition: main.py:10
pa::ALMParams::Σ₀
real_t Σ₀
Initial penalty parameter.
Definition: decl/alm.hpp:26
pa::PANOCParams::max_iter
unsigned max_iter
Maximum number of inner PANOC iterations.
Definition: inner/decl/panoc.hpp:24
pa::ALMParams::Σ₀_lower
real_t Σ₀_lower
Factor to reduce the initial penalty factor by if convergence fails in in the first iteration.
Definition: decl/alm.hpp:32
pa::GAAPGAParams::full_flush_on_γ_change
bool full_flush_on_γ_change
Definition: guarded-aa-pga.hpp:45
pa::PANOCParams::update_lipschitz_in_linesearch
bool update_lipschitz_in_linesearch
Definition: inner/decl/panoc.hpp:45
pa::PANOCParams::alternative_linesearch_cond
bool alternative_linesearch_cond
Definition: inner/decl/panoc.hpp:46
attr_setter_fun_t::get
std::function< py::object(const T &)> get
Definition: kwargs-to-struct.hpp:32
struct_to_dict
py::dict struct_to_dict(const T &t)
Definition: kwargs-to-struct.hpp:74
pa::ALMParams::σ₀
real_t σ₀
Initial penalty parameter factor.
Definition: decl/alm.hpp:29