alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
alm-driver.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include "options.hpp"
8#include "problem.hpp"
9#include "results.hpp"
10
11template <class InnerSolver>
12auto make_alm_solver(InnerSolver &&inner_solver, Options &opts) {
13 // Settings for the ALM solver
14 using ALMSolver = alpaqa::ALMSolver<InnerSolver>;
15 typename ALMSolver::Params alm_param;
16 alm_param.max_iter = 200;
17 alm_param.tolerance = 1e-8;
18 alm_param.dual_tolerance = 1e-8;
19 alm_param.print_interval = 1;
20 alm_param.print_precision = 1;
21 set_params(alm_param, "alm", opts);
22 return ALMSolver{alm_param, std::forward<InnerSolver>(inner_solver)};
23}
24
26
27template <class InnerSolver>
29 // Settings for the solver
30 typename InnerSolver::Params solver_param;
31 solver_param.max_iter = 50'000;
32 solver_param.print_interval = 0;
33 solver_param.stop_crit = alpaqa::PANOCStopCrit::ProjGradUnitNorm;
34 set_params(solver_param, "solver", opts);
35
36 if constexpr (requires { typename InnerSolver::Direction; }) {
37 // Settings for the direction provider
38 using Direction = typename InnerSolver::Direction;
39 typename Direction::DirectionParams dir_param;
40 typename Direction::AcceleratorParams accel_param;
41 set_params(dir_param, "dir", opts);
42 set_params(accel_param, "accel", opts);
43 return InnerSolver{
44 solver_param,
45 Direction{{
46 .accelerator = accel_param,
47 .direction = dir_param,
48 }},
49 };
50 } else {
51 return InnerSolver{solver_param};
52 }
53}
54
55template <class Solver>
57 std::ostream &os, unsigned N_exp) {
58
59 solver.os = &os;
60
61 // Initial guess
62 vec x = problem.initial_guess_x, y = problem.initial_guess_y;
63 // Final penalties
64 vec Σ = vec::Constant(problem.problem.get_m(), alpaqa::NaN<config_t>);
65
66 // Solve the problem
67 auto stats = solver(problem.problem, x, y, Σ);
68
69 // Store the evaluation counters
70 auto evals = *problem.evaluations;
71
72 // Solve the problems again to average runtimes
73 auto avg_duration = stats.elapsed_time;
74 os.setstate(std::ios_base::badbit); // suppress output
75 for (unsigned i = 0; i < N_exp; ++i) {
76 x = problem.initial_guess_x;
77 y = problem.initial_guess_y;
78 auto s = solver(problem.problem, x, y);
79 if (s.status == alpaqa::SolverStatus::Interrupted) {
80 os.clear();
81 os << "\rInterrupted after " << i << " runs" << std::endl;
82 N_exp = i;
83 break;
84 }
85 avg_duration += s.elapsed_time;
86 }
87 os.clear();
88 avg_duration /= (N_exp + 1);
89
90 solver.os = &std::cout;
91
92 // Results
93 real_t final_γ = 0, final_h = 0;
94 if constexpr (requires { stats.inner.final_γ; })
95 final_γ = stats.inner.final_γ;
96 if constexpr (requires { stats.inner.final_h; })
97 final_h = stats.inner.final_h;
98 decltype(SolverResults::extra) extra{};
99 if constexpr (requires { stats.inner.linesearch_failures; })
100 extra.emplace_back(
101 "linesearch_failures",
102 static_cast<index_t>(stats.inner.linesearch_failures));
103 if constexpr (requires { stats.inner.linesearch_backtracks; })
104 extra.emplace_back(
105 "linesearch_backtracks",
106 static_cast<index_t>(stats.inner.linesearch_backtracks));
107 if constexpr (requires { stats.inner.stepsize_backtracks; })
108 extra.emplace_back(
109 "stepsize_backtracks",
110 static_cast<index_t>(stats.inner.stepsize_backtracks));
111 if constexpr (requires { stats.inner.lbfgs_failures; })
112 extra.emplace_back("lbfgs_failures",
113 static_cast<index_t>(stats.inner.lbfgs_failures));
114 if constexpr (requires { stats.inner.lbfgs_rejected; })
115 extra.emplace_back("lbfgs_rejected",
116 static_cast<index_t>(stats.inner.lbfgs_rejected));
117 if constexpr (requires { stats.inner.accelerated_step_rejected; })
118 extra.emplace_back(
119 "accelerated_step_rejected",
120 static_cast<index_t>(stats.inner.accelerated_step_rejected));
121 if constexpr (requires { stats.inner.direction_failures; })
122 extra.emplace_back(
123 "direction_failures",
124 static_cast<index_t>(stats.inner.direction_failures));
125 if constexpr (requires { stats.inner.direction_update_rejected; })
126 extra.emplace_back(
127 "direction_update_rejected",
128 static_cast<index_t>(stats.inner.direction_update_rejected));
129 return SolverResults{
130 .status = enum_name(stats.status),
131 .success = stats.status == alpaqa::SolverStatus::Converged,
132 .evals = evals,
133 .duration = avg_duration,
134 .solver = solver.get_name(),
135 .h = final_h,
136 .δ = stats.δ,
137 .ε = stats.ε,
138 .γ = final_γ,
139 .Σ = stats.norm_penalty,
140 .solution = x,
141 .multipliers = y,
142 .multipliers_bounds = vec(0),
143 .penalties = Σ,
144 .outer_iter = static_cast<index_t>(stats.outer_iterations),
145 .inner_iter = static_cast<index_t>(stats.inner.iterations),
146 .extra = std::move(extra),
147 };
148}
auto make_inner_solver(Options &opts)
SolverResults run_alm_solver(LoadedProblem &problem, Solver &solver, std::ostream &os, unsigned N_exp)
auto make_alm_solver(InnerSolver &&inner_solver, Options &opts)
Augmented Lagrangian Method solver.
Definition alm.hpp:69
length_t get_m() const
[Required] Number of constraints.
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:63
std::string_view enum_name(Ipopt::ApplicationReturnStatus s)
@ ProjGradUnitNorm
∞-norm of the projected gradient with unit step size:
@ Interrupted
Solver was interrupted by the user.
@ Converged
Converged and reached given tolerance.
constexpr const auto inf
Definition config.hpp:98
decltype(auto) set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:86
vec initial_guess_y
Multipliers g.
Definition problem.hpp:31
vec initial_guess_x
Unknowns.
Definition problem.hpp:30
alpaqa::TypeErasedProblem< config_t > problem
Definition problem.hpp:25
std::shared_ptr< alpaqa::EvalCounter > evaluations
Definition problem.hpp:29
std::vector< std::pair< std::string, any_stat_t > > extra
Definition results.hpp:38
std::string status
Definition results.hpp:25
Double-precision double configuration.
Definition config.hpp:160