Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh, |
5 |
|
|
// Heriot-Watt University |
6 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
7 |
|
|
// All rights reserved. |
8 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
9 |
|
|
|
10 |
|
|
// Auto-generated file for double |
11 |
|
|
#include "crocoddyl/core/costs/cost-sum.hpp" |
12 |
|
|
|
13 |
|
|
#include "python/crocoddyl/core/action-base.hpp" |
14 |
|
|
#include "python/crocoddyl/core/core.hpp" |
15 |
|
|
#include "python/crocoddyl/core/diff-action-base.hpp" |
16 |
|
|
#include "python/crocoddyl/utils/deprecate.hpp" |
17 |
|
|
#include "python/crocoddyl/utils/map-converter.hpp" |
18 |
|
|
|
19 |
|
|
namespace crocoddyl { |
20 |
|
|
namespace python { |
21 |
|
|
|
22 |
|
|
template <typename Model> |
23 |
|
|
struct CostItemVisitor : public bp::def_visitor<CostItemVisitor<Model>> { |
24 |
|
|
template <class PyClass> |
25 |
|
✗ |
void visit(PyClass& cl) const { |
26 |
|
✗ |
cl.def_readwrite("name", &Model::name, "cost name") |
27 |
|
✗ |
.add_property( |
28 |
|
|
"cost", |
29 |
|
|
bp::make_getter(&Model::cost, |
30 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
31 |
|
|
"cost model") |
32 |
|
✗ |
.def_readwrite("weight", &Model::weight, "cost weight") |
33 |
|
✗ |
.def_readwrite("active", &Model::active, "cost status"); |
34 |
|
|
} |
35 |
|
|
}; |
36 |
|
|
|
37 |
|
|
template <typename Model> |
38 |
|
|
struct CostModelSumVisitor |
39 |
|
|
: public bp::def_visitor<CostModelSumVisitor<Model>> { |
40 |
|
✗ |
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(CostModelSum_addCost_wrap, |
41 |
|
|
Model::addCost, 3, 4) |
42 |
|
|
typedef typename Model::CostDataSum Data; |
43 |
|
|
typedef typename Model::StateAbstract State; |
44 |
|
|
typedef typename Model::VectorXs VectorXs; |
45 |
|
|
template <class PyClass> |
46 |
|
✗ |
void visit(PyClass& cl) const { |
47 |
|
✗ |
cl.def(bp::init<std::shared_ptr<State>, std::size_t>( |
48 |
|
|
bp::args("self", "state", "nu"), |
49 |
|
|
"Initialize the total cost model.\n\n" |
50 |
|
|
"For this case the default nu is equals to model.nv.\n" |
51 |
|
|
":param state: state description\n" |
52 |
|
|
":param nu: dimension of control vector")) |
53 |
|
✗ |
.def(bp::init<std::shared_ptr<State>>( |
54 |
|
|
bp::args("self", "state"), |
55 |
|
|
"Initialize the total cost model.\n\n" |
56 |
|
|
"For this case the default nu is equals to model.nv.\n" |
57 |
|
|
":param state: state description")) |
58 |
|
✗ |
.def("addCost", &Model::addCost, |
59 |
|
✗ |
CostModelSum_addCost_wrap( |
60 |
|
|
bp::args("self", "name", "cost", "weight", "active"), |
61 |
|
|
"Add a cost item.\n\n" |
62 |
|
|
":param name: cost name\n" |
63 |
|
|
":param cost: cost model\n" |
64 |
|
|
":param weight: cost weight\n" |
65 |
|
|
":param active: True if the cost is activated (default true)")) |
66 |
|
✗ |
.def("removeCost", &Model::removeCost, bp::args("self", "name"), |
67 |
|
|
"Remove a cost item.\n\n" |
68 |
|
|
":param name: cost name") |
69 |
|
✗ |
.def("changeCostStatus", &Model::changeCostStatus, |
70 |
|
|
bp::args("self", "name", "active"), |
71 |
|
|
"Change the cost status.\n\n" |
72 |
|
|
":param name: cost name\n" |
73 |
|
|
":param active: cost status (true for active and false for " |
74 |
|
|
"inactive)") |
75 |
|
✗ |
.def( |
76 |
|
|
"calc", |
77 |
|
|
static_cast<void (Model::*)( |
78 |
|
|
const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, |
79 |
|
|
const Eigen::Ref<const VectorXs>&)>(&Model::calc), |
80 |
|
|
bp::args("self", "data", "x", "u"), |
81 |
|
|
"Compute the total cost.\n\n" |
82 |
|
|
":param data: cost-sum data\n" |
83 |
|
|
":param x: state point (dim. state.nx)\n" |
84 |
|
|
":param u: control input (dim. nu)") |
85 |
|
✗ |
.def( |
86 |
|
|
"calc", |
87 |
|
|
static_cast<void (Model::*)(const std::shared_ptr<Data>&, |
88 |
|
|
const Eigen::Ref<const VectorXs>&)>( |
89 |
|
|
&Model::calc), |
90 |
|
|
bp::args("self", "data", "x"), |
91 |
|
|
"Compute the total cost value for nodes that depends only on the " |
92 |
|
|
"state.\n\n" |
93 |
|
|
"It updates the total cost based on the state only.\n" |
94 |
|
|
"This function is used in the terminal nodes of an optimal control " |
95 |
|
|
"problem.\n" |
96 |
|
|
":param data: cost-sum data\n" |
97 |
|
|
":param x: state point (dim. state.nx)") |
98 |
|
✗ |
.def( |
99 |
|
|
"calcDiff", |
100 |
|
|
static_cast<void (Model::*)( |
101 |
|
|
const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, |
102 |
|
|
const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), |
103 |
|
|
bp::args("self", "data", "x", "u"), |
104 |
|
|
"Compute the derivatives of the total cost.\n\n" |
105 |
|
|
"It assumes that calc has been run first.\n" |
106 |
|
|
":param data: action data\n" |
107 |
|
|
":param x: state point (dim. state.nx)\n" |
108 |
|
|
":param u: control input (dim. nu)") |
109 |
|
✗ |
.def( |
110 |
|
|
"calcDiff", |
111 |
|
|
static_cast<void (Model::*)(const std::shared_ptr<Data>&, |
112 |
|
|
const Eigen::Ref<const VectorXs>&)>( |
113 |
|
|
&Model::calcDiff), |
114 |
|
|
bp::args("self", "data", "x"), |
115 |
|
|
"Compute the Jacobian and Hessian of the total cost for nodes that " |
116 |
|
|
"depends on the state only.\n\n" |
117 |
|
|
"It updates the Jacobian and Hessian of the total cost based on " |
118 |
|
|
"the " |
119 |
|
|
"state only.\n" |
120 |
|
|
"This function is used in the terminal nodes of an optimal control " |
121 |
|
|
"problem.\n" |
122 |
|
|
":param data: cost-sum data\n" |
123 |
|
|
":param x: state point (dim. state.nx)") |
124 |
|
✗ |
.def("createData", &Model::createData, |
125 |
|
✗ |
bp::with_custodian_and_ward_postcall<0, 2>(), |
126 |
|
|
bp::args("self", "data"), |
127 |
|
|
"Create the total cost data.\n\n" |
128 |
|
|
":param data: shared data\n" |
129 |
|
|
":return total cost data.") |
130 |
|
✗ |
.add_property( |
131 |
|
|
"state", |
132 |
|
|
bp::make_function(&Model::get_state, |
133 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
134 |
|
|
"state description") |
135 |
|
✗ |
.add_property( |
136 |
|
|
"costs", |
137 |
|
|
bp::make_function(&Model::get_costs, |
138 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
139 |
|
|
"stack of costs") |
140 |
|
✗ |
.add_property("nu", bp::make_function(&Model::get_nu), |
141 |
|
|
"dimension of control vector") |
142 |
|
✗ |
.add_property("nr", bp::make_function(&Model::get_nr), |
143 |
|
|
"dimension of the residual vector of active cost") |
144 |
|
✗ |
.add_property("nr_total", bp::make_function(&Model::get_nr_total), |
145 |
|
|
"dimension of the total residual vector") |
146 |
|
✗ |
.add_property( |
147 |
|
|
"active", |
148 |
|
|
bp::make_function( |
149 |
|
|
&Model::get_active, |
150 |
|
✗ |
deprecated<bp::return_value_policy<bp::return_by_value>>( |
151 |
|
|
"Deprecated. Use property active_set")), |
152 |
|
|
"list of names of active contact items") |
153 |
|
✗ |
.add_property( |
154 |
|
|
"inactive", |
155 |
|
|
bp::make_function( |
156 |
|
|
&Model::get_inactive, |
157 |
|
✗ |
deprecated<bp::return_value_policy<bp::return_by_value>>( |
158 |
|
|
"Deprecated. Use property inactive_set")), |
159 |
|
|
"list of names of inactive contact items") |
160 |
|
✗ |
.add_property( |
161 |
|
|
"active_set", |
162 |
|
|
bp::make_function(&Model::get_active_set, |
163 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
164 |
|
|
"name of the active set of cost items") |
165 |
|
✗ |
.add_property( |
166 |
|
|
"inactive_set", |
167 |
|
|
bp::make_function(&Model::get_inactive_set, |
168 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
169 |
|
|
"name of the inactive set of cost items") |
170 |
|
✗ |
.def("getCostStatus", &Model::getCostStatus, bp::args("self", "name"), |
171 |
|
|
"Return the cost status of a given cost name.\n\n" |
172 |
|
|
":param name: cost name"); |
173 |
|
|
} |
174 |
|
|
}; |
175 |
|
|
|
176 |
|
|
template <typename Data> |
177 |
|
|
struct CostDataSumVisitor : public bp::def_visitor<CostDataSumVisitor<Data>> { |
178 |
|
|
typedef typename Data::Scalar Scalar; |
179 |
|
|
typedef DifferentialActionDataAbstractTpl<Scalar> DifferentialActionData; |
180 |
|
|
typedef ActionDataAbstractTpl<Scalar> ActionData; |
181 |
|
|
template <class PyClass> |
182 |
|
✗ |
void visit(PyClass& cl) const { |
183 |
|
✗ |
cl.def( |
184 |
|
|
"shareMemory", &Data::template shareMemory<DifferentialActionData>, |
185 |
|
|
bp::args("self", "model"), |
186 |
|
|
"Share memory with a given differential action data\n\n" |
187 |
|
|
":param model: differential action data that we want to share memory") |
188 |
|
✗ |
.def("shareMemory", &Data::template shareMemory<ActionData>, |
189 |
|
|
bp::args("self", "model"), |
190 |
|
|
"Share memory with a given action data\n\n" |
191 |
|
|
":param model: action data that we want to share memory") |
192 |
|
✗ |
.add_property( |
193 |
|
|
"costs", |
194 |
|
|
bp::make_getter(&Data::costs, |
195 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
196 |
|
|
"stack of costs data") |
197 |
|
✗ |
.add_property( |
198 |
|
|
"shared", |
199 |
|
✗ |
bp::make_getter(&Data::shared, bp::return_internal_reference<>()), |
200 |
|
|
"shared data") |
201 |
|
✗ |
.add_property( |
202 |
|
|
"cost", |
203 |
|
|
bp::make_getter(&Data::cost, |
204 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
205 |
|
|
bp::make_setter(&Data::cost), "cost value") |
206 |
|
✗ |
.add_property( |
207 |
|
|
"Lx", |
208 |
|
|
bp::make_function(&Data::get_Lx, |
209 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
210 |
|
|
bp::make_function(&Data::set_Lx), "Jacobian of the cost") |
211 |
|
✗ |
.add_property( |
212 |
|
|
"Lu", |
213 |
|
|
bp::make_function(&Data::get_Lu, |
214 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
215 |
|
|
bp::make_function(&Data::set_Lu), "Jacobian of the cost") |
216 |
|
✗ |
.add_property( |
217 |
|
|
"Lxx", |
218 |
|
|
bp::make_function(&Data::get_Lxx, |
219 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
220 |
|
|
bp::make_function(&Data::set_Lxx), "Hessian of the cost") |
221 |
|
✗ |
.add_property( |
222 |
|
|
"Lxu", |
223 |
|
|
bp::make_function(&Data::get_Lxu, |
224 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
225 |
|
|
bp::make_function(&Data::set_Lxu), "Hessian of the cost") |
226 |
|
✗ |
.add_property( |
227 |
|
|
"Luu", |
228 |
|
|
bp::make_function(&Data::get_Luu, |
229 |
|
✗ |
bp::return_value_policy<bp::return_by_value>()), |
230 |
|
|
bp::make_function(&Data::set_Luu), "Hessian of the cost"); |
231 |
|
|
} |
232 |
|
|
}; |
233 |
|
|
|
234 |
|
|
#define CROCODDYL_COST_ITEM_PYTHON_BINDINGS(Scalar) \ |
235 |
|
|
typedef CostItemTpl<Scalar> Item; \ |
236 |
|
|
typedef Item::CostModelAbstract CostModel; \ |
237 |
|
|
typedef std::shared_ptr<Item> CostItemPtr; \ |
238 |
|
|
StdMapPythonVisitor< \ |
239 |
|
|
std::string, CostItemPtr, std::less<std::string>, \ |
240 |
|
|
std::allocator<std::pair<const std::string, CostItemPtr>>, \ |
241 |
|
|
true>::expose("StdMap_CostItem"); \ |
242 |
|
|
typedef CostDataAbstractTpl<Scalar> CostData; \ |
243 |
|
|
typedef std::shared_ptr<CostData> CostDataPtr; \ |
244 |
|
|
StdMapPythonVisitor< \ |
245 |
|
|
std::string, CostDataPtr, std::less<std::string>, \ |
246 |
|
|
std::allocator<std::pair<const std::string, CostDataPtr>>, \ |
247 |
|
|
true>::expose("StdMap_CostData"); \ |
248 |
|
|
bp::register_ptr_to_python<std::shared_ptr<Item>>(); \ |
249 |
|
|
bp::class_<Item>( \ |
250 |
|
|
"CostItem", "Describe a cost item.\n\n", \ |
251 |
|
|
bp::init<std::string, std::shared_ptr<CostModel>, Scalar, \ |
252 |
|
|
bp::optional<bool>>( \ |
253 |
|
|
bp::args("self", "name", "cost", "weight", "active"), \ |
254 |
|
|
"Initialize the cost item.\n\n" \ |
255 |
|
|
":param name: cost name\n" \ |
256 |
|
|
":param cost: cost model\n" \ |
257 |
|
|
":param weight: cost weight\n" \ |
258 |
|
|
":param active: True if the cost is activated (default true)")) \ |
259 |
|
|
.def(CostItemVisitor<Item>()) \ |
260 |
|
|
.def(CastVisitor<Item>()) \ |
261 |
|
|
.def(PrintableVisitor<Item>()) \ |
262 |
|
|
.def(CopyableVisitor<Item>()); |
263 |
|
|
|
264 |
|
|
#define CROCODDYL_COST_MODEL_SUM_PYTHON_BINDINGS(Scalar) \ |
265 |
|
|
typedef CostModelSumTpl<Scalar> Model; \ |
266 |
|
|
typedef Model::StateAbstract State; \ |
267 |
|
|
bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ |
268 |
|
|
bp::class_<Model>("CostModelSum", \ |
269 |
|
|
bp::init<std::shared_ptr<State>, std::size_t>( \ |
270 |
|
|
bp::args("self", "state", "nu"), \ |
271 |
|
|
"Initialize the total cost model.\n\n" \ |
272 |
|
|
":param state: state description\n" \ |
273 |
|
|
":param nu: dimension of control vector")) \ |
274 |
|
|
.def(CostModelSumVisitor<Model>()) \ |
275 |
|
|
.def(CastVisitor<Model>()) \ |
276 |
|
|
.def(PrintableVisitor<Model>()) \ |
277 |
|
|
.def(CopyableVisitor<Model>()); |
278 |
|
|
|
279 |
|
|
#define CROCODDYL_COST_DATA_SUM_PYTHON_BINDINGS(Scalar) \ |
280 |
|
|
typedef CostDataSumTpl<Scalar> Data; \ |
281 |
|
|
typedef CostModelSumTpl<Scalar> Model; \ |
282 |
|
|
typedef Model::DataCollectorAbstract DataCollector; \ |
283 |
|
|
bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ |
284 |
|
|
bp::class_<Data>( \ |
285 |
|
|
"CostDataSum", "Class for total cost data.\n\n", \ |
286 |
|
|
bp::init<Model*, DataCollector*>( \ |
287 |
|
|
bp::args("self", "model", "data"), \ |
288 |
|
|
"Create total cost data.\n\n" \ |
289 |
|
|
":param model: total cost model\n" \ |
290 |
|
|
":param data: shared data")[bp::with_custodian_and_ward<1, 3>()]) \ |
291 |
|
|
.def(CostDataSumVisitor<Data>()) \ |
292 |
|
|
.def(CopyableVisitor<Data>()); |
293 |
|
|
|
294 |
|
✗ |
void exposeCostSum() { |
295 |
|
✗ |
CROCODDYL_COST_ITEM_PYTHON_BINDINGS(double) |
296 |
|
✗ |
CROCODDYL_COST_MODEL_SUM_PYTHON_BINDINGS(double) |
297 |
|
✗ |
CROCODDYL_COST_DATA_SUM_PYTHON_BINDINGS(double) |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
} // namespace python |
301 |
|
|
} // namespace crocoddyl |
302 |
|
|
|