Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/residual-base.cpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 61 | 65 | 93.8% |
Branches: | 53 | 106 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2023, University of Edinburgh, Heriot-Watt University | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #include "python/crocoddyl/core/residual-base.hpp" | ||
10 | |||
11 | #include "python/crocoddyl/utils/copyable.hpp" | ||
12 | #include "python/crocoddyl/utils/printable.hpp" | ||
13 | |||
14 | namespace crocoddyl { | ||
15 | namespace python { | ||
16 | |||
17 | 10 | void exposeResidualAbstract() { | |
18 | 10 | bp::register_ptr_to_python<boost::shared_ptr<ResidualModelAbstract> >(); | |
19 | |||
20 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<ResidualModelAbstract_wrap, boost::noncopyable>( |
21 | "ResidualModelAbstract", | ||
22 | "Abstract class for residual models.\n\n" | ||
23 | "A residual model defines a vector function r(x,u) in R^nr, where nr " | ||
24 | "describes its dimension in the\n" | ||
25 | "the Euclidean space. For each residual model, we need to provide ways " | ||
26 | "of computing the residual\n" | ||
27 | "vector and its Jacobians. These computations are mainly carried out " | ||
28 | "inside calc() and calcDiff(),\n" | ||
29 | "respectively.", | ||
30 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<boost::shared_ptr<StateAbstract>, std::size_t, std::size_t, |
31 | bp::optional<bool, bool, bool> >( | ||
32 | 20 | bp::args("self", "state", "nr", "nu", "q_dependent", "v_dependent", | |
33 | "u_dependent"), | ||
34 | "Initialize the residual model.\n\n" | ||
35 | ":param state: state description,\n" | ||
36 | ":param nr: dimension of the residual vector\n" | ||
37 | ":param nu: dimension of control vector (default state.nv)\n" | ||
38 | ":param q_dependent: define if the residual function depends on q " | ||
39 | "(default true)\n" | ||
40 | ":param v_dependent: define if the residual function depends on v " | ||
41 | "(default true)\n" | ||
42 | ":param u_dependent: define if the residual function depends on u " | ||
43 | "(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<boost::shared_ptr<StateAbstract>, std::size_t, |
45 | bp::optional<bool, bool, bool> >( | ||
46 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "state", "nr", "q_dependent", "v_dependent", |
47 | "u_dependent"), | ||
48 | "Initialize the cost model.\n\n" | ||
49 | ":param state: state description\n" | ||
50 | ":param nr: dimension of the residual vector\n" | ||
51 | ":param q_dependent: define if the residual function depends on q " | ||
52 | "(default true)\n" | ||
53 | ":param v_dependent: define if the residual function depends on v " | ||
54 | "(default true)\n" | ||
55 | ":param u_dependent: define if the residual function depends on u " | ||
56 | "(default true)")) | ||
57 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | .def("calc", pure_virtual(&ResidualModelAbstract_wrap::calc), |
58 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x", "u"), |
59 | "Compute the residual vector.\n\n" | ||
60 | ":param data: residual data\n" | ||
61 | ":param x: state point (dim. state.nx)\n" | ||
62 | ":param u: control input (dim. nu)") | ||
63 | .def<void (ResidualModelAbstract::*)( | ||
64 | const boost::shared_ptr<ResidualDataAbstract>&, | ||
65 | 20 | const Eigen::Ref<const Eigen::VectorXd>&)>( | |
66 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | "calc", &ResidualModelAbstract::calc, bp::args("self", "data", "x"), |
67 | "Compute the residual vector for nodes that depends only on the " | ||
68 | "state.\n\n" | ||
69 | "It updates the residual vector based on the state only.\n" | ||
70 | "This function is used in the terminal nodes of an optimal control " | ||
71 | "problem.\n" | ||
72 | ":param data: residual data\n" | ||
73 | ":param x: state point (dim. state.nx)") | ||
74 |
3/6✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 10 times.
✗ Branch 8 not taken.
|
20 | .def("calcDiff", pure_virtual(&ResidualModelAbstract_wrap::calcDiff), |
75 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x", "u"), |
76 | "Compute the Jacobians of the residual function.\n\n" | ||
77 | ":param data: residual data\n" | ||
78 | ":param x: state point (dim. state.nx)\n" | ||
79 | ":param u: control input (dim. nu)") | ||
80 | .def<void (ResidualModelAbstract::*)( | ||
81 | const boost::shared_ptr<ResidualDataAbstract>&, | ||
82 | 20 | const Eigen::Ref<const Eigen::VectorXd>&)>( | |
83 | "calcDiff", &ResidualModelAbstract::calcDiff, | ||
84 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x"), |
85 | "Compute the Jacobian of the residual functions with respect to the " | ||
86 | "state only.\n\n" | ||
87 | "It updates the Jacobian of the residual function based on the state " | ||
88 | "only.\n" | ||
89 | "This function is used in the terminal nodes of an optimal control " | ||
90 | "problem.\n" | ||
91 | ":param data: residual data\n" | ||
92 | ":param x: state point (dim. state.nx)") | ||
93 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def("createData", &ResidualModelAbstract_wrap::createData, |
94 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | &ResidualModelAbstract_wrap::default_createData, bp::args("self"), |
95 | "Create the residual data.\n\n" | ||
96 | "Each residual model might has its own data that needs to be " | ||
97 | "allocated.") | ||
98 | 20 | .def("calcCostDiff", &ResidualModelAbstract_wrap::calcCostDiff, | |
99 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | &ResidualModelAbstract_wrap::default_calcCostDiff, |
100 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "cdata", "rdata", "adata", "update_u"), |
101 | "Compute the derivative of the cost function.\n\n" | ||
102 | "This function assumes that the derivatives of the activation and " | ||
103 | "residual are\n" | ||
104 | "computed via calcDiff functions.\n" | ||
105 | ":param cdata: cost data\n" | ||
106 | ":param rdata: residual data\n" | ||
107 | ":param adata: activation data\n" | ||
108 | ":param update_u: update the derivative of the cost function w.r.t. " | ||
109 | "to the control if True.") | ||
110 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def("calcCostDiff", |
111 | &ResidualModelAbstract_wrap::default_calcCostDiff_noupdate_u) | ||
112 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
113 | "state", | ||
114 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&ResidualModelAbstract_wrap::get_state, |
115 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
116 | "state") | ||
117 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("nr", |
118 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&ResidualModelAbstract_wrap::get_nr), |
119 | "dimension of residual vector") | ||
120 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("nu", |
121 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&ResidualModelAbstract_wrap::get_nu), |
122 | "dimension of control vector") | ||
123 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
124 | "q_dependent", | ||
125 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&ResidualModelAbstract_wrap::get_q_dependent), |
126 | "flag that indicates if the residual function depends on q") | ||
127 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
128 | "v_dependent", | ||
129 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&ResidualModelAbstract_wrap::get_v_dependent), |
130 | "flag that indicates if the residual function depends on v") | ||
131 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
132 | "u_dependent", | ||
133 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&ResidualModelAbstract_wrap::get_u_dependent), |
134 | "flag that indicates if the residual function depends on u") | ||
135 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<ResidualModelAbstract_wrap>()) |
136 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(PrintableVisitor<ResidualModelAbstract>()); |
137 | |||
138 | 10 | bp::register_ptr_to_python<boost::shared_ptr<ResidualDataAbstract> >(); | |
139 | |||
140 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<ResidualDataAbstract>( |
141 | "ResidualDataAbstract", | ||
142 | "Abstract class for residual data.\n\n" | ||
143 | "In crocoddyl, a residual data contains all the required information for " | ||
144 | "processing an\n" | ||
145 | "user-defined residual models. The residual data typically is allocated " | ||
146 | "once and containts\n" | ||
147 | "the residual vector and its Jacobians.", | ||
148 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<ResidualModelAbstract*, DataCollectorAbstract*>( |
149 | 10 | bp::args("self", "model", "data"), | |
150 | "Create common data shared between residual models.\n\n" | ||
151 | ":param model: residual model\n" | ||
152 | ✗ | ":param data: shared data")[bp::with_custodian_and_ward< | |
153 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | 1, 2, bp::with_custodian_and_ward<1, 3> >()]) |
154 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("shared", |
155 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&ResidualDataAbstract::shared, |
156 | 10 | bp::return_internal_reference<>()), | |
157 | "shared data") | ||
158 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("r", |
159 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&ResidualDataAbstract::r, |
160 | ✗ | bp::return_internal_reference<>()), | |
161 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&ResidualDataAbstract::r), |
162 | "residual vector") | ||
163 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Rx", |
164 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&ResidualDataAbstract::Rx, |
165 | ✗ | bp::return_internal_reference<>()), | |
166 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&ResidualDataAbstract::Rx), |
167 | "Jacobian of the residual") | ||
168 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Ru", |
169 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&ResidualDataAbstract::Ru, |
170 | ✗ | bp::return_internal_reference<>()), | |
171 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&ResidualDataAbstract::Ru), |
172 | "Jacobian of the residual") | ||
173 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Arr_Rx", |
174 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&ResidualDataAbstract::Arr_Rx, |
175 | 10 | bp::return_internal_reference<>()), | |
176 | "Intermediate product of Arr (2nd deriv of Activation) " | ||
177 | "with Rx (deriv of residue)") | ||
178 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Arr_Ru", |
179 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&ResidualDataAbstract::Arr_Ru, |
180 | 10 | bp::return_internal_reference<>()), | |
181 | "Intermediate product of Arr (2nd deriv of Activation) " | ||
182 | "with Ru (deriv of residue)") | ||
183 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<ResidualDataAbstract>()); |
184 | 10 | } | |
185 | |||
186 | } // namespace python | ||
187 | } // namespace crocoddyl | ||
188 |