| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/controls/poly-two-rk.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 17 | 17 | 100.0% |
| Branches: | 46 | 92 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2021-2025, University of Edinburgh, University of Trento | ||
| 5 | // Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | #include "crocoddyl/core/controls/poly-two-rk.hpp" | ||
| 11 | |||
| 12 | #include "python/crocoddyl/core/control-base.hpp" | ||
| 13 | #include "python/crocoddyl/core/core.hpp" | ||
| 14 | #include "python/crocoddyl/utils/copyable.hpp" | ||
| 15 | |||
| 16 | namespace crocoddyl { | ||
| 17 | namespace python { | ||
| 18 | |||
| 19 | template <typename Model> | ||
| 20 | struct ControlParametrizationModelPolyTwoRKVisitor | ||
| 21 | : public bp::def_visitor< | ||
| 22 | ControlParametrizationModelPolyTwoRKVisitor<Model>> { | ||
| 23 | typedef typename Model::Scalar Scalar; | ||
| 24 | typedef typename Model::ControlParametrizationDataAbstract Data; | ||
| 25 | typedef typename Model::VectorXs VectorXs; | ||
| 26 | template <class PyClass> | ||
| 27 | 40 | void visit(PyClass& cl) const { | |
| 28 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
80 | cl.def("calc", |
| 29 | static_cast<void (Model::*)( | ||
| 30 | const std::shared_ptr<Data>&, const Scalar, | ||
| 31 | const Eigen::Ref<const VectorXs>&) const>(&Model::calc), | ||
| 32 | bp::args("self", "data", "t", "u"), | ||
| 33 | bp::args("self", "data", "t", "u"), | ||
| 34 | "Compute the control value.\n\n" | ||
| 35 | ":param data: poly-two-rk data\n" | ||
| 36 | ":param t: normalized time in [0, 1]\n" | ||
| 37 | ":param u: control parameters (dim control.nu)") | ||
| 38 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calcDiff", |
| 39 | static_cast<void (Model::*)( | ||
| 40 | const std::shared_ptr<Data>&, const Scalar, | ||
| 41 | const Eigen::Ref<const VectorXs>&) const>(&Model::calcDiff), | ||
| 42 | bp::args("self", "data", "t", "u"), | ||
| 43 | "Compute the Jacobian of the control value with respect to the " | ||
| 44 | "control parameters.\n\n" | ||
| 45 | "It assumes that calc has been run first.\n" | ||
| 46 | ":param data: poly-two-rk data\n" | ||
| 47 | ":param t: normalized time in [0, 1]\n" | ||
| 48 | ":param u: control parameters (dim control.nu)") | ||
| 49 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, bp::args("self"), |
| 50 | "Create the poly-two-rk data.") | ||
| 51 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .def("params", |
| 52 | static_cast<void (Model::*)( | ||
| 53 | const std::shared_ptr<Data>&, const Scalar, | ||
| 54 | const Eigen::Ref<const VectorXs>&) const>(&Model::params), | ||
| 55 | bp::args("self", "data", "t", "w"), | ||
| 56 | "Compute the control parameters.\n\n" | ||
| 57 | ":param data: poly-two-rk data\n" | ||
| 58 | ":param t: normalized time in [0, 1]\n" | ||
| 59 | ":param w: control value (dim control.nw)") | ||
| 60 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("convertBounds", &Model::convertBounds, |
| 61 | bp::args("self", "w_lb", "w_ub"), | ||
| 62 | "Convert the bounds on the control to bounds on the control " | ||
| 63 | "parameters.\n\n" | ||
| 64 | ":param w_lb: lower bounds on w (dim control.nw)\n" | ||
| 65 | ":param w_ub: upper bounds on w (dim control.nw)\n" | ||
| 66 | ":return u_lb, u_ub: lower and upper bounds on the control " | ||
| 67 | "parameters (dim control.nu)") | ||
| 68 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("multiplyByJacobian", &Model::multiplyByJacobian_J, |
| 69 | bp::args("self", "data", "A"), | ||
| 70 | "Compute the product between the given matrix A and the " | ||
| 71 | "derivative of the control with respect to the parameters.\n\n" | ||
| 72 | "It assumes that calc has been run first.\n" | ||
| 73 | ":param data: poly-two-rk data\n" | ||
| 74 | ":param A: matrix to multiply (dim na x control.nw)\n" | ||
| 75 | ":return Product between A and the partial derivative of the " | ||
| 76 | "control (dim na x control.nu)") | ||
| 77 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
40 | .def("multiplyJacobianTransposeBy", |
| 78 | &Model::multiplyJacobianTransposeBy_J, | ||
| 79 | bp::args("self", "data", "A"), | ||
| 80 | "Compute the product between the transpose of the derivative of " | ||
| 81 | "the control with respect to the parameters and a given matrix " | ||
| 82 | "A.\n\n" | ||
| 83 | "It assumes that calc has been run first.\n" | ||
| 84 | ":param data: poly-two-rk data\n" | ||
| 85 | ":param A: matrix to multiply (dim control.nw x na)\n" | ||
| 86 | ":return Product between the partial derivative of the control " | ||
| 87 | "(transposed) and A (dim control.nu x " | ||
| 88 | "na)"); | ||
| 89 | 40 | } | |
| 90 | }; | ||
| 91 | |||
| 92 | template <typename Data> | ||
| 93 | struct ControlParametrizationDataPolyTwoRKVisitor | ||
| 94 | : public bp::def_visitor<ControlParametrizationDataPolyTwoRKVisitor<Data>> { | ||
| 95 | template <class PyClass> | ||
| 96 | 40 | void visit(PyClass& cl) const { | |
| 97 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
| 98 | 40 | "c", bp::make_getter(&Data::c, bp::return_internal_reference<>()), | |
| 99 | "polynomial coefficients of the second-order control model"); | ||
| 100 | 40 | } | |
| 101 | }; | ||
| 102 | |||
| 103 | #define CROCODDYL_CONTROL_PARAMETRIZATION_MODEL_POLYTWORK_PYTHON_BINDINGS( \ | ||
| 104 | Scalar) \ | ||
| 105 | typedef ControlParametrizationModelPolyTwoRKTpl<Scalar> Model; \ | ||
| 106 | typedef ControlParametrizationModelAbstractTpl<Scalar> ModelBase; \ | ||
| 107 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 108 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 109 | "ControlParametrizationModelPolyTwoRK", \ | ||
| 110 | "Second-order polynomial control for RK integrators.\n\n" \ | ||
| 111 | "This control is a quadratic function of time (normalized in [0,1]). " \ | ||
| 112 | "It comes in two versions, one specialized for RK3 integration, " \ | ||
| 113 | "another for RK4 integration. The first third of the parameter vector " \ | ||
| 114 | "contains the initial value of the differential control w, the second " \ | ||
| 115 | "third contains the value of w at t=0.5 (for RK4) or 1/3 (for RK3), " \ | ||
| 116 | "and the last third is the final value\n of w at time t=1 (for RK4) or " \ | ||
| 117 | "2/3 (for RK3).", \ | ||
| 118 | bp::init<std::size_t, RKType>( \ | ||
| 119 | bp::args("self", "nw", "rktype"), \ | ||
| 120 | "Initialize the control dimensions.\n\n" \ | ||
| 121 | ":param nw: dimension of differential control space\n" \ | ||
| 122 | ":param rktype: type of RK parametrization")) \ | ||
| 123 | .def(ControlParametrizationModelPolyTwoRKVisitor<Model>()) \ | ||
| 124 | .def(CastVisitor<Model>()) \ | ||
| 125 | .def(PrintableVisitor<Model>()) \ | ||
| 126 | .def(CopyableVisitor<Model>()); | ||
| 127 | |||
| 128 | #define CROCODDYL_CONTROL_PARAMETRIZATION_DATA_POLYTWORK_PYTHON_BINDINGS( \ | ||
| 129 | Scalar) \ | ||
| 130 | typedef ControlParametrizationDataPolyTwoRKTpl<Scalar> Data; \ | ||
| 131 | typedef ControlParametrizationDataAbstractTpl<Scalar> DataBase; \ | ||
| 132 | typedef ControlParametrizationModelPolyTwoRKTpl<Scalar> Model; \ | ||
| 133 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 134 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 135 | "ControlParametrizationDataPolyTwoRK", \ | ||
| 136 | "Control-parametrization data for the second-order polynomial control.", \ | ||
| 137 | bp::init<Model*>(bp::args("self", "model"), \ | ||
| 138 | "Create control-parametrization data.\n\n" \ | ||
| 139 | ":param model: second-order polynomial control model")) \ | ||
| 140 | .def(ControlParametrizationDataPolyTwoRKVisitor<Data>()) \ | ||
| 141 | .def(CopyableVisitor<Data>()); | ||
| 142 | |||
| 143 | 10 | void exposeControlParametrizationPolyTwoRK() { | |
| 144 |
17/34✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 10 times.
✗ Branch 31 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
✓ Branch 53 taken 10 times.
✗ Branch 54 not taken.
✓ Branch 56 taken 10 times.
✗ Branch 57 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS( |
| 145 | CROCODDYL_CONTROL_PARAMETRIZATION_MODEL_POLYTWORK_PYTHON_BINDINGS) | ||
| 146 |
13/26✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 29 taken 10 times.
✗ Branch 30 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS( |
| 147 | CROCODDYL_CONTROL_PARAMETRIZATION_DATA_POLYTWORK_PYTHON_BINDINGS) | ||
| 148 | 10 | } | |
| 149 | |||
| 150 | } // namespace python | ||
| 151 | } // namespace crocoddyl | ||
| 152 |