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