| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2021-2025, University of Edinburgh, Heriot-Watt University | ||
| 5 | // Copyright note valid unless otherwise stated in individual files. | ||
| 6 | // All rights reserved. | ||
| 7 | /////////////////////////////////////////////////////////////////////////////// | ||
| 8 | |||
| 9 | // Auto-generated file for float | ||
| 10 | #include "crocoddyl/core/costs/residual.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/core/core.hpp" | ||
| 13 | |||
| 14 | namespace crocoddyl { | ||
| 15 | namespace python { | ||
| 16 | |||
| 17 | template <typename Model> | ||
| 18 | struct CostModelResidualVisitor | ||
| 19 | : public bp::def_visitor<CostModelResidualVisitor<Model>> { | ||
| 20 | typedef typename Model::CostDataAbstract Data; | ||
| 21 | typedef typename Model::Base ModelBase; | ||
| 22 | typedef typename Model::StateAbstract State; | ||
| 23 | typedef typename Model::ResidualModelAbstract ResidualModel; | ||
| 24 | typedef typename Model::VectorXs VectorXs; | ||
| 25 | template <class PyClass> | ||
| 26 | ✗ | void visit(PyClass& cl) const { | |
| 27 | ✗ | cl.def(bp::init<std::shared_ptr<State>, std::shared_ptr<ResidualModel>>( | |
| 28 | bp::args("self", "state", "residual"), | ||
| 29 | "Initialize the residual cost model.\n\n" | ||
| 30 | ":param state: state description\n" | ||
| 31 | ":param residual: residual model")) | ||
| 32 | ✗ | .def( | |
| 33 | "calc", | ||
| 34 | static_cast<void (Model::*)( | ||
| 35 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 36 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
| 37 | bp::args("self", "data", "x", "u"), | ||
| 38 | "Compute the residual cost.\n\n" | ||
| 39 | ":param data: cost residual data\n" | ||
| 40 | ":param x: state point (dim. state.nx)\n" | ||
| 41 | ":param u: control input (dim. nu)") | ||
| 42 | ✗ | .def("calc", | |
| 43 | static_cast<void (Model::*)(const std::shared_ptr<Data>&, | ||
| 44 | const Eigen::Ref<const VectorXs>&)>( | ||
| 45 | &Model::calc), | ||
| 46 | bp::args("self", "data", "x"), | ||
| 47 | "Compute the residual cost based on state only.\n\n" | ||
| 48 | "It updates the total cost based on the state only. This function " | ||
| 49 | "is used in the terminal nodes of an optimal control problem.\n" | ||
| 50 | ":param data: cost data\n" | ||
| 51 | ":param x: state point (dim. state.nx)") | ||
| 52 | ✗ | .def( | |
| 53 | "calcDiff", | ||
| 54 | static_cast<void (Model::*)( | ||
| 55 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 56 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
| 57 | bp::args("self", "data", "x", "u"), | ||
| 58 | "Compute the derivatives of the residual cost.\n\n" | ||
| 59 | "It assumes that calc has been run first.\n" | ||
| 60 | ":param data: cost residual data\n" | ||
| 61 | ":param x: state point (dim. state.nx)\n" | ||
| 62 | ":param u: control input (dim. nu)") | ||
| 63 | ✗ | .def("calcDiff", | |
| 64 | static_cast<void (Model::*)(const std::shared_ptr<Data>&, | ||
| 65 | const Eigen::Ref<const VectorXs>&)>( | ||
| 66 | &Model::calcDiff), | ||
| 67 | bp::args("self", "data", "x"), | ||
| 68 | "Compute the derivatives of the residual cost with respect to the " | ||
| 69 | "state only.\n\n" | ||
| 70 | "It updates the Jacobian and Hessian of the cost function based " | ||
| 71 | "on the state only. This function is used in the terminal nodes " | ||
| 72 | "of an optimal control problem.\n" | ||
| 73 | ":param data: cost residual data\n" | ||
| 74 | ":param x: state point (dim. state.nx)") | ||
| 75 | ✗ | .def("createData", &Model::createData, | |
| 76 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
| 77 | bp::args("self", "data"), | ||
| 78 | "Create the residual cost data.\n\n" | ||
| 79 | "Each cost model has its own data that needs to be allocated. " | ||
| 80 | "This function returns the allocated data for a predefined cost.\n" | ||
| 81 | ":param data: shared data\n" | ||
| 82 | ":return cost data."); | ||
| 83 | ✗ | } | |
| 84 | }; | ||
| 85 | |||
| 86 | #define CROCODDYL_COST_MODEL_RESIDUAL_PYTHON_BINDINGS(Scalar) \ | ||
| 87 | typedef CostModelResidualTpl<Scalar> Model; \ | ||
| 88 | typedef CostModelAbstractTpl<Scalar> ModelBase; \ | ||
| 89 | typedef typename ModelBase::StateAbstract State; \ | ||
| 90 | typedef typename ModelBase::ActivationModelAbstract ActivationModel; \ | ||
| 91 | typedef typename ModelBase::ResidualModelAbstract ResidualModel; \ | ||
| 92 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 93 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 94 | "CostModelResidual", \ | ||
| 95 | "This cost function uses a residual vector with a Gauss-Newton " \ | ||
| 96 | "assumption to define a cost term.", \ | ||
| 97 | bp::init<std::shared_ptr<State>, std::shared_ptr<ActivationModel>, \ | ||
| 98 | std::shared_ptr<ResidualModel>>( \ | ||
| 99 | bp::args("self", "state", "activation", "residual"), \ | ||
| 100 | "Initialize the residual cost model.\n\n" \ | ||
| 101 | ":param state: state description\n" \ | ||
| 102 | ":param activation: activation model\n" \ | ||
| 103 | ":param residual: residual model")) \ | ||
| 104 | .def(CostModelResidualVisitor<Model>()) \ | ||
| 105 | .def(CastVisitor<Model>()) \ | ||
| 106 | .def(PrintableVisitor<Model>()) \ | ||
| 107 | .def(CopyableVisitor<Model>()); | ||
| 108 | |||
| 109 | #define CROCODDYL_COST_DATA_RESIDUAL_PYTHON_BINDINGS(Scalar) \ | ||
| 110 | typedef CostDataResidualTpl<Scalar> Data; \ | ||
| 111 | typedef CostDataAbstractTpl<Scalar> DataBase; \ | ||
| 112 | typedef CostModelResidualTpl<Scalar> Model; \ | ||
| 113 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
| 114 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 115 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 116 | "CostDataResidual", "Data for residual cost.\n\n", \ | ||
| 117 | bp::init<Model*, DataCollector*>( \ | ||
| 118 | bp::args("self", "model", "data"), \ | ||
| 119 | "Create residual cost data.\n\n" \ | ||
| 120 | ":param model: residual cost model\n" \ | ||
| 121 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
| 122 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 123 | .def(CopyableVisitor<Data>()); | ||
| 124 | |||
| 125 | ✗ | void exposeCostResidual() { | |
| 126 | ✗ | CROCODDYL_COST_MODEL_RESIDUAL_PYTHON_BINDINGS(float) | |
| 127 | ✗ | CROCODDYL_COST_DATA_RESIDUAL_PYTHON_BINDINGS(float) | |
| 128 | ✗ | } | |
| 129 | |||
| 130 | } // namespace python | ||
| 131 | } // namespace crocoddyl | ||
| 132 |