Nonconvex constrained optimization
Loading...
Searching...
No Matches
qpalm-driver.cpp
Go to the documentation of this file.
1#ifdef ALPAQA_WITH_QPALM
2
5#include <qpalm/constants.h>
6
7#include <cstdarg>
8#include <stdexcept>
9#include <string>
10
11#include "results.hpp"
12#include "solver-driver.hpp"
13
14namespace {
15
17
18std::ostream *qpalm_os = nullptr;
19int print_wrap(const char *fmt, ...) LADEL_ATTR_PRINTF_LIKE;
20int print_wrap_noop(const char *, ...) LADEL_ATTR_PRINTF_LIKE;
21
22void compress_multipliers_bounds(const alpaqa::sets::Box<config_t> &C, rvec y,
23 crvec multipliers_bounds) {
25 index_t shift = 0;
26 for (index_t i = 0; i < C.lower.size(); ++i)
27 if (Conv::is_bound(C, i))
28 y(shift++) = multipliers_bounds(i);
29}
30
31void expand_multipliers_bounds(const alpaqa::sets::Box<config_t> &C, crvec y,
32 rvec multipliers_bounds) {
34 index_t shift = 0;
35 for (index_t i = 0; i < C.lower.size(); ++i)
36 if (Conv::is_bound(C, i))
37 multipliers_bounds(i) = y(shift++);
38}
39
40SolverResults run_qpalm_solver(auto &problem, const qpalm::Settings &settings,
41 std::ostream &os, unsigned N_exp) {
42
43 // Set up output stream
44 qpalm_os = &os;
45 auto old_print = ladel_set_print_config_printf(&print_wrap);
46 struct PrintRestorer {
47 printf_sig *print;
48 ~PrintRestorer() { ladel_set_print_config_printf(print); }
49 } restore_print{old_print};
50
51 // Adapt problem
52 auto qp = alpaqa::build_qpalm_problem(problem.problem);
53 qpalm::Solver solver{&qp, settings};
54
55 // Dimensions
56 length_t n = problem.problem.get_num_variables(),
57 m = problem.problem.get_num_constraints();
58 [[maybe_unused]] length_t num_bounds = static_cast<length_t>(qp.m) - m;
59
60 // Initial guess
61 vec initial_guess_mult;
62 if (auto sz = problem.initial_guess_x.size(); sz != n)
63 throw std::invalid_argument(
64 "Invalid size for initial_guess_x (expected " + std::to_string(n) +
65 ", but got " + std::to_string(sz) + ")");
66 if (auto sz = problem.initial_guess_y.size(); sz != m)
67 throw std::invalid_argument(
68 "Invalid size for initial_guess_y (expected " + std::to_string(m) +
69 ", but got " + std::to_string(sz) + ")");
70 if (auto sz = problem.initial_guess_w.size(); sz > 0) {
71 if (sz != n)
72 throw std::invalid_argument(
73 "Invalid size for initial_guess_w (expected " +
74 std::to_string(n) + ", but got " + std::to_string(sz) + ")");
75 initial_guess_mult.resize(static_cast<length_t>(qp.m));
76 if (problem.problem.provides_get_variable_bounds())
77 compress_multipliers_bounds(problem.problem.get_variable_bounds(),
78 initial_guess_mult,
79 problem.initial_guess_w);
80 else
81 assert(num_bounds == 0 && initial_guess_mult.size() == m);
82 initial_guess_mult.bottomRows(m) = problem.initial_guess_y;
83 }
84 auto warm_start = [&] {
85 problem.initial_guess_w.size() > 0
86 ? solver.warm_start(problem.initial_guess_x, initial_guess_mult)
87 : solver.warm_start(problem.initial_guess_x, std::nullopt);
88 };
89
90 // Solve the problem
91 auto t0 = std::chrono::steady_clock::now();
92 warm_start();
93 solver.solve();
94 auto t1 = std::chrono::steady_clock::now();
95 auto info = solver.get_info();
96 vec sol_x = solver.get_solution().x, sol_y = solver.get_solution().y;
97
98 // Solve the problems again to average runtimes
99 using ns = std::chrono::nanoseconds;
100 auto avg_duration = duration_cast<ns>(t1 - t0);
101 ladel_set_print_config_printf(&print_wrap_noop);
102 os.setstate(std::ios_base::badbit);
103 for (unsigned i = 0; i < N_exp; ++i) {
104 auto t0 = std::chrono::steady_clock::now();
105 warm_start();
106 solver.solve();
107 auto t1 = std::chrono::steady_clock::now();
108 avg_duration += duration_cast<ns>(t1 - t0);
109 }
110 os.clear();
111 avg_duration /= (N_exp + 1);
112 auto evals = *problem.evaluations;
113
114 // Results
115 SolverResults results{
116 .status = info.status,
117 .success = info.status_val == QPALM_SOLVED,
118 .evals = evals,
119 .duration = avg_duration,
120 .solver = "QPALM",
121 .h = 0,
122 .δ = info.pri_res_norm,
123 .ε = info.dua_res_norm,
124 .γ = 0,
125 .Σ = 0,
126 .solution = sol_x,
127 .multipliers = sol_y.bottomRows(m),
128 .multipliers_bounds = vec::Zero(n), // see bleow
129 .penalties = vec::Zero(n),
130 .outer_iter = info.iter_out,
131 .inner_iter = info.iter,
132 .extra = {{"dua2_res_norm", info.dua2_res_norm}},
133 };
134 // Expand the multipliers for the bounds constraints again
135 if (problem.problem.provides_get_variable_bounds())
136 expand_multipliers_bounds(problem.problem.get_variable_bounds(), sol_y,
137 results.multipliers_bounds);
138 return results;
139}
140
141int print_wrap(const char *fmt, ...) {
142 static std::vector<char> buffer(1024);
143 std::va_list args, args2;
144 va_start(args, fmt);
145 va_copy(args2, args);
146 int needed = vsnprintf(buffer.data(), buffer.size(), fmt, args);
147 va_end(args);
148 // Error occurred
149 if (needed < 0) {
150 // ignore and return
151 }
152 // Buffer was too small
153 else if (auto buf_needed = static_cast<size_t>(needed) + 1;
154 buf_needed > buffer.size()) {
155 buffer.resize(buf_needed);
156 va_start(args2, fmt);
157 needed = vsnprintf(buffer.data(), buffer.size(), fmt, args2);
158 va_end(args2);
159 }
160 if (needed >= 0) {
161 assert(qpalm_os);
162 std::string_view out{buffer.data(), static_cast<size_t>(needed)};
163 *qpalm_os << out;
164 }
165 return needed;
166}
167
168int print_wrap_noop(const char *, ...) { return 0; }
169
170auto get_qpalm_settings(Options &opts) {
171 qpalm::Settings settings;
172 settings.eps_abs = 1e-8;
173 settings.eps_rel = 1e-8;
174 set_params(settings, "solver", opts);
175 return settings;
176}
177
178template <class LoadedProblem>
179SharedSolverWrapper make_qpalm_drive_impl(std::string_view direction,
180 Options &opts) {
181 if (!direction.empty())
182 throw std::invalid_argument(
183 "QPALM solver does not support any directions");
184 auto settings = get_qpalm_settings(opts);
185 unsigned N_exp = 0;
186 set_params(N_exp, "num_exp", opts);
187 return std::make_shared<SolverWrapper>(
188 [settings, N_exp](LoadedProblem &problem,
189 std::ostream &os) mutable -> SolverResults {
190 return run_qpalm_solver(problem, settings, os, N_exp);
191 });
192}
193
194} // namespace
195
196SharedSolverWrapper make_qpalm_driver(std::string_view direction,
197 Options &opts) {
198 static constexpr bool valid_config =
199 std::is_same_v<LoadedProblem::config_t, alpaqa::EigenConfigd>;
200 if constexpr (valid_config)
201 return make_qpalm_drive_impl<LoadedProblem>(direction, opts);
202 else
203 throw std::invalid_argument(
204 "QPALM solver only supports double precision");
205}
206
207#else
208
209#include "solver-driver.hpp"
210
212 throw std::invalid_argument(
213 "This version of alpaqa was compiled without QPALM support.");
214}
215
216#endif
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
OwningQPALMData build_qpalm_problem(const TypeErasedProblem< EigenConfigd > &problem)
typename Conf::index_t index_t
Definition config.hpp:104
typename Conf::length_t length_t
Definition config.hpp:103
typename Conf::vec vec
Definition config.hpp:88
void set_params(T &t, std::string_view prefix, Options &opts)
Definition options.hpp:126
SharedSolverWrapper make_qpalm_driver(std::string_view, Options &)
std::shared_ptr< SolverWrapper > SharedSolverWrapper
vec multipliers_bounds
Definition results.hpp:33
Double-precision double configuration.
Definition config.hpp:176