| 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 double | ||
| 10 | #include "python/crocoddyl/core/residual-base.hpp" | ||
| 11 | |||
| 12 | namespace crocoddyl { | ||
| 13 | namespace python { | ||
| 14 | |||
| 15 | template <typename Model> | ||
| 16 | struct ResidualModelAbstractVisitor | ||
| 17 | : public bp::def_visitor<ResidualModelAbstractVisitor<Model>> { | ||
| 18 | typedef typename Model::ResidualModel ResidualModel; | ||
| 19 | typedef typename Model::ResidualData ResidualData; | ||
| 20 | typedef typename Model::State State; | ||
| 21 | typedef typename Model::VectorXs VectorXs; | ||
| 22 | template <class PyClass> | ||
| 23 | ✗ | void visit(PyClass& cl) const { | |
| 24 | ✗ | cl.def(bp::init<std::shared_ptr<State>, std::size_t, | |
| 25 | bp::optional<bool, bool, bool>>( | ||
| 26 | bp::args("self", "state", "nr", "q_dependent", "v_dependent", | ||
| 27 | "u_dependent"), | ||
| 28 | "Initialize the cost model.\n\n" | ||
| 29 | ":param state: state description\n" | ||
| 30 | ":param nr: dimension of the residual vector\n" | ||
| 31 | ":param q_dependent: define if the residual function depends on " | ||
| 32 | "q (default true)\n" | ||
| 33 | ":param v_dependent: define if the residual function depends on " | ||
| 34 | "v (default true)\n" | ||
| 35 | ":param u_dependent: define if the residual function depends on " | ||
| 36 | "u (default true)")) | ||
| 37 | ✗ | .def("calc", pure_virtual(&Model::calc), | |
| 38 | bp::args("self", "data", "x", "u"), | ||
| 39 | "Compute the residual vector.\n\n" | ||
| 40 | ":param data: residual data\n" | ||
| 41 | ":param x: state point (dim. state.nx)\n" | ||
| 42 | ":param u: control input (dim. nu)") | ||
| 43 | ✗ | .def("calc", | |
| 44 | static_cast<void (ResidualModel::*)( | ||
| 45 | const std::shared_ptr<ResidualData>&, | ||
| 46 | const Eigen::Ref<const VectorXs>&)>(&ResidualModel::calc), | ||
| 47 | bp::args("self", "data", "x"), | ||
| 48 | "Compute the residual vector for nodes that depends only on the " | ||
| 49 | "state.\n\n" | ||
| 50 | "This function is used in the terminal nodes of an optimal " | ||
| 51 | "control problem. This is the reason why it updates the residual " | ||
| 52 | "vector based on the state only.\n" | ||
| 53 | ":param data: residual data\n" | ||
| 54 | ":param x: state point (dim. state.nx)") | ||
| 55 | ✗ | .def("calcDiff", pure_virtual(&Model::calcDiff), | |
| 56 | bp::args("self", "data", "x", "u"), | ||
| 57 | "Compute the Jacobians of the residual function.\n\n" | ||
| 58 | ":param data: residual data\n" | ||
| 59 | ":param x: state point (dim. state.nx)\n" | ||
| 60 | ":param u: control input (dim. nu)") | ||
| 61 | ✗ | .def("calcDiff", | |
| 62 | static_cast<void (ResidualModel::*)( | ||
| 63 | const std::shared_ptr<ResidualData>&, | ||
| 64 | const Eigen::Ref<const VectorXs>&)>(&ResidualModel::calcDiff), | ||
| 65 | bp::args("self", "data", "x"), | ||
| 66 | "Compute the Jacobian of the residual functions with respect to " | ||
| 67 | "the state only.\n\n" | ||
| 68 | "This function is used in the terminal nodes of an optimal " | ||
| 69 | "control problem. This is the reason why it updates the residual " | ||
| 70 | "vector based on the state only.\n" | ||
| 71 | ":param data: residual data\n" | ||
| 72 | ":param x: state point (dim. state.nx)") | ||
| 73 | ✗ | .def("createData", &Model::createData, &Model::default_createData, | |
| 74 | bp::args("self"), | ||
| 75 | "Create the residual data.\n\n" | ||
| 76 | "Each residual model might has its own data that needs to be " | ||
| 77 | "allocated.") | ||
| 78 | ✗ | .def("calcCostDiff", &Model::calcCostDiff, &Model::default_calcCostDiff, | |
| 79 | bp::args("self", "cdata", "rdata", "adata", "update_u"), | ||
| 80 | "Compute the derivative of the cost function.\n\n" | ||
| 81 | "This function assumes that the derivatives of the activation and " | ||
| 82 | "residual are computed via calcDiff functions.\n" | ||
| 83 | ":param cdata: cost data\n" | ||
| 84 | ":param rdata: residual data\n" | ||
| 85 | ":param adata: activation data\n" | ||
| 86 | ":param update_u: update the derivative of the cost function " | ||
| 87 | "w.r.t. to the control if True.") | ||
| 88 | ✗ | .def("calcCostDiff", &Model::default_calcCostDiff_noupdate_u) | |
| 89 | ✗ | .add_property( | |
| 90 | "state", | ||
| 91 | bp::make_function(&Model::get_state, | ||
| 92 | ✗ | bp::return_value_policy<bp::return_by_value>()), | |
| 93 | "state") | ||
| 94 | ✗ | .add_property("nr", bp::make_function(&Model::get_nr), | |
| 95 | "dimension of residual vector") | ||
| 96 | ✗ | .add_property("nu", bp::make_function(&Model::get_nu), | |
| 97 | "dimension of control vector") | ||
| 98 | ✗ | .add_property( | |
| 99 | "q_dependent", bp::make_function(&Model::get_q_dependent), | ||
| 100 | "flag that indicates if the residual function depends on q") | ||
| 101 | ✗ | .add_property( | |
| 102 | "v_dependent", bp::make_function(&Model::get_v_dependent), | ||
| 103 | "flag that indicates if the residual function depends on v") | ||
| 104 | ✗ | .add_property( | |
| 105 | "u_dependent", bp::make_function(&Model::get_u_dependent), | ||
| 106 | "flag that indicates if the residual function depends on u"); | ||
| 107 | ✗ | } | |
| 108 | }; | ||
| 109 | |||
| 110 | template <typename Data> | ||
| 111 | struct ResidualDataAbstractVisitor | ||
| 112 | : public bp::def_visitor<ResidualDataAbstractVisitor<Data>> { | ||
| 113 | template <class PyClass> | ||
| 114 | ✗ | void visit(PyClass& cl) const { | |
| 115 | ✗ | cl.add_property( | |
| 116 | "shared", | ||
| 117 | ✗ | bp::make_getter(&Data::shared, bp::return_internal_reference<>()), | |
| 118 | "shared data") | ||
| 119 | ✗ | .add_property( | |
| 120 | ✗ | "r", bp::make_getter(&Data::r, bp::return_internal_reference<>()), | |
| 121 | ✗ | bp::make_setter(&Data::r), "residual vector") | |
| 122 | ✗ | .add_property( | |
| 123 | ✗ | "Rx", bp::make_getter(&Data::Rx, bp::return_internal_reference<>()), | |
| 124 | ✗ | bp::make_setter(&Data::Rx), "Jacobian of the residual") | |
| 125 | ✗ | .add_property( | |
| 126 | ✗ | "Ru", bp::make_getter(&Data::Ru, bp::return_internal_reference<>()), | |
| 127 | ✗ | bp::make_setter(&Data::Ru), "Jacobian of the residual") | |
| 128 | ✗ | .add_property( | |
| 129 | "Arr_Rx", | ||
| 130 | ✗ | bp::make_getter(&Data::Arr_Rx, bp::return_internal_reference<>()), | |
| 131 | "Intermediate product of Arr (2nd deriv of Activation) with Rx " | ||
| 132 | "(deriv of residue)") | ||
| 133 | ✗ | .add_property( | |
| 134 | "Arr_Ru", | ||
| 135 | ✗ | bp::make_getter(&Data::Arr_Ru, bp::return_internal_reference<>()), | |
| 136 | "Intermediate product of Arr (2nd deriv of Activation) with Ru " | ||
| 137 | "(deriv of residue)"); | ||
| 138 | ✗ | } | |
| 139 | }; | ||
| 140 | |||
| 141 | #define CROCODDYL_RESIDUAL_MODEL_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
| 142 | typedef ResidualModelAbstractTpl<Scalar> Model; \ | ||
| 143 | typedef ResidualModelAbstractTpl_wrap<Scalar> Model_wrap; \ | ||
| 144 | typedef StateAbstractTpl<Scalar> State; \ | ||
| 145 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 146 | bp::class_<Model_wrap, boost::noncopyable>( \ | ||
| 147 | "ResidualModelAbstract", \ | ||
| 148 | "Abstract class for residual models.\n\n" \ | ||
| 149 | "A residual model defines a vector function r(x,u) in R^nr, where nr " \ | ||
| 150 | "describes its dimension in the the Euclidean space. For each residual " \ | ||
| 151 | "model, we need to provide ways of computing the residual vector and " \ | ||
| 152 | "its Jacobians. These computations are mainly carried out inside " \ | ||
| 153 | "calc() and calcDiff(), respectively.", \ | ||
| 154 | bp::init<std::shared_ptr<State>, std::size_t, std::size_t, \ | ||
| 155 | bp::optional<bool, bool, bool>>( \ | ||
| 156 | bp::args("self", "state", "nr", "nu", "q_dependent", "v_dependent", \ | ||
| 157 | "u_dependent"), \ | ||
| 158 | "Initialize the residual model.\n\n" \ | ||
| 159 | ":param state: state description,\n" \ | ||
| 160 | ":param nr: dimension of the residual vector\n" \ | ||
| 161 | ":param nu: dimension of control vector (default state.nv)\n" \ | ||
| 162 | ":param q_dependent: define if the residual function depends on q " \ | ||
| 163 | "(default true)\n" \ | ||
| 164 | ":param v_dependent: define if the residual function depends on v " \ | ||
| 165 | "(default true)\n" \ | ||
| 166 | ":param u_dependent: define if the residual function depends on u " \ | ||
| 167 | "(default true)")) \ | ||
| 168 | .def(ResidualModelAbstractVisitor<Model_wrap>()) \ | ||
| 169 | .def(PrintableVisitor<Model_wrap>()) \ | ||
| 170 | .def(CopyableVisitor<Model_wrap>()); | ||
| 171 | |||
| 172 | #define CROCODDYL_RESIDUAL_DATA_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
| 173 | typedef ResidualDataAbstractTpl<Scalar> Data; \ | ||
| 174 | typedef ResidualModelAbstractTpl<Scalar> Model; \ | ||
| 175 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
| 176 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 177 | bp::class_<Data>( \ | ||
| 178 | "ResidualDataAbstract", \ | ||
| 179 | "Abstract class for residual data.\n\n" \ | ||
| 180 | "In Crocoddyl, a residual data contains all the required information " \ | ||
| 181 | "for processing an user-defined residual models. The residual data " \ | ||
| 182 | "typically is allocated once and containts the residual vector and its " \ | ||
| 183 | "Jacobians.", \ | ||
| 184 | bp::init<Model*, DataCollector*>( \ | ||
| 185 | bp::args("self", "model", "data"), \ | ||
| 186 | "Create common data shared between residual models.\n\n" \ | ||
| 187 | ":param model: residual model\n" \ | ||
| 188 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
| 189 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 190 | .def(ResidualDataAbstractVisitor<Data>()) \ | ||
| 191 | .def(CopyableVisitor<Data>()); | ||
| 192 | |||
| 193 | ✗ | void exposeResidualAbstract() { | |
| 194 | ✗ | CROCODDYL_RESIDUAL_MODEL_ABSTRACT_PYTHON_BINDINGS(double) | |
| 195 | ✗ | CROCODDYL_RESIDUAL_DATA_ABSTRACT_PYTHON_BINDINGS(double) | |
| 196 | ✗ | } | |
| 197 | |||
| 198 | } // namespace python | ||
| 199 | } // namespace crocoddyl | ||
| 200 |