17std::ostream *qpalm_os =
nullptr;
18int print_wrap(
const char *fmt, ...) LADEL_ATTR_PRINTF_LIKE;
19int print_wrap_noop(const
char *, ...) LADEL_ATTR_PRINTF_LIKE;
21void compress_multipliers_bounds(length_t n, length_t m,
22 const
alpaqa::OwningQPALMData &qp,
23 crvec multipliers_bounds, rvec y) {
24 for (index_t col = 0; col < n; ++col) {
26 auto outer = qp.A->p[col];
27 auto nnz = qp.A->p[col + 1] - outer;
30 auto row = qp.A->i[outer];
31 if (row + m >= y.size())
35 auto val_lb = multipliers_bounds(col);
36 auto val_ub = multipliers_bounds(col + n);
37 y(row) = val_lb < 0 ? val_lb : val_ub;
41void expand_multipliers_bounds(length_t n, length_t m,
43 rvec multipliers_bounds) {
44 for (index_t col = 0; col < n; ++col) {
46 auto outer = qp.A->p[col];
47 auto nnz = qp.A->p[col + 1] - outer;
50 auto row = qp.A->i[outer];
51 if (row + m >= sol_y.size())
55 auto value = sol_y(row);
56 index_t offset = value > 0 ? n : 0;
57 multipliers_bounds(col + offset) = value;
61SolverResults run_qpalm_solver(
auto &problem,
const qpalm::Settings &settings,
62 std::ostream &os,
unsigned N_exp) {
66 auto old_print = ladel_set_print_config_printf(&print_wrap);
67 struct PrintRestorer {
69 ~PrintRestorer() { ladel_set_print_config_printf(print); }
70 } restore_print{old_print};
73 length_t n = problem.problem.get_n(), m = problem.problem.get_m();
77 qpalm::Solver solver{&qp, settings};
80 vec initial_guess_mult;
81 if (
auto sz = problem.initial_guess_x.size(); sz != n)
82 throw std::invalid_argument(
"Invalid size for initial_guess_x (got " +
83 std::to_string(sz) +
", expected " +
84 std::to_string(n) +
")");
85 if (
auto sz = problem.initial_guess_y.size(); sz != m)
86 throw std::invalid_argument(
"Invalid size for initial_guess_y (got " +
87 std::to_string(sz) +
", expected " +
88 std::to_string(m) +
")");
89 if (
auto sz = problem.initial_guess_w.size(); sz > 0) {
91 throw std::invalid_argument(
92 "Invalid size for initial_guess_w (got " + std::to_string(sz) +
93 ", expected " + std::to_string(n * 2) +
")");
94 initial_guess_mult.resize(
static_cast<length_t>(qp.m));
95 compress_multipliers_bounds(n, m, qp, problem.initial_guess_w,
97 initial_guess_mult.bottomRows(m) = problem.initial_guess_y;
99 auto warm_start = [&] {
100 problem.initial_guess_w.size() > 0
101 ? solver.warm_start(problem.initial_guess_x, initial_guess_mult)
102 : solver.warm_start(problem.initial_guess_x, std::nullopt);
106 auto t0 = std::chrono::steady_clock::now();
109 auto t1 = std::chrono::steady_clock::now();
110 auto evals = *problem.evaluations;
111 auto info = solver.get_info();
112 vec sol_x = solver.get_solution().x, sol_y = solver.get_solution().y;
115 using ns = std::chrono::nanoseconds;
116 auto avg_duration = duration_cast<ns>(t1 - t0);
117 ladel_set_print_config_printf(&print_wrap_noop);
118 os.setstate(std::ios_base::badbit);
119 for (
unsigned i = 0; i < N_exp; ++i) {
120 auto t0 = std::chrono::steady_clock::now();
123 auto t1 = std::chrono::steady_clock::now();
124 avg_duration += duration_cast<ns>(t1 - t0);
127 avg_duration /= (N_exp + 1);
132 .success = info.status_val == QPALM_SOLVED,
134 .duration = avg_duration,
137 .δ = info.dua_res_norm,
138 .ε = info.pri_res_norm,
142 .multipliers = sol_y.bottomRows(m),
143 .multipliers_bounds = vec::Zero(n * 2),
144 .penalties = vec::Zero(n),
145 .outer_iter = info.iter_out,
146 .inner_iter = info.iter,
147 .extra = {{
"dua2_res_norm", info.dua2_res_norm}},
150 expand_multipliers_bounds(n, m, qp, sol_y, results.multipliers_bounds);
154int print_wrap(
const char *fmt, ...) {
155 static std::vector<char> buffer(1024);
156 std::va_list args, args2;
158 va_copy(args2, args);
159 int needed = vsnprintf(buffer.data(), buffer.size(), fmt, args);
166 else if (
auto buf_needed =
static_cast<size_t>(needed) + 1;
167 buf_needed > buffer.size()) {
168 buffer.resize(buf_needed);
169 va_start(args2, fmt);
170 needed = vsnprintf(buffer.data(), buffer.size(), fmt, args2);
175 std::string_view out{buffer.data(),
static_cast<size_t>(needed)};
181int print_wrap_noop(
const char *, ...) {
return 0; }
183auto get_qpalm_settings(
Options &opts) {
184 qpalm::Settings settings;
185 settings.eps_abs = 1e-8;
186 settings.eps_rel = 1e-8;
191template <
class LoadedProblem>
193 if (!direction.empty())
194 throw std::invalid_argument(
195 "QPALM solver does not support any directions");
196 auto settings = get_qpalm_settings(opts);
201 return run_qpalm_solver(problem, settings, os, N_exp);
208 static constexpr bool valid_config =
209 std::is_same_v<LoadedProblem::config_t, alpaqa::EigenConfigd>;
210 if constexpr (valid_config)
211 return make_qpalm_drive_impl<LoadedProblem>(direction, opts);
213 throw std::invalid_argument(
214 "QPALM solver only supports double precision");
222 throw std::invalid_argument(
223 "This version of alpaqa was compiled without QPALM support.");
#define USING_ALPAQA_CONFIG(Conf)
QPALM_ADAPTER_EXPORT OwningQPALMData build_qpalm_problem(const TypeErasedProblem< EigenConfigd > &problem)
typename Conf::index_t index_t
typename Conf::length_t length_t
decltype(auto) set_params(T &t, std::string_view prefix, Options &opts)
solver_func_t make_qpalm_driver(std::string_view, Options &)
std::function< solver_free_func_t > solver_func_t
Double-precision double configuration.