alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
panoc-ocp.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <chrono>
8#include <iostream>
9#include <limits>
10#include <string>
11
12namespace alpaqa {
13
14/// Tuning parameters for the PANOC algorithm.
15/// @ingroup grp_Parameters
16template <Config Conf = DefaultConfig>
19
20 /// Parameters related to the Lipschitz constant estimate and step size.
22 /// Maximum number of inner PANOC iterations.
23 unsigned max_iter = 100;
24 /// Maximum duration.
25 std::chrono::nanoseconds max_time = std::chrono::minutes(5);
26 /// Minimum weight factor between Newton step and projected gradient step,
27 /// line search parameter.
29 /// Parameter β used in the line search (see Algorithm 2 in
30 /// @cite de_marchi_proximal_2022). @f$ 0 < \beta < 1 @f$
32 /// Minimum Lipschitz constant estimate.
34 /// Maximum Lipschitz constant estimate.
36 /// Maximum number of times to double the Lipschitz constant estimate per
37 /// iteration.
38 unsigned L_max_inc = 16;
39 /// What stopping criterion to use.
41 /// Maximum number of iterations without any progress before giving up.
42 unsigned max_no_progress = 10;
43 /// How often to use a Gauss-Newton step. Zero to disable GN entirely.
44 unsigned gn_interval = 1;
45 /// Keep trying to apply a Gauss-Newton step as long as they keep getting
46 /// accepted with step size one.
47 bool gn_sticky = true;
48 /// Flush the L-BFGS buffer when a Gauss-Newton step is accepted.
50 /// Use a Cholesky factorization for the Riccati recursion. Use LU if set
51 /// to false.
53
54 /// L-BFGS parameters (e.g. memory).
56
57 /// When to print progress. If set to zero, nothing will be printed.
58 /// If set to N != 0, progress is printed every N iterations.
59 unsigned print_interval = 0;
60 /// The precision of the floating point values printed by the solver.
61 int print_precision = std::numeric_limits<real_t>::max_digits10 / 2;
62
63 /// Tolerance factor used in the quadratic upper bound condition that
64 /// determines the step size. Its goal is to account for numerical errors
65 /// in the function and gradient evaluations. If you notice that the step
66 /// size γ becomes very small, you may want to increase this factor.
68 real_t(1e2) * std::numeric_limits<real_t>::epsilon();
69 /// Tolerance factor used in the line search. Its goal is to account for
70 /// numerical errors in the function and gradient evaluations. If you notice
71 /// that accelerated steps are rejected (τ = 0) when getting closer to the
72 /// solution, you may want to increase this factor.
74 real_t(1e2) * std::numeric_limits<real_t>::epsilon();
75
76 /// Don't compute accelerated steps, fall back to forward-backward splitting.
77 /// For testing purposes.
79};
80
81template <Config Conf = DefaultConfig>
112
113template <Config Conf = DefaultConfig>
116
119 std::chrono::nanoseconds elapsed_time{};
120 std::chrono::nanoseconds time_prox{};
121 std::chrono::nanoseconds time_forward{};
122 std::chrono::nanoseconds time_backward{};
123 std::chrono::nanoseconds time_jacobians{};
124 std::chrono::nanoseconds time_hessians{};
125 std::chrono::nanoseconds time_indices{};
126 std::chrono::nanoseconds time_lqr_factor{};
127 std::chrono::nanoseconds time_lqr_solve{};
128 std::chrono::nanoseconds time_lbfgs_indices{};
129 std::chrono::nanoseconds time_lbfgs_apply{};
130 std::chrono::nanoseconds time_lbfgs_update{};
131 std::chrono::nanoseconds time_progress_callback{};
132 unsigned iterations = 0;
136 unsigned lbfgs_failures = 0;
137 unsigned lbfgs_rejected = 0;
138 unsigned τ_1_accepted = 0;
139 unsigned count_τ = 0;
145};
146
147template <Config Conf>
149 public:
151
157
159
160 Stats operator()(const Problem &problem, // in
161 const SolveOptions &opts, // in
162 rvec u, // inout
163 rvec y, // in
164 crvec μ, // in
165 rvec err_z); // out
166
167 template <class P>
168 Stats operator()(const P &problem, const SolveOptions &opts, rvec u, rvec y,
169 crvec μ, rvec e) {
170 return operator()(Problem{&problem}, opts, u, y, μ, e);
171 }
172
173 template <class P>
174 Stats operator()(const P &problem, const SolveOptions &opts, rvec u) {
175 if (problem.get_m() != 0)
176 throw std::invalid_argument("Missing arguments y, Σ, e");
177 mvec y{nullptr, 0}, Σ{nullptr, 0}, e{nullptr, 0};
178 return operator()(problem, opts, u, y, Σ, e);
179 }
180
181 /// Specify a callable that is invoked with some intermediate results on
182 /// each iteration of the algorithm.
183 /// @see @ref ProgressInfo
185 set_progress_callback(std::function<void(const ProgressInfo &)> cb) {
186 this->progress_cb = cb;
187 return *this;
188 }
189
190 std::string get_name() const;
191
192 void stop() { stop_signal.stop(); }
193
194 const Params &get_params() const { return params; }
195
196 private:
199 std::function<void(const ProgressInfo &)> progress_cb;
201
202 public:
203 std::ostream *os = &std::cout;
204};
205
206template <Config Conf>
209
210 /// Total elapsed time in the inner solver.
211 std::chrono::nanoseconds elapsed_time{};
212 /// Total time spent computing proximal mappings.
213 std::chrono::nanoseconds time_prox{};
214 /// Total time spent doing forward simulations.
215 std::chrono::nanoseconds time_forward{};
216 /// Total time spent doing backward gradient evaluations.
217 std::chrono::nanoseconds time_backward{};
218 /// Total time spent computing dynamics Jacobians.
219 std::chrono::nanoseconds time_jacobians{};
220 /// Total time spent computing cost Hessians and Hessian-vector products.
221 std::chrono::nanoseconds time_hessians{};
222 /// Total time spent determining active indices.
223 std::chrono::nanoseconds time_indices{};
224 /// Total time spent performing LQR factorizations.
225 std::chrono::nanoseconds time_lqr_factor{};
226 /// Total time spent solving the (factorized) LQR problem.
227 std::chrono::nanoseconds time_lqr_solve{};
228 /// Total time spent determining active indices for L-BFGS applications.
229 std::chrono::nanoseconds time_lbfgs_indices{};
230 /// Total time spent applying L-BFGS estimates.
231 std::chrono::nanoseconds time_lbfgs_apply{};
232 /// Total time spent updating the L-BFGS estimate.
233 std::chrono::nanoseconds time_lbfgs_update{};
234 /// Total time spent in the user-provided progress callback.
235 std::chrono::nanoseconds time_progress_callback{};
236 /// Total number of inner PANOC iterations.
237 unsigned iterations = 0;
238 /// Total number of PANOC line search failures.
239 unsigned linesearch_failures = 0;
240 /// Total number of PANOC line search backtracking steps.
241 unsigned linesearch_backtracks = 0;
242 /// Total number of PANOC step size reductions.
243 unsigned stepsize_backtracks = 0;
244 /// Total number of times that the L-BFGS direction was not finite.
245 unsigned lbfgs_failures = 0;
246 /// Total number of times that the L-BFGS update was rejected (i.e. it
247 /// could have resulted in a non-positive definite Hessian estimate).
248 unsigned lbfgs_rejected = 0;
249 /// Total number of times that a line search parameter of @f$ \tau = 1 @f$
250 /// was accepted (i.e. no backtracking necessary).
251 unsigned τ_1_accepted = 0;
252 /// The total number of line searches performed (used for computing the
253 /// average value of @f$ \tau @f$).
254 unsigned count_τ = 0;
255 /// The sum of the line search parameter @f$ \tau @f$ in all iterations
256 /// (used for computing the average value of @f$ \tau @f$).
257 real_t sum_τ = 0;
258 /// The final PANOC step size γ.
259 real_t final_γ = 0;
260 /// Final value of the smooth cost @f$ \psi(\hat x) @f$.
261 real_t final_ψ = 0;
262 /// Final value of the nonsmooth cost @f$ h(\hat x) @f$.
263 real_t final_h = 0;
264 /// Final value of the forward-backward envelope, @f$ \varphi_\gamma(x) @f$
265 /// (note that this is in the point @f$ x @f$, not @f$ \hat x @f$).
266 real_t final_φγ = 0;
267};
268
269template <Config Conf>
272 const PANOCOCPStats<Conf> &s) {
274 acc.elapsed_time += s.elapsed_time;
275 acc.time_prox += s.time_prox;
276 acc.time_forward += s.time_forward;
277 acc.time_backward += s.time_backward;
278 acc.time_jacobians += s.time_jacobians;
279 acc.time_hessians += s.time_hessians;
280 acc.time_indices += s.time_indices;
281 acc.time_lqr_factor += s.time_lqr_factor;
282 acc.time_lqr_solve += s.time_lqr_solve;
283 acc.time_lbfgs_indices += s.time_lbfgs_indices;
284 acc.time_lbfgs_apply += s.time_lbfgs_apply;
285 acc.time_lbfgs_update += s.time_lbfgs_update;
286 acc.time_progress_callback += s.time_progress_callback;
287 acc.linesearch_failures += s.linesearch_failures;
288 acc.linesearch_backtracks += s.linesearch_backtracks;
289 acc.stepsize_backtracks += s.stepsize_backtracks;
290 acc.lbfgs_failures += s.lbfgs_failures;
291 acc.lbfgs_rejected += s.lbfgs_rejected;
292 acc.τ_1_accepted += s.τ_1_accepted;
293 acc.count_τ += s.count_τ;
294 acc.sum_τ += s.sum_τ;
295 acc.final_γ = s.final_γ;
296 acc.final_ψ = s.final_ψ;
297 acc.final_h = s.final_h;
298 acc.final_φγ = s.final_φγ;
299 return acc;
300}
301// clang-format off
302ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigd);
303ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigf);)
304ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigl);)
305ALPAQA_IF_QUADF(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigq);)
306
307ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigd);
308ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigf);)
309ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigl);)
311// clang-format on
312
313} // namespace alpaqa
std::string get_name() const
Definition panoc-ocp.tpp:41
std::function< void(const ProgressInfo &)> progress_cb
InnerSolveOptions< config_t > SolveOptions
Stats operator()(const P &problem, const SolveOptions &opts, rvec u, rvec y, crvec μ, rvec e)
AtomicStopSignal stop_signal
PANOCOCPSolver(const Params &params)
alpaqa::TypeErasedControlProblem< config_t > Problem
const Params & get_params() const
Stats operator()(const Problem &problem, const SolveOptions &opts, rvec u, rvec y, crvec μ, rvec err_z)
Definition panoc-ocp.tpp:46
Stats operator()(const P &problem, const SolveOptions &opts, rvec u)
PANOCOCPStats< config_t > Stats
PANOCOCPSolver & set_progress_callback(std::function< void(const ProgressInfo &)> cb)
Specify a callable that is invoked with some intermediate results on each iteration of the algorithm.
Nonlinear optimal control problem with finite horizon .
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
#define ALPAQA_IF_QUADF(...)
Definition config.hpp:221
#define ALPAQA_IF_LONGD(...)
Definition config.hpp:233
#define ALPAQA_IF_FLOAT(...)
Definition config.hpp:227
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition export.hpp:21
real_t linesearch_tolerance_factor
Tolerance factor used in the line search.
Definition panoc-ocp.hpp:73
LipschitzEstimateParams< config_t > Lipschitz
Parameters related to the Lipschitz constant estimate and step size.
Definition panoc-ocp.hpp:21
bool disable_acceleration
Don't compute accelerated steps, fall back to forward-backward splitting.
Definition panoc-ocp.hpp:78
unsigned max_no_progress
Maximum number of iterations without any progress before giving up.
Definition panoc-ocp.hpp:42
bool reset_lbfgs_on_gn_step
Flush the L-BFGS buffer when a Gauss-Newton step is accepted.
Definition panoc-ocp.hpp:49
real_t L_max
Maximum Lipschitz constant estimate.
Definition panoc-ocp.hpp:35
real_t min_linesearch_coefficient
Minimum weight factor between Newton step and projected gradient step, line search parameter.
Definition panoc-ocp.hpp:28
std::chrono::nanoseconds max_time
Maximum duration.
Definition panoc-ocp.hpp:25
real_t quadratic_upperbound_tolerance_factor
Tolerance factor used in the quadratic upper bound condition that determines the step size.
Definition panoc-ocp.hpp:67
real_t linesearch_strictness_factor
Parameter β used in the line search (see Algorithm 2 in ).
Definition panoc-ocp.hpp:31
bool lqr_factor_cholesky
Use a Cholesky factorization for the Riccati recursion.
Definition panoc-ocp.hpp:52
unsigned print_interval
When to print progress.
Definition panoc-ocp.hpp:59
unsigned L_max_inc
Maximum number of times to double the Lipschitz constant estimate per iteration.
Definition panoc-ocp.hpp:38
LBFGSParams< config_t > lbfgs_params
L-BFGS parameters (e.g. memory).
Definition panoc-ocp.hpp:55
int print_precision
The precision of the floating point values printed by the solver.
Definition panoc-ocp.hpp:61
real_t L_min
Minimum Lipschitz constant estimate.
Definition panoc-ocp.hpp:33
bool gn_sticky
Keep trying to apply a Gauss-Newton step as long as they keep getting accepted with step size one.
Definition panoc-ocp.hpp:47
unsigned gn_interval
How often to use a Gauss-Newton step. Zero to disable GN entirely.
Definition panoc-ocp.hpp:44
unsigned max_iter
Maximum number of inner PANOC iterations.
Definition panoc-ocp.hpp:23
PANOCStopCrit stop_crit
What stopping criterion to use.
Definition panoc-ocp.hpp:40
Parameters for the LBFGS class.
Definition lbfgs.hpp:42
Parameters for the estimation of the Lipschitz constant of the gradient of the smooth term of the cos...
Definition lipschitz.hpp:12
Tuning parameters for the PANOC algorithm.
Definition panoc-ocp.hpp:17
typename Conf::mvec mvec
Definition config.hpp:89
std::chrono::nanoseconds time_jacobians
@ ApproxKKT
Find an ε-approximate KKT point in the ∞-norm:
std::chrono::nanoseconds time_hessians
std::chrono::nanoseconds time_lbfgs_apply
std::chrono::nanoseconds time_lbfgs_indices
SolverStatus
Exit status of a numerical solver such as ALM or PANOC.
@ Busy
In progress.
std::chrono::nanoseconds time_progress_callback
std::chrono::nanoseconds time_indices
std::chrono::nanoseconds elapsed_time
std::chrono::nanoseconds time_backward
typename Conf::real_t real_t
Definition config.hpp:86
std::chrono::nanoseconds time_lqr_factor
unsigned linesearch_backtracks
InnerStatsAccumulator< FISTAStats< Conf > > & operator+=(InnerStatsAccumulator< FISTAStats< Conf > > &acc, const FISTAStats< Conf > &s)
Definition fista.hpp:189
typename Conf::length_t length_t
Definition config.hpp:103
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::crvec crvec
Definition config.hpp:92
std::chrono::nanoseconds time_prox
std::chrono::nanoseconds time_forward
typename Conf::vec vec
Definition config.hpp:88
std::chrono::nanoseconds time_lbfgs_update
std::chrono::nanoseconds time_lqr_solve
const TypeErasedControlProblem< config_t > * problem
const PANOCOCPParams< config_t > * params