20template <
class DirectionProv
iderT>
22 return "ZeroFPRSolver<" + std::string(direction.get_name()) +
">";
25template <
class DirectionProv
iderT>
43 using std::chrono::nanoseconds;
44 auto os = opts.os ? opts.os : this->os;
45 auto start_time = std::chrono::steady_clock::now();
48 const auto n = problem.get_n();
49 const auto m = problem.get_m();
57 real_t pᵀp = NaN<config_t>;
58 real_t grad_ψᵀp = NaN<config_t>;
59 real_t hx̂ = NaN<config_t>;
72 real_t ψx̂ = NaN<config_t>;
75 real_t pᵀp = NaN<config_t>;
76 real_t grad_ψᵀp = NaN<config_t>;
77 real_t hx̂ = NaN<config_t>;
81 real_t fbe()
const {
return ψx + hx̂ + pᵀp / (2 * γ) + grad_ψᵀp; }
84 } iterates[2]{{n, m}, {n, m}};
85 Iterate *curr = &iterates[0];
86 ProxIterate *prox = &prox_iterate;
87 Iterate *next = &iterates[1];
89 vec work_n(n), work_m(m);
94 auto qub_violated = [
this](
const Iterate &i) {
96 (1 + std::abs(i.ψx)) * params.quadratic_upperbound_tolerance_factor;
97 return i.ψx̂ > i.ψx + i.grad_ψᵀp +
real_t(0.5) * i.L * i.pᵀp + margin;
100 auto linesearch_violated = [
this](
const Iterate &curr,
101 const Iterate &next) {
102 if (params.force_linesearch)
104 real_t β = params.linesearch_strictness_factor;
105 real_t σ = β * (1 - curr.γ * curr.L) / (2 * curr.γ);
107 real_t margin = (1 + std::abs(φγ)) * params.linesearch_tolerance_factor;
108 return next.fbe() > φγ - σ * curr.pᵀp + margin;
113 auto eval_ψ_grad_ψ = [&problem, &y, &Σ, &work_n, &work_m](Iterate &i) {
114 i.ψx = problem.eval_ψ_grad_ψ(i.x, y, Σ, i.grad_ψ, work_n, work_m);
116 auto eval_prox_grad_step = [&problem](Iterate &i) {
117 i.hx̂ = problem.eval_prox_grad_step(i.γ, i.x, i.grad_ψ, i.x̂, i.p);
118 i.pᵀp = i.p.squaredNorm();
119 i.grad_ψᵀp = i.p.dot(i.grad_ψ);
121 auto eval_cost_in_prox = [&problem, &y, &Σ](Iterate &i) {
122 i.ψx̂ = problem.eval_ψ(i.x̂, y, Σ, i.ŷx̂);
124 auto eval_grad_in_prox = [&problem, &prox, &work_n](
const Iterate &i) {
125 problem.eval_grad_L(i.x̂, i.ŷx̂, prox->grad_ψ, work_n);
127 auto eval_prox_grad_step_in_prox = [&problem, &prox](
const Iterate &i) {
128 prox->hx̂ = problem.eval_prox_grad_step(i.γ, i.x̂, prox->grad_ψ, prox->x̂,
130 prox->pᵀp = prox->p.squaredNorm();
131 prox->grad_ψᵀp = prox->p.dot(prox->grad_ψ);
136 std::array<char, 64> print_buf;
137 auto print_real = [
this, &print_buf](
real_t x) {
140 auto print_real3 = [&print_buf](
real_t x) {
143 auto print_progress_1 = [&print_real, os](
unsigned k,
real_t φₖ,
real_t ψₖ,
147 *os <<
"┌─[ZeroFPR]\n";
149 *os <<
"├─ " << std::setw(6) << k <<
'\n';
150 *os <<
"│ φγ = " << print_real(φₖ)
151 <<
", ψ = " << print_real(ψₖ)
152 <<
", ‖∇ψ‖ = " << print_real(grad_ψₖ.norm())
153 <<
", ‖p‖ = " << print_real(std::sqrt(pₖᵀpₖ))
154 <<
", γ = " << print_real(γₖ)
155 <<
", ε = " << print_real(εₖ) <<
'\n';
157 auto print_progress_2 = [&print_real, &print_real3, os](
crvec qₖ,
159 *os <<
"│ ‖q‖ = " << print_real(qₖ.norm())
160 <<
", τ = " << print_real3(τₖ)
164 *os <<
"└─ " << status <<
" ──"
168 auto do_progress_cb = [
this, &s, &problem, &Σ, &y, &opts](
169 unsigned k, Iterate &it,
crvec q,
crvec grad_ψx̂,
187 .grad_ψ_hat = grad_ψx̂,
195 .outer_iter = opts.outer_iter,
208 if (params.Lipschitz.L_0 <= 0) {
209 curr->L = Helpers::initial_lipschitz_estimate(
210 problem, curr->x, y, Σ, params.Lipschitz.ε, params.Lipschitz.δ,
211 params.L_min, params.L_max,
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;
230 eval_prox_grad_step(*curr);
231 eval_cost_in_prox(*curr);
234 while (curr->L < params.L_max && qub_violated(*curr)) {
237 eval_prox_grad_step(*curr);
238 eval_cost_in_prox(*curr);
246 unsigned no_progress = 0;
257 eval_grad_in_prox(*curr);
258 eval_prox_grad_step_in_prox(*curr);
260 real_t εₖ = Helpers::calc_error_stop_crit(
261 problem, params.stop_crit, curr->p, curr->γ, curr->x, curr->x̂,
262 curr->ŷx̂, curr->grad_ψ, prox->grad_ψ, 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;
274 auto stop_status = Helpers::check_all_stop_conditions(
275 params, opts, time_elapsed, k, stop_signal, εₖ, no_progress);
277 do_progress_cb(k, *curr, null_vec<config_t>, prox->grad_ψ, -1, εₖ,
279 bool do_final_print = params.print_interval != 0;
280 if (!do_print && do_final_print)
281 print_progress_1(k, curr->fbe(), curr->ψx, curr->grad_ψ,
282 curr->pᵀp, curr->γ, εₖ);
283 if (do_print || do_final_print)
284 print_progress_n(stop_status);
287 opts.always_overwrite_results) {
289 if (err_z.size() > 0)
290 err_z = Σ.asDiagonal().inverse() * (ŷ - y);
291 x = std::move(curr->x̂);
292 y = std::move(curr->ŷx̂);
296 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
307 real_t τ_init = NaN<config_t>;
310 direction.initialize(problem, y, Σ, curr->γ, curr->x̂, prox->x̂,
311 prox->p, prox->grad_ψ);
314 if (k > 0 || direction.has_initial_direction()) {
315 τ_init = direction.apply(curr->γ, curr->x̂, prox->x̂, prox->p,
320 if (τ_init == 1 && not q.allFinite())
334 bool update_lbfgs_in_linesearch = params.update_direction_in_candidate;
335 bool update_lbfgs_later = !update_lbfgs_in_linesearch;
338 auto take_safe_step = [&] {
340 next->ψx = curr->ψx̂;
341 next->grad_ψ = prox->grad_ψ;
346 auto take_accelerated_step = [&](
real_t τ) {
348 next->x = curr->x̂ + q;
350 next->x = curr->x̂ + τ * q;
352 eval_ψ_grad_ψ(*next);
355 while (!stop_signal.stop_requested()) {
359 τ != 0 ? take_accelerated_step(τ) : take_safe_step();
365 if (τ > 0 && !std::isfinite(next->ψx)) {
372 eval_prox_grad_step(*next);
373 eval_cost_in_prox(*next);
376 if (next->L < params.L_max && qub_violated(*next)) {
381 update_lbfgs_in_linesearch =
false;
382 update_lbfgs_later =
true;
387 if (τ == 1 && update_lbfgs_in_linesearch) {
388 if (params.update_direction_from_prox_step) {
390 curr->γ, next->γ, curr->x̂, next->x, prox->p, next->p,
391 prox->grad_ψ, next->grad_ψ);
394 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
395 curr->grad_ψ, next->grad_ψ);
397 update_lbfgs_in_linesearch =
false;
398 update_lbfgs_later =
false;
402 if (τ > 0 && linesearch_violated(*curr, *next)) {
404 if (τ < params.min_linesearch_coefficient)
420 if (no_progress > 0 || k % params.max_no_progress == 0)
421 no_progress = curr->x == next->x ? no_progress + 1 : 0;
425 if (τ_init < 1 || update_lbfgs_later) {
426 if (curr->γ != next->γ) {
427 direction.changed_γ(next->γ, curr->γ);
428 if (params.recompute_last_prox_step_after_lbfgs_flush) {
431 eval_prox_grad_step_in_prox(*curr);
434 if (τ > 0 && params.update_direction_from_prox_step) {
436 curr->γ, next->γ, curr->x̂, next->x, prox->p, next->p,
437 prox->grad_ψ, next->grad_ψ);
440 curr->γ, next->γ, curr->x, next->x, curr->p, next->p,
441 curr->grad_ψ, next->grad_ψ);
447 if (do_print && (k != 0 || direction.has_initial_direction()))
448 print_progress_2(q, τ);
451 std::swap(curr, next);
462 throw std::logic_error(
"[ZeroFPR] 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