GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/costs/cost-sum.cpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 100 107 93.5%
Branches: 97 194 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 "crocoddyl/core/costs/cost-sum.hpp"
11
12 #include <functional>
13 #include <map>
14 #include <memory>
15
16 #include "python/crocoddyl/core/action-base.hpp"
17 #include "python/crocoddyl/core/core.hpp"
18 #include "python/crocoddyl/core/diff-action-base.hpp"
19 #include "python/crocoddyl/utils/copyable.hpp"
20 #include "python/crocoddyl/utils/deprecate.hpp"
21 #include "python/crocoddyl/utils/map-converter.hpp"
22 #include "python/crocoddyl/utils/printable.hpp"
23
24 namespace crocoddyl {
25 namespace python {
26
27
1/2
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
130 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(CostModelSum_addCost_wrap,
28 CostModelSum::addCost, 3, 4)
29
30 10 void exposeCostSum() {
31 // Register custom converters between std::map and Python dict
32 typedef boost::shared_ptr<CostItem> CostItemPtr;
33 typedef boost::shared_ptr<CostDataAbstract> CostDataPtr;
34 StdMapPythonVisitor<std::string, CostItemPtr, std::less<std::string>,
35 std::allocator<std::pair<const std::string, CostItemPtr>>,
36
3/6
✓ 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.
10 true>::expose("StdMap_CostItem");
37 StdMapPythonVisitor<std::string, CostDataPtr, std::less<std::string>,
38 std::allocator<std::pair<const std::string, CostDataPtr>>,
39
3/6
✓ 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.
10 true>::expose("StdMap_CostData");
40
41 10 bp::register_ptr_to_python<boost::shared_ptr<CostItem>>();
42
43
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<CostItem>(
44 "CostItem", "Describe a cost item.\n\n",
45
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<std::string, boost::shared_ptr<CostModelAbstract>, double,
46 bp::optional<bool>>(
47 20 bp::args("self", "name", "cost", "weight", "active"),
48 "Initialize the cost item.\n\n"
49 ":param name: cost name\n"
50 ":param cost: cost model\n"
51 ":param weight: cost weight\n"
52 ":param active: True if the cost is activated (default true)"))
53
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def_readwrite("name", &CostItem::name, "cost name")
54
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
55 "cost",
56
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&CostItem::cost,
57 10 bp::return_value_policy<bp::return_by_value>()),
58 "cost model")
59
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def_readwrite("weight", &CostItem::weight, "cost weight")
60
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def_readwrite("active", &CostItem::active, "cost status")
61
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<CostItem>())
62
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(PrintableVisitor<CostItem>());
63
64 10 bp::register_ptr_to_python<boost::shared_ptr<CostModelSum>>();
65
66
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<CostModelSum>(
67
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 "CostModelSum", bp::init<boost::shared_ptr<StateAbstract>, std::size_t>(
68 20 bp::args("self", "state", "nu"),
69 "Initialize the total cost model.\n\n"
70 ":param state: state description\n"
71 ":param nu: dimension of control vector"))
72
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>(
73
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "state", "nu"),
74 "Initialize the total cost model.\n\n"
75 "For this case the default nu is equals to model.nv.\n"
76 ":param state: state description\n"
77 ":param nu: dimension of control vector"))
78
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>>(
79
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "state"),
80 "Initialize the total cost model.\n\n"
81 "For this case the default nu is equals to model.nv.\n"
82 ":param state: state description"))
83
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("addCost", &CostModelSum::addCost,
84
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 CostModelSum_addCost_wrap(
85
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name", "cost", "weight", "active"),
86 "Add a cost item.\n\n"
87 ":param name: cost name\n"
88 ":param cost: cost model\n"
89 ":param weight: cost weight\n"
90 ":param active: True if the cost is activated (default true)"))
91
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .def("removeCost", &CostModelSum::removeCost, bp::args("self", "name"),
92 "Remove a cost item.\n\n"
93 ":param name: cost name")
94
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(
95 "changeCostStatus", &CostModelSum::changeCostStatus,
96
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name", "active"),
97 "Change the cost status.\n\n"
98 ":param name: cost name\n"
99 ":param active: cost status (true for active and false for inactive)")
100 .def<void (CostModelSum::*)(const boost::shared_ptr<CostDataSum>&,
101 const Eigen::Ref<const Eigen::VectorXd>&,
102 20 const Eigen::Ref<const Eigen::VectorXd>&)>(
103
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 "calc", &CostModelSum::calc, bp::args("self", "data", "x", "u"),
104 "Compute the total cost.\n\n"
105 ":param data: cost-sum data\n"
106 ":param x: state point (dim. state.nx)\n"
107 ":param u: control input (dim. nu)")
108 .def<void (CostModelSum::*)(const boost::shared_ptr<CostDataSum>&,
109
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 const Eigen::Ref<const Eigen::VectorXd>&)>(
110
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 "calc", &CostModelSum::calc, bp::args("self", "data", "x"),
111 "Compute the total cost value for nodes that depends only on the "
112 "state.\n\n"
113 "It updates the total cost based on the state only.\n"
114 "This function is used in the terminal nodes of an optimal control "
115 "problem.\n"
116 ":param data: cost-sum data\n"
117 ":param x: state point (dim. state.nx)")
118 .def<void (CostModelSum::*)(const boost::shared_ptr<CostDataSum>&,
119 const Eigen::Ref<const Eigen::VectorXd>&,
120
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 const Eigen::Ref<const Eigen::VectorXd>&)>(
121 "calcDiff", &CostModelSum::calcDiff,
122
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x", "u"),
123 "Compute the derivatives of the total cost.\n\n"
124 "It assumes that calc has been run first.\n"
125 ":param data: action data\n"
126 ":param x: state point (dim. state.nx)\n"
127 ":param u: control input (dim. nu)")
128 .def<void (CostModelSum::*)(const boost::shared_ptr<CostDataSum>&,
129
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 const Eigen::Ref<const Eigen::VectorXd>&)>(
130
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 "calcDiff", &CostModelSum::calcDiff, bp::args("self", "data", "x"),
131 "Compute the Jacobian and Hessian of the total cost for nodes that "
132 "depends on the state only.\n\n"
133 "It updates the Jacobian and Hessian of the total cost based on the "
134 "state only.\n"
135 "This function is used in the terminal nodes of an optimal control "
136 "problem.\n"
137 ":param data: cost-sum data\n"
138 ":param x: state point (dim. state.nx)")
139
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .def("createData", &CostModelSum::createData,
140 bp::with_custodian_and_ward_postcall<0, 2>(),
141
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data"),
142 "Create the total cost data.\n\n"
143 ":param data: shared data\n"
144 ":return total cost data.")
145
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
146 "state",
147
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostModelSum::get_state,
148 10 bp::return_value_policy<bp::return_by_value>()),
149 "state description")
150
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
151 "costs",
152
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostModelSum::get_costs,
153 10 bp::return_value_policy<bp::return_by_value>()),
154 "stack of costs")
155
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(&CostModelSum::get_nu),
156 "dimension of control vector")
157
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .add_property("nr", bp::make_function(&CostModelSum::get_nr),
158 "dimension of the residual vector of active cost")
159
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .add_property("nr_total", bp::make_function(&CostModelSum::get_nr_total),
160 "dimension of the total residual vector")
161
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
162 "active",
163
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(
164 &CostModelSum::get_active,
165
2/4
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
20 deprecated<bp::return_value_policy<bp::return_by_value>>(
166 "Deprecated. Use property active_set")),
167 "list of names of active contact items")
168
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
169 "inactive",
170
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(
171 &CostModelSum::get_inactive,
172
2/4
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
20 deprecated<bp::return_value_policy<bp::return_by_value>>(
173 "Deprecated. Use property inactive_set")),
174 "list of names of inactive contact items")
175
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
176 "active_set",
177
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostModelSum::get_active_set,
178 10 bp::return_value_policy<bp::return_by_value>()),
179 "name of the active set of cost items")
180
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
181 "inactive_set",
182
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostModelSum::get_inactive_set,
183 10 bp::return_value_policy<bp::return_by_value>()),
184 "name of the inactive set of cost items")
185
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("getCostStatus", &CostModelSum::getCostStatus,
186
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name"),
187 "Return the cost status of a given cost name.\n\n"
188 ":param name: cost name")
189
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<CostModelSum>())
190
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(PrintableVisitor<CostModelSum>());
191
192 10 bp::register_ptr_to_python<boost::shared_ptr<CostDataSum>>();
193
194
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<CostDataSum>(
195 "CostDataSum", "Class for total cost data.\n\n",
196
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<CostModelSum*, DataCollectorAbstract*>(
197 10 bp::args("self", "model", "data"),
198 "Create total cost data.\n\n"
199 ":param model: total cost model\n"
200
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 ":param data: shared data")[bp::with_custodian_and_ward<1, 3>()])
201 20 .def(
202 "shareMemory",
203 &CostDataSum::shareMemory<DifferentialActionDataAbstract>,
204
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "model"),
205 "Share memory with a given differential action data\n\n"
206 ":param model: differential action data that we want to share memory")
207
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("shareMemory", &CostDataSum::shareMemory<ActionDataAbstract>,
208
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "model"),
209 "Share memory with a given action data\n\n"
210 ":param model: action data that we want to share memory")
211
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .add_property(
212 "costs",
213
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&CostDataSum::costs,
214 10 bp::return_value_policy<bp::return_by_value>()),
215 "stack of costs data")
216
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("shared",
217
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&CostDataSum::shared,
218 10 bp::return_internal_reference<>()),
219 "shared data")
220
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
221 "cost",
222
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&CostDataSum::cost,
223 bp::return_value_policy<bp::return_by_value>()),
224
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&CostDataSum::cost), "cost value")
225
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
226 "Lx",
227
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostDataSum::get_Lx,
228 bp::return_value_policy<bp::return_by_value>()),
229
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&CostDataSum::set_Lx), "Jacobian of the cost")
230
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
231 "Lu",
232
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostDataSum::get_Lu,
233 bp::return_value_policy<bp::return_by_value>()),
234
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&CostDataSum::set_Lu), "Jacobian of the cost")
235
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
236 "Lxx",
237
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostDataSum::get_Lxx,
238 bp::return_value_policy<bp::return_by_value>()),
239
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&CostDataSum::set_Lxx), "Hessian of the cost")
240
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
241 "Lxu",
242
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostDataSum::get_Lxu,
243 bp::return_value_policy<bp::return_by_value>()),
244
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&CostDataSum::set_Lxu), "Hessian of the cost")
245
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
246 "Luu",
247
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&CostDataSum::get_Luu,
248 bp::return_value_policy<bp::return_by_value>()),
249
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&CostDataSum::set_Luu), "Hessian of the cost")
250
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<CostDataSum>());
251 10 }
252
253 } // namespace python
254 } // namespace crocoddyl
255