aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
expose-functions.cpp
Go to the documentation of this file.
1
5
10
11namespace aligator {
12namespace python {
13
14using context::ConstMatrixRef;
15using context::ConstVectorRef;
17using context::MatrixXs;
18using context::Scalar;
22using context::VectorXs;
23using PolyFunction = xyz::polymorphic<StageFunction>;
27
29struct FunctionDataWrapper : StageFunctionData, bp::wrapper<StageFunctionData> {
30 using StageFunctionData::StageFunctionData;
31};
32
34 register_polymorphic_to_python<PolyFunction>();
35 bp::class_<PyStageFunction<>, boost::noncopyable>(
36 "StageFunction",
37 "Base class for ternary functions f(x,u,x') on a stage of the problem.",
38 bp::no_init)
39 .def(bp::init<const int, const int, const int>(
40 ("self"_a, "ndx", "nu", "nr")))
41 .def("evaluate", bp::pure_virtual(&StageFunction::evaluate),
42 ("self"_a, "x", "u", "data"))
43 .def("computeJacobians",
44 bp::pure_virtual(&StageFunction::computeJacobians),
45 ("self"_a, "x", "u", "data"))
46 .def("computeVectorHessianProducts",
47 &StageFunction::computeVectorHessianProducts,
48 ("self"_a, "x", "u", "lbda", "data"))
49 .def_readonly("ndx1", &StageFunction::ndx1, "Current state space.")
50 .def_readonly("nu", &StageFunction::nu, "Control dimension.")
51 .def_readonly("nr", &StageFunction::nr, "Function codimension.")
53 .def(func_visitor)
54 .def(CreateDataPolymorphicPythonVisitor<StageFunction,
56 .enable_pickling_(true);
57
58 bp::register_ptr_to_python<shared_ptr<StageFunctionData>>();
59
60 bp::class_<FunctionDataWrapper, boost::noncopyable>(
61 "StageFunctionData", "Data struct for holding data about functions.",
62 bp::no_init)
63 .def(bp::init<const StageFunction &>(("self"_a, "model")))
64 .def(bp::init<int, int, int>(bp::args("self", "ndx", "nu", "nr")))
65 .add_property(
66 "value",
67 bp::make_getter(&StageFunctionData::valref_,
68 bp::return_value_policy<bp::return_by_value>()),
69 "Function value.")
70 .add_property("jac_buffer",
71 make_getter_eigen_matrix(&StageFunctionData::jac_buffer_),
72 "Buffer of the full function Jacobian wrt (x,u,y).")
73 .add_property(
74 "vhp_buffer",
75 make_getter_eigen_matrix(&StageFunctionData::vhp_buffer_),
76 "Buffer of the full function vector-Hessian product wrt (x,u,y).")
77 .add_property(
78 "Jx",
79 bp::make_getter(&StageFunctionData::Jx_,
80 bp::return_value_policy<bp::return_by_value>()),
81 "Jacobian with respect to $x$.")
82 .add_property(
83 "Ju",
84 bp::make_getter(&StageFunctionData::Ju_,
85 bp::return_value_policy<bp::return_by_value>()),
86 "Jacobian with respect to $u$.")
87 .add_property(
88 "Hxx",
89 bp::make_getter(&StageFunctionData::Hxx_,
90 bp::return_value_policy<bp::return_by_value>()),
91 "Hessian with respect to $(x, x)$.")
92 .add_property(
93 "Hxu",
94 bp::make_getter(&StageFunctionData::Hxu_,
95 bp::return_value_policy<bp::return_by_value>()),
96 "Hessian with respect to $(x, u)$.")
97 .add_property(
98 "Huu",
99 bp::make_getter(&StageFunctionData::Huu_,
100 bp::return_value_policy<bp::return_by_value>()),
101 "Hessian with respect to $(u, u)$.")
104
105 StdVectorPythonVisitor<std::vector<PolyFunction>, true>::expose(
106 "StdVec_StageFunction",
107 eigenpy::details::overload_base_get_item_for_std_vector<
108 std::vector<PolyFunction>>{});
109 StdVectorPythonVisitor<std::vector<shared_ptr<StageFunctionData>>,
110 true>::expose("StdVec_StageFunctionData");
111}
112
113// fwd-decl exposeUnaryFunctions()
115
116// fwd-decl exposeFunctionExpressions()
118
123
124 bp::class_<StateErrorResidual, bp::bases<UnaryFunction>>(
125 "StateErrorResidual",
126 bp::init<const xyz::polymorphic<context::Manifold> &, const int,
127 const context::VectorXs &>(("self"_a, "space", "nu", "target")))
128 .def_readonly("space", &StateErrorResidual::space_)
129 .def_readwrite("target", &StateErrorResidual::target_)
131
132 bp::class_<ControlErrorResidual, bp::bases<StageFunction>>(
133 "ControlErrorResidual",
134 bp::init<const int, const xyz::polymorphic<context::Manifold> &,
135 const context::VectorXs &>(
136 ("self"_a, "ndx", "uspace", "target")))
137 .def(bp::init<const int, const context::VectorXs &>(
138 ("self"_a, "ndx", "target")))
139 .def(bp::init<int, int>(("self"_a, "ndx", "nu")))
140 .def_readonly("space", &ControlErrorResidual::space_)
141 .def_readwrite("target", &ControlErrorResidual::target_)
142 .def(func_visitor);
143
144 using LinearFunction = LinearFunctionTpl<Scalar>;
145 bp::class_<LinearFunction, bp::bases<StageFunction>>(
146 "LinearFunction",
147 bp::init<const int, const int, const int>(("self"_a, "ndx", "nu", "nr")))
148 .def(bp::init<const ConstMatrixRef &, const ConstMatrixRef &,
149 const ConstVectorRef &>("Constructor with C=0.",
150 ("self"_a, "A", "B", "d")))
151 .def_readonly("A", &LinearFunction::A_)
152 .def_readonly("B", &LinearFunction::B_)
153 .def_readonly("d", &LinearFunction::d_)
154 .def(func_visitor);
155
156 bp::class_<ControlBoxFunctionTpl<Scalar>, bp::bases<StageFunction>>(
157 "ControlBoxFunction",
158 bp::init<const int, const VectorXs &, const VectorXs &>(
159 bp::args("self", "ndx", "umin", "umax")))
160 .def(bp::init<const int, const int, const Scalar, const Scalar>(
161 bp::args("self", "ndx", "nu", "umin", "umax")))
162 .def(func_visitor);
163}
164
165} // namespace python
166} // namespace aligator
DynamicsModelTpl< Scalar > DynamicsModel
Definition context.hpp:27
StageFunctionTpl< Scalar > StageFunction
Definition context.hpp:16
UnaryFunctionTpl< Scalar > UnaryFunction
Definition context.hpp:17
StageFunctionDataTpl< Scalar > StageFunctionData
Definition context.hpp:18
xyz::polymorphic< StageFunction > PolyFunction
bp::object make_getter_eigen_matrix(MatrixType C::*v)
Create a getter for Eigen::Matrix type objects which returns an Eigen::Ref.
void exposeUnaryFunctions()
Expose the UnaryFunction type and its member function overloads.
void exposeFunctions()
Expose stagewise function classes.
PolymorphicMultiBaseVisitor< StageFunction > func_visitor
Main package namespace.