19template <
class DirectionProv
iderT>
21 return "PANOCSolver<" + std::string(direction.get_name()) +
">";
24template <
class DirectionProv
iderT>
42 using std::chrono::nanoseconds;
43 auto os = opts.os ? opts.os : this->os;
44 auto start_time = std::chrono::steady_clock::now();
47 const auto n = problem.get_n();
48 const auto m = problem.get_m();
60 real_t ψx̂ = NaN<config_t>;
63 real_t pᵀp = NaN<config_t>;
64 real_t grad_ψᵀp = NaN<config_t>;
65 real_t hx̂ = NaN<config_t>;
66 bool have_grad_ψx̂ =
false;
70 real_t fbe()
const {
return ψx + hx̂ + pᵀp / (2 * γ) + grad_ψᵀp; }
73 : x(n), x̂(n), grad_ψ(n), grad_ψx̂(n), p(n), ŷx̂(m) {}
74 } iterates[2]{{n, m}, {n, m}};
75 Iterate *curr = &iterates[0];
76 Iterate *next = &iterates[1];
78 bool need_grad_ψx̂ = Helpers::stop_crit_requires_grad_ψx̂(params.stop_crit);
79 vec work_n(n), work_m(m);
84 auto qub_violated = [
this](
const Iterate &i) {
86 (1 + std::abs(i.ψx)) * params.quadratic_upperbound_tolerance_factor;
87 return i.ψx̂ > i.ψx + i.grad_ψᵀp +
real_t(0.5) * i.L * i.pᵀp + margin;
90 auto linesearch_violated = [
this](
const Iterate &curr,
91 const Iterate &next) {
92 if (params.force_linesearch)
94 real_t β = params.linesearch_strictness_factor;
95 real_t σ = β * (1 - curr.γ * curr.L) / (2 * curr.γ);
97 real_t margin = (1 + std::abs(φγ)) * params.linesearch_tolerance_factor;
98 return next.fbe() > φγ - σ * curr.pᵀp + margin;
103 auto eval_ψ_grad_ψ = [&problem, &y, &Σ, &work_n, &work_m](Iterate &i) {
104 i.ψx = problem.eval_ψ_grad_ψ(i.x, y, Σ, i.grad_ψ, work_n, work_m);
106 auto eval_prox_grad_step = [&problem](Iterate &i) {
107 i.hx̂ = problem.eval_prox_grad_step(i.γ, i.x, i.grad_ψ, i.x̂, i.p);
108 i.pᵀp = i.p.squaredNorm();
109 i.grad_ψᵀp = i.p.dot(i.grad_ψ);
111 auto eval_ψx̂ = [&problem, &y, &Σ, &work_n,
this](Iterate &i) {
112 if (params.eager_gradient_eval)
113 i.ψx̂ = problem.eval_ψ_grad_ψ(i.x̂, y, Σ, i.grad_ψx̂, work_n, i.ŷx̂);
115 i.ψx̂ = problem.eval_ψ(i.x̂, y, Σ, i.ŷx̂);
116 i.have_grad_ψx̂ = params.eager_gradient_eval;
118 auto eval_grad_ψx̂ = [&problem, &work_n](Iterate &i) {
119 problem.eval_grad_L(i.x̂, i.ŷx̂, i.grad_ψx̂, work_n);
120 i.have_grad_ψx̂ =
true;
125 std::array<char, 64> print_buf;
126 auto print_real = [
this, &print_buf](
real_t x) {
129 auto print_real3 = [&print_buf](
real_t x) {
132 auto print_progress_1 = [&print_real, os](
unsigned k,
real_t φₖ,
real_t ψₖ,
136 *os <<
"┌─[PANOC]\n";
138 *os <<
"├─ " << std::setw(6) << k <<
'\n';
139 *os <<
"│ φγ = " << print_real(φₖ)
140 <<
", ψ = " << print_real(ψₖ)
141 <<
", ‖∇ψ‖ = " << print_real(grad_ψₖ.norm())
142 <<
", ‖p‖ = " << print_real(std::sqrt(pₖᵀpₖ))
143 <<
", γ = " << print_real(γₖ)
144 <<
", ε = " << print_real(εₖ) <<
'\n';
146 auto print_progress_2 = [&print_real, &print_real3, os](
crvec qₖ,
real_t τₖ,
148 const char *color = τₖ == 1 ?
"\033[0;32m"
149 : τₖ > 0 ?
"\033[0;33m"
151 *os <<
"│ ‖q‖ = " << print_real(qₖ.norm())
152 <<
", τ = " << color << print_real3(τₖ) <<
"\033[0m"
154 << (reject ?
"\033[0;31mrejected\033[0m"
155 :
"\033[0;32maccepted\033[0m")
159 *os <<
"└─ " << status <<
" ──"
163 auto do_progress_cb = [
this, &s, &problem, &Σ, &y,
171 it.have_grad_ψx̂ ?
crvec{it.grad_ψx̂} :
crvec{null_vec<config_t>};
183 .grad_ψ_hat = grad_ψx̂,
191 .outer_iter = opts.outer_iter,
204 if (params.Lipschitz.L_0 <= 0) {
205 curr->L = Helpers::initial_lipschitz_estimate(
206 problem, curr->x, y, Σ, params.Lipschitz.ε, params.Lipschitz.δ,
207 params.L_min, params.L_max,
208 curr->ψx, curr->grad_ψ, curr->x̂, next->grad_ψ,
213 curr->L = params.Lipschitz.L_0;
215 eval_ψ_grad_ψ(*curr);
217 if (not std::isfinite(curr->L)) {
221 curr->γ = params.Lipschitz.Lγ_factor / curr->L;
225 eval_prox_grad_step(*curr);
229 while (curr->L < params.L_max && qub_violated(*curr)) {
232 eval_prox_grad_step(*curr);
242 unsigned no_progress = 0;
253 if (need_grad_ψx̂ && !curr->have_grad_ψx̂)
254 eval_grad_ψx̂(*curr);
256 real_t εₖ = Helpers::calc_error_stop_crit(
257 problem, params.stop_crit, curr->p, curr->γ, curr->x, curr->x̂,
258 curr->ŷx̂, curr->grad_ψ, curr->grad_ψx̂, work_n, next->p);
262 params.print_interval != 0 && k % params.print_interval == 0;
264 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ, curr->pᵀp,
269 auto time_elapsed = std::chrono::steady_clock::now() - start_time;
270 auto stop_status = Helpers::check_all_stop_conditions(
271 params, opts, time_elapsed, k, stop_signal, εₖ, no_progress);
273 do_progress_cb(k, *curr, null_vec<config_t>, -1, εₖ, stop_status);
274 bool do_final_print = params.print_interval != 0;
275 if (!do_print && do_final_print)
276 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ,
277 curr->pᵀp, curr->γ, εₖ);
278 if (do_print || do_final_print)
279 print_progress_n(stop_status);
282 opts.always_overwrite_results) {
284 if (err_z.size() > 0)
285 err_z = Σ.asDiagonal().inverse() * (ŷ - y);
286 x = std::move(curr->x̂);
287 y = std::move(curr->ŷx̂);
291 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
302 real_t τ_init = NaN<config_t>;
305 direction.initialize(problem, y, Σ, curr->γ, curr->x, curr->x̂,
306 curr->p, curr->grad_ψ);
309 if (k > 0 || direction.has_initial_direction()) {
310 τ_init = direction.apply(curr->γ, curr->x, curr->x̂, curr->p,
315 if (τ_init == 1 && not q.allFinite())
329 bool update_lbfgs_in_linesearch = params.update_direction_in_candidate;
330 bool updated_lbfgs =
false;
331 bool dir_rejected =
true;
334 auto take_safe_step = [&] {
336 if (not curr->have_grad_ψx̂)
337 eval_grad_ψx̂(*curr);
339 next->ψx = curr->ψx̂;
340 next->grad_ψ.swap(curr->grad_ψx̂);
341 curr->have_grad_ψx̂ = next->have_grad_ψx̂ =
false;
345 auto take_accelerated_step = [&](
real_t τ) {
347 next->x = curr->x + q;
349 next->x = curr->x + (1 - τ) * curr->p + τ * q;
351 eval_ψ_grad_ψ(*next);
352 next->have_grad_ψx̂ =
false;
355 while (!stop_signal.stop_requested()) {
359 τ != 0 ? take_accelerated_step(τ) : take_safe_step();
366 bool fail = !std::isfinite(next->ψx);
367 fail |= next->L >= params.L_max && !(curr->L >= params.L_max);
377 update_lbfgs_in_linesearch =
false;
382 eval_prox_grad_step(*next);
386 if (next->L < params.L_max && qub_violated(*next)) {
394 update_lbfgs_in_linesearch =
false;
399 if (update_lbfgs_in_linesearch && !updated_lbfgs) {
401 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
402 curr->grad_ψ, next->grad_ψ);
403 update_lbfgs_in_linesearch =
false;
404 updated_lbfgs =
true;
408 if (τ > 0 && linesearch_violated(*curr, *next)) {
410 if (τ < params.min_linesearch_coefficient)
426 if (no_progress > 0 || k % params.max_no_progress == 0)
427 no_progress = curr->x == next->x ? no_progress + 1 : 0;
431 if (!updated_lbfgs) {
432 if (curr->γ != next->γ) {
433 direction.changed_γ(next->γ, curr->γ);
434 if (params.recompute_last_prox_step_after_lbfgs_flush) {
437 eval_prox_grad_step(*curr);
441 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
442 curr->grad_ψ, next->grad_ψ);
447 if (do_print && (k != 0 || direction.has_initial_direction()))
448 print_progress_2(q, τ, dir_rejected);
451 std::swap(curr, next);
454 throw std::logic_error(
"[PANOC] loop error");
std::string get_name() const
Stats operator()(const Problem &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec err_z)
unsigned stepsize_backtracks
SolverStatus
Exit status of a numerical solver such as ALM or PANOC.
@ Interrupted
Solver was interrupted by the user.
@ Converged
Converged and reached given tolerance.
@ NotFinite
Intermediate results were infinite or not-a-number.
std::chrono::nanoseconds time_progress_callback
std::chrono::nanoseconds elapsed_time
typename Conf::real_t real_t
unsigned linesearch_backtracks
typename Conf::length_t length_t
std::string_view float_to_str_vw(auto &buf, double value, int precision=std::numeric_limits< double >::max_digits10)
typename Conf::crvec crvec
unsigned linesearch_failures