aligator  0.9.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
expose-cost-stack.cpp
Go to the documentation of this file.
3
5
6#include <eigenpy/std-pair.hpp>
7#include <eigenpy/variant.hpp>
8#if EIGENPY_VERSION_AT_LEAST(3, 9, 1)
9#define ALIGATOR_EIGENPY_HAS_MAP_SUPPORT 1
10#else
11#define ALIGATOR_EIGENPY_HAS_MAP_SUPPORT 0
12#endif
13
14#if ALIGATOR_EIGENPY_HAS_MAP_SUPPORT
15#include <eigenpy/std-map.hpp>
16#endif
17
18namespace aligator {
19namespace python {
23using context::Scalar;
24
26 using CostStack = CostStackTpl<Scalar>;
27 using CostStackData = CostStackDataTpl<Scalar>;
28 using CostKey = CostStack::CostKey;
29 using PolyCost = CostStack::PolyCost;
30 using PolyManifold = xyz::polymorphic<Manifold>;
31 using CostItem = CostStack::CostItem;
32 using CostMap = CostStack::CostMap;
33 eigenpy::StdPairConverter<CostItem>::registration();
34 eigenpy::VariantConverter<CostKey>::registration();
35
36 {
37 bp::scope scope =
38 bp::class_<CostStack, bp::bases<CostAbstract>>(
39 "CostStack", "A weighted sum of other cost functions.", bp::no_init)
40 .def(
41 bp::init<PolyManifold, const int, const std::vector<PolyCost> &,
42 const std::vector<Scalar> &>(
43 ("self"_a, "space", "nu", "components"_a = bp::list(),
44 "weights"_a = bp::list())))
45 .def(bp::init<const PolyCost &>(("self"_a, "cost")))
47 .def(bp::init<PolyManifold, int, const CostMap &>(
48 ("self"_a, "components"),
49 "Construct the CostStack from a CostMap object."))
50 .def_readonly("components", &CostStack::components_,
51 "Components of this cost stack.")
52#endif
53 .def(
54 "getComponent",
55 +[](CostStack &self, const CostKey &key) -> PolyCost & {
56 if (!self.components_.contains(key)) {
57 PyErr_SetString(PyExc_KeyError, "Key not found.");
58 bp::throw_error_already_set();
59 }
60 auto &c = self.components_.at(key);
61 return c.first;
62 },
63 ("self"_a, "key"), bp::return_internal_reference<>())
64 .def(
65 "addCost",
66 +[](CostStack &self, const PolyCost &cost, const Scalar weight)
67 -> PolyCost & { return self.addCost(cost, weight).first; },
68 bp::return_internal_reference<>(),
69 ("self"_a, "cost", "weight"_a = 1.))
70 .def(
71 "addCost",
72 +[](CostStack &self, CostKey key, const PolyCost &cost,
73 const Scalar weight) -> PolyCost & {
74 return self.addCost(key, cost, weight).first;
75 },
76 bp::return_internal_reference<>(),
77 ("self"_a, "key", "cost", "weight"_a = 1.))
78 .def("size", &CostStack::size, "Get the number of cost components.")
81#if ALIGATOR_EIGENPY_HAS_MAP_SUPPORT
82 eigenpy::GenericMapVisitor<CostMap, true>::expose("CostMap");
83#endif
84 }
85
86 {
87 bp::register_ptr_to_python<shared_ptr<CostStackData>>();
88 bp::scope scope =
89 bp::class_<CostStackData, bp::bases<CostData>>(
90 "CostStackData", "Data struct for CostStack.", bp::no_init)
91 .def_readonly("sub_cost_data", &CostStackData::sub_cost_data);
92#if ALIGATOR_EIGENPY_HAS_MAP_SUPPORT
93 eigenpy::GenericMapVisitor<CostStackData::DataMap, true>::expose("CostMap");
94#endif
95 }
96}
97
98} // namespace python
99} // namespace aligator
#define ALIGATOR_EIGENPY_HAS_MAP_SUPPORT
CostAbstractTpl< Scalar > CostAbstract
Definition context.hpp:25
ManifoldAbstractTpl< Scalar > Manifold
Definition context.hpp:14
CostDataAbstractTpl< Scalar > CostData
Definition context.hpp:26
xyz::polymorphic< CostAbstract > PolyCost
void exposeCostStack()
fwd-declare exposeCostStack()
xyz::polymorphic< Manifold > PolyManifold
Main package namespace.
Weighted sum of multiple cost components.