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();
59 real_t ψx̂ = NaN<config_t>;
62 real_t pᵀp = NaN<config_t>;
63 real_t grad_ψᵀp = NaN<config_t>;
64 real_t hx̂ = NaN<config_t>;
68 real_t fbe()
const {
return ψx + hx̂ + pᵀp / (2 * γ) + grad_ψᵀp; }
71 } iterates[2]{{n, m}, {n, m}};
72 Iterate *curr = &iterates[0];
73 Iterate *next = &iterates[1];
75 bool need_grad_ψx̂ = Helpers::stop_crit_requires_grad_ψx̂(params.stop_crit);
77 vec work_n(n), work_m(m);
82 auto qub_violated = [
this](
const Iterate &i) {
84 (1 + std::abs(i.ψx)) * params.quadratic_upperbound_tolerance_factor;
85 return i.ψx̂ > i.ψx + i.grad_ψᵀp +
real_t(0.5) * i.L * i.pᵀp + margin;
88 auto linesearch_violated = [
this](
const Iterate &curr,
89 const Iterate &next) {
90 if (params.force_linesearch)
92 real_t β = params.linesearch_strictness_factor;
93 real_t σ = β * (1 - curr.γ * curr.L) / (2 * curr.γ);
95 real_t margin = (1 + std::abs(φγ)) * params.linesearch_tolerance_factor;
96 return next.fbe() > φγ - σ * curr.pᵀp + margin;
101 auto eval_ψ_grad_ψ = [&problem, &y, &Σ, &work_n, &work_m](Iterate &i) {
102 i.ψx = problem.eval_ψ_grad_ψ(i.x, y, Σ, i.grad_ψ, work_n, work_m);
104 auto eval_prox_grad_step = [&problem](Iterate &i) {
105 i.hx̂ = problem.eval_prox_grad_step(i.γ, i.x, i.grad_ψ, i.x̂, i.p);
106 i.pᵀp = i.p.squaredNorm();
107 i.grad_ψᵀp = i.p.dot(i.grad_ψ);
109 auto eval_ψx̂ = [&problem, &y, &Σ](Iterate &i) {
110 i.ψx̂ = problem.eval_ψ(i.x̂, y, Σ, i.ŷx̂);
112 auto eval_grad_ψx̂ = [&problem, &work_n](Iterate &i,
rvec grad_ψx̂) {
113 problem.eval_grad_L(i.x̂, i.ŷx̂, grad_ψx̂, work_n);
118 std::array<char, 64> print_buf;
119 auto print_real = [
this, &print_buf](
real_t x) {
122 auto print_real3 = [&print_buf](
real_t x) {
125 auto print_progress_1 = [&print_real, os](
unsigned k,
real_t φₖ,
real_t ψₖ,
129 *os <<
"┌─[PANOC]\n";
131 *os <<
"├─ " << std::setw(6) << k <<
'\n';
132 *os <<
"│ φγ = " << print_real(φₖ)
133 <<
", ψ = " << print_real(ψₖ)
134 <<
", ‖∇ψ‖ = " << print_real(grad_ψₖ.norm())
135 <<
", ‖p‖ = " << print_real(std::sqrt(pₖᵀpₖ))
136 <<
", γ = " << print_real(γₖ)
137 <<
", ε = " << print_real(εₖ) <<
'\n';
139 auto print_progress_2 = [&print_real, &print_real3, os](
crvec qₖ,
real_t τₖ,
141 const char *color = τₖ == 1 ?
"\033[0;32m"
142 : τₖ > 0 ?
"\033[0;33m"
144 *os <<
"│ ‖q‖ = " << print_real(qₖ.norm())
145 <<
", τ = " << color << print_real3(τₖ) <<
"\033[0m"
147 << (reject ?
"\033[0;31mrejected\033[0m"
148 :
"\033[0;32maccepted\033[0m")
152 *os <<
"└─ " << status <<
" ──"
156 auto do_progress_cb = [
this, &s, &problem, &Σ, &y, &opts](
157 unsigned k, Iterate &it,
crvec q,
crvec grad_ψx̂,
174 .grad_ψ_hat = grad_ψx̂,
182 .outer_iter = opts.outer_iter,
195 if (params.Lipschitz.L_0 <= 0) {
196 curr->L = Helpers::initial_lipschitz_estimate(
197 problem, curr->x, y, Σ, params.Lipschitz.ε, params.Lipschitz.δ,
198 params.L_min, params.L_max,
199 curr->ψx, curr->grad_ψ, curr->x̂, next->grad_ψ,
204 curr->L = params.Lipschitz.L_0;
206 eval_ψ_grad_ψ(*curr);
208 if (not std::isfinite(curr->L)) {
212 curr->γ = params.Lipschitz.Lγ_factor / curr->L;
216 eval_prox_grad_step(*curr);
220 while (curr->L < params.L_max && qub_violated(*curr)) {
223 eval_prox_grad_step(*curr);
233 unsigned no_progress = 0;
245 eval_grad_ψx̂(*curr, grad_ψx̂);
246 bool have_grad_ψx̂ = need_grad_ψx̂;
248 real_t εₖ = Helpers::calc_error_stop_crit(
249 problem, params.stop_crit, curr->p, curr->γ, curr->x, curr->x̂,
250 curr->ŷx̂, curr->grad_ψ, grad_ψx̂, work_n, next->p);
254 params.print_interval != 0 && k % params.print_interval == 0;
256 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ, curr->pᵀp,
261 auto time_elapsed = std::chrono::steady_clock::now() - start_time;
262 auto stop_status = Helpers::check_all_stop_conditions(
263 params, opts, time_elapsed, k, stop_signal, εₖ, no_progress);
265 do_progress_cb(k, *curr, null_vec<config_t>, grad_ψx̂, -1, εₖ,
267 bool do_final_print = params.print_interval != 0;
268 if (!do_print && do_final_print)
269 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ,
270 curr->pᵀp, curr->γ, εₖ);
271 if (do_print || do_final_print)
272 print_progress_n(stop_status);
275 opts.always_overwrite_results) {
277 if (err_z.size() > 0)
278 err_z = Σ.asDiagonal().inverse() * (ŷ - y);
279 x = std::move(curr->x̂);
280 y = std::move(curr->ŷx̂);
284 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
295 real_t τ_init = NaN<config_t>;
298 direction.initialize(problem, y, Σ, curr->γ, curr->x, curr->x̂,
299 curr->p, curr->grad_ψ);
302 if (k > 0 || direction.has_initial_direction()) {
303 τ_init = direction.apply(curr->γ, curr->x, curr->x̂, curr->p,
308 if (τ_init == 1 && not q.allFinite())
322 bool update_lbfgs_in_linesearch = params.update_direction_in_candidate;
323 bool updated_lbfgs =
false;
324 bool dir_rejected =
true;
327 auto take_safe_step = [&] {
329 if (not have_grad_ψx̂)
330 eval_grad_ψx̂(*curr, grad_ψx̂);
331 have_grad_ψx̂ =
true;
333 next->ψx = curr->ψx̂;
334 next->grad_ψ.swap(grad_ψx̂);
338 auto take_accelerated_step = [&](
real_t τ) {
340 next->x = curr->x + q;
342 next->x = curr->x + (1 - τ) * curr->p + τ * q;
344 eval_ψ_grad_ψ(*next);
347 while (!stop_signal.stop_requested()) {
351 τ != 0 ? take_accelerated_step(τ) : take_safe_step();
358 bool fail = next->L >= params.L_max || !std::isfinite(next->ψx);
368 update_lbfgs_in_linesearch =
false;
373 eval_prox_grad_step(*next);
377 if (next->L < params.L_max && qub_violated(*next)) {
385 update_lbfgs_in_linesearch =
false;
390 if (update_lbfgs_in_linesearch && !updated_lbfgs) {
392 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
393 curr->grad_ψ, next->grad_ψ);
394 update_lbfgs_in_linesearch =
false;
395 updated_lbfgs =
true;
399 if (τ > 0 && linesearch_violated(*curr, *next)) {
401 if (τ < params.min_linesearch_coefficient)
417 if (no_progress > 0 || k % params.max_no_progress == 0)
418 no_progress = curr->x == next->x ? no_progress + 1 : 0;
422 if (!updated_lbfgs) {
423 if (curr->γ != next->γ) {
424 direction.changed_γ(next->γ, curr->γ);
425 if (params.recompute_last_prox_step_after_lbfgs_flush) {
428 eval_prox_grad_step(*curr);
432 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
433 curr->grad_ψ, next->grad_ψ);
438 if (do_print && (k != 0 || direction.has_initial_direction()))
439 print_progress_2(q, τ, dir_rejected);
442 std::swap(curr, next);
445 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