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_num_variables();
48 const auto m = problem.get_num_constraints();
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];
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)
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_augmented_lagrangian_and_gradient(
105 i.x, y, Σ, i.grad_ψ, work_n, work_m);
107 auto eval_prox_grad_step = [&problem](Iterate &i) {
109 problem.eval_proximal_gradient_step(i.γ, i.x, i.grad_ψ, i.x̂, i.p);
110 i.pᵀp = i.p.squaredNorm();
111 i.grad_ψᵀp = i.p.dot(i.grad_ψ);
113 auto eval_ψx̂ = [&problem, &y, &Σ, &work_n,
this](Iterate &i) {
114 if (
params.eager_gradient_eval)
115 i.ψx̂ = problem.eval_augmented_lagrangian_and_gradient(
116 i.x̂, y, Σ, i.grad_ψx̂, work_n, i.ŷx̂);
118 i.ψx̂ = problem.eval_augmented_lagrangian(i.x̂, y, Σ, i.ŷx̂);
119 i.have_grad_ψx̂ =
params.eager_gradient_eval;
121 auto eval_grad_ψx̂ = [&problem, &work_n](Iterate &i) {
122 problem.eval_lagrangian_gradient(i.x̂, i.ŷx̂, i.grad_ψx̂, work_n);
123 i.have_grad_ψx̂ =
true;
128 std::array<char, 64> print_buf;
129 auto print_real = [
this, &print_buf](
real_t x) {
130 return float_to_str_vw(print_buf, x,
params.print_precision);
132 auto print_real3 = [&print_buf](
real_t x) {
133 return float_to_str_vw(print_buf, x, 3);
135 auto print_progress_1 = [&print_real,
os](
unsigned k,
real_t φₖ,
real_t ψₖ,
139 *
os <<
"┌─[PANOC]\n";
141 *
os <<
"├─ " << std::setw(6) << k <<
'\n';
142 *
os <<
"│ φγ = " << print_real(φₖ)
143 <<
", ψ = " << print_real(ψₖ)
144 <<
", ‖∇ψ‖ = " << print_real(grad_ψₖ.norm())
145 <<
", ‖p‖ = " << print_real(std::sqrt(pₖᵀpₖ))
146 <<
", γ = " << print_real(γₖ)
147 <<
", ε = " << print_real(εₖ) <<
'\n';
149 auto print_progress_2 = [&print_real, &print_real3,
os](
crvec qₖ,
real_t τₖ,
151 const char *color = τₖ == 1 ?
"\033[0;32m"
152 : τₖ > 0 ?
"\033[0;33m"
154 *
os <<
"│ ‖q‖ = " << print_real(qₖ.norm())
155 <<
", τ = " << color << print_real3(τₖ) <<
"\033[0m"
157 << (reject ?
"\033[0;31mrejected\033[0m"
158 :
"\033[0;32maccepted\033[0m")
162 *
os <<
"└─ " << status <<
" ──"
166 auto do_progress_cb = [
this, &s, &problem, &Σ, &y,
187 .grad_ψ_hat = grad_ψx̂,
195 .outer_iter = opts.outer_iter,
208 if (
params.Lipschitz.L_0 <= 0) {
210 problem, curr->x, y, Σ,
params.Lipschitz.ε,
params.Lipschitz.δ,
212 curr->ψx, curr->grad_ψ, curr->x̂, next->grad_ψ,
217 curr->L =
params.Lipschitz.L_0;
219 eval_ψ_grad_ψ(*curr);
221 if (not std::isfinite(curr->L)) {
225 curr->γ =
params.Lipschitz.Lγ_factor / curr->L;
229 eval_prox_grad_step(*curr);
233 while (curr->L <
params.L_max && qub_violated(*curr)) {
236 eval_prox_grad_step(*curr);
246 unsigned no_progress = 0;
257 if (need_grad_ψx̂ && !curr->have_grad_ψx̂)
258 eval_grad_ψx̂(*curr);
261 problem,
params.stop_crit, curr->p, curr->γ, curr->x, curr->x̂,
262 curr->ŷx̂, curr->grad_ψ, curr->grad_ψx̂, work_n, next->p);
266 params.print_interval != 0 && k %
params.print_interval == 0;
268 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ, curr->pᵀp,
273 auto time_elapsed = std::chrono::steady_clock::now() - start_time;
278 bool do_final_print =
params.print_interval != 0;
279 if (!do_print && do_final_print)
280 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ,
281 curr->pᵀp, curr->γ, εₖ);
282 if (do_print || do_final_print)
283 print_progress_n(stop_status);
286 opts.always_overwrite_results) {
288 if (
params.eager_gradient_eval)
291 if (err_z.size() > 0)
292 err_z = (ŷ - y).cwiseQuotient(Σ);
298 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
312 direction.initialize(problem, y, Σ, curr->γ, curr->x, curr->x̂,
313 curr->p, curr->grad_ψ);
316 if (k > 0 ||
direction.has_initial_direction()) {
317 τ_init =
direction.apply(curr->γ, curr->x, curr->x̂, curr->p,
322 if (τ_init == 1 && not q.allFinite())
336 bool update_lbfgs_in_linesearch =
params.update_direction_in_candidate;
337 bool updated_lbfgs =
false;
338 bool dir_rejected =
true;
341 auto take_safe_step = [&] {
343 if (not curr->have_grad_ψx̂)
344 eval_grad_ψx̂(*curr);
346 next->ψx = curr->ψx̂;
347 next->grad_ψ.swap(curr->grad_ψx̂);
348 curr->have_grad_ψx̂ = next->have_grad_ψx̂ =
false;
352 auto take_accelerated_step = [&](
real_t τ) {
354 next->x = curr->x + q;
356 next->x = curr->x + (1 - τ) * curr->p + τ * q;
358 eval_ψ_grad_ψ(*next);
359 next->have_grad_ψx̂ =
false;
366 τ != 0 ? take_accelerated_step(τ) : take_safe_step();
373 bool fail = !std::isfinite(next->ψx);
374 fail |= next->L >=
params.L_max && !(curr->L >=
params.L_max);
384 update_lbfgs_in_linesearch =
false;
389 eval_prox_grad_step(*next);
393 if (next->L <
params.L_max && qub_violated(*next)) {
401 update_lbfgs_in_linesearch =
false;
406 if (update_lbfgs_in_linesearch && !updated_lbfgs) {
408 not
direction.update(curr->γ, next->γ, curr->x, next->x,
409 curr->p, next->p, curr->grad_ψ,
411 update_lbfgs_in_linesearch =
false;
412 updated_lbfgs =
true;
416 if (τ > 0 && linesearch_violated(*curr, *next)) {
417 τ *=
params.linesearch_coefficient_update_factor;
418 if (τ <
params.min_linesearch_coefficient)
434 if (no_progress > 0 || k %
params.max_no_progress == 0)
435 no_progress = curr->x == next->x ? no_progress + 1 : 0;
439 if (!updated_lbfgs) {
440 if (curr->γ != next->γ) {
442 if (
params.recompute_last_prox_step_after_stepsize_change) {
445 eval_prox_grad_step(*curr);
449 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
450 curr->grad_ψ, next->grad_ψ);
455 if (do_print && (k != 0 ||
direction.has_initial_direction()))
456 print_progress_2(q, τ, dir_rejected);
459 std::swap(curr, next);
462 throw std::logic_error(
"[PANOC] loop error");