GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/cost-base-double.cpp
Date: 2025-04-18 16:41:15
Exec Total Coverage
Lines: 0 52 0.0%
Branches: 0 180 0.0%

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