Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/control-base.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 23 | 23 | 100.0% |
Branches: | 70 | 140 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2025, LAAS-CNRS, University of Edinburgh, | ||
5 | // University of Trento, Heriot-Watt University | ||
6 | // Copyright note valid unless otherwise controld in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #include "python/crocoddyl/core/control-base.hpp" | ||
11 | |||
12 | #include "python/crocoddyl/core/core.hpp" | ||
13 | #include "python/crocoddyl/utils/vector-converter.hpp" | ||
14 | |||
15 | namespace crocoddyl { | ||
16 | namespace python { | ||
17 | |||
18 | template <typename Model> | ||
19 | struct ControlParametrizationModelAbstractVisitor | ||
20 | : public bp::def_visitor< | ||
21 | ControlParametrizationModelAbstractVisitor<Model>> { | ||
22 | template <class PyClass> | ||
23 | 40 | void visit(PyClass& cl) const { | |
24 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
40 | cl.def("calc", pure_virtual(&Model::calc), bp::args("self", "t", "u"), |
25 | "Compute the control inputs.\n\n" | ||
26 | ":param data: control-parametrization data\n" | ||
27 | ":param t: normalized time in [0, 1]\n" | ||
28 | ":param u: control parameters (dim control.nu)") | ||
29 |
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("calcDiff", pure_virtual(&Model::calcDiff), |
30 | bp::args("self", "data", "t", "u"), | ||
31 | "Compute the Jacobian of the control inputs with respect to the " | ||
32 | "control parameters.\n\n" | ||
33 | "It assumes that calc has been run first.\n" | ||
34 | ":param data: control-parametrization data\n" | ||
35 | ":param t: normalized time in [0, 1]\n" | ||
36 | ":param u: control parameters (dim control.nu)") | ||
37 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, &Model::default_createData, |
38 | bp::args("self"), | ||
39 | "Create the control-parametrization data.\n\n" | ||
40 | "Each control parametrization model has its own data that needs " | ||
41 | "to be allocated. This function returns the allocated data for a " | ||
42 | "predefined control parametrization model.\n" | ||
43 | ":return data.") | ||
44 |
4/8✓ 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.
✓ Branch 10 taken 20 times.
✗ Branch 11 not taken.
|
80 | .def("params", pure_virtual(&Model::params), |
45 | bp::args("self", "data", "t", "w"), | ||
46 | "Update the control parameters u for a specified time t given the " | ||
47 | "control input w.\n\n" | ||
48 | ":param data: control-parametrization data\n" | ||
49 | ":param t: normalized time in [0, 1]\n" | ||
50 | ":param w: control inputs (dim control.nw)") | ||
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("convertBounds", pure_virtual(&Model::convertBounds_wrap), |
52 | bp::args("self", "w_lb", "w_ub"), | ||
53 | "Convert the bounds on the control inputs w to bounds on the " | ||
54 | "control parameters u.\n\n" | ||
55 | ":param w_lb: control lower bounds (dim control.nw)\n" | ||
56 | ":param w_ub: control upper bounds (dim control.nw)\n" | ||
57 | ":return p_lb, p_ub: lower and upper bounds on the control " | ||
58 | "parameters (dim control.nu)") | ||
59 |
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("multiplyByJacobian", |
60 | pure_virtual(&Model::multiplyByJacobian_wrap), | ||
61 | bp::args("self", "data", "A"), | ||
62 | "Compute the product between the given matrix A and the " | ||
63 | "derivative of the control input with respect to the control " | ||
64 | "parameters (i.e., A*dw_du).\n\n" | ||
65 | "It assumes that calc has been run first.\n" | ||
66 | ":param data: control-parametrization data\n" | ||
67 | ":param A: matrix to multiply (dim na x control.nw)\n" | ||
68 | ":return Product between A and the partial derivative of the calc " | ||
69 | "function (dim na x control.nu)") | ||
70 |
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("multiplyJacobianTransposeBy", |
71 | pure_virtual(&Model::multiplyJacobianTransposeBy_wrap), | ||
72 | bp::args("self", "data", "A"), | ||
73 | "Compute the product between the transpose of the derivative of " | ||
74 | "the control input with respect to the control parameters and a " | ||
75 | "given matrix A (i.e., dw_du^T*A).\n\n" | ||
76 | "It assumes that calc has been run first.\n" | ||
77 | ":param data: control-parametrization data\n" | ||
78 | ":param A: matrix to multiply (dim control.nw x na)\n" | ||
79 | ":return Product between the partial derivative of the calc " | ||
80 | "function (transposed) and A (dim control.nu x " | ||
81 | "na)") | ||
82 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
80 | .add_property("nw", bp::make_function(&Model::get_nw), |
83 | "dimension of control inputs") | ||
84 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property("nu", bp::make_function(&Model::get_nu), |
85 | "dimension of the control parameters"); | ||
86 | 40 | } | |
87 | }; | ||
88 | |||
89 | template <typename Data> | ||
90 | struct ControlParametrizationDataAbstractVisitor | ||
91 | : public bp::def_visitor<ControlParametrizationDataAbstractVisitor<Data>> { | ||
92 | template <class PyClass> | ||
93 | 40 | void visit(PyClass& cl) const { | |
94 |
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 | cl.add_property( |
95 | 40 | "w", bp::make_getter(&Data::w, bp::return_internal_reference<>()), | |
96 | bp::make_setter(&Data::w), "differential control") | ||
97 |
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 | .add_property( |
98 | 40 | "u", bp::make_getter(&Data::u, bp::return_internal_reference<>()), | |
99 | bp::make_setter(&Data::u), "control parameters") | ||
100 |
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 | .add_property( |
101 | "dw_du", | ||
102 | 40 | bp::make_getter(&Data::dw_du, bp::return_internal_reference<>()), | |
103 | bp::make_setter(&Data::dw_du), | ||
104 | "Jacobian of the differential control wrt the control parameters"); | ||
105 | 40 | } | |
106 | }; | ||
107 | |||
108 | #define CROCODDYL_CONTROL_PARAMETRIZATION_MODEL_ABSTRACT_PYTHON_BINDINGS( \ | ||
109 | Scalar) \ | ||
110 | typedef ControlParametrizationModelAbstractTpl<Scalar> Model; \ | ||
111 | typedef ControlParametrizationModelAbstractTpl_wrap<Scalar> Model_wrap; \ | ||
112 | typedef std::shared_ptr<Model> ModelPtr; \ | ||
113 | StdVectorPythonVisitor<std::vector<ModelPtr>, true>::expose( \ | ||
114 | "StdVec_ControlParametrizationModel"); \ | ||
115 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
116 | bp::class_<Model_wrap, boost::noncopyable>( \ | ||
117 | "ControlParametrizationModelAbstract", \ | ||
118 | "Abstract class for the control parametrization.\n\n" \ | ||
119 | "A control is a function of time (normalized in [0,1]) and the control " \ | ||
120 | "parameters u.", \ | ||
121 | bp::init<std::size_t, std::size_t>( \ | ||
122 | bp::args("self", "nw", "nu"), \ | ||
123 | "Initialize the control dimensions.\n\n" \ | ||
124 | ":param nw: dimension of control inputs\n" \ | ||
125 | ":param nu: dimension of control parameters")) \ | ||
126 | .def(ControlParametrizationModelAbstractVisitor<Model_wrap>()) \ | ||
127 | .def(CopyableVisitor<Model_wrap>()); | ||
128 | // .def(PrintableVisitor<Model_wrap>()) | ||
129 | |||
130 | #define CROCODDYL_CONTROL_PARAMETRIZATION_DATA_ABSTRACT_PYTHON_BINDINGS( \ | ||
131 | Scalar) \ | ||
132 | typedef ControlParametrizationDataAbstractTpl<Scalar> Data; \ | ||
133 | typedef ControlParametrizationModelAbstractTpl<Scalar> Model; \ | ||
134 | typedef std::shared_ptr<Data> DataPtr; \ | ||
135 | StdVectorPythonVisitor<std::vector<DataPtr>, true>::expose( \ | ||
136 | "StdVec_ControlParametrizationData"); \ | ||
137 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
138 | bp::class_<Data>( \ | ||
139 | "ControlParametrizationDataAbstract", \ | ||
140 | "Abstract class for control parametrization data.\n", \ | ||
141 | bp::init<Model*>(bp::args("self", "model"), \ | ||
142 | "Create common data shared between control " \ | ||
143 | "parametrization models.\n\n" \ | ||
144 | ":param model: control parametrization model")) \ | ||
145 | .def(ControlParametrizationDataAbstractVisitor<Data>()) \ | ||
146 | .def(CopyableVisitor<Data>()); | ||
147 | |||
148 | 10 | void exposeControlParametrizationAbstract() { | |
149 |
19/38✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 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 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 33 taken 10 times.
✗ Branch 34 not taken.
✓ Branch 36 taken 10 times.
✗ Branch 37 not taken.
✓ Branch 39 taken 10 times.
✗ Branch 40 not taken.
✓ Branch 45 taken 10 times.
✗ Branch 46 not taken.
✓ Branch 49 taken 10 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 10 times.
✗ Branch 53 not taken.
✓ Branch 59 taken 10 times.
✗ Branch 60 not taken.
✓ Branch 62 taken 10 times.
✗ Branch 63 not taken.
✓ Branch 65 taken 10 times.
✗ Branch 66 not taken.
✓ Branch 68 taken 10 times.
✗ Branch 69 not taken.
✓ Branch 71 taken 10 times.
✗ Branch 72 not taken.
✓ Branch 74 taken 10 times.
✗ Branch 75 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS( |
150 | CROCODDYL_CONTROL_PARAMETRIZATION_MODEL_ABSTRACT_PYTHON_BINDINGS) | ||
151 |
19/38✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 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 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 33 taken 10 times.
✗ Branch 34 not taken.
✓ Branch 36 taken 10 times.
✗ Branch 37 not taken.
✓ Branch 39 taken 10 times.
✗ Branch 40 not taken.
✓ Branch 45 taken 10 times.
✗ Branch 46 not taken.
✓ Branch 49 taken 10 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 10 times.
✗ Branch 53 not taken.
✓ Branch 59 taken 10 times.
✗ Branch 60 not taken.
✓ Branch 62 taken 10 times.
✗ Branch 63 not taken.
✓ Branch 65 taken 10 times.
✗ Branch 66 not taken.
✓ Branch 68 taken 10 times.
✗ Branch 69 not taken.
✓ Branch 71 taken 10 times.
✗ Branch 72 not taken.
✓ Branch 74 taken 10 times.
✗ Branch 75 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS( |
152 | CROCODDYL_CONTROL_PARAMETRIZATION_DATA_ABSTRACT_PYTHON_BINDINGS) | ||
153 | 10 | } | |
154 | |||
155 | } // namespace python | ||
156 | } // namespace crocoddyl | ||
157 |