alpaqa 1.0.0a11
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.
15template <Config Conf = DefaultConfig>
18
19 /// Parameters related to the Lipschitz constant estimate and step size.
21 /// Maximum number of inner PANOC iterations.
22 unsigned max_iter = 100;
23 /// Maximum duration.
24 std::chrono::nanoseconds max_time = std::chrono::minutes(5);
25 /// Minimum weight factor between Newton step and projected gradient step,
26 /// line search parameter.
28 /// Parameter β used in the line search (see Algorithm 2 in
29 /// @cite de_marchi_proximal_2022). @f$ 0 < \beta < 1 @f$
31 /// Minimum Lipschitz constant estimate.
33 /// Maximum Lipschitz constant estimate.
35 /// Maximum number of times to double the Lipschitz constant estimate per
36 /// iteration.
37 unsigned L_max_inc = 16;
38 /// What stopping criterion to use.
40 /// Maximum number of iterations without any progress before giving up.
41 unsigned max_no_progress = 10;
42 /// How often to use a Gauss-Newton step. Zero to disable GN entirely.
43 unsigned gn_interval = 1;
44 bool gn_sticky = true;
47
48 /// L-BFGS parameters (e.g. memory).
50
51 /// When to print progress. If set to zero, nothing will be printed.
52 /// If set to N != 0, progress is printed every N iterations.
53 unsigned print_interval = 0;
54 /// The precision of the floating point values printed by the solver.
55 int print_precision = std::numeric_limits<real_t>::max_digits10 / 2;
56
58 real_t(1e2) * std::numeric_limits<real_t>::epsilon();
60 real_t(1e2) * std::numeric_limits<real_t>::epsilon();
61
63};
64
65template <Config Conf = DefaultConfig>
68
69 unsigned k;
80 bool gn;
87 unsigned outer_iter;
90
91 [[nodiscard]] vec u() const;
92 [[nodiscard]] vec û() const;
93 [[nodiscard]] vec x() const;
94 [[nodiscard]] vec () const;
95};
96
97template <Config Conf = DefaultConfig>
100
102 real_t ε = inf<config_t>;
103 std::chrono::nanoseconds elapsed_time{};
104 std::chrono::nanoseconds time_prox{};
105 std::chrono::nanoseconds time_forward{};
106 std::chrono::nanoseconds time_backward{};
107 std::chrono::nanoseconds time_jacobians{};
108 std::chrono::nanoseconds time_hessians{};
109 std::chrono::nanoseconds time_indices{};
110 std::chrono::nanoseconds time_lqr_factor{};
111 std::chrono::nanoseconds time_lqr_solve{};
112 std::chrono::nanoseconds time_lbfgs_indices{};
113 std::chrono::nanoseconds time_lbfgs_apply{};
114 std::chrono::nanoseconds time_lbfgs_update{};
115 std::chrono::nanoseconds time_progress_callback{};
116 unsigned iterations = 0;
120 unsigned lbfgs_failures = 0;
121 unsigned lbfgs_rejected = 0;
122 unsigned τ_1_accepted = 0;
123 unsigned count_τ = 0;
129};
130
131template <Config Conf>
133 public:
135
141
143
144 Stats operator()(const Problem &problem, // in
145 const SolveOptions &opts, // in
146 rvec u, // inout
147 rvec y, // in
148 crvec μ, // in
149 rvec err_z); // out
150
151 template <class P>
152 Stats operator()(const P &problem, const SolveOptions &opts, rvec u, rvec y,
153 crvec μ, rvec e) {
154 return operator()(Problem::template make<P>(problem), opts, u, y, μ, e);
155 }
156
157 template <class P>
158 Stats operator()(const P &problem, const SolveOptions &opts, rvec u) {
159 if (problem.get_m() != 0)
160 throw std::invalid_argument("Missing arguments y, Σ, e");
161 mvec y{nullptr, 0}, Σ{nullptr, 0}, e{nullptr, 0};
162 return operator()(problem, opts, u, y, Σ, e);
163 }
164
165 /// Specify a callable that is invoked with some intermediate results on
166 /// each iteration of the algorithm.
167 /// @see @ref ProgressInfo
169 set_progress_callback(std::function<void(const ProgressInfo &)> cb) {
170 this->progress_cb = cb;
171 return *this;
172 }
173
174 std::string get_name() const;
175
176 void stop() { stop_signal.stop(); }
177
178 const Params &get_params() const { return params; }
179
180 private:
183 std::function<void(const ProgressInfo &)> progress_cb;
185
186 public:
187 std::ostream *os = &std::cout;
188};
189
190template <Config Conf>
193
194 /// Total elapsed time in the inner solver.
195 std::chrono::nanoseconds elapsed_time{};
196 /// Total time spent computing proximal mappings.
197 std::chrono::nanoseconds time_prox{};
198 /// Total time spent doing forward simulations.
199 std::chrono::nanoseconds time_forward{};
200 /// Total time spent doing backward gradient evaluations.
201 std::chrono::nanoseconds time_backward{};
202 /// Total time spent computing dynamics Jacobians.
203 std::chrono::nanoseconds time_jacobians{};
204 /// Total time spent computing cost Hessians and Hessian-vector products.
205 std::chrono::nanoseconds time_hessians{};
206 /// Total time spent determining active indices.
207 std::chrono::nanoseconds time_indices{};
208 /// Total time spent performing LQR factorizations.
209 std::chrono::nanoseconds time_lqr_factor{};
210 /// Total time spent solving the (factorized) LQR problem.
211 std::chrono::nanoseconds time_lqr_solve{};
212 /// Total time spent determining active indices for L-BFGS applications.
213 std::chrono::nanoseconds time_lbfgs_indices{};
214 /// Total time spent applying L-BFGS estimates.
215 std::chrono::nanoseconds time_lbfgs_apply{};
216 /// Total time spent updating the L-BFGS estimate.
217 std::chrono::nanoseconds time_lbfgs_update{};
218 /// Total time spent in the user-provided progress callback.
219 std::chrono::nanoseconds time_progress_callback{};
220 /// Total number of inner PANOC iterations.
221 unsigned iterations = 0;
222 /// Total number of PANOC line search failures.
223 unsigned linesearch_failures = 0;
224 /// Total number of PANOC line search backtracking steps.
225 unsigned linesearch_backtracks = 0;
226 /// Total number of PANOC step size reductions.
227 unsigned stepsize_backtracks = 0;
228 /// Total number of times that the L-BFGS direction was not finite.
229 unsigned lbfgs_failures = 0;
230 /// Total number of times that the L-BFGS update was rejected (i.e. it
231 /// could have resulted in a non-positive definite Hessian estimate).
232 unsigned lbfgs_rejected = 0;
233 /// Total number of times that a line search parameter of @f$ \tau = 1 @f$
234 /// was accepted (i.e. no backtracking necessary).
235 unsigned τ_1_accepted = 0;
236 /// The total number of line searches performed (used for computing the
237 /// average value of @f$ \tau @f$).
238 unsigned count_τ = 0;
239 /// The sum of the line search parameter @f$ \tau @f$ in all iterations
240 /// (used for computing the average value of @f$ \tau @f$).
241 real_t sum_τ = 0;
242 /// The final PANOC step size γ.
243 real_t final_γ = 0;
244 /// Final value of the smooth cost @f$ \psi(\hat x) @f$.
245 real_t final_ψ = 0;
246 /// Final value of the nonsmooth cost @f$ h(\hat x) @f$.
247 real_t final_h = 0;
248 /// Final value of the forward-backward envelope, @f$ \varphi_\gamma(x) @f$
249 /// (note that this is in the point @f$ x @f$, not @f$ \hat x @f$).
250 real_t final_φγ = 0;
251};
252
253template <Config Conf>
256 const PANOCOCPStats<Conf> &s) {
257 acc.iterations += s.iterations;
258 acc.elapsed_time += s.elapsed_time;
259 acc.time_prox += s.time_prox;
260 acc.time_forward += s.time_forward;
261 acc.time_backward += s.time_backward;
262 acc.time_jacobians += s.time_jacobians;
263 acc.time_hessians += s.time_hessians;
264 acc.time_indices += s.time_indices;
265 acc.time_lqr_factor += s.time_lqr_factor;
266 acc.time_lqr_solve += s.time_lqr_solve;
267 acc.time_lbfgs_indices += s.time_lbfgs_indices;
268 acc.time_lbfgs_apply += s.time_lbfgs_apply;
269 acc.time_lbfgs_update += s.time_lbfgs_update;
270 acc.time_progress_callback += s.time_progress_callback;
271 acc.linesearch_failures += s.linesearch_failures;
272 acc.linesearch_backtracks += s.linesearch_backtracks;
273 acc.stepsize_backtracks += s.stepsize_backtracks;
274 acc.lbfgs_failures += s.lbfgs_failures;
275 acc.lbfgs_rejected += s.lbfgs_rejected;
276 acc.τ_1_accepted += s.τ_1_accepted;
277 acc.count_τ += s.count_τ;
278 acc.sum_τ += s.sum_τ;
279 acc.final_γ = s.final_γ;
280 acc.final_ψ = s.final_ψ;
281 acc.final_h = s.final_h;
282 acc.final_φγ = s.final_φγ;
283 return acc;
284}
285// clang-format off
286ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigd);
287ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigf);)
288ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigl);)
289ALPAQA_IF_QUADF(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCOCPProgressInfo, EigenConfigq);)
290
291ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigd);
292ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigf);)
293ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigl);)
294ALPAQA_IF_QUADF(ALPAQA_EXPORT_EXTERN_TEMPLATE(class, PANOCOCPSolver, EigenConfigq);)
295// clang-format on
296
297} // namespace alpaqa
std::string get_name() const
Definition: panoc-ocp.tpp:41
std::function< void(const ProgressInfo &)> progress_cb
Definition: panoc-ocp.hpp:183
InnerSolveOptions< config_t > SolveOptions
Definition: panoc-ocp.hpp:140
Stats operator()(const P &problem, const SolveOptions &opts, rvec u, rvec y, crvec μ, rvec e)
Definition: panoc-ocp.hpp:152
AtomicStopSignal stop_signal
Definition: panoc-ocp.hpp:182
PANOCOCPSolver(const Params &params)
Definition: panoc-ocp.hpp:142
alpaqa::TypeErasedControlProblem< config_t > Problem
Definition: panoc-ocp.hpp:136
const Params & get_params() const
Definition: panoc-ocp.hpp:178
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)
Definition: panoc-ocp.hpp:158
PANOCOCPStats< config_t > Stats
Definition: panoc-ocp.hpp:138
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.
Definition: panoc-ocp.hpp:169
std::ostream * os
Definition: panoc-ocp.hpp:187
Nonlinear optimal control problem with finite horizon .
Definition: ocproblem.hpp:240
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:54
#define ALPAQA_IF_QUADF(...)
Definition: config.hpp:171
#define ALPAQA_IF_LONGD(...)
Definition: config.hpp:183
#define ALPAQA_IF_FLOAT(...)
Definition: config.hpp:177
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition: export.hpp:21
unsigned stepsize_backtracks
Definition: panoc-ocp.hpp:119
typename Conf::mvec mvec
Definition: config.hpp:65
std::chrono::nanoseconds time_jacobians
Definition: panoc-ocp.hpp:107
@ ApproxKKT
Find an ε-approximate KKT point in the ∞-norm:
real_t linesearch_tolerance_factor
Definition: panoc-ocp.hpp:59
LipschitzEstimateParams< config_t > Lipschitz
Parameters related to the Lipschitz constant estimate and step size.
Definition: panoc-ocp.hpp:20
std::chrono::nanoseconds time_hessians
Definition: panoc-ocp.hpp:108
std::chrono::nanoseconds time_lbfgs_apply
Definition: panoc-ocp.hpp:113
InnerStatsAccumulator< PANOCOCPStats< Conf > > & operator+=(InnerStatsAccumulator< PANOCOCPStats< Conf > > &acc, const PANOCOCPStats< Conf > &s)
Definition: panoc-ocp.hpp:255
unsigned max_no_progress
Maximum number of iterations without any progress before giving up.
Definition: panoc-ocp.hpp:41
real_t L_max
Maximum Lipschitz constant estimate.
Definition: panoc-ocp.hpp:34
std::chrono::nanoseconds time_lbfgs_indices
Definition: panoc-ocp.hpp:112
SolverStatus
Exit status of a numerical solver such as ALM or PANOC.
@ Busy
In progress.
std::chrono::nanoseconds time_progress_callback
Definition: panoc-ocp.hpp:115
std::chrono::nanoseconds time_indices
Definition: panoc-ocp.hpp:109
std::chrono::nanoseconds elapsed_time
Definition: panoc-ocp.hpp:103
std::chrono::nanoseconds time_backward
Definition: panoc-ocp.hpp:106
typename Conf::real_t real_t
Definition: config.hpp:63
std::chrono::nanoseconds time_lqr_factor
Definition: panoc-ocp.hpp:110
unsigned linesearch_backtracks
Definition: panoc-ocp.hpp:118
real_t min_linesearch_coefficient
Minimum weight factor between Newton step and projected gradient step, line search parameter.
Definition: panoc-ocp.hpp:27
std::chrono::nanoseconds max_time
Maximum duration.
Definition: panoc-ocp.hpp:24
typename Conf::length_t length_t
Definition: config.hpp:74
real_t quadratic_upperbound_tolerance_factor
Definition: panoc-ocp.hpp:57
real_t linesearch_strictness_factor
Parameter β used in the line search (see Algorithm 2 in ).
Definition: panoc-ocp.hpp:30
typename Conf::rvec rvec
Definition: config.hpp:67
typename Conf::crvec crvec
Definition: config.hpp:68
std::chrono::nanoseconds time_prox
Definition: panoc-ocp.hpp:104
std::chrono::nanoseconds time_forward
Definition: panoc-ocp.hpp:105
unsigned linesearch_failures
Definition: panoc-ocp.hpp:117
typename Conf::vec vec
Definition: config.hpp:64
unsigned print_interval
When to print progress.
Definition: panoc-ocp.hpp:53
unsigned L_max_inc
Maximum number of times to double the Lipschitz constant estimate per iteration.
Definition: panoc-ocp.hpp:37
LBFGSParams< config_t > lbfgs_params
L-BFGS parameters (e.g. memory).
Definition: panoc-ocp.hpp:49
int print_precision
The precision of the floating point values printed by the solver.
Definition: panoc-ocp.hpp:55
real_t L_min
Minimum Lipschitz constant estimate.
Definition: panoc-ocp.hpp:32
std::chrono::nanoseconds time_lbfgs_update
Definition: panoc-ocp.hpp:114
SolverStatus status
Definition: panoc-ocp.hpp:101
unsigned gn_interval
How often to use a Gauss-Newton step. Zero to disable GN entirely.
Definition: panoc-ocp.hpp:43
unsigned max_iter
Maximum number of inner PANOC iterations.
Definition: panoc-ocp.hpp:22
PANOCStopCrit stop_crit
What stopping criterion to use.
Definition: panoc-ocp.hpp:39
std::chrono::nanoseconds time_lqr_solve
Definition: panoc-ocp.hpp:111
Parameters for the LBFGS class.
Definition: lbfgs.hpp:39
Tuning parameters for the PANOC algorithm.
Definition: panoc-ocp.hpp:16
const TypeErasedControlProblem< config_t > * problem
Definition: panoc-ocp.hpp:88
const PANOCOCPParams< config_t > * params
Definition: panoc-ocp.hpp:89