Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/controls/poly-one.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 17 | 17 | 100.0% |
Branches: | 45 | 90 | 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-one.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 ControlParametrizationModelPolyOneVisitor | ||
21 | : public bp::def_visitor<ControlParametrizationModelPolyOneVisitor<Model>> { | ||
22 | typedef typename Model::Scalar Scalar; | ||
23 | typedef typename Model::ControlParametrizationDataAbstract Data; | ||
24 | typedef typename Model::VectorXs VectorXs; | ||
25 | template <class PyClass> | ||
26 | 40 | void visit(PyClass& cl) const { | |
27 |
1/2✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
|
40 | cl.def("calc", |
28 | static_cast<void (Model::*)( | ||
29 | const std::shared_ptr<Data>&, const Scalar, | ||
30 | const Eigen::Ref<const VectorXs>&) const>(&Model::calc), | ||
31 | bp::args("self", "data", "t", "u"), | ||
32 | "Compute the control value.\n\n" | ||
33 | ":param data: control-parametrization data\n" | ||
34 | ":param t: normalized time in [0, 1]\n" | ||
35 | ":param u: control parameters (dim control.nu)") | ||
36 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def("calcDiff", |
37 | static_cast<void (Model::*)( | ||
38 | const std::shared_ptr<Data>&, const Scalar, | ||
39 | const Eigen::Ref<const VectorXs>&) const>(&Model::calcDiff), | ||
40 | bp::args("self", "data", "t", "u"), | ||
41 | "Compute the Jacobian of the control value with respect to the " | ||
42 | "control parameters.\n\n" | ||
43 | "It assumes that calc has been run first.\n\n" | ||
44 | ":param data: control-parametrization data\n" | ||
45 | ":param t: normalized time in [0, 1]\n" | ||
46 | ":param u: control parameters (dim control.nu)") | ||
47 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, bp::args("self"), |
48 | "Create the poly-one data.") | ||
49 |
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", |
50 | static_cast<void (Model::*)( | ||
51 | const std::shared_ptr<Data>&, const Scalar, | ||
52 | const Eigen::Ref<const VectorXs>&) const>(&Model::params), | ||
53 | bp::args("self", "data", "t", "w"), | ||
54 | "Compute the control parameters.\n\n" | ||
55 | ":param data: control-parametrization data\n" | ||
56 | ":param t: normalized time in [0, 1]\n" | ||
57 | ":param w: control value (dim control.nw)") | ||
58 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("convertBounds", &Model::convertBounds, |
59 | bp::args("self", "w_lb", "w_ub"), | ||
60 | "Convert the bounds on the control to bounds on the control " | ||
61 | "parameters.\n\n" | ||
62 | ":param w_lb: lower bounds on u (dim control.nw).\n" | ||
63 | ":param w_ub: upper bounds on u (dim control.nw).\n" | ||
64 | ":return p_lb, p_ub: lower and upper bounds on the control " | ||
65 | "parameters (dim control.nu).") | ||
66 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .def( |
67 | "multiplyByJacobian", &Model::multiplyByJacobian_J, | ||
68 | bp::args("self", "data", "A"), | ||
69 | "Compute the product between the given matrix A and the derivative " | ||
70 | "of the control with respect to the parameters.\n\n" | ||
71 | "It assumes that calc has been run first.\n" | ||
72 | ":param data: control-parametrization data\n" | ||
73 | ":param A: matrix to multiply (dim na x control.nw)\n" | ||
74 | ":return Product between A and the partial derivative of the value " | ||
75 | "function (dim na x control.nu)") | ||
76 |
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", |
77 | &Model::multiplyJacobianTransposeBy_J, | ||
78 | bp::args("self", "data", "A"), | ||
79 | "Compute the product between the transpose of the derivative of " | ||
80 | "the control with respect to the parameters and a given matrix " | ||
81 | "A.\n\n" | ||
82 | "It assumes that calc has been run first.\n" | ||
83 | ":param data: control-parametrization data\n" | ||
84 | ":param A: matrix to multiply (dim control.nw x na)\n" | ||
85 | ":return Product between the partial derivative of the value " | ||
86 | "function (transposed) and A (dim control.nu x " | ||
87 | "na)"); | ||
88 | 40 | } | |
89 | }; | ||
90 | |||
91 | template <typename Data> | ||
92 | struct ControlParametrizationDataPolyOneVisitor | ||
93 | : public bp::def_visitor<ControlParametrizationDataPolyOneVisitor<Data>> { | ||
94 | template <class PyClass> | ||
95 | 40 | void visit(PyClass& cl) const { | |
96 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
97 | 40 | "c", bp::make_getter(&Data::c, bp::return_internal_reference<>()), | |
98 | "polynomial coefficients of the linear control model that depends on " | ||
99 | "time"); | ||
100 | 40 | } | |
101 | }; | ||
102 | |||
103 | #define CROCODDYL_CONTROL_PARAMETRIZATION_MODEL_POLYONE_PYTHON_BINDINGS( \ | ||
104 | Scalar) \ | ||
105 | typedef ControlParametrizationModelPolyOneTpl<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 | "ControlParametrizationModelPolyOne", \ | ||
110 | "Linear polynomial control.\n\n" \ | ||
111 | "This control is a linear function of time (normalized in [0,1]). The " \ | ||
112 | "first half of the parameter vector contains the initial value of the " \ | ||
113 | "differential control w, whereas the second half contains the value of " \ | ||
114 | "w at t=0.5.", \ | ||
115 | bp::init<std::size_t>( \ | ||
116 | bp::args("self", "nw"), \ | ||
117 | "Initialize the control dimensions.\n\n" \ | ||
118 | ":param nw: dimension of differential control space")) \ | ||
119 | .def(ControlParametrizationModelPolyOneVisitor<Model>()) \ | ||
120 | .def(CastVisitor<Model>()) \ | ||
121 | .def(PrintableVisitor<Model>()) \ | ||
122 | .def(CopyableVisitor<Model>()); | ||
123 | |||
124 | #define CROCODDYL_CONTROL_PARAMETRIZATION_DATA_POLYONE_PYTHON_BINDINGS(Scalar) \ | ||
125 | typedef ControlParametrizationDataPolyOneTpl<Scalar> Data; \ | ||
126 | typedef ControlParametrizationDataAbstractTpl<Scalar> DataBase; \ | ||
127 | typedef ControlParametrizationModelPolyOneTpl<Scalar> Model; \ | ||
128 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
129 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
130 | "ControlParametrizationDataPolyOne", \ | ||
131 | "Control-parametrization data for the linear polynomial control.", \ | ||
132 | bp::init<Model*>(bp::args("self", "model"), \ | ||
133 | "Create control-parametrization data.\n\n" \ | ||
134 | ":param model: linear polynomial control model")) \ | ||
135 | .def(ControlParametrizationDataPolyOneVisitor<Data>()) \ | ||
136 | .def(CopyableVisitor<Data>()); | ||
137 | |||
138 | 10 | void exposeControlParametrizationPolyOne() { | |
139 |
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( |
140 | CROCODDYL_CONTROL_PARAMETRIZATION_MODEL_POLYONE_PYTHON_BINDINGS) | ||
141 |
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( |
142 | CROCODDYL_CONTROL_PARAMETRIZATION_DATA_POLYONE_PYTHON_BINDINGS) | ||
143 | 10 | } | |
144 | |||
145 | } // namespace python | ||
146 | } // namespace crocoddyl | ||
147 |