aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
functions.hpp
Go to the documentation of this file.
1
2#pragma once
3
6
9
12
14
15#include "proxsuite-nlp/python/polymorphic.hpp"
16
17namespace aligator {
18namespace python {
26template <class FunctionBase = context::StageFunction>
27struct PyStageFunction final
28 : FunctionBase,
29 proxsuite::nlp::python::PolymorphicWrapper<PyStageFunction<FunctionBase>,
30 FunctionBase> {
31 using Scalar = typename FunctionBase::Scalar;
33 using FunctionBase::FunctionBase;
35
36 void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
37 Data &data) const override {
38 ALIGATOR_PYTHON_OVERRIDE_PURE(void, "evaluate", x, u, boost::ref(data));
39 }
40
41 void computeJacobians(const ConstVectorRef &x, const ConstVectorRef &u,
42 Data &data) const override {
43 ALIGATOR_PYTHON_OVERRIDE_PURE(void, "computeJacobians", x, u,
44 boost::ref(data));
45 }
46
47 void computeVectorHessianProducts(const ConstVectorRef &x,
48 const ConstVectorRef &u,
49 const ConstVectorRef &lbda,
50 Data &data) const override {
52 x, u, lbda, boost::ref(data));
53 }
54
55 shared_ptr<Data> createData() const override {
56 ALIGATOR_PYTHON_OVERRIDE(shared_ptr<Data>, FunctionBase, createData, );
57 }
58
59 shared_ptr<Data> default_createData() const {
60 return FunctionBase::createData();
61 }
62};
63
64template <typename UFunction = context::UnaryFunction>
65struct PyUnaryFunction final
66 : UFunction,
67 proxsuite::nlp::python::PolymorphicWrapper<PyUnaryFunction<UFunction>,
68 UFunction> {
69 using Scalar = typename UFunction::Scalar;
70 static_assert(
71 std::is_base_of_v<UnaryFunctionTpl<Scalar>, UFunction>,
72 "Template parameter UFunction must derive from UnaryFunctionTpl<>.");
76
77 using UFunction::UFunction;
78
79 void evaluate(const ConstVectorRef &x, Data &data) const override {
80 ALIGATOR_PYTHON_OVERRIDE_PURE(void, "evaluate", x, boost::ref(data));
81 }
82
83 void computeJacobians(const ConstVectorRef &x, Data &data) const override {
84 ALIGATOR_PYTHON_OVERRIDE_PURE(void, "computeJacobians", x,
85 boost::ref(data));
86 }
87
88 void computeVectorHessianProducts(const ConstVectorRef &x,
89 const ConstVectorRef &lbda,
90 Data &data) const override {
92 lbda, boost::ref(data));
93 }
94
95 void default_computeVectorHessianProducts(const ConstVectorRef &x,
96 const ConstVectorRef &lbda,
97 Data &data) const {
98 UFunction::computeVectorHessianProducts(x, lbda, data);
99 }
100
101 shared_ptr<Data> createData() const override {
102 ALIGATOR_PYTHON_OVERRIDE(shared_ptr<Data>, UFunction, createData, );
103 }
104
105 shared_ptr<Data> default_createData() const {
106 return UFunction::createData();
107 }
108};
109
110template <typename Class>
111struct SlicingVisitor : bp::def_visitor<SlicingVisitor<Class>> {
112 using Scalar = typename Class::Scalar;
114
115 template <typename Iterator, typename Fn>
116 static auto do_with_slice(Fn &&fun, bp::slice::range<Iterator> &range) {
117 while (range.start != range.stop) {
118 fun(*range.start);
119 std::advance(range.start, range.step);
120 }
121 fun(*range.start);
122 }
123
124 static auto get_slice(xyz::polymorphic<Class> const &fn,
125 bp::slice slice_obj) {
126 std::vector<int> indices((unsigned)fn->nr);
127 std::iota(indices.begin(), indices.end(), 0);
128 auto bounds = slice_obj.get_indices(indices.cbegin(), indices.cend());
129 std::vector<int> out{};
130
131 do_with_slice([&](int i) { out.push_back(i); }, bounds);
132 return SliceType(fn, out);
133 }
134
135 static auto get_from_index(xyz::polymorphic<Class> const &fn, const int idx) {
136 return SliceType(fn, idx);
137 }
138
139 static auto get_from_indices(xyz::polymorphic<Class> const &fn,
140 std::vector<int> const &indices) {
141 return SliceType(fn, indices);
142 }
143
144 template <typename... Args> void visit(bp::class_<Args...> &cl) const {
145 cl.def("__getitem__", &get_from_index, bp::args("self", "idx"))
146 .def("__getitem__", &get_from_indices, bp::args("self", "indices"))
147 .def("__getitem__", &get_slice, bp::args("self", "sl"));
148 }
149};
150
151} // namespace python
152} // namespace aligator
153
154namespace boost::python::objects {
155
156template <>
157struct value_holder<aligator::python::PyStageFunction<>>
158 : proxsuite::nlp::python::OwningNonOwningHolder<
159 aligator::python::PyStageFunction<>> {
160 using OwningNonOwningHolder::OwningNonOwningHolder;
161};
162
163template <>
164struct value_holder<aligator::python::PyUnaryFunction<>>
165 : proxsuite::nlp::python::OwningNonOwningHolder<
166 aligator::python::PyUnaryFunction<>> {
167 using OwningNonOwningHolder::OwningNonOwningHolder;
168};
169
170} // namespace boost::python::objects
#define ALIGATOR_PYTHON_OVERRIDE(ret_type, cname, fname,...)
Define the body of a virtual function override. This is meant to reduce boilerplate code when exposin...
Definition macros.hpp:45
#define ALIGATOR_PYTHON_OVERRIDE_PURE(ret_type, pyname,...)
Define the body of a virtual function override. This is meant to reduce boilerplate code when exposin...
Definition macros.hpp:37
Base definitions for ternary functions.
Base definitions for numerical integrators.
Main package namespace.
Represents a function of which the output is a subset of another function, for instance where is gi...
Base struct for function data.
Definition fwd.hpp:59
void computeJacobians(const ConstVectorRef &x, const ConstVectorRef &u, Data &data) const override
Compute Jacobians of this function.
Definition functions.hpp:41
shared_ptr< Data > default_createData() const
Definition functions.hpp:59
typename FunctionBase::Scalar Scalar
Definition functions.hpp:31
shared_ptr< Data > createData() const override
Instantiate a Data object.
Definition functions.hpp:55
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u, Data &data) const override
Evaluate the function.
Definition functions.hpp:36
void computeVectorHessianProducts(const ConstVectorRef &x, const ConstVectorRef &u, const ConstVectorRef &lbda, Data &data) const override
Compute the vector-hessian products of this function.
Definition functions.hpp:47
void evaluate(const ConstVectorRef &x, Data &data) const override
Definition functions.hpp:79
shared_ptr< Data > default_createData() const
typename UFunction::Scalar Scalar
Definition functions.hpp:69
shared_ptr< Data > createData() const override
Instantiate a Data object.
void default_computeVectorHessianProducts(const ConstVectorRef &x, const ConstVectorRef &lbda, Data &data) const
Definition functions.hpp:95
void computeVectorHessianProducts(const ConstVectorRef &x, const ConstVectorRef &lbda, Data &data) const override
Definition functions.hpp:88
void computeJacobians(const ConstVectorRef &x, Data &data) const override
Definition functions.hpp:83
static auto get_from_index(xyz::polymorphic< Class > const &fn, const int idx)
FunctionSliceXprTpl< Scalar, Class > SliceType
static auto get_from_indices(xyz::polymorphic< Class > const &fn, std::vector< int > const &indices)
void visit(bp::class_< Args... > &cl) const
static auto get_slice(xyz::polymorphic< Class > const &fn, bp::slice slice_obj)
static auto do_with_slice(Fn &&fun, bp::slice::range< Iterator > &range)
typename Class::Scalar Scalar