GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/constraints/constraint-manager.cpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 115 125 92.0%
Branches: 104 210 49.5%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2020-2024, University of Edinburgh, Heriot-Watt University
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
7 ///////////////////////////////////////////////////////////////////////////////
8
9 #include "crocoddyl/core/constraints/constraint-manager.hpp"
10
11 #include <functional>
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <utility>
16
17 #include "python/crocoddyl/core/action-base.hpp"
18 #include "python/crocoddyl/core/core.hpp"
19 #include "python/crocoddyl/core/diff-action-base.hpp"
20 #include "python/crocoddyl/utils/copyable.hpp"
21 #include "python/crocoddyl/utils/map-converter.hpp"
22 #include "python/crocoddyl/utils/printable.hpp"
23 #include "python/crocoddyl/utils/set-converter.hpp"
24
25 namespace crocoddyl {
26 namespace python {
27
28
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
20 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(
29 ConstraintModelManager_addConstraint_wrap,
30 ConstraintModelManager::addConstraint, 2, 3)
31 10 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(ConstraintDataManager_resizeConst_wrap,
32 ConstraintDataManager::resize, 1, 2)
33 20 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(ConstraintDataManager_resize_wrap,
34 ConstraintDataManager::resize, 2, 3)
35
36 10 void exposeConstraintManager() {
37 // Register custom converters between std::map and Python dict
38 typedef boost::shared_ptr<ConstraintItem> ConstraintItemPtr;
39 typedef boost::shared_ptr<ConstraintDataAbstract> ConstraintDataPtr;
40 StdMapPythonVisitor<
41 std::string, ConstraintItemPtr, std::less<std::string>,
42 std::allocator<std::pair<const std::string, ConstraintItemPtr> >,
43
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_ConstraintItem");
44 StdMapPythonVisitor<
45 std::string, ConstraintDataPtr, std::less<std::string>,
46 std::allocator<std::pair<const std::string, ConstraintDataPtr> >,
47
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_ConstraintData");
48
49 10 bp::register_ptr_to_python<boost::shared_ptr<ConstraintItem> >();
50
51
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<ConstraintItem>(
52 "ConstraintItem", "Describe a constraint item.\n\n",
53
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<std::string, boost::shared_ptr<ConstraintModelAbstract>,
54 bp::optional<bool> >(
55 20 bp::args("self", "name", "constraint", "active"),
56 "Initialize the constraint item.\n\n"
57 ":param name: constraint name\n"
58 ":param constraint: constraint model\n"
59 ":param active: True if the constraint is activated (default true)"))
60
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def_readwrite("name", &ConstraintItem::name, "constraint name")
61
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
62 "constraint",
63
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ConstraintItem::constraint,
64 10 bp::return_value_policy<bp::return_by_value>()),
65 "constraint model")
66
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def_readwrite("active", &ConstraintItem::active, "constraint status")
67
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<ConstraintItem>())
68
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(PrintableVisitor<ConstraintItem>());
69
70 10 bp::register_ptr_to_python<boost::shared_ptr<ConstraintModelManager> >();
71
72
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<ConstraintModelManager>(
73 "ConstraintModelManager",
74
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<boost::shared_ptr<StateAbstract>, std::size_t>(
75 20 bp::args("self", "state", "nu"),
76 "Initialize the total constraint model.\n\n"
77 ":param state: state description\n"
78 ":param nu: dimension of control vector"))
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", "nu"),
81 "Initialize the total constraint model.\n\n"
82 "For this case the default nu is equals to model.nv.\n"
83 ":param state: state description\n"
84 ":param nu: dimension of control vector"))
85
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> >(
86
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "state"),
87 "Initialize the total constraint model.\n\n"
88 "For this case the default nu is equals to model.nv.\n"
89 ":param state: state description"))
90
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("addConstraint", &ConstraintModelManager::addConstraint,
91
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 ConstraintModelManager_addConstraint_wrap(
92
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name", "constraint", "active"),
93 "Add a constraint item.\n\n"
94 ":param name: constraint name\n"
95 ":param constraint: constraint model\n"
96 ":param active: True if the constraint is activated (default "
97 "true)"))
98
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("removeConstraint", &ConstraintModelManager::removeConstraint,
99
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name"),
100 "Remove a constraint item.\n\n"
101 ":param name: constraint name")
102
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("changeConstraintStatus",
103 &ConstraintModelManager::changeConstraintStatus,
104
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name", "active"),
105 "Change the constraint status.\n\n"
106 ":param name: constraint name\n"
107 ":param active: constraint status (true for active and false for "
108 "inactive)")
109 .def<void (ConstraintModelManager::*)(
110 const boost::shared_ptr<ConstraintDataManager>&,
111 const Eigen::Ref<const Eigen::VectorXd>&,
112 20 const Eigen::Ref<const Eigen::VectorXd>&)>(
113 "calc", &ConstraintModelManager::calc,
114
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x", "u"),
115 "Compute the total constraint.\n\n"
116 ":param data: constraint-manager data\n"
117 ":param x: state point (dim. state.nx)\n"
118 ":param u: control input (dim. nu)")
119 .def<void (ConstraintModelManager::*)(
120 const boost::shared_ptr<ConstraintDataManager>&,
121
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 const Eigen::Ref<const Eigen::VectorXd>&)>(
122
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 "calc", &ConstraintModelManager::calc, bp::args("self", "data", "x"),
123 "Compute the total constraint value for nodes that depends only on "
124 "the state.\n\n"
125 "It updates the total constraint based on the state only.\n"
126 "This function is used in the terminal nodes of an optimal control "
127 "problem.\n"
128 ":param data: constraint-manager data\n"
129 ":param x: state point (dim. state.nx)")
130 .def<void (ConstraintModelManager::*)(
131 const boost::shared_ptr<ConstraintDataManager>&,
132 const Eigen::Ref<const Eigen::VectorXd>&,
133
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 const Eigen::Ref<const Eigen::VectorXd>&)>(
134 "calcDiff", &ConstraintModelManager::calcDiff,
135
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x", "u"),
136 "Compute the derivatives of the total constraint.\n\n"
137 "It assumes that calc has been run first.\n"
138 ":param data: constraint-manager data\n"
139 ":param x: state point (dim. state.nx)\n"
140 ":param u: control input (dim. nu)\n")
141 .def<void (ConstraintModelManager::*)(
142 const boost::shared_ptr<ConstraintDataManager>&,
143
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 const Eigen::Ref<const Eigen::VectorXd>&)>(
144 "calcDiff", &ConstraintModelManager::calcDiff,
145
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x"),
146 "Compute the Jacobian of the total constraint for nodes that depends "
147 "on the state only.\n\n"
148 "It updates the Jacobian of the total constraint based on the state "
149 "only.\n"
150 "This function is used in the terminal nodes of an optimal control "
151 "problem.\n"
152 ":param data: constraint-manager data\n"
153 ":param x: state point (dim. state.nx)")
154
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .def("createData", &ConstraintModelManager::createData,
155 bp::with_custodian_and_ward_postcall<0, 2>(),
156
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data"),
157 "Create the total constraint data.\n\n"
158 ":param data: shared data\n"
159 ":return total constraint data.")
160
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
161 "state",
162
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintModelManager::get_state,
163 10 bp::return_value_policy<bp::return_by_value>()),
164 "state description")
165
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
166 "constraints",
167
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintModelManager::get_constraints,
168 10 bp::return_value_policy<bp::return_by_value>()),
169 "stack of constraints")
170
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(&ConstraintModelManager::get_nu),
171 "dimension of control vector")
172
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .add_property("ng", bp::make_function(&ConstraintModelManager::get_ng),
173 "number of active inequality constraints")
174
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .add_property("nh", bp::make_function(&ConstraintModelManager::get_nh),
175 "number of active equality constraints")
176
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("ng_T",
177
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintModelManager::get_ng_T),
178 "number of active inequality terminal constraints")
179
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("nh_T",
180
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintModelManager::get_nh_T),
181 "number of active equality terminal constraints")
182
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
183 "active_set",
184
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintModelManager::get_active_set,
185 10 bp::return_value_policy<bp::return_by_value>()),
186 "name of the active set of constraint items")
187
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
188 "inactive_set",
189
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintModelManager::get_inactive_set,
190 10 bp::return_value_policy<bp::return_by_value>()),
191 "name of the inactive set of constraint items")
192
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("g_lb",
193
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintModelManager::get_lb,
194 10 bp::return_internal_reference<>()),
195 "lower bound of the inequality constraints")
196
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("g_ub",
197
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintModelManager::get_ub,
198 10 bp::return_internal_reference<>()),
199 "upper bound of the inequality constraints")
200
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("getConstraintStatus", &ConstraintModelManager::getConstraintStatus,
201
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "name"),
202 "Return the constraint status of a given constraint name.\n\n"
203 ":param name: constraint name")
204
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<ConstraintModelManager>())
205
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(PrintableVisitor<ConstraintModelManager>());
206
207 10 bp::register_ptr_to_python<boost::shared_ptr<ConstraintDataManager> >();
208
209
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<ConstraintDataManager>(
210 "ConstraintDataManager", "Class for total constraint data.\n\n",
211
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<ConstraintModelManager*, DataCollectorAbstract*>(
212 10 bp::args("self", "model", "data"),
213 "Create total constraint data.\n\n"
214 ":param model: total constraint model\n"
215
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 ":param data: shared data")[bp::with_custodian_and_ward<1, 3>()])
216 20 .def(
217 "shareMemory",
218 &ConstraintDataManager::shareMemory<DifferentialActionDataAbstract>,
219
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data"),
220 "Share memory with a given differential action data\n\n"
221 ":param model: differential action data that we want to share memory")
222
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("shareMemory",
223 &ConstraintDataManager::shareMemory<ActionDataAbstract>,
224
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data"),
225 "Share memory with a given action data\n\n"
226 ":param model: action data that we want to share memory")
227
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("resize", &ConstraintDataManager::resize<ConstraintModelManager>,
228 bp::with_custodian_and_ward_postcall<0, 1>(),
229
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 ConstraintDataManager_resizeConst_wrap(
230
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "model", "running_node"),
231 "Resize the data given differential action data\n\n"
232 ":param model: constraint manager model that defines how to "
233 "resize "
234 "the data\n"
235 ":param running_node: true if we are resizing for a running "
236 "node, false for terminal ones (default true)"))
237
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("resize",
238 &ConstraintDataManager::resize<DifferentialActionModelAbstract,
239 DifferentialActionDataAbstract>,
240 bp::with_custodian_and_ward_postcall<0, 2>(),
241
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 ConstraintDataManager_resize_wrap(
242
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "model", "data", "running_node"),
243 "Resize the data given differential action data\n\n"
244 ":param model: differential action model that defines how to "
245 "resize "
246 "the data\n"
247 ":param data: differential action data that we want to resize\n"
248 ":param running_node: true if we are resizing for a running "
249 "node, false for terminal ones (default true)"))
250
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(
251 "resize",
252 &ConstraintDataManager::resize<ActionModelAbstract,
253 ActionDataAbstract>,
254 bp::with_custodian_and_ward_postcall<0, 2>(),
255
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 ConstraintDataManager_resize_wrap(
256
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "model", "data", "running_node"),
257 "Resize the data given action data\n\n"
258 ":param model: action model that defines how to resize the data\n"
259 ":param data: action data that we want to resize\n"
260 ":param running_node: true if we are resizing for a running "
261 "node, false for terminal ones (default true)"))
262
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 .add_property(
263 "constraints",
264
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ConstraintDataManager::constraints,
265 10 bp::return_value_policy<bp::return_by_value>()),
266 "stack of constraints data")
267
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("shared",
268
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ConstraintDataManager::shared,
269 10 bp::return_internal_reference<>()),
270 "shared data")
271
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
272 "g",
273
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintDataManager::get_g,
274 bp::return_value_policy<bp::return_by_value>()),
275
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintDataManager::set_g),
276 "Inequality constraint residual")
277
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
278 "Gx",
279
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintDataManager::get_Gx,
280 bp::return_value_policy<bp::return_by_value>()),
281
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintDataManager::set_Gx),
282 "Jacobian of the inequality constraint")
283
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
284 "Gu",
285
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintDataManager::get_Gu,
286 bp::return_value_policy<bp::return_by_value>()),
287
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintDataManager::set_Gu),
288 "Jacobian of the inequality constraint")
289
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
290 "h",
291
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintDataManager::get_h,
292 bp::return_value_policy<bp::return_by_value>()),
293
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintDataManager::set_h),
294 "Equality constraint residual")
295
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
296 "Hx",
297
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintDataManager::get_Hx,
298 bp::return_value_policy<bp::return_by_value>()),
299
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintDataManager::set_Hx),
300 "Jacobian of the equality constraint")
301
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
302 "Hu",
303
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ConstraintDataManager::get_Hu,
304 bp::return_value_policy<bp::return_by_value>()),
305
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ConstraintDataManager::set_Hu),
306 "Jacobian of the equality constraint")
307
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<ConstraintDataManager>());
308 10 }
309
310 } // namespace python
311 } // namespace crocoddyl
312