19template <
class InnerSolverT>
20typename ALMSolver<InnerSolverT>::Stats
22 using std::chrono::duration_cast;
23 using std::chrono::nanoseconds;
24 auto start_time = std::chrono::steady_clock::now();
35 .max_time = params.max_time,
36 .tolerance = params.tolerance,
40 auto ps = inner_solver(p, opts, x, y, Σ, error);
42 auto time_elapsed = std::chrono::steady_clock::now() - start_time;
49 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
54 constexpr auto NaN = alpaqa::NaN<config_t>;
55 vec Σ = vec::Constant(m,
NaN);
56 vec Σ_old = vec::Constant(m,
NaN);
57 vec error_1 = vec::Constant(m,
NaN);
58 vec error_2 = vec::Constant(m,
NaN);
62 std::array<char, 64> printbuf;
63 auto print_real = [&](
real_t x) {
70 if (params.initial_penalty > 0) {
71 Σ.fill(params.initial_penalty);
75 Helpers::initialize_penalty(p, params, x, Σ);
78 real_t ε = params.initial_tolerance;
80 real_t Δ = params.penalty_update_factor;
81 real_t ρ = params.tolerance_update_factor;
83 unsigned num_successful_iters = 0;
85 for (
unsigned i = 0; i < params.max_iter; ++i) {
88 p.eval_proj_multipliers(y, params.max_multiplier);
90 bool out_of_penalty_factor_updates =
91 (num_successful_iters == 0
95 params.max_total_num_retries);
96 bool out_of_iter = i + 1 == params.max_iter;
100 bool overwrite_results = out_of_iter || out_of_penalty_factor_updates;
105 auto time_elapsed = std::chrono::steady_clock::now() - start_time;
108 .max_time = params.max_time - time_elapsed,
116 auto ps = inner_solver(p, opts, x, y, Σ, error_2);
123 time_elapsed = std::chrono::steady_clock::now() - start_time;
124 bool out_of_time = time_elapsed > params.max_time;
126 not inner_converged && not overwrite_results && not out_of_time;
129 if (params.print_interval != 0 && i % params.print_interval == 0) {
131 const char *color = inner_converged ?
"\x1b[0;32m" :
"\x1b[0;31m";
132 const char *color_end =
"\x1b[0m";
133 *os <<
"[\x1b[0;34mALM\x1b[0m] " << std::setw(5) << i
134 <<
": ‖Σ‖ = " << print_real(Σ.norm())
135 <<
", ‖y‖ = " << print_real(y.norm())
136 <<
", δ = " << print_real(δ) <<
", ε = " << print_real(ps.ε)
137 <<
", Δ = " << print_real(Δ) <<
", status = " << color
138 << std::setw(13) << ps.status << color_end
139 <<
", iter = " << std::setw(13) << ps.iterations
149 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
168 if (num_successful_iters > 0) {
171 Δ = std::fmax(params.min_penalty_update_factor,
172 Δ * params.penalty_update_factor_lower);
173 Helpers::update_penalty_weights(params, Δ,
false, error_1,
174 error_2, norm_e_1, norm_e_2,
177 ρ = std::fmin(params.ρ_max, ρ * params.ρ_increase);
178 ε = std::fmax(ρ * ε_old, params.tolerance);
183 Σ *= params.initial_penalty_lower;
184 ε *= params.initial_tolerance_increase;
194 error_2.swap(error_1);
198 bool alm_converged = ps.ε <= params.tolerance && inner_converged &&
199 norm_e_1 <= params.dual_tolerance;
200 bool exit = alm_converged || out_of_iter || out_of_time;
206 s.
elapsed_time = duration_cast<nanoseconds>(time_elapsed);
217 Helpers::update_penalty_weights(
218 params, Δ, num_successful_iters == 0, error_1, error_2,
219 norm_e_1, norm_e_2, Σ_old, Σ,
true);
221 ε_old = std::exchange(ε, std::fmax(ρ * ε, params.tolerance));
222 ++num_successful_iters;
225 throw std::logic_error(
"[ALM] loop error");
unsigned penalty_reduced
The number of times that the penalty update factor ALMParams::penalty_update_factor was reduced,...
real_t δ
Final dual tolerance or constraint violation that was reached:
real_t norm_penalty
2-norm of the final penalty factors .
typename InnerSolver::Problem Problem
std::chrono::nanoseconds elapsed_time
Total elapsed time.
unsigned initial_penalty_reduced
The number of times that the initial penalty factor was reduced by ALMParams::initial_penalty_lower a...
Stats operator()(const Problem &problem, rvec x, rvec y)
InnerStatsAccumulator< typename InnerSolver::Stats > inner
The statistics of the inner solver invocations, accumulated over all ALM iterations.
unsigned inner_convergence_failures
The total number of times that the inner solver failed to converge.
real_t ε
Final primal tolerance that was reached, depends on the stopping criterion used by the inner solver,...
unsigned outer_iterations
Total number of outer ALM iterations (i.e.
SolverStatus status
Whether the solver converged or not.
auto norm_inf(const Eigen::MatrixBase< Derived > &v)
Get the maximum or infinity-norm of the given vector.
@ Interrupted
Solver was interrupted by the user.
@ MaxTime
Maximum allowed execution time exceeded.
@ MaxIter
Maximum number of iterations exceeded.
@ Converged
Converged and reached given tolerance.
typename Conf::real_t real_t
bool always_overwrite_results
Return the final iterate and multipliers, even if the solver did not converge.
std::string_view float_to_str_vw(auto &buf, double value, int precision=std::numeric_limits< double >::max_digits10)