aligator
0.19.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Toggle main menu visibility
Loading...
Searching...
No Matches
expose-cost-stack.cpp
Go to the documentation of this file.
1
#include "
aligator/python/fwd.hpp
"
2
#include "
aligator/python/visitors.hpp
"
3
4
#include "
aligator/overloads.hpp
"
5
#include "
aligator/modelling/costs/sum-of-costs.hpp
"
6
7
#include <eigenpy/std-pair.hpp>
8
#include <eigenpy/variant.hpp>
9
#include <eigenpy/std-map.hpp>
10
11
namespace
aligator
{
12
namespace
python
{
13
using
context::CostAbstract
;
14
using
context::CostData
;
15
using
context::Manifold
;
16
using
context::Scalar
;
17
using
CostStack
=
CostStackTpl<Scalar>
;
18
using
CostKey
=
CostStack::CostKey
;
19
20
void
key_err_python_manager
(
const
CostKey
&key) {
21
char
msg[100];
22
std::visit(
23
overloads
{
24
[&](
size_t
k) { snprintf(msg, 100ul,
"Key %zu not found."
, k); },
25
[&](
const
std::string &k) {
26
snprintf(msg, 100ul,
"Key %s not found."
, k.data());
27
},
28
},
29
key);
30
PyErr_SetString(PyExc_KeyError, msg);
31
bp::throw_error_already_set();
32
};
33
34
void
exposeCostStack
() {
35
using
CostStackData =
CostStackDataTpl<Scalar>
;
36
using
PolyCost
=
CostStack::PolyCost
;
37
using
PolyManifold
= xyz::polymorphic<Manifold>;
38
using
CostItem =
CostStack::CostItem
;
39
using
CostMap =
CostStack::CostMap
;
40
eigenpy::StdPairConverter<CostItem>::registration();
41
eigenpy::VariantConverter<CostKey>::registration();
42
eigenpy::GenericMapVisitor<CostMap, true>::expose(
"CostMap"
);
43
44
{
45
bp::scope scope =
46
bp::class_<CostStack, bp::bases<CostAbstract>>(
47
"CostStack"
,
"A weighted sum of other cost functions."
, bp::no_init)
48
.def(
49
bp::init<
PolyManifold
,
const
int
,
const
std::vector<PolyCost> &,
50
const
std::vector<Scalar> &>(
51
(
"self"
_a,
"space"
,
"nu"
,
"components"
_a = bp::list(),
52
"weights"
_a = bp::list())))
53
.def(bp::init<const PolyCost &>((
"self"
_a,
"cost"
)))
54
.def(bp::init<PolyManifold, int, const CostMap &>(
55
(
"self"
_a,
"components"
),
56
"Construct the CostStack from a CostMap object."
))
57
.def_readonly(
"components"
, &
CostStack::components_
,
58
"Components of this cost stack."
)
59
.def(
60
"getComponent"
,
61
+[](
CostStack
&self,
const
CostKey
&key) ->
PolyCost
& {
62
if
(self.
components_
.find(key) == self.
components_
.end()) {
63
key_err_python_manager
(key);
64
}
65
CostItem &c = self.
components_
.at(key);
66
return
c.first;
67
},
68
(
"self"
_a,
"key"
), bp::return_internal_reference<>())
69
.def(
70
"getWeight"
,
71
+[](
CostStack
&self,
const
CostKey
&key) {
72
return
self.
getWeight
(key);
73
},
74
(
"self"
_a,
"key"
))
75
.def(
"setWeight"
, &
CostStack::setWeight
, (
"self"
_a,
"key"
,
"value"
),
76
bp::return_internal_reference<>())
77
.def(
78
"addCost"
,
79
+[](
CostStack
&self,
const
PolyCost
&cost,
const
Scalar
weight)
80
->
PolyCost
& {
return
self.
addCost
(cost, weight).first; },
81
bp::return_internal_reference<>(),
82
(
"self"
_a,
"cost"
,
"weight"
_a = 1.))
83
.def(
84
"addCost"
,
85
+[](
CostStack
&self,
CostKey
key,
const
PolyCost
&cost,
86
const
Scalar
weight) ->
PolyCost
& {
87
return
self.
addCost
(key, cost, weight).first;
88
},
89
bp::return_internal_reference<>(),
90
(
"self"
_a,
"key"
,
"cost"
,
"weight"
_a = 1.))
91
.add_property(
"size"
, &
CostStack::size
,
92
"Number of cost components."
)
93
.def(
CopyableVisitor<CostStack>
())
94
.def(
PolymorphicMultiBaseVisitor<CostAbstract>
());
95
}
96
97
{
98
bp::register_ptr_to_python<shared_ptr<CostStackData>>();
99
bp::scope scope =
100
bp::class_<CostStackData, bp::bases<CostData>>(
101
"CostStackData"
,
"Data struct for CostStack."
, bp::no_init)
102
.def_readonly(
"sub_cost_data"
, &CostStackData::sub_cost_data);
103
eigenpy::GenericMapVisitor<CostStackData::DataMap, true>::expose(
"CostMap"
);
104
}
105
}
106
107
}
// namespace python
108
}
// namespace aligator
fwd.hpp
aligator::context::CostAbstract
CostAbstractTpl< Scalar > CostAbstract
Definition
context.hpp:25
aligator::context::Manifold
ManifoldAbstractTpl< Scalar > Manifold
Definition
context.hpp:14
aligator::context::Scalar
double Scalar
Definition
context.hpp:9
aligator::context::CostData
CostDataAbstractTpl< Scalar > CostData
Definition
context.hpp:26
aligator::python
The Python bindings.
Definition
blk-matrix.hpp:7
aligator::python::PolyManifold
xyz::polymorphic< Manifold > PolyManifold
Definition
expose-manifold.cpp:19
aligator::python::key_err_python_manager
void key_err_python_manager(const CostKey &key)
Definition
expose-cost-stack.cpp:20
aligator::python::PolyCost
xyz::polymorphic< CostAbstract > PolyCost
Definition
expose-costs.cpp:21
aligator::python::CostKey
CostStack::CostKey CostKey
Definition
expose-cost-stack.cpp:18
aligator::python::exposeCostStack
void exposeCostStack()
fwd-declare exposeCostStack()
Definition
expose-cost-stack.cpp:34
aligator::python::CostStack
CostStackTpl< Scalar > CostStack
Definition
expose-cost-stack.cpp:17
aligator
Main package namespace.
Definition
action-model-wrap.hpp:14
overloads.hpp
aligator::CostStackDataTpl
Definition
sum-of-costs.hpp:163
aligator::CostStackTpl
Weighted sum of multiple cost components.
Definition
sum-of-costs.hpp:19
aligator::CostStackTpl::addCost
CostItem & addCost(const PolyCost &cost, const Scalar weight=1.)
Definition
sum-of-costs.hpp:63
aligator::CostStackTpl< Scalar >::CostKey
std::variant< std::size_t, std::string > CostKey
Definition
sum-of-costs.hpp:28
aligator::CostStackTpl< Scalar >::PolyCost
xyz::polymorphic< CostBase > PolyCost
Definition
sum-of-costs.hpp:24
aligator::CostStackTpl< Scalar >::CostMap
boost::unordered::unordered_map< CostKey, CostItem > CostMap
Definition
sum-of-costs.hpp:29
aligator::CostStackTpl::getWeight
Scalar getWeight(const CostKey &key) const
Get weight for a given component.
Definition
sum-of-costs.hpp:87
aligator::CostStackTpl< Scalar >::components_
CostMap components_
Definition
sum-of-costs.hpp:32
aligator::CostStackTpl< Scalar >::setWeight
CostStackTpl & setWeight(const CostKey &key, const Scalar value)
Definition
sum-of-costs.hpp:101
aligator::CostStackTpl< Scalar >::CostItem
std::pair< PolyCost, Scalar > CostItem
Definition
sum-of-costs.hpp:27
aligator::CostStackTpl< Scalar >::size
std::size_t size() const
Definition
sum-of-costs.hpp:71
aligator::overloads
Utility helper struct for creating visitors from lambdas.
Definition
overloads.hpp:6
aligator::python::CopyableVisitor
Definition
visitors.hpp:38
aligator::python::PolymorphicMultiBaseVisitor
Definition
polymorphic.hpp:107
sum-of-costs.hpp
visitors.hpp
bindings
python
src
modelling
expose-cost-stack.cpp
Generated by
1.17.0