alpaqa dll
Nonconvex constrained optimization
Loading...
Searching...
No Matches
dl-problem.h
Go to the documentation of this file.
1// NOLINTBEGIN(modernize-use-using,modernize-deprecated-headers)
2
3#pragma once
4
5#include <stddef.h>
6#include <stdint.h>
7
8#define ALPAQA_DL_ABI_VERSION 0xA1A000000005
9
10#ifdef ALPAQA_DL_PROBLEM_EXPORT
11#elif defined(_WIN32)
12#define ALPAQA_DL_PROBLEM_EXPORT
13#elif defined(__GNUC__)
14#define ALPAQA_DL_PROBLEM_EXPORT __attribute__((visibility("default")))
15#else
16#define ALPAQA_DL_PROBLEM_EXPORT
17#endif
18
19#ifdef __cplusplus
20extern "C" {
21#define ALPAQA_DEFAULT(...) {__VA_ARGS__}
22#else
23#define ALPAQA_DEFAULT(...)
24#endif
25
26typedef double alpaqa_real_t;
27typedef ptrdiff_t alpaqa_length_t;
29typedef uint64_t alpaqa_dl_abi_version_t;
30
31typedef enum {
32 /// No type was specified (discouraged).
34 /// The @ref alpaqa_register_arg_t::data pointer points to a C++ `std::any`
35 /// object. Use `reinterpret_cast<std::any *>(data)` to convert back.
36 /// Then use `std::any_cast` to get the actual value out.
37 /// @warning `std::any` relies on RTTI that is included in both the
38 /// dynamically loaded problem module and in the code that
39 /// actually loads that module. Types that need to be passed
40 /// between the loading and loaded code need to be exported as
41 /// public classes to ensure that only a single symbol ends up
42 /// in the final application. If the RTTI symbols are private,
43 /// each library will end up with its own local copy, and the
44 /// type information won't compare equal when using libc++,
45 /// causing `std::any_cast` to fail.
47 /// The @ref alpaqa_register_arg_t::data pointer points to a dynamic C++
48 /// `std::span` of `std::string_view`.
49 /// Use `reinterpret_cast<std::span<std::string_view> *>(data)` to
50 /// convert back.
52 /// The @ref alpaqa_register_arg_t::data pointer points to a C++ tuple of
53 /// `pybind11::args` and `pybind11::kwargs`.
54 /// Use `reinterpret_cast<std::tuple<pybind11::args, pybind11::kwargs> *>(data)`
55 /// to convert back.
58
59/// User-provided argument that is passed to the problem registration functions.
61 /// Pointer to the user-provided argument passed to the problem registration
62 /// functions. Use the @ref type member to determine its type.
63 void *data ALPAQA_DEFAULT(nullptr);
64 /// Specifies the type of the data pointed to by @ref data.
67};
69
70/// @see @ref alpaqa::sparsity::Symmetry
76
77/// @see @ref alpaqa::sparsity::Dense
83
84/// @see @ref alpaqa::sparsity::SparseCSC
85typedef struct {
89 const int *inner_idx;
90 const int *outer_ptr;
91 /// @see @ref alpaqa::sparsity::SparseCSC::Order
92 enum {
95 } order;
97
98/// @see @ref alpaqa::sparsity::SparseCSC
99typedef struct {
103 const long *inner_idx;
104 const long *outer_ptr;
105 /// @see @ref alpaqa::sparsity::SparseCSC::Order
106 enum {
109 } order;
111
112/// @see @ref alpaqa::sparsity::SparseCSC
113typedef struct {
117 const long long *inner_idx;
118 const long long *outer_ptr;
119 /// @see @ref alpaqa::sparsity::SparseCSC::Order
120 enum {
123 } order;
125
126/// @see @ref alpaqa::sparsity::SparseCOO
143
144/// @see @ref alpaqa::sparsity::SparseCOO
161
162/// @see @ref alpaqa::sparsity::SparseCOO
179
180/// Sparsity of matrices.
181/// @see @ref alpaqa::sparsity::Sparsity
202
203/// C API providing function pointers to problem functions.
204/// Used by @ref alpaqa::dl::DLProblem.
206 /// Number of decision variables.
207 /// @see @ref alpaqa::TypeErasedProblem::get_num_variables()
209 /// Number of constraints.
210 /// @see @ref alpaqa::TypeErasedProblem::get_num_constraints()
212 /// Name of the problem.
213 /// @see @ref alpaqa::TypeErasedProblem::get_name()
214 const char *name ALPAQA_DEFAULT(nullptr);
215
216 // clang-format off
217 /// Cost function.
218 /// @see @ref alpaqa::TypeErasedProblem::eval_objective()
220 void *instance,
221 const alpaqa_real_t *x) ALPAQA_DEFAULT(nullptr);
222 /// Gradient of the cost function.
223 /// @see @ref alpaqa::TypeErasedProblem::eval_objective_gradient()
225 void *instance,
226 const alpaqa_real_t *x,
227 alpaqa_real_t *grad_fx) ALPAQA_DEFAULT(nullptr);
228 /// Constraints function.
229 /// @see @ref alpaqa::TypeErasedProblem::eval_constraints()
231 void *instance,
232 const alpaqa_real_t *x,
233 alpaqa_real_t *gx) ALPAQA_DEFAULT(nullptr);
234 /// Gradient-vector product of the constraints function.
235 /// @see @ref alpaqa::TypeErasedProblem::eval_constraints_gradient_product()
237 void *instance,
238 const alpaqa_real_t *x,
239 const alpaqa_real_t *y,
240 alpaqa_real_t *grad_gxy) ALPAQA_DEFAULT(nullptr);
241
242 /// Difference between point and its projection onto the general constraint
243 /// set D.
244 /// @see @ref alpaqa::TypeErasedProblem::eval_projecting_difference_constraints()
246 void *instance,
247 const alpaqa_real_t *z,
248 alpaqa_real_t *e) ALPAQA_DEFAULT(nullptr);
249 /// Project the Lagrange multipliers.
250 /// @see @ref alpaqa::TypeErasedProblem::eval_projection_multipliers()
252 void *instance,
254 /// Proximal gradient step.
255 /// @see @ref alpaqa::TypeErasedProblem::eval_proximal_gradient_step()
256 /// If not set, the default implementation from
257 /// @ref alpaqa::BoxConstrProblem is used.
259 void *instance,
260 alpaqa_real_t γ,
261 const alpaqa_real_t *x,
262 const alpaqa_real_t *grad_ψ,
263 alpaqa_real_t *x̂,
264 alpaqa_real_t *p) ALPAQA_DEFAULT(nullptr);
265 /// Active indices for proximal operator.
266 /// @see @ref alpaqa::TypeErasedProblem::eval_inactive_indices_res_lna()
267 /// If not set, the default implementation from
268 /// @ref alpaqa::BoxConstrProblem is used.
270 void *instance,
271 alpaqa_real_t γ,
272 const alpaqa_real_t *x,
273 const alpaqa_real_t *grad_ψ,
274 alpaqa_index_t *J) ALPAQA_DEFAULT(nullptr);
275
276 /// Jacobian of the constraints function.
277 /// @see @ref alpaqa::TypeErasedProblem::eval_constraints_jacobian()
279 void *instance,
280 const alpaqa_real_t *x,
281 alpaqa_real_t *J_values) ALPAQA_DEFAULT(nullptr);
282 /// Get the sparsity pattern of the Jacobian of the constraints function.
283 /// @see @ref alpaqa::TypeErasedProblem::get_constraints_jacobian_sparsity()
286 /// Gradient of specific constraint function.
287 /// @see @ref alpaqa::TypeErasedProblem::eval_grad_gi()
289 void *instance,
290 const alpaqa_real_t *x,
292 alpaqa_real_t *grad_gi) ALPAQA_DEFAULT(nullptr);
293 /// Hessian-vector product of the Lagrangian.
294 /// @see @ref alpaqa::TypeErasedProblem::eval_lagrangian_hessian_product()
296 void *instance,
297 const alpaqa_real_t *x,
298 const alpaqa_real_t *y,
299 alpaqa_real_t scale,
300 const alpaqa_real_t *v,
301 alpaqa_real_t *Hv) ALPAQA_DEFAULT(nullptr);
302 /// Hessian of the Lagrangian.
303 /// @see @ref alpaqa::TypeErasedProblem::eval_lagrangian_hessian()
305 void *instance,
306 const alpaqa_real_t *x,
307 const alpaqa_real_t *y,
308 alpaqa_real_t scale,
309 alpaqa_real_t *H_values) ALPAQA_DEFAULT(nullptr);
310 /// Get the sparsity pattern of the Hessian of the Lagrangian.
311 /// @see @ref alpaqa::TypeErasedProblem::get_lagrangian_hessian_sparsity()
314 /// Hessian-vector product of the augmented Lagrangian.
315 /// @see @ref alpaqa::TypeErasedProblem::eval_augmented_lagrangian_hessian_product()
317 void *instance,
318 const alpaqa_real_t *x,
319 const alpaqa_real_t *y,
320 const alpaqa_real_t *Σ,
321 alpaqa_real_t scale,
322 const alpaqa_real_t *zl,
323 const alpaqa_real_t *zu,
324 const alpaqa_real_t *v,
325 alpaqa_real_t *Hv) ALPAQA_DEFAULT(nullptr);
326 /// Hessian of the augmented Lagrangian.
327 /// @see @ref alpaqa::TypeErasedProblem::eval_augmented_lagrangian_hessian()
329 void *instance,
330 const alpaqa_real_t *x,
331 const alpaqa_real_t *y,
332 const alpaqa_real_t *Σ,
333 alpaqa_real_t scale,
334 const alpaqa_real_t *zl,
335 const alpaqa_real_t *zu,
336 alpaqa_real_t *H_values) ALPAQA_DEFAULT(nullptr);
337 /// Get the sparsity pattern of the Hessian of the augmented Lagrangian.
338 /// @see @ref alpaqa::TypeErasedProblem::get_augmented_lagrangian_hessian_sparsity()
341
342 /// Cost and its gradient.
343 /// @see @ref alpaqa::TypeErasedProblem::eval_objective_and_gradient()
345 void *instance,
346 const alpaqa_real_t *x,
347 alpaqa_real_t *grad_fx) ALPAQA_DEFAULT(nullptr);
348 /// Cost and constraints.
349 /// @see @ref alpaqa::TypeErasedProblem::eval_objective_and_constraints()
351 void *instance,
352 const alpaqa_real_t *x,
353 alpaqa_real_t *g) ALPAQA_DEFAULT(nullptr);
354 /// Gradient of the cost and gradient-vector product of the constraints.
355 /// @see @ref alpaqa::TypeErasedProblem::eval_objective_gradient_and_constraints_gradient_product()
357 void *instance,
358 const alpaqa_real_t *x,
359 const alpaqa_real_t *y,
360 alpaqa_real_t *grad_f,
361 alpaqa_real_t *grad_gxy) ALPAQA_DEFAULT(nullptr);
362 /// Gradient of the Lagrangian.
363 /// @see @ref alpaqa::TypeErasedProblem::eval_lagrangian_gradient()
365 void *instance,
366 const alpaqa_real_t *x,
367 const alpaqa_real_t *y,
368 alpaqa_real_t *grad_L,
369 alpaqa_real_t *work_n) ALPAQA_DEFAULT(nullptr);
370
371 /// Augmented Lagrangian.
372 /// @see @ref alpaqa::TypeErasedProblem::eval_augmented_lagrangian()
374 void *instance,
375 const alpaqa_real_t *x,
376 const alpaqa_real_t *y,
377 const alpaqa_real_t *Σ,
378 const alpaqa_real_t *zl,
379 const alpaqa_real_t *zu,
380 alpaqa_real_t *ŷ) ALPAQA_DEFAULT(nullptr);
381 /// Gradient of the augmented Lagrangian.
382 /// @see @ref alpaqa::TypeErasedProblem::eval_augmented_lagrangian_gradient()
384 void *instance,
385 const alpaqa_real_t *x,
386 const alpaqa_real_t *y,
387 const alpaqa_real_t *Σ,
388 const alpaqa_real_t *zl,
389 const alpaqa_real_t *zu,
390 alpaqa_real_t *grad_ψ,
391 alpaqa_real_t *work_n,
392 alpaqa_real_t *work_m) ALPAQA_DEFAULT(nullptr);
393 /// Augmented Lagrangian and its gradient.
394 /// @see @ref alpaqa::TypeErasedProblem::eval_augmented_lagrangian_and_gradient()
396 void *instance,
397 const alpaqa_real_t *x,
398 const alpaqa_real_t *y,
399 const alpaqa_real_t *Σ,
400 const alpaqa_real_t *zl,
401 const alpaqa_real_t *zu,
402 alpaqa_real_t *grad_ψ,
403 alpaqa_real_t *work_n,
404 alpaqa_real_t *work_m) ALPAQA_DEFAULT(nullptr);
405
406 /// Provide the initial values for the bounds of
407 /// @ref alpaqa::BoxConstrProblem::variable_bounds, i.e. the box constraints
408 /// on the decision variables.
410 void *instance,
411 alpaqa_real_t *lb,
412 alpaqa_real_t *ub) ALPAQA_DEFAULT(nullptr);
413 /// Provide the initial values for the bounds of
414 /// @ref alpaqa::BoxConstrProblem::general_bounds, i.e. the general
415 /// constraints.
417 void *instance,
418 alpaqa_real_t *lb,
419 alpaqa_real_t *ub) ALPAQA_DEFAULT(nullptr);
420 /// Provide the initial values for @ref alpaqa::BoxConstrProblem::l1_reg,
421 /// the ℓ₁-regularization factor.
422 /// This function is called twice:
423 /// 1. with @p lambda set to `nullptr`, and the user should set the size.
424 /// 2. with @p lambda pointing to an array of that size, and the user
425 /// should initialize this array.
427 void *instance,
428 alpaqa_real_t *lambda,
429 alpaqa_length_t *size) ALPAQA_DEFAULT(nullptr);
430 // clang-format on
431};
433
434/// Opaque type for a C++-only map of extra functions.
436/// Opaque type for a C++-only exception pointer.
438
439/// @note When used in C, you should initialize this struct by passing a pointer
440/// to your instance to the @ref ALPAQA_PROBLEM_REGISTER_INIT macro.
441/// In C++, this is not necessary, because all members have default
442/// initializers.
444 /// To check whether the loaded problem is compatible with the version of
445 /// the solver.
447 /// Owning pointer.
448 void *instance ALPAQA_DEFAULT(nullptr);
449 /// Non-owning pointer, lifetime at least as long as @ref instance.
451 /// Pointer to the function to clean up @ref instance.
452 void (*cleanup)(void *) ALPAQA_DEFAULT(nullptr);
453 /// Pointer to a map of extra functions (C++ only).
454 /// @see @ref alpaqa::register_function
455 /// @see @ref alpaqa::register_member_function
457 /// Pointer to an exception that ocurred during problem creation.
459};
461
466
467 // clang-format off
468 void (*get_U)(
469 void *instance,
470 alpaqa_real_t *lb,
471 alpaqa_real_t *ub) ALPAQA_DEFAULT(nullptr);
472 void (*get_D)(
473 void *instance,
474 alpaqa_real_t *lb,
475 alpaqa_real_t *ub) ALPAQA_DEFAULT(nullptr);
476 void (*get_D_N)(
477 void *instance,
478 alpaqa_real_t *lb,
479 alpaqa_real_t *ub) ALPAQA_DEFAULT(nullptr);
480 void (*get_x_init)(
481 void *instance,
482 alpaqa_real_t *x_init) ALPAQA_DEFAULT(nullptr);
483 void (*eval_f)(
484 void *instance,
485 alpaqa_index_t timestep,
486 const alpaqa_real_t *x,
487 const alpaqa_real_t *u,
488 alpaqa_real_t *fxu) ALPAQA_DEFAULT(nullptr);
489 void (*eval_jac_f)(
490 void *instance,
491 alpaqa_index_t timestep,
492 const alpaqa_real_t *x,
493 const alpaqa_real_t *u,
494 alpaqa_real_t *J_fxu) ALPAQA_DEFAULT(nullptr);
496 void *instance,
497 alpaqa_index_t timestep,
498 const alpaqa_real_t *x,
499 const alpaqa_real_t *u,
500 const alpaqa_real_t *p,
501 alpaqa_real_t *grad_fxu_p) ALPAQA_DEFAULT(nullptr);
502 void (*eval_h)(
503 void *instance,
504 alpaqa_index_t timestep,
505 const alpaqa_real_t *x,
506 const alpaqa_real_t *u,
507 alpaqa_real_t *h) ALPAQA_DEFAULT(nullptr);
508 void (*eval_h_N)(
509 void *instance,
510 const alpaqa_real_t *x,
511 alpaqa_real_t *h) ALPAQA_DEFAULT(nullptr);
513 void *instance,
514 alpaqa_index_t timestep,
515 const alpaqa_real_t *h) ALPAQA_DEFAULT(nullptr);
517 void *instance,
518 const alpaqa_real_t *h) ALPAQA_DEFAULT(nullptr);
519 void (*eval_qr)(
520 void *instance,
521 alpaqa_index_t timestep,
522 const alpaqa_real_t *xu,
523 const alpaqa_real_t *h,
524 alpaqa_real_t *qr) ALPAQA_DEFAULT(nullptr);
525 void (*eval_q_N)(
526 void *instance,
527 const alpaqa_real_t *x,
528 const alpaqa_real_t *h,
529 alpaqa_real_t *q) ALPAQA_DEFAULT(nullptr);
530 void (*eval_add_Q)(
531 void *instance,
532 alpaqa_index_t timestep,
533 const alpaqa_real_t *xu,
534 const alpaqa_real_t *h,
535 alpaqa_real_t *Q) ALPAQA_DEFAULT(nullptr);
537 void *instance,
538 const alpaqa_real_t *x,
539 const alpaqa_real_t *h,
540 alpaqa_real_t *Q) ALPAQA_DEFAULT(nullptr);
542 void *instance,
543 alpaqa_index_t timestep,
544 const alpaqa_real_t *xu,
545 const alpaqa_real_t *h,
546 const alpaqa_index_t *mask,
547 alpaqa_real_t *R,
548 alpaqa_real_t *work) ALPAQA_DEFAULT(nullptr);
550 void *instance,
551 alpaqa_index_t timestep,
552 const alpaqa_real_t *xu,
553 const alpaqa_real_t *h,
554 const alpaqa_index_t *mask,
555 alpaqa_real_t *S,
556 alpaqa_real_t *work) ALPAQA_DEFAULT(nullptr);
558 void *instance,
559 alpaqa_index_t timestep,
560 const alpaqa_real_t *xu,
561 const alpaqa_real_t *h,
562 const alpaqa_index_t *mask_J,
563 const alpaqa_index_t *mask_K,
564 const alpaqa_real_t *v,
565 alpaqa_real_t *out,
566 alpaqa_real_t *work) ALPAQA_DEFAULT(nullptr);
568 void *instance,
569 alpaqa_index_t timestep,
570 const alpaqa_real_t *xu,
571 const alpaqa_real_t *h,
572 const alpaqa_index_t *mask_K,
573 const alpaqa_real_t *v,
574 alpaqa_real_t *out,
575 alpaqa_real_t *work) ALPAQA_DEFAULT(nullptr);
577 void *instance) ALPAQA_DEFAULT(nullptr);
579 void *instance) ALPAQA_DEFAULT(nullptr);
580 void (*eval_constr)(
581 void *instance,
582 alpaqa_index_t timestep,
583 const alpaqa_real_t *x,
584 alpaqa_real_t *c) ALPAQA_DEFAULT(nullptr);
586 void *instance,
587 const alpaqa_real_t *x,
588 alpaqa_real_t *c) ALPAQA_DEFAULT(nullptr);
590 void *instance,
591 alpaqa_index_t timestep,
592 const alpaqa_real_t *x,
593 const alpaqa_real_t *p,
594 alpaqa_real_t *grad_cx_p) ALPAQA_DEFAULT(nullptr);
596 void *instance,
597 const alpaqa_real_t *x,
598 const alpaqa_real_t *p,
599 alpaqa_real_t *grad_cx_p) ALPAQA_DEFAULT(nullptr);
601 void *instance,
602 alpaqa_index_t timestep,
603 const alpaqa_real_t *x,
604 const alpaqa_real_t *M,
605 alpaqa_real_t *out) ALPAQA_DEFAULT(nullptr);
607 void *instance,
608 const alpaqa_real_t *x,
609 const alpaqa_real_t *M,
610 alpaqa_real_t *out) ALPAQA_DEFAULT(nullptr);
611 // clang-format on
612};
615
616/// @note When used in C, you should initialize this struct by passing a pointer
617/// to your instance to the @ref ALPAQA_PROBLEM_REGISTER_INIT macro.
618/// In C++, this is not necessary, because all members have default
619/// initializers.
621 /// To check whether the loaded problem is compatible with the version of
622 /// the solver.
624 /// Owning pointer.
625 void *instance ALPAQA_DEFAULT(nullptr);
626 /// Non-owning pointer, lifetime at least as long as @ref instance.
628 /// Pointer to the function to clean up @ref instance.
629 void (*cleanup)(void *) ALPAQA_DEFAULT(nullptr);
630 /// Pointer to a map of extra functions (C++ only).
632 /// Pointer to an exception that ocurred during problem creation.
634};
637
638#ifdef __cplusplus
639}
640#endif
641
642#if !defined(__cplusplus) || defined(DOXYGEN)
643inline static void
649inline static void
655/// Initialize an instance of @ref alpaqa_problem_register_t or
656/// @ref alpaqa_control_problem_register_t. It initializes all members to zero,
657/// except for the ABI version, which is initialized to the current ABI version.
658/// Available in C only (unnecessary in C++).
659/// @param self
660/// A pointer to the instance to initialize.
661#define ALPAQA_PROBLEM_REGISTER_INIT(self) \
662 _Generic((self), \
663 alpaqa_problem_register_t *: alpaqa_problem_register_init, \
664 alpaqa_control_problem_register_t *: alpaqa_control_problem_register_init)( \
665 self)
666#endif
667
668#if defined(__cplusplus) && \
669 (__cplusplus > 201703L || (defined(_MSVC_LANG) && _MSVC_LANG > 201703L))
670
671#include <any>
672#include <exception>
673#include <functional>
674#include <map>
675#include <string>
676
678 std::map<std::string, std::any> dict{};
679};
680
682 std::exception_ptr exc{};
683};
684
685namespace alpaqa {
686
692
693namespace detail {
694/// Custom type for which we can export the RTTI to support std::any across
695/// shared library boundaries when using libc++.
696template <class Signature>
698 std::function<Signature> function;
699};
700template <class Signature>
702} // namespace detail
703
704/// Make the given function available to alpaqa.
705/// @see @ref alpaqa::dl::DLProblem::call_extra_func
706/// @see @ref alpaqa::dl::DLControlProblem::call_extra_func
707template <class Func>
708void register_function(function_dict_t *&extra_functions, std::string name,
709 Func &&func) {
710 if (extra_functions == nullptr)
711 extra_functions = new function_dict_t{};
712 extra_functions->dict.insert_or_assign(
713 std::move(name),
714 detail::function_wrapper_t{std::function{std::forward<Func>(func)}});
715}
716
717template <class Func>
718void register_function(problem_register_t &result, std::string name,
719 Func &&func) {
720 register_function(result.extra_functions, std::move(name),
721 std::forward<Func>(func));
722}
723
724template <class Func>
725void register_function(control_problem_register_t &result, std::string name,
726 Func &&func) {
727 register_function(result.extra_functions, std::move(name),
728 std::forward<Func>(func));
729}
730
731template <class Result, class T, class Ret, class... Args>
732void register_member_function(Result &result, std::string name,
733 Ret (T::*member)(Args...)) {
734 register_function(result, std::move(name),
735 [member](void *self_, Args... args) -> Ret {
736 auto *self = reinterpret_cast<T *>(self_);
737 return (self->*member)(std::forward<Args>(args)...);
738 });
739}
740
741template <class Result, class T, class Ret, class... Args>
742void register_member_function(Result &result, std::string name,
743 Ret (T::*member)(Args...) const) {
744 register_function(result, std::move(name),
745 [member](const void *self_, Args... args) -> Ret {
746 const auto *self = reinterpret_cast<const T *>(self_);
747 return (self->*member)(std::forward<Args>(args)...);
748 });
749}
750
751namespace detail {
752/// Overload for non-const-qualified member functions.
753/// @see @ref alpaqa::member_caller
754template <auto Member, class Class, class Ret, class... Args>
755static auto member_caller(Ret (Class::*)(Args...)) {
756 return [](void *self_, Args... args) -> Ret {
757 auto *self = reinterpret_cast<Class *>(self_);
758 return (self->*Member)(std::forward<Args>(args)...);
759 };
760}
761/// Overload for const-qualified member functions.
762/// @see @ref alpaqa::member_caller
763template <auto Member, class Class, class Ret, class... Args>
764static auto member_caller(Ret (Class::*)(Args...) const) {
765 return []<class Self>(Self *self_, Args... args) -> Ret
766 requires std::is_void_v<Self>
767 {
768 const auto *self = reinterpret_cast<const Class *>(self_);
769 return (self->*Member)(std::forward<Args>(args)...);
770 };
771}
772/// Overload for member variables.
773/// @see @ref alpaqa::member_caller
774template <auto Member, class Class, class Ret>
775static auto member_caller(Ret Class::*) {
776 return []<class Self>(Self *self_) -> decltype(auto)
777 requires std::is_void_v<Self>
778 {
779 using CClass = std::conditional_t<std::is_const_v<Self>,
780 std::add_const_t<Class>, Class>;
781 auto *self = reinterpret_cast<CClass *>(self_);
782 return self->*Member;
783 };
784}
785} // namespace detail
786
787/// Wrap the given member function or variable into a (possibly generic) lambda
788/// function that accepts the instance as a type-erased void pointer.
789///
790/// The signature of the resulting function depends on the signature of the
791/// member function:
792///
793/// - `Ret Class::member(args...)` → `Ret(void *self, args...)`
794/// - `Ret Class::member(args...) const` → `Ret(void *self, args...)`
795/// - `Ret Class::member(args...) const` → `Ret(const void *self, args...)`
796///
797/// If the given member is a variable:
798///
799/// - `Type Class::member` → `Type &(void *self)`
800/// - `Type Class::member` → `const Type &(const void *self)`
801template <auto Member>
802static auto member_caller() {
803 return detail::member_caller<Member>(Member);
804}
805
806/// Cleans up the extra functions registered by @ref register_function.
807/// @note This does not need to be called for the functions returned by the
808/// registration function, those functions will be cleaned up by alpaqa.
809/// @note The @ref alpaqa_problem_register_t and
810/// @ref alpaqa_control_problem_register_t structs are part of the C API
811/// and do not automatically clean up their resources when destroyed,
812/// you have to do it manually by calling this function.
813inline void unregister_functions(function_dict_t *&extra_functions) {
814 delete extra_functions;
815 extra_functions = nullptr;
816}
817
818} // namespace alpaqa
819
820#endif
821
822#undef ALPAQA_DEFAULT
823
824// NOLINTEND(modernize-use-using,modernize-deprecated-headers)
alpaqa_length_t cols
Definition dl-problem.h:79
void(* eval_add_Q)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, alpaqa_real_t *Q)
Definition dl-problem.h:530
static void alpaqa_control_problem_register_init(alpaqa_control_problem_register_t *self)
Definition dl-problem.h:650
void(* eval_grad_f_prod)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *u, const alpaqa_real_t *p, alpaqa_real_t *grad_fxu_p)
Definition dl-problem.h:495
#define ALPAQA_DL_ABI_VERSION
Definition dl-problem.h:8
void(* eval_projection_multipliers)(void *instance, alpaqa_real_t *y, alpaqa_real_t M)
Project the Lagrange multipliers.
Definition dl-problem.h:251
alpaqa_length_t num_variables
Number of decision variables.
Definition dl-problem.h:208
alpaqa_sparse_coo_ll_t sparse_coo_ll
alpaqa_register_arg_type_t
Definition dl-problem.h:31
@ alpaqa_register_arg_unspecified
No type was specified (discouraged).
Definition dl-problem.h:33
@ alpaqa_register_arg_strings
The data pointer points to a dynamic C++ std::span of std::string_view.
Definition dl-problem.h:51
@ alpaqa_register_arg_std_any
The data pointer points to a C++ std::any object.
Definition dl-problem.h:46
@ alpaqa_register_arg_py_args
The data pointer points to a C++ tuple of pybind11::args and pybind11::kwargs.
Definition dl-problem.h:56
void(* cleanup)(void *)
Pointer to the function to clean up instance.
Definition dl-problem.h:452
alpaqa_real_t(* eval_augmented_lagrangian)(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 *ŷ)
Augmented Lagrangian.
Definition dl-problem.h:373
alpaqa_sparsity_t(* get_lagrangian_hessian_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the Lagrangian.
Definition dl-problem.h:312
alpaqa_sparsity_t(* get_augmented_lagrangian_hessian_sparsity)(void *instance)
Get the sparsity pattern of the Hessian of the augmented Lagrangian.
Definition dl-problem.h:339
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)
Definition dl-problem.h:600
void(* eval_qr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, alpaqa_real_t *qr)
Definition dl-problem.h:519
struct alpaqa_function_dict_s alpaqa_function_dict_t
Opaque type for a C++-only map of extra functions.
Definition dl-problem.h:435
alpaqa_length_t(* get_S_work_size)(void *instance)
Definition dl-problem.h:578
ptrdiff_t alpaqa_length_t
Definition dl-problem.h:27
void(* eval_objective_gradient)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *grad_fx)
Gradient of the cost function.
Definition dl-problem.h:224
#define ALPAQA_DEFAULT(...)
Definition dl-problem.h:21
alpaqa_real_t(* eval_l_N)(void *instance, const alpaqa_real_t *h)
Definition dl-problem.h:516
alpaqa_length_t alpaqa_index_t
Definition dl-problem.h:28
alpaqa_length_t rows
Definition dl-problem.h:79
void(* eval_grad_constr_prod_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *p, alpaqa_real_t *grad_cx_p)
Definition dl-problem.h:595
void(* get_x_init)(void *instance, alpaqa_real_t *x_init)
Definition dl-problem.h:480
void(* eval_add_S_masked)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, const alpaqa_index_t *mask, alpaqa_real_t *S, alpaqa_real_t *work)
Definition dl-problem.h:549
void(* initialize_variable_bounds)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Provide the initial values for the bounds of variable_bounds, i.e.
Definition dl-problem.h:409
alpaqa_symmetry symmetry
Definition dl-problem.h:80
alpaqa_exception_ptr_t * exception
Pointer to an exception that ocurred during problem creation.
Definition dl-problem.h:458
void(* eval_q_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *h, alpaqa_real_t *q)
Definition dl-problem.h:525
std::exception_ptr exc
Definition dl-problem.h:682
void(* eval_objective_gradient_and_constraints_gradient_product)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_f, alpaqa_real_t *grad_gxy)
Gradient of the cost and gradient-vector product of the constraints.
Definition dl-problem.h:356
void(* eval_lagrangian_hessian_product)(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)
Hessian-vector product of the Lagrangian.
Definition dl-problem.h:295
uint64_t alpaqa_dl_abi_version_t
Definition dl-problem.h:29
#define ALPAQA_DL_PROBLEM_EXPORT
Definition dl-problem.h:16
alpaqa_real_t(* eval_objective)(void *instance, const alpaqa_real_t *x)
Cost function.
Definition dl-problem.h:219
void(* eval_lagrangian_gradient)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_L, alpaqa_real_t *work_n)
Gradient of the Lagrangian.
Definition dl-problem.h:364
void * data
Pointer to the user-provided argument passed to the problem registration functions.
Definition dl-problem.h:63
void(* eval_augmented_lagrangian_gradient)(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)
Gradient of the augmented Lagrangian.
Definition dl-problem.h:383
void(* eval_augmented_lagrangian_hessian_product)(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)
Hessian-vector product of the augmented Lagrangian.
Definition dl-problem.h:316
void(* get_D)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:472
void(* eval_f)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *u, alpaqa_real_t *fxu)
Definition dl-problem.h:483
alpaqa_dl_abi_version_t abi_version
To check whether the loaded problem is compatible with the version of the solver.
Definition dl-problem.h:446
void(* eval_grad_gi)(void *instance, const alpaqa_real_t *x, alpaqa_index_t i, alpaqa_real_t *grad_gi)
Gradient of specific constraint function.
Definition dl-problem.h:288
struct alpaqa_exception_ptr_s alpaqa_exception_ptr_t
Opaque type for a C++-only exception pointer.
Definition dl-problem.h:437
alpaqa_real_t(* eval_augmented_lagrangian_and_gradient)(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)
Augmented Lagrangian and its gradient.
Definition dl-problem.h:395
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)
Definition dl-problem.h:589
double alpaqa_real_t
Definition dl-problem.h:26
const char * name
Name of the problem.
Definition dl-problem.h:214
void(* initialize_l1_reg)(void *instance, alpaqa_real_t *lambda, alpaqa_length_t *size)
Provide the initial values for l1_reg, the ℓ₁-regularization factor.
Definition dl-problem.h:426
void(* eval_add_R_masked)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *xu, const alpaqa_real_t *h, const alpaqa_index_t *mask, alpaqa_real_t *R, alpaqa_real_t *work)
Definition dl-problem.h:541
alpaqa_index_t(* eval_inactive_indices_res_lna)(void *instance, alpaqa_real_t γ, const alpaqa_real_t *x, const alpaqa_real_t *grad_ψ, alpaqa_index_t *J)
Active indices for proximal operator.
Definition dl-problem.h:269
alpaqa_sparse_csc_ll_t sparse_csc_ll
alpaqa_sparse_csc_t sparse_csc
void(* eval_h)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *u, alpaqa_real_t *h)
Definition dl-problem.h:502
std::map< std::string, std::any > dict
Definition dl-problem.h:678
alpaqa_real_t(* eval_l)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *h)
Definition dl-problem.h:512
void(* eval_jac_f)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, const alpaqa_real_t *u, alpaqa_real_t *J_fxu)
Definition dl-problem.h:489
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)
Definition dl-problem.h:557
alpaqa_real_t(* eval_proximal_gradient_step)(void *instance, alpaqa_real_t γ, const alpaqa_real_t *x, const alpaqa_real_t *grad_ψ, alpaqa_real_t *x̂, alpaqa_real_t *p)
Proximal gradient step.
Definition dl-problem.h:258
alpaqa_length_t num_constraints
Number of constraints.
Definition dl-problem.h:211
void(* eval_add_gn_hess_constr_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *M, alpaqa_real_t *out)
Definition dl-problem.h:606
alpaqa_length_t(* get_R_work_size)(void *instance)
Definition dl-problem.h:576
alpaqa_sparse_csc_l_t sparse_csc_l
void(* initialize_general_bounds)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Provide the initial values for the bounds of general_bounds, i.e.
Definition dl-problem.h:416
alpaqa_real_t(* eval_objective_and_gradient)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *grad_fx)
Cost and its gradient.
Definition dl-problem.h:344
void(* get_D_N)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:476
void(* eval_constraints_jacobian)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *J_values)
Jacobian of the constraints function.
Definition dl-problem.h:278
alpaqa_symmetry
Definition dl-problem.h:71
@ alpaqa_unsymmetric
Definition dl-problem.h:72
@ alpaqa_lower
Definition dl-problem.h:74
@ alpaqa_upper
Definition dl-problem.h:73
alpaqa_register_arg_type_t type
Specifies the type of the data pointed to by data.
Definition dl-problem.h:66
void(* eval_constr)(void *instance, alpaqa_index_t timestep, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:580
void(* eval_constraints)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *gx)
Constraints function.
Definition dl-problem.h:230
void(* eval_add_Q_N)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *h, alpaqa_real_t *Q)
Definition dl-problem.h:536
alpaqa_function_dict_t * extra_functions
Pointer to a map of extra functions (C++ only).
Definition dl-problem.h:456
alpaqa_control_problem_functions_t * functions
Non-owning pointer, lifetime at least as long as instance.
Definition dl-problem.h:627
void(* eval_constraints_gradient_product)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t *grad_gxy)
Gradient-vector product of the constraints function.
Definition dl-problem.h:236
void(* eval_projecting_difference_constraints)(void *instance, const alpaqa_real_t *z, alpaqa_real_t *e)
Difference between point and its projection onto the general constraint set D.
Definition dl-problem.h:245
void(* eval_lagrangian_hessian)(void *instance, const alpaqa_real_t *x, const alpaqa_real_t *y, alpaqa_real_t scale, alpaqa_real_t *H_values)
Hessian of the Lagrangian.
Definition dl-problem.h:304
alpaqa_sparse_coo_t sparse_coo
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)
Definition dl-problem.h:567
void(* eval_augmented_lagrangian_hessian)(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_real_t *H_values)
Hessian of the augmented Lagrangian.
Definition dl-problem.h:328
void * instance
Owning pointer.
Definition dl-problem.h:448
void(* get_U)(void *instance, alpaqa_real_t *lb, alpaqa_real_t *ub)
Definition dl-problem.h:468
alpaqa_problem_functions_t * functions
Non-owning pointer, lifetime at least as long as instance.
Definition dl-problem.h:450
void(* eval_constr_N)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *c)
Definition dl-problem.h:585
static void alpaqa_problem_register_init(alpaqa_problem_register_t *self)
Definition dl-problem.h:644
alpaqa_sparsity_t(* get_constraints_jacobian_sparsity)(void *instance)
Get the sparsity pattern of the Jacobian of the constraints function.
Definition dl-problem.h:284
alpaqa_sparse_coo_l_t sparse_coo_l
alpaqa_real_t(* eval_objective_and_constraints)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *g)
Cost and constraints.
Definition dl-problem.h:350
void(* eval_h_N)(void *instance, const alpaqa_real_t *x, alpaqa_real_t *h)
Definition dl-problem.h:508
C API providing function pointers to problem functions.
Definition dl-problem.h:205
User-provided argument that is passed to the problem registration functions.
Definition dl-problem.h:60
static auto member_caller(Ret(Class::*)(Args...))
Overload for non-const-qualified member functions.
Definition dl-problem.h:755
function_wrapper_t(std::function< Signature >) -> function_wrapper_t< Signature >
std::function< Signature > function
Definition dl-problem.h:698
Custom type for which we can export the RTTI to support std::any across shared library boundaries whe...
Definition dl-problem.h:697
alpaqa_problem_register_t problem_register_t
Definition dl-problem.h:688
alpaqa_control_problem_register_t control_problem_register_t
Definition dl-problem.h:689
alpaqa_problem_functions_t problem_functions_t
Definition dl-problem.h:690
void unregister_functions(function_dict_t *&extra_functions)
Cleans up the extra functions registered by register_function.
Definition dl-problem.h:813
alpaqa_function_dict_t function_dict_t
Definition dl-problem.h:687
alpaqa_control_problem_functions_t control_problem_functions_t
Definition dl-problem.h:691
void register_function(function_dict_t *&extra_functions, std::string name, Func &&func)
Make the given function available to alpaqa.
Definition dl-problem.h:708
void register_member_function(Result &result, std::string name, Ret(T::*member)(Args...))
Definition dl-problem.h:732
static auto member_caller()
Wrap the given member function or variable into a (possibly generic) lambda function that accepts the...
Definition dl-problem.h:802
alpaqa_length_t cols
Definition dl-problem.h:146
@ alpaqa_sparse_coo_l_sorted_by_rows_and_cols
Definition dl-problem.h:156
@ alpaqa_sparse_coo_l_sorted_by_cols_and_rows
Definition dl-problem.h:154
@ alpaqa_sparse_coo_l_sorted_by_cols_only
Definition dl-problem.h:155
@ alpaqa_sparse_coo_l_sorted_by_rows_only
Definition dl-problem.h:157
alpaqa_length_t rows
Definition dl-problem.h:146
alpaqa_symmetry symmetry
Definition dl-problem.h:147
alpaqa_length_t nnz
Definition dl-problem.h:148
const long * row_indices
Definition dl-problem.h:149
const long * col_indices
Definition dl-problem.h:150
alpaqa_length_t cols
Definition dl-problem.h:164
@ alpaqa_sparse_coo_ll_sorted_by_rows_and_cols
Definition dl-problem.h:174
@ alpaqa_sparse_coo_ll_sorted_by_cols_and_rows
Definition dl-problem.h:172
alpaqa_length_t rows
Definition dl-problem.h:164
alpaqa_symmetry symmetry
Definition dl-problem.h:165
const long long * row_indices
Definition dl-problem.h:167
alpaqa_length_t nnz
Definition dl-problem.h:166
const long long * col_indices
Definition dl-problem.h:168
alpaqa_length_t cols
Definition dl-problem.h:128
const int * col_indices
Definition dl-problem.h:132
alpaqa_length_t rows
Definition dl-problem.h:128
alpaqa_symmetry symmetry
Definition dl-problem.h:129
const int * row_indices
Definition dl-problem.h:131
@ alpaqa_sparse_coo_sorted_by_cols_only
Definition dl-problem.h:137
@ alpaqa_sparse_coo_sorted_by_rows_and_cols
Definition dl-problem.h:138
@ alpaqa_sparse_coo_sorted_by_cols_and_rows
Definition dl-problem.h:136
@ alpaqa_sparse_coo_sorted_by_rows_only
Definition dl-problem.h:139
alpaqa_length_t nnz
Definition dl-problem.h:130
alpaqa_length_t cols
Definition dl-problem.h:100
alpaqa_length_t rows
Definition dl-problem.h:100
alpaqa_symmetry symmetry
Definition dl-problem.h:101
alpaqa_length_t nnz
Definition dl-problem.h:102
const long * outer_ptr
Definition dl-problem.h:104
const long * inner_idx
Definition dl-problem.h:103
alpaqa_length_t cols
Definition dl-problem.h:114
const long long * inner_idx
Definition dl-problem.h:117
alpaqa_length_t rows
Definition dl-problem.h:114
alpaqa_symmetry symmetry
Definition dl-problem.h:115
const long long * outer_ptr
Definition dl-problem.h:118
alpaqa_length_t nnz
Definition dl-problem.h:116
alpaqa_length_t cols
Definition dl-problem.h:86
const int * inner_idx
Definition dl-problem.h:89
alpaqa_length_t rows
Definition dl-problem.h:86
alpaqa_symmetry symmetry
Definition dl-problem.h:87
const int * outer_ptr
Definition dl-problem.h:90
alpaqa_length_t nnz
Definition dl-problem.h:88
Sparsity of matrices.
Definition dl-problem.h:182