15 void *h = ::dlopen(
so_filename.c_str(), RTLD_LOCAL | RTLD_NOW);
16 if (
auto *err = ::dlerror())
17 throw std::runtime_error(err);
18 return std::shared_ptr<void>{h, &::dlclose};
26 auto *h = ::dlsym(
handle.get(), full_name.c_str());
27 if (
auto *err = ::dlerror())
28 throw std::runtime_error(err);
30 return reinterpret_cast<F *
>(h);
34 : so_filename(std::move(so_filename)),
35 symbol_prefix(std::move(symbol_prefix)), handle(load_lib()) {}
39 :
DLLoader{std::move(so_filename), std::move(symbol_prefix)},
41 auto *register_func = load_func<problem_register_t(void *)>(
"register");
42 auto r = register_func(user_param);
44 std::unique_ptr<void, void (*)(
void *)> unique_inst{r.instance, r.cleanup};
45 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
47 static constexpr auto format_abi_version = [](uint64_t version) {
48 std::string s(16,
'0');
49 auto begin = s.data(), end = begin + s.size();
50 auto [ptr, ec] = std::to_chars(begin, end, version, 16);
51 if (ec != std::errc())
52 throw std::logic_error(std::make_error_code(ec).message());
53 std::rotate(begin, ptr, end);
57 auto prob_version = format_abi_version(r.functions->abi_version);
59 throw std::runtime_error(
60 "alpaqa::dl::DLProblem::DLProblem: "
61 "Incompatible problem definition (problem ABI version 0x" +
62 prob_version +
", this version of alpaqa supports 0x" +
63 alpaqa_version +
")");
66 instance = std::shared_ptr<void>{std::move(unique_inst)};
68 this->
n = r.functions->n;
69 this->
m = r.functions->m;
70 this->
C =
Box{this->
n};
71 this->
D =
Box{this->
m};
74 this->C.upperbound.data());
77 this->D.upperbound.data());
87 extra_functions = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
92 if (functions->eval_prox_grad_step)
93 return functions->eval_prox_grad_step(
94 instance.get(), γ, x.data(), grad_ψ.data(), x̂.data(), p.data());
104auto DLProblem::eval_jac_g(
crvec x,
rindexvec inner_idx,
rindexvec outer_ptr,
rvec J_values)
const ->
void {
return functions->eval_jac_g(instance.get(), x.data(), inner_idx.data(), outer_ptr.data(), J_values.size() == 0 ?
nullptr : J_values.data()); }
107auto DLProblem::eval_hess_L(
crvec x,
crvec y,
real_t scale,
rindexvec inner_idx,
rindexvec outer_ptr,
rvec H_values)
const ->
void {
return functions->eval_hess_L(instance.get(), x.data(), y.data(), scale, inner_idx.data(), outer_ptr.data(), H_values.size() == 0 ?
nullptr : H_values.data()); }
109auto DLProblem::eval_hess_ψ_prod(
crvec x,
crvec y,
crvec Σ,
real_t scale,
crvec v,
rvec Hv)
const ->
void {
return functions->eval_hess_ψ_prod(instance.get(), x.data(), y.data(), Σ.data(), scale, D.lowerbound.data(), D.upperbound.data(), v.data(), Hv.data()); }
110auto DLProblem::eval_hess_ψ(
crvec x,
crvec y,
crvec Σ,
real_t scale,
rindexvec inner_idx,
rindexvec outer_ptr,
rvec H_values)
const ->
void {
return functions->eval_hess_ψ(instance.get(), x.data(), y.data(), Σ.data(), scale, D.lowerbound.data(), D.upperbound.data(), inner_idx.data(), outer_ptr.data(), H_values.size() == 0 ?
nullptr : H_values.data()); }
117auto DLProblem::eval_grad_ψ(
crvec x,
crvec y,
crvec Σ,
rvec grad_ψ,
rvec work_n,
rvec work_m)
const ->
void {
return functions->eval_grad_ψ(instance.get(), x.data(), y.data(), Σ.data(), D.lowerbound.data(), D.upperbound.data(), grad_ψ.data(), work_n.data(), work_m.data()); }
118auto DLProblem::eval_ψ_grad_ψ(
crvec x,
crvec y,
crvec Σ,
rvec grad_ψ,
rvec work_n,
rvec work_m)
const ->
real_t {
return functions->eval_ψ_grad_ψ(instance.get(), x.data(), y.data(), Σ.data(), D.lowerbound.data(), D.upperbound.data(), grad_ψ.data(), work_n.data(), work_m.data()); }
146 std::string symbol_prefix,
void *user_param)
147 :
DLLoader{std::move(so_filename), std::move(symbol_prefix)} {
148 auto *register_func =
149 load_func<control_problem_register_t(void *)>(
"register");
150 auto r = register_func(user_param);
152 std::unique_ptr<void, void (*)(
void *)> unique_inst{r.instance, r.cleanup};
153 std::unique_ptr<alpaqa_function_dict_t> unique_extra{r.extra_functions};
155 instance = std::shared_ptr<void>{std::move(unique_inst)};
157 extra_functions = std::shared_ptr<function_dict_t>{std::move(unique_extra)};
176auto DLControlProblem::eval_add_R_masked(
index_t timestep,
crvec xu,
crvec h,
crindexvec mask,
rmat R,
rvec work)
const ->
void {
return functions->eval_add_R_masked(instance.get(), timestep, xu.data(), h.data(), mask.data(), R.data(), work.data()); }
177auto DLControlProblem::eval_add_S_masked(
index_t timestep,
crvec xu,
crvec h,
crindexvec mask,
rmat S,
rvec work)
const ->
void {
return functions->eval_add_S_masked(instance.get(), timestep, xu.data(), h.data(), mask.data(), S.data(), work.data()); }
178auto DLControlProblem::eval_add_R_prod_masked(
index_t timestep,
crvec xu,
crvec h,
crindexvec mask_J,
crindexvec mask_K,
crvec v,
rvec out,
rvec work)
const ->
void {
return functions->eval_add_R_prod_masked(instance.get(), timestep, xu.data(), h.data(), mask_J.data(), mask_K.data(), v.data(), out.data(), work.data()); }
179auto DLControlProblem::eval_add_S_prod_masked(
index_t timestep,
crvec xu,
crvec h,
crindexvec mask_K,
crvec v,
rvec out,
rvec work)
const ->
void {
return functions->eval_add_S_prod_masked(instance.get(), timestep, xu.data(), h.data(), mask_K.data(), v.data(), out.data(), work.data()); }
Implements common problem functions for minimization problems with box constraints.
Box C
Constraints of the decision variables, .
length_t m
Number of constraints, dimension of g(x) and z.
length_t n
Number of decision variables, dimension of x.
Box D
Other constraints, .
real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const
vec l1_reg
(1-norm) regularization parameter.
void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const
bool provides_eval_grad_constr_prod() const
void eval_add_Q_N(crvec x, crvec h, rmat Q) const
bool provides_eval_add_gn_hess_constr() const
void eval_q_N(crvec x, crvec h, rvec q) const
void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const
void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const
void eval_add_R_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_J, crindexvec mask_K, crvec v, rvec out, rvec work) const
bool provides_eval_add_R_prod_masked() const
length_t get_S_work_size() const
void eval_constr_N(crvec x, rvec c) const
void get_D_N(Box &D) const
bool provides_get_D() const
bool provides_get_S_work_size() const
void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const
bool provides_eval_add_Q_N() const
std::shared_ptr< void > instance
Problem instance created by the registration function, including the deleter to destroy it.
void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const
real_t eval_l_N(crvec h) const
void eval_h_N(crvec x, rvec h) const
real_t eval_l(index_t timestep, crvec h) const
void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const
bool provides_eval_constr() const
control_problem_functions_t * functions
Pointer to the struct of function pointers for evaluating the objective, constraints,...
bool provides_get_D_N() const
void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const
DLControlProblem(std::string so_filename, std::string symbol_prefix="alpaqa_control_problem", void *user_param=nullptr)
Load a problem from a shared library.
void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const
bool provides_eval_grad_constr_prod_N() const
length_t get_R_work_size() const
void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const
bool provides_eval_add_S_prod_masked() const
void get_x_init(rvec x_init) const
void eval_h(index_t timestep, crvec x, crvec u, rvec h) const
void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const
void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const
bool provides_eval_constr_N() const
void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const
bool provides_get_R_work_size() const
bool provides_eval_add_gn_hess_constr_N() const
void eval_constr(index_t timestep, crvec x, rvec c) const
DLLoader(std::string so_filename, std::string symbol_prefix)
Load a shared library.
std::shared_ptr< function_dict_t > extra_functions
An associative array of additional functions exposed by the problem.
F * load_func(std::string_view name) const
Load a function with signature F from the library using dlsym.
std::string symbol_prefix
std::shared_ptr< void > load_lib() const
Open the shared library using dlopen
dl_handle_t handle
Handle to the shared library (returned by dlopen).
bool provides_eval_hess_L() const
real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const
real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const
void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
bool provides_eval_hess_ψ_prod() const
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
bool provides_eval_ψ_grad_ψ() const
bool provides_get_box_C() const
real_t eval_f_g(crvec x, rvec g) const
bool provides_get_hess_L_num_nonzeros() const
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
void eval_grad_f(crvec x, rvec grad_fx) const
bool provides_get_jac_g_num_nonzeros() const
bool provides_eval_jac_g() const
void eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const
real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const
std::shared_ptr< void > instance
Problem instance created by the registration function, including the deleter to destroy it.
bool provides_eval_g() const
void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
bool provides_eval_grad_f_grad_g_prod() const
bool provides_eval_f() const
bool provides_eval_hess_L_prod() const
bool provides_eval_grad_g_prod() const
bool provides_get_hess_ψ_num_nonzeros() const
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
bool provides_eval_f_grad_f() const
bool provides_eval_grad_f() const
bool provides_eval_grad_gi() const
bool provides_eval_f_g() const
problem_functions_t * functions
Pointer to the struct of function pointers for evaluating the objective, constraints,...
real_t eval_f(crvec x) const
void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const
bool provides_eval_grad_L() const
void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const
bool provides_eval_grad_ψ() const
length_t get_jac_g_num_nonzeros() const
DLProblem(std::string so_filename, std::string symbol_prefix="alpaqa_problem", void *user_param=nullptr)
Load a problem from a shared library.
bool provides_eval_hess_ψ() const
void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const
void eval_g(crvec x, rvec gx) const
void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
bool provides_eval_ψ() const
length_t get_hess_ψ_num_nonzeros() const
length_t get_hess_L_num_nonzeros() const
#define ALPAQA_DL_ABI_VERSION
typename Conf::real_t real_t
typename Conf::rindexvec rindexvec
typename Conf::index_t index_t
typename Conf::length_t length_t
typename Conf::crvec crvec
typename Conf::crindexvec crindexvec
void(* eval_add_gn_hess_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *M, alpaqa_real_t *out)
alpaqa_length_t(* get_S_work_size)(void *instance)
void(* eval_grad_constr_prod_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *p, alpaqa_real_t *grad_cx_p)
void(* get_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
void(* eval_grad_constr_prod)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *p, alpaqa_real_t *grad_cx_p)
void(* eval_add_R_prod_masked)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, const alpaqa_index_t *mask_J, const alpaqa_index_t *mask_K, const alpaqa_real_t *v, alpaqa_real_t *out, alpaqa_real_t *work)
void(* eval_add_gn_hess_constr_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *M, alpaqa_real_t *out)
alpaqa_length_t(* get_R_work_size)(void *instance)
void(* get_D_N)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
void(* eval_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, alpaqa_real_t *c)
void(* eval_add_Q_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *h, alpaqa_real_t *Q)
void(* eval_add_S_prod_masked)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, const alpaqa_index_t *mask_K, const alpaqa_real_t *v, alpaqa_real_t *out, alpaqa_real_t *work)
void(* eval_constr_N)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *c)
void(* eval_grad_f)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *grad_fx)
alpaqa_real_t(* eval_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *ŷ)
void(* eval_hess_ψ_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, alpaqa_real_t scale, const alpaqa_real_t *zl, const alpaqa_real_t *zu, const alpaqa_real_t *v, alpaqa_real_t *Hv)
void(* initialize_box_C)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
alpaqa_real_t(* eval_prox_grad_step)(void *instance, alpaqa_real_t γ, const alpaqa_real_t *x, const alpaqa_real_t *grad_ψ, alpaqa_real_t *x̂, alpaqa_real_t *p)
alpaqa_real_t(* eval_f_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *g)
void(* eval_grad_f_grad_g_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_f, alpaqa_real_t *grad_gxy)
alpaqa_length_t(* get_hess_ψ_num_nonzeros)(void *instance)
void(* eval_grad_L)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_L, alpaqa_real_t *work_n)
alpaqa_real_t(* eval_f_grad_f)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *grad_fx)
alpaqa_real_t(* eval_f)(void *instance, const alpaqa_real_t *x)
void(* eval_hess_L)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t scale, alpaqa_index_t *inner_idx, alpaqa_index_t *outer_ptr, alpaqa_real_t *H_values)
void(* eval_grad_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *grad_ψ, alpaqa_real_t *work_n, alpaqa_real_t *work_m)
void(* eval_grad_gi)(void *instance, const alpaqa_real_t *x, alpaqa_index_t i, alpaqa_real_t *grad_gi)
void(* eval_hess_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, alpaqa_real_t scale, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_index_t *inner_idx, alpaqa_index_t *outer_ptr, alpaqa_real_t *H_values)
void(* initialize_l1_reg)(void *instance, alpaqa_real_t *lambda, alpaqa_length_t *size)
alpaqa_length_t(* get_jac_g_num_nonzeros)(void *instance)
void(* eval_hess_L_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t scale, const alpaqa_real_t *v, alpaqa_real_t *Hv)
void(* eval_g)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *gx)
void(* initialize_box_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
void(* eval_jac_g)(void *instance, const alpaqa_real_t *x, alpaqa_index_t *inner_idx, alpaqa_index_t *outer_ptr, alpaqa_real_t *J_values)
alpaqa_length_t(* get_hess_L_num_nonzeros)(void *instance)
alpaqa_real_t(* eval_ψ_grad_ψ)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, const alpaqa_real_t *Σ, const alpaqa_real_t *zl, const alpaqa_real_t *zu, alpaqa_real_t *grad_ψ, alpaqa_real_t *work_n, alpaqa_real_t *work_m)
void(* eval_grad_g_prod)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_gxy)