alpaqa cmake-targets
Nonconvex constrained optimization
Loading...
Searching...
No Matches
lbfgsb-driver.cpp
Go to the documentation of this file.
1#if ALPAQA_WITH_LBFGSB
2
5
6#include "alm-driver.hpp"
7#include "cancel.hpp"
8#include "extra-stats.hpp"
9#include "lbfgsb-driver.hpp"
10#include "solver-driver.hpp"
11
12namespace {
13
14using InnerLBFGSBSolver = alpaqa::lbfgsb::LBFGSBSolver;
15
16auto make_inner_lbfgsb_solver(Options &opts) {
17 // Settings for the solver
18 InnerLBFGSBSolver::Params solver_param;
19 solver_param.max_iter = 50'000;
20 solver_param.print_interval = 0;
21 solver_param.stop_crit = alpaqa::PANOCStopCrit::ProjGradUnitNorm;
22 set_params(solver_param, "solver", opts);
23 return InnerLBFGSBSolver{solver_param};
24}
25
26template <class LoadedProblem>
27SharedSolverWrapper make_lbfgsb_driver_impl(std::string_view direction,
28 Options &opts) {
29 if (!direction.empty())
30 throw std::invalid_argument(
31 "L-BFGS-B solver does not support any directions");
32 using collector_t = AlpaqaSolverStatsCollector<config_t>;
33 std::shared_ptr<collector_t> collector;
34 auto inner_solver = make_inner_lbfgsb_solver(opts);
35 bool extra_stats = false;
36 set_params(extra_stats, "extra_stats", opts);
37 if (extra_stats) {
38 collector = std::make_shared<collector_t>();
39 inner_solver.set_progress_callback(
40 [collector](const auto &progress_info) {
41 collector->update_iter(progress_info);
42 });
43 }
44 auto solver = make_alm_solver(std::move(inner_solver), opts);
45 unsigned N_exp = 0;
46 set_params(N_exp, "num_exp", opts);
47 auto run = [solver{std::move(solver)},
48 N_exp](LoadedProblem &problem,
49 std::ostream &os) mutable -> SolverResults {
50 auto cancel = alpaqa::attach_cancellation(solver);
51 return run_alm_solver(problem, solver, os, N_exp);
52 };
53 return std::make_shared<AlpaqaSolverWrapperStats<config_t>>(
54 std::move(run), std::move(collector));
55}
56
57} // namespace
58
59SharedSolverWrapper make_lbfgsb_driver(std::string_view direction,
60 Options &opts) {
61 static constexpr bool valid_config =
62 std::is_same_v<LoadedProblem::config_t, InnerLBFGSBSolver::config_t>;
63 if constexpr (valid_config)
64 return make_lbfgsb_driver_impl<LoadedProblem>(direction, opts);
65 else
66 throw std::invalid_argument(
67 "L-BFGS-B solver only supports double precision");
68}
69
71
72#else
73
74#include "solver-driver.hpp"
75
77 throw std::invalid_argument(
78 "This version of alpaqa was compiled without L-BFGS-B support.");
79}
80
81#endif
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
L-BFGS-B solver for ALM.
SharedSolverWrapper make_lbfgsb_driver(std::string_view, Options &)
@ ProjGradUnitNorm
∞-norm of the projected gradient with unit step size:
auto attach_cancellation(Solver &solver)
Attach SIGINT and SIGTERM handlers to stop the given solver.
Definition cancel.hpp:21
decltype(auto) set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:86
std::shared_ptr< SolverWrapper > SharedSolverWrapper