aligator  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
expose-costs.cpp
Go to the documentation of this file.
1
5
9
10namespace aligator {
11namespace python {
12using context::ConstMatrixRef;
13using context::ConstVectorRef;
17using context::MatrixXs;
18using context::Scalar;
19using context::VectorXs;
21using PolyCost = xyz::polymorphic<CostAbstract>;
22
23struct CostDataWrapper : CostData, bp::wrapper<CostData> {
24 using CostData::CostData;
25};
26
27PolymorphicVisitor<PolyCost> poly_visitor;
28
30
31 bp::class_<ConstantCostTpl<Scalar>, bp::bases<CostAbstract>>(
32 "ConstantCost", "A constant cost term.",
33 bp::init<xyz::polymorphic<Manifold>, int, Scalar>(
34 bp::args("self", "space", "nu", "value")))
35 .def_readwrite("value", &ConstantCostTpl<Scalar>::value_)
37 .def(poly_visitor);
38
39 bp::class_<QuadraticCost, bp::bases<CostAbstract>>(
40 "QuadraticCost",
41 "Quadratic cost in both state and control - only for Euclidean spaces.",
42 bp::no_init)
43 .def(bp::init<ConstMatrixRef, ConstMatrixRef, ConstMatrixRef,
44 ConstVectorRef, ConstVectorRef>(
45 bp::args("self", "w_x", "w_u", "w_cross", "interp_x", "interp_u")))
46 .def(bp::init<ConstMatrixRef, ConstMatrixRef, ConstVectorRef,
47 ConstVectorRef>(
48 bp::args("self", "w_x", "w_u", "interp_x", "interp_u")))
49 .def(bp::init<ConstMatrixRef, ConstMatrixRef>(
50 "Constructor with just weights (no cross-term).",
51 bp::args("self", "w_x", "w_u")))
52 .def(bp::init<ConstMatrixRef, ConstMatrixRef, ConstMatrixRef>(
53 "Constructor with just weights (with cross-term).",
54 bp::args("self", "w_x", "w_u", "w_cross")))
55 .def_readwrite("w_x", &QuadraticCost::Wxx_, "Weights on the state.")
56 .def_readwrite("w_u", &QuadraticCost::Wuu_, "Weights on the control.")
57 .def_readwrite("interp_x", &QuadraticCost::interp_x)
58 .def_readwrite("interp_u", &QuadraticCost::interp_u)
59 .add_property("has_cross_term", &QuadraticCost::hasCrossTerm,
60 "Whether there is a cross term.")
61 .add_property("weights_cross", &QuadraticCost::getCrossWeights,
62 &QuadraticCost::setCrossWeight, "Cross term weight.")
64 .def(poly_visitor);
65
66 bp::class_<QuadraticCost::Data, bp::bases<CostData>>(
67 "QuadraticCostData", "Quadratic cost data.", bp::no_init);
68}
69
71void exposeComposites();
72
74void exposeContactMap();
77void exposeCostStack();
78
80 register_polymorphic_to_python<PolyCost>();
81 bp::class_<PyCostFunction, boost::noncopyable>(
82 "CostAbstract", "Base class for cost functions.", bp::no_init)
83 .def(bp::init<const xyz::polymorphic<Manifold> &, const int>(
84 bp::args("self", "space", "nu")))
85 .def("evaluate", bp::pure_virtual(&CostAbstract::evaluate),
86 bp::args("self", "x", "u", "data"), "Evaluate the cost function.")
87 .def("computeGradients",
88 bp::pure_virtual(&CostAbstract::computeGradients),
89 bp::args("self", "x", "u", "data"),
90 "Compute the cost function gradients.")
91 .def("computeHessians", bp::pure_virtual(&CostAbstract::computeHessians),
92 bp::args("self", "x", "u", "data"),
93 "Compute the cost function hessians.")
94 .def_readonly("space", &CostAbstract::space)
95 .add_property("nx", &CostAbstract::nx)
96 .add_property("ndx", &CostAbstract::ndx)
97 .add_property("nu", &CostAbstract::nu)
99 .def(poly_visitor);
100
101 bp::register_ptr_to_python<shared_ptr<CostData>>();
102 bp::class_<CostDataWrapper, boost::noncopyable>(
103 "CostData", "Cost function data struct.", bp::no_init)
104 .def(bp::init<const int, const int>(bp::args("self", "ndx", "nu")))
105 .def(bp::init<const CostAbstract &>(bp::args("self", "cost")))
106 .def_readwrite("value", &CostData::value_)
107 .def_readwrite("grad", &CostData::grad_)
108 .def_readwrite("hess", &CostData::hess_)
109 .add_property(
110 "Lx", bp::make_getter(&CostData::Lx_,
111 bp::return_value_policy<bp::return_by_value>()))
112 .add_property(
113 "Lu", bp::make_getter(&CostData::Lu_,
114 bp::return_value_policy<bp::return_by_value>()))
115 .add_property("Lxx", bp::make_getter(
116 &CostData::Lxx_,
117 bp::return_value_policy<bp::return_by_value>()))
118 .add_property("Lxu", bp::make_getter(
119 &CostData::Lxu_,
120 bp::return_value_policy<bp::return_by_value>()))
121 .add_property("Lux", bp::make_getter(
122 &CostData::Lux_,
123 bp::return_value_policy<bp::return_by_value>()))
124 .add_property("Luu", bp::make_getter(
125 &CostData::Luu_,
126 bp::return_value_policy<bp::return_by_value>()));
127
128 StdVectorPythonVisitor<std::vector<PolyCost>, true>::expose(
129 "StdVec_CostAbstract",
130 eigenpy::details::overload_base_get_item_for_std_vector<
131 std::vector<PolyCost>>{});
132 StdVectorPythonVisitor<std::vector<shared_ptr<CostData>>, true>::expose(
133 "StdVec_CostData");
134}
135
136void exposeCostOps();
137
147
148} // namespace python
149} // namespace aligator
Define cost functions.
CostAbstractTpl< Scalar > CostAbstract
Definition context.hpp:25
ManifoldAbstractTpl< Scalar > Manifold
Definition context.hpp:14
CostDataAbstractTpl< Scalar > CostData
Definition context.hpp:26
PolymorphicVisitor< PolyCost > poly_visitor
void exposeContactMap()
Centroidal cost functions.
void exposeComposites()
Composite cost functions.
void exposeCosts()
Expose cost functions.
xyz::polymorphic< CostAbstract > PolyCost
void exposeCostStack()
fwd-declare exposeCostStack()
Main package namespace.
Euclidean quadratic cost.
VectorXs interp_x
Affine term in .
VectorXs interp_u
Affine term in .
ConstMatrixRef getCrossWeights() const
bool hasCrossTerm() const
Whether a cross term exists.
void setCrossWeight(const ConstMatrixRef &w)