1#ifdef ALPAQA_WITH_QPALM
5#include <qpalm/constants.h>
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;
22void compress_multipliers_bounds(const
alpaqa::sets::Box<config_t> &C, rvec y,
23 crvec multipliers_bounds) {
26 for (index_t i = 0; i < C.lowerbound.size(); ++i)
27 if (Conv::is_bound(C, i))
28 y(shift++) = multipliers_bounds(i);
32 rvec multipliers_bounds) {
35 for (index_t i = 0; i < C.
lowerbound.size(); ++i)
36 if (Conv::is_bound(C, i))
37 multipliers_bounds(i) = y(shift++);
40SolverResults run_qpalm_solver(
auto &problem,
const qpalm::Settings &settings,
41 std::ostream &os,
unsigned N_exp) {
45 auto old_print = ladel_set_print_config_printf(&print_wrap);
46 struct PrintRestorer {
48 ~PrintRestorer() { ladel_set_print_config_printf(print); }
49 } restore_print{old_print};
53 qpalm::Solver solver{&qp, settings};
56 length_t n = problem.problem.get_n(), m = problem.problem.get_m();
60 vec initial_guess_mult;
61 if (
auto sz = problem.initial_guess_x.size(); sz != n)
62 throw std::invalid_argument(
63 "Invalid size for initial_guess_x (expected " + std::to_string(n) +
64 ", but got " + std::to_string(sz) +
")");
65 if (
auto sz = problem.initial_guess_y.size(); sz != m)
66 throw std::invalid_argument(
67 "Invalid size for initial_guess_y (expected " + std::to_string(m) +
68 ", but got " + std::to_string(sz) +
")");
69 if (
auto sz = problem.initial_guess_w.size(); sz > 0) {
71 throw std::invalid_argument(
72 "Invalid size for initial_guess_w (expected " +
73 std::to_string(n) +
", but got " + std::to_string(sz) +
")");
74 initial_guess_mult.resize(
static_cast<length_t>(qp.m));
75 if (problem.problem.provides_get_box_C())
76 compress_multipliers_bounds(problem.problem.get_box_C(),
78 problem.initial_guess_w);
80 assert(num_bounds == 0 && initial_guess_mult.size() == m);
81 initial_guess_mult.bottomRows(m) = problem.initial_guess_y;
83 auto warm_start = [&] {
84 problem.initial_guess_w.size() > 0
85 ? solver.warm_start(problem.initial_guess_x, initial_guess_mult)
86 : solver.warm_start(problem.initial_guess_x, std::nullopt);
90 auto t0 = std::chrono::steady_clock::now();
93 auto t1 = std::chrono::steady_clock::now();
94 auto info = solver.get_info();
95 vec sol_x = solver.get_solution().x, sol_y = solver.get_solution().y;
98 using ns = std::chrono::nanoseconds;
99 auto avg_duration = duration_cast<ns>(t1 - t0);
100 ladel_set_print_config_printf(&print_wrap_noop);
101 os.setstate(std::ios_base::badbit);
102 for (
unsigned i = 0; i < N_exp; ++i) {
103 auto t0 = std::chrono::steady_clock::now();
106 auto t1 = std::chrono::steady_clock::now();
107 avg_duration += duration_cast<ns>(t1 - t0);
110 avg_duration /= (N_exp + 1);
111 auto evals = *problem.evaluations;
116 .success = info.status_val == QPALM_SOLVED,
118 .duration = avg_duration,
121 .δ = info.pri_res_norm,
122 .ε = info.dua_res_norm,
126 .multipliers = sol_y.bottomRows(m),
127 .multipliers_bounds = vec::Zero(n),
128 .penalties = vec::Zero(n),
129 .outer_iter = info.iter_out,
130 .inner_iter = info.iter,
131 .extra = {{
"dua2_res_norm", info.dua2_res_norm}},
134 if (problem.problem.provides_get_box_C())
135 expand_multipliers_bounds(problem.problem.get_box_C(), sol_y,
136 results.multipliers_bounds);
140int print_wrap(
const char *fmt, ...) {
141 static std::vector<char> buffer(1024);
142 std::va_list args, args2;
144 va_copy(args2, args);
145 int needed = vsnprintf(buffer.data(), buffer.size(), fmt, args);
152 else if (
auto buf_needed =
static_cast<size_t>(needed) + 1;
153 buf_needed > buffer.size()) {
154 buffer.resize(buf_needed);
155 va_start(args2, fmt);
156 needed = vsnprintf(buffer.data(), buffer.size(), fmt, args2);
161 std::string_view out{buffer.data(),
static_cast<size_t>(needed)};
167int print_wrap_noop(
const char *, ...) {
return 0; }
169auto get_qpalm_settings(
Options &opts) {
170 qpalm::Settings settings;
171 settings.eps_abs = 1e-8;
172 settings.eps_rel = 1e-8;
177template <
class LoadedProblem>
180 if (!direction.empty())
181 throw std::invalid_argument(
182 "QPALM solver does not support any directions");
183 auto settings = get_qpalm_settings(opts);
186 return std::make_shared<SolverWrapper>(
189 return run_qpalm_solver(problem, settings, os, N_exp);
197 static constexpr bool valid_config =
198 std::is_same_v<LoadedProblem::config_t, alpaqa::EigenConfigd>;
199 if constexpr (valid_config)
200 return make_qpalm_drive_impl<LoadedProblem>(direction, opts);
202 throw std::invalid_argument(
203 "QPALM solver only supports double precision");
211 throw std::invalid_argument(
212 "This version of alpaqa was compiled without QPALM support.");
#define USING_ALPAQA_CONFIG(Conf)
OwningQPALMData build_qpalm_problem(const TypeErasedProblem< EigenConfigd > &problem)
typename Conf::index_t index_t
typename Conf::length_t length_t
void set_params(T &t, std::string_view prefix, Options &opts)
SharedSolverWrapper make_qpalm_driver(std::string_view, Options &)
std::shared_ptr< SolverWrapper > SharedSolverWrapper
Double-precision double configuration.