Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/cost-base.cpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 86 | 95 | 90.5% |
Branches: | 88 | 176 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh, | ||
5 | // Heriot-Watt University | ||
6 | // Copyright note valid unless otherwise stated in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #include "python/crocoddyl/core/cost-base.hpp" | ||
11 | |||
12 | #include "python/crocoddyl/utils/copyable.hpp" | ||
13 | #include "python/crocoddyl/utils/deprecate.hpp" | ||
14 | #include "python/crocoddyl/utils/printable.hpp" | ||
15 | |||
16 | namespace crocoddyl { | ||
17 | namespace python { | ||
18 | |||
19 | 10 | void exposeCostAbstract() { | |
20 | // TODO: Remove once the deprecated update call has been removed in a future | ||
21 | // release | ||
22 | #pragma GCC diagnostic push | ||
23 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||
24 | |||
25 | 10 | bp::register_ptr_to_python<boost::shared_ptr<CostModelAbstract> >(); | |
26 | |||
27 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<CostModelAbstract_wrap, boost::noncopyable>( |
28 | "CostModelAbstract", | ||
29 | "Abstract multibody cost models.\n\n" | ||
30 | "In Crocoddyl, a cost model is defined by the scalar activation function " | ||
31 | "a(.) and by the residual\n" | ||
32 | " function r(.) as follows:\n" | ||
33 | " cost = a(r(x, u)),\n" | ||
34 | "where the residual function depends on the state point x, which lies in " | ||
35 | "the state manifold\n" | ||
36 | "described with a nq-tuple, its velocity xd that belongs to the tangent " | ||
37 | "space with nv dimension,\n" | ||
38 | "and the control input u. The dimension of the residual vector is " | ||
39 | "defined by nr, which belongs to\n" | ||
40 | "the Euclidean space. On the other hand, the activation function builds " | ||
41 | "a cost value based on the\n" | ||
42 | "definition of the residual vector. The residual vector has to be " | ||
43 | "specialized in a derived classes.", | ||
44 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<boost::shared_ptr<StateAbstract>, |
45 | boost::shared_ptr<ActivationModelAbstract>, | ||
46 | boost::shared_ptr<ResidualModelAbstract> >( | ||
47 | 20 | bp::args("self", "state", "activation", "residual"), | |
48 | "Initialize the cost model.\n\n" | ||
49 | ":param state: state description\n" | ||
50 | ":param activation: activation model\n" | ||
51 | ":param residual: residual model")) | ||
52 |
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>, |
53 | boost::shared_ptr<ActivationModelAbstract>, std::size_t>( | ||
54 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "state", "activation", "nu"), |
55 | "Initialize the cost model.\n\n" | ||
56 | ":param state: state description\n" | ||
57 | ":param activation: activation model\n" | ||
58 | ":param nu: dimension of control vector (default state.nv)")) | ||
59 |
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>, |
60 | boost::shared_ptr<ActivationModelAbstract> >( | ||
61 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "state", "activation"), |
62 | "Initialize the cost model.\n\n" | ||
63 | ":param state: state description\n" | ||
64 | ":param activation: activation model")) | ||
65 |
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>, |
66 | boost::shared_ptr<ResidualModelAbstract> >( | ||
67 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "state", "residual"), |
68 | "Initialize the cost model.\n\n" | ||
69 | ":param state: state description\n" | ||
70 | ":param residual: residual model")) | ||
71 |
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, std::size_t>( |
72 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "state", "nr", "nu"), |
73 | "Initialize the cost model.\n\n" | ||
74 | "We use ActivationModelQuad as a default activation model (i.e. " | ||
75 | "a=0.5*||r||^2).\n" | ||
76 | ":param state: state description\n" | ||
77 | ":param nr: dimension of residual vector\n" | ||
78 | ":param nu: dimension of control vector (default state.nv)")) | ||
79 |
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>( |
80 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "state", "nr"), |
81 | "Initialize the cost model.\n\n" | ||
82 | "We use ActivationModelQuad as a default activation model (i.e. " | ||
83 | "a=0.5*||r||^2), and the default nu value is " | ||
84 | "obtained from state.nv.\n" | ||
85 | ":param state: state description\n" | ||
86 | ":param nr: dimension of cost vector")) | ||
87 |
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(&CostModelAbstract_wrap::calc), |
88 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x", "u"), |
89 | "Compute the cost value and its residuals.\n\n" | ||
90 | ":param data: cost data\n" | ||
91 | ":param x: state point (dim. state.nx)\n" | ||
92 | ":param u: control input (dim. nu)") | ||
93 | .def<void (CostModelAbstract::*)( | ||
94 | const boost::shared_ptr<CostDataAbstract>&, | ||
95 | 20 | const Eigen::Ref<const Eigen::VectorXd>&)>( | |
96 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | "calc", &CostModelAbstract::calc, bp::args("self", "data", "x"), |
97 | "Compute the total cost value for nodes that depends only on the " | ||
98 | "state.\n\n" | ||
99 | "It updates the total cost based on the state only.\n" | ||
100 | "This function is used in the terminal nodes of an optimal control " | ||
101 | "problem.\n" | ||
102 | ":param data: cost data\n" | ||
103 | ":param x: state point (dim. state.nx)") | ||
104 |
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(&CostModelAbstract_wrap::calcDiff), |
105 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x", "u"), |
106 | "Compute the derivatives of the cost function and its residuals.\n\n" | ||
107 | "It computes the partial derivatives of the cost function.\n" | ||
108 | "It assumes that calc has been run first.\n" | ||
109 | ":param data: cost data\n" | ||
110 | ":param x: state point (dim. state.nx)\n" | ||
111 | ":param u: control input (dim. nu)") | ||
112 | .def<void (CostModelAbstract::*)( | ||
113 | const boost::shared_ptr<CostDataAbstract>&, | ||
114 | 20 | const Eigen::Ref<const Eigen::VectorXd>&)>( | |
115 | "calcDiff", &CostModelAbstract::calcDiff, | ||
116 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x"), |
117 | "Compute the Jacobian and Hessian of the cost functions with respect " | ||
118 | "to the state only.\n\n" | ||
119 | "It updates the Jacobian and Hessian of the cost function based on " | ||
120 | "the state only.\n" | ||
121 | "This function is used in the terminal nodes of an optimal control " | ||
122 | "problem.\n" | ||
123 | ":param data: cost data\n" | ||
124 | ":param x: state point (dim. state.nx)") | ||
125 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | .def("createData", &CostModelAbstract_wrap::createData, |
126 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
127 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data"), |
128 | "Create the cost data.\n\n" | ||
129 | "Each cost model has its own data that needs to be allocated. This " | ||
130 | "function\n" | ||
131 | "returns the allocated data for a predefined cost.\n" | ||
132 | ":param data: shared data\n" | ||
133 | ":return cost data.") | ||
134 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def("createData", &CostModelAbstract_wrap::default_createData, |
135 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>()) | |
136 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
137 | "state", | ||
138 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&CostModelAbstract_wrap::get_state, |
139 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
140 | "state description") | ||
141 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
142 | "activation", | ||
143 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&CostModelAbstract_wrap::get_activation, |
144 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
145 | "activation model") | ||
146 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
147 | "residual", | ||
148 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&CostModelAbstract_wrap::get_residual, |
149 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
150 | "residual model") | ||
151 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | .add_property("nu", bp::make_function(&CostModelAbstract_wrap::get_nu), |
152 | "dimension of control vector") | ||
153 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(PrintableVisitor<CostModelAbstract>()); |
154 | |||
155 | 10 | bp::register_ptr_to_python<boost::shared_ptr<CostDataAbstract> >(); | |
156 | |||
157 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<CostDataAbstract>( |
158 | "CostDataAbstract", "Abstract class for cost data.\n\n", | ||
159 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<CostModelAbstract*, DataCollectorAbstract*>( |
160 | 10 | bp::args("self", "model", "data"), | |
161 | "Create common data shared between cost models.\n\n" | ||
162 | ":param model: cost model\n" | ||
163 | ✗ | ":param data: shared data")[bp::with_custodian_and_ward< | |
164 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | 1, 2, bp::with_custodian_and_ward<1, 3> >()]) |
165 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("shared", |
166 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::shared, |
167 | 10 | bp::return_internal_reference<>()), | |
168 | "shared data") | ||
169 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
170 | "activation", | ||
171 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::activation, |
172 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
173 | "activation data") | ||
174 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
175 | "residual", | ||
176 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::residual, |
177 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
178 | "residual data") | ||
179 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
180 | "cost", | ||
181 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::cost, |
182 | ✗ | bp::return_value_policy<bp::return_by_value>()), | |
183 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&CostDataAbstract::cost), "cost value") |
184 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Lx", |
185 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::Lx, |
186 | ✗ | bp::return_internal_reference<>()), | |
187 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&CostDataAbstract::Lx), |
188 | "Jacobian of the cost") | ||
189 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Lu", |
190 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::Lu, |
191 | ✗ | bp::return_internal_reference<>()), | |
192 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&CostDataAbstract::Lu), |
193 | "Jacobian of the cost") | ||
194 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Lxx", |
195 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::Lxx, |
196 | ✗ | bp::return_internal_reference<>()), | |
197 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&CostDataAbstract::Lxx), |
198 | "Hessian of the cost") | ||
199 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Lxu", |
200 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::Lxu, |
201 | ✗ | bp::return_internal_reference<>()), | |
202 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&CostDataAbstract::Lxu), |
203 | "Hessian of the cost") | ||
204 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Luu", |
205 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&CostDataAbstract::Luu, |
206 | ✗ | bp::return_internal_reference<>()), | |
207 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_setter(&CostDataAbstract::Luu), |
208 | "Hessian of the cost") | ||
209 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
210 | "r", | ||
211 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&CostDataAbstract::get_r, |
212 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<bp::return_internal_reference<> >( |
213 | "Deprecated. Use residual.r.")), | ||
214 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&CostDataAbstract::set_r, |
215 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<>("Deprecated. Use residual.r.")), |
216 | "cost residual") | ||
217 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
218 | "Rx", | ||
219 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&CostDataAbstract::get_Rx, |
220 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<bp::return_internal_reference<> >( |
221 | "Deprecated. Use residual.Rx.")), | ||
222 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&CostDataAbstract::set_Rx, |
223 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<>("Deprecated. Use residual.Rx.")), |
224 | "Jacobian of the cost residual") | ||
225 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
226 | "Ru", | ||
227 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&CostDataAbstract::get_Ru, |
228 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<bp::return_internal_reference<> >( |
229 | "Deprecated. Use residual.Ru.")), | ||
230 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&CostDataAbstract::set_Ru, |
231 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
20 | deprecated<>("Deprecated. Use residual.Ru.")), |
232 | "Jacobian of the cost residual") | ||
233 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<CostDataAbstract>()); |
234 | |||
235 | #pragma GCC diagnostic pop | ||
236 | 10 | } | |
237 | |||
238 | } // namespace python | ||
239 | } // namespace crocoddyl | ||
240 |