| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/integrator/rk4.cpp |
| Date: | 2025-03-13 15:22:42 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 80 | 80 | 100.0% |
| Branches: | 57 | 114 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh, IRI: CSIC-UPC, | ||
| 5 | // University of Trento, Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | #include "crocoddyl/core/integrator/rk4.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/core/core.hpp" | ||
| 13 | #include "python/crocoddyl/core/integ-action-base.hpp" | ||
| 14 | #include "python/crocoddyl/utils/copyable.hpp" | ||
| 15 | |||
| 16 | #pragma GCC diagnostic push | ||
| 17 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
| 18 | |||
| 19 | namespace crocoddyl { | ||
| 20 | namespace python { | ||
| 21 | |||
| 22 | 10 | void exposeIntegratedActionRK4() { | |
| 23 | 10 | bp::register_ptr_to_python<std::shared_ptr<IntegratedActionModelRK4> >(); | |
| 24 | |||
| 25 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<IntegratedActionModelRK4, |
| 26 | bp::bases<IntegratedActionModelAbstract, ActionModelAbstract> >( | ||
| 27 | "IntegratedActionModelRK4", | ||
| 28 | "RK4 integrator for differential action models.\n\n" | ||
| 29 | "This class implements an RK4 integrator\n" | ||
| 30 | "given a differential action model, i.e.:\n" | ||
| 31 | " [q+, v+] = State.integrate([q, v], dt / 6 (k0 + k1 + k2 + k3)) with\n" | ||
| 32 | "k0 = f(x, u)\n" | ||
| 33 | "k1 = f(x + dt / 2 * k0, u)\n" | ||
| 34 | "k2 = f(x + dt / 2 * k1, u)\n" | ||
| 35 | "k3 = f(x + dt * k2, u)", | ||
| 36 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<std::shared_ptr<DifferentialActionModelAbstract>, |
| 37 | bp::optional<double, bool> >( | ||
| 38 | 20 | bp::args("self", "diffModel", "stepTime", "withCostResidual"), | |
| 39 | "Initialize the RK4 integrator.\n\n" | ||
| 40 | ":param diffModel: differential action model\n" | ||
| 41 | ":param stepTime: step time (default 1e-3)\n" | ||
| 42 | ":param withCostResidual: includes the cost residuals and " | ||
| 43 | "derivatives (default True).")) | ||
| 44 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | .def(bp::init<std::shared_ptr<DifferentialActionModelAbstract>, |
| 45 | std::shared_ptr<ControlParametrizationModelAbstract>, | ||
| 46 | bp::optional<double, bool> >( | ||
| 47 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "diffModel", "control", "stepTime", |
| 48 | "withCostResidual"), | ||
| 49 | "Initialize the RK4 integrator.\n\n" | ||
| 50 | ":param diffModel: differential action model\n" | ||
| 51 | ":param control: the control parametrization\n" | ||
| 52 | ":param stepTime: step time (default 1e-3)\n" | ||
| 53 | ":param withCostResidual: includes the cost residuals and " | ||
| 54 | "derivatives (default True).")) | ||
| 55 | .def<void (IntegratedActionModelRK4::*)( | ||
| 56 | const std::shared_ptr<ActionDataAbstract>&, | ||
| 57 | const Eigen::Ref<const Eigen::VectorXd>&, | ||
| 58 | 20 | const Eigen::Ref<const Eigen::VectorXd>&)>( | |
| 59 | "calc", &IntegratedActionModelRK4::calc, | ||
| 60 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x", "u"), |
| 61 | "Compute the time-discrete evolution of a differential action " | ||
| 62 | "model.\n\n" | ||
| 63 | "It describes the time-discrete evolution of action model.\n" | ||
| 64 | ":param data: action data\n" | ||
| 65 | ":param x: state point (dim. state.nx)\n" | ||
| 66 | ":param u: control input (dim. nu)") | ||
| 67 | .def<void (IntegratedActionModelRK4::*)( | ||
| 68 | const std::shared_ptr<ActionDataAbstract>&, | ||
| 69 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const Eigen::Ref<const Eigen::VectorXd>&)>( |
| 70 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | "calc", &ActionModelAbstract::calc, bp::args("self", "data", "x")) |
| 71 | .def<void (IntegratedActionModelRK4::*)( | ||
| 72 | const std::shared_ptr<ActionDataAbstract>&, | ||
| 73 | const Eigen::Ref<const Eigen::VectorXd>&, | ||
| 74 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const Eigen::Ref<const Eigen::VectorXd>&)>( |
| 75 | "calcDiff", &IntegratedActionModelRK4::calcDiff, | ||
| 76 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x", "u"), |
| 77 | "Computes the derivatives of the integrated action model wrt state " | ||
| 78 | "and control. \n\n" | ||
| 79 | "This function builds a quadratic approximation of the\n" | ||
| 80 | "action model (i.e. dynamical system and cost function).\n" | ||
| 81 | "It assumes that calc has been run first.\n" | ||
| 82 | ":param data: action data\n" | ||
| 83 | ":param x: state point (dim. state.nx)\n" | ||
| 84 | ":param u: control input (dim. nu)") | ||
| 85 | .def<void (IntegratedActionModelRK4::*)( | ||
| 86 | const std::shared_ptr<ActionDataAbstract>&, | ||
| 87 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const Eigen::Ref<const Eigen::VectorXd>&)>( |
| 88 | "calcDiff", &ActionModelAbstract::calcDiff, | ||
| 89 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x")) |
| 90 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | .def("createData", &IntegratedActionModelRK4::createData, |
| 91 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self"), "Create the RK4 integrator data.") |
| 92 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<IntegratedActionModelRK4>()); |
| 93 | |||
| 94 | 10 | bp::register_ptr_to_python<std::shared_ptr<IntegratedActionDataRK4> >(); | |
| 95 | |||
| 96 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<IntegratedActionDataRK4, bp::bases<IntegratedActionDataAbstract> >( |
| 97 | "IntegratedActionDataRK4", "RK4 integrator data.", | ||
| 98 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
20 | bp::init<IntegratedActionModelRK4*>(bp::args("self", "model"), |
| 99 | "Create RK4 integrator data.\n\n" | ||
| 100 | ":param model: RK4 integrator model")) | ||
| 101 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 102 | "differential", | ||
| 103 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::differential, |
| 104 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 105 | "list of differential action data") | ||
| 106 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 107 | "control", | ||
| 108 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::control, |
| 109 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 110 | "list of control parametrization data") | ||
| 111 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 112 | "integral", | ||
| 113 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::integral, |
| 114 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 115 | "list of RK4 terms related to the cost") | ||
| 116 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dx", |
| 117 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dx, |
| 118 | 10 | bp::return_internal_reference<>()), | |
| 119 | "state rate.") | ||
| 120 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ki", |
| 121 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ki, |
| 122 | 10 | bp::return_internal_reference<>()), | |
| 123 | "list of RK4 terms related to system dynamics") | ||
| 124 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 125 | "y", | ||
| 126 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::y, |
| 127 | 10 | bp::return_internal_reference<>()), | |
| 128 | "list of states where f is evaluated in the RK4 integration") | ||
| 129 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ws", |
| 130 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ws, |
| 131 | 10 | bp::return_internal_reference<>()), | |
| 132 | "control inputs evaluated in the RK4 integration") | ||
| 133 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dki_dx", |
| 134 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dki_dx, |
| 135 | 10 | bp::return_internal_reference<>()), | |
| 136 | "list of partial derivatives of RK4 nodes with respect to " | ||
| 137 | "the state of the RK4 " | ||
| 138 | "integration. dki/dx") | ||
| 139 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dki_du", |
| 140 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dki_du, |
| 141 | 10 | bp::return_internal_reference<>()), | |
| 142 | "list of partial derivatives of RK4 nodes with respect to " | ||
| 143 | "the control parameters of the RK4 " | ||
| 144 | "integration. dki/du") | ||
| 145 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dyi_dx", |
| 146 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dyi_dx, |
| 147 | 10 | bp::return_internal_reference<>()), | |
| 148 | "list of partial derivatives of RK4 dynamics with respect " | ||
| 149 | "to the state of the RK4 integrator. dyi/dx") | ||
| 150 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dyi_du", |
| 151 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dyi_du, |
| 152 | 10 | bp::return_internal_reference<>()), | |
| 153 | "list of partial derivatives of RK4 dynamics with respect " | ||
| 154 | "to the control parameters of the " | ||
| 155 | "RK4 integrator. dyi/du") | ||
| 156 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dli_dx", |
| 157 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dli_dx, |
| 158 | 10 | bp::return_internal_reference<>()), | |
| 159 | "list of partial derivatives of the cost with respect to " | ||
| 160 | "the state of the RK4 " | ||
| 161 | "integration. dli_dx") | ||
| 162 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dli_du", |
| 163 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::dli_du, |
| 164 | 10 | bp::return_internal_reference<>()), | |
| 165 | "list of partial derivatives of the cost with respect to " | ||
| 166 | "the control input of the RK4 " | ||
| 167 | "integration. dli_du") | ||
| 168 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ddli_ddx", |
| 169 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ddli_ddx, |
| 170 | 10 | bp::return_internal_reference<>()), | |
| 171 | "list of second partial derivatives of the cost with " | ||
| 172 | "respect to the state of the RK4 " | ||
| 173 | "integration. ddli_ddx") | ||
| 174 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ddli_ddw", |
| 175 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ddli_ddw, |
| 176 | 10 | bp::return_internal_reference<>()), | |
| 177 | "list of second partial derivatives of the cost with " | ||
| 178 | "respect to the control parameters of " | ||
| 179 | "the RK4 integration. ddli_ddw") | ||
| 180 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ddli_ddu", |
| 181 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ddli_ddu, |
| 182 | 10 | bp::return_internal_reference<>()), | |
| 183 | "list of second partial derivatives of the cost with " | ||
| 184 | "respect to the control input of the RK4 " | ||
| 185 | "integration. ddli_ddu") | ||
| 186 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ddli_dxdw", |
| 187 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ddli_dxdw, |
| 188 | 10 | bp::return_internal_reference<>()), | |
| 189 | "list of second partial derivatives of the cost with " | ||
| 190 | "respect to the state and control parameters of " | ||
| 191 | "the RK4 integration. ddli_dxdw") | ||
| 192 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ddli_dxdu", |
| 193 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ddli_dxdu, |
| 194 | 10 | bp::return_internal_reference<>()), | |
| 195 | "list of second partial derivatives of the cost with " | ||
| 196 | "respect to the state and control input " | ||
| 197 | "of the RK4 integration. ddli_dxdu") | ||
| 198 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("ddli_dwdu", |
| 199 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&IntegratedActionDataRK4::ddli_dwdu, |
| 200 | 10 | bp::return_internal_reference<>()), | |
| 201 | "list of second partial derivatives of the cost with " | ||
| 202 | "respect to the control parameters and " | ||
| 203 | "inputs control of the RK4 integration. ddli_dxdu") | ||
| 204 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<IntegratedActionDataRK4>()); |
| 205 | 10 | } | |
| 206 | |||
| 207 | } // namespace python | ||
| 208 | } // namespace crocoddyl | ||
| 209 | |||
| 210 | #pragma GCC diagnostic pop | ||
| 211 |