GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/action-base.cpp
Date: 2025-01-16 08:47:40
Exec Total Coverage
Lines: 113 129 87.6%
Branches: 106 212 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2024, LAAS-CNRS, University of Edinburgh,
5 // University of Oxford, Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "python/crocoddyl/core/action-base.hpp"
11
12 #include "python/crocoddyl/utils/copyable.hpp"
13 #include "python/crocoddyl/utils/printable.hpp"
14 #include "python/crocoddyl/utils/vector-converter.hpp"
15
16 namespace crocoddyl {
17 namespace python {
18
19 10 void exposeActionAbstract() {
20 // Register custom converters between std::vector and Python list
21 typedef boost::shared_ptr<ActionModelAbstract> ActionModelPtr;
22 typedef boost::shared_ptr<ActionDataAbstract> ActionDataPtr;
23
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 StdVectorPythonVisitor<std::vector<ActionModelPtr>, true>::expose(
24 "StdVec_ActionModel");
25
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 StdVectorPythonVisitor<std::vector<ActionDataPtr>, true>::expose(
26 "StdVec_ActionData");
27
28 10 bp::register_ptr_to_python<boost::shared_ptr<ActionModelAbstract> >();
29
30
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<ActionModelAbstract_wrap, boost::noncopyable>(
31 "ActionModelAbstract",
32 "Abstract class for action models.\n\n"
33 "An action model combines dynamics and cost data. Each node, in our "
34 "optimal control\n"
35 "problem, is described through an action model. Every time that we want "
36 "to describe\n"
37 "a problem, we need to provide ways of computing the dynamics, cost "
38 "functions and their\n"
39 "derivatives. These computations are mainly carried out inside calc() "
40 "and calcDiff(),\n"
41 "respectively.",
42
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<boost::shared_ptr<StateAbstract>, std::size_t,
43 bp::optional<std::size_t, std::size_t, std::size_t, std::size_t,
44 std::size_t> >(
45 20 bp::args("self", "state", "nu", "nr", "ng", "nh", "ng_T", "nh_T"),
46 "Initialize the action model.\n\n"
47 "We can also describe autonomous systems by setting nu = 0.\n"
48 ":param state: state description,\n"
49 ":param nu: dimension of control vector,\n"
50 ":param nr: dimension of the cost-residual vector (default 1)\n"
51 ":param ng: number of inequality constraints (default 0)\n"
52 ":param nh: number of equality constraints (default 0)\n"
53 ":param ng_T: number of inequality terminal constraints (default 0)\n"
54 ":param nh_T: number of equality terminal constraints (default 0)"))
55
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(&ActionModelAbstract_wrap::calc),
56
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x", "u"),
57 "Compute the next state and cost value.\n\n"
58 "It describes the time-discrete evolution of our dynamical system\n"
59 "in which we obtain the next state. Additionally it computes the\n"
60 "cost value associated to this discrete state and control pair.\n"
61 ":param data: action data\n"
62 ":param x: state point (dim. state.nx)\n"
63 ":param u: control input (dim. nu)")
64 .def<void (ActionModelAbstract::*)(
65 const boost::shared_ptr<ActionDataAbstract>&,
66 20 const Eigen::Ref<const Eigen::VectorXd>&)>(
67
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 "calc", &ActionModelAbstract::calc, bp::args("self", "data", "x"),
68 "Compute the total cost value for nodes that depends only on the "
69 "state.\n\n"
70 "It updates the total cost and the next state is not computed as it "
71 "is not expected to change.\n"
72 "This function is used in the terminal nodes of an optimal control "
73 "problem.\n"
74 ":param data: action data\n"
75 ":param x: state point (dim. state.nx)")
76
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(&ActionModelAbstract_wrap::calcDiff),
77
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x", "u"),
78 "Compute the derivatives of the dynamics and cost functions.\n\n"
79 "It computes the partial derivatives of the dynamical system and "
80 "the\n"
81 "cost function. It assumes that calc has been run first.\n"
82 "This function builds a quadratic approximation of the\n"
83 "action model (i.e. linear dynamics and quadratic cost).\n"
84 ":param data: action data\n"
85 ":param x: state point (dim. state.nx)\n"
86 ":param u: control input (dim. nu)")
87 .def<void (ActionModelAbstract::*)(
88 const boost::shared_ptr<ActionDataAbstract>&,
89 20 const Eigen::Ref<const Eigen::VectorXd>&)>(
90 "calcDiff", &ActionModelAbstract::calcDiff,
91
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x"),
92 "Compute the derivatives of the cost functions with respect to the "
93 "state only.\n\n"
94 "It updates the derivatives of the cost function with respect to the "
95 "state only.\n"
96 "This function is used in the terminal nodes of an optimal control "
97 "problem.\n"
98 ":param data: action data\n"
99 ":param x: state point (dim. state.nx)")
100
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("createData", &ActionModelAbstract_wrap::createData,
101
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 &ActionModelAbstract_wrap::default_createData, bp::args("self"),
102 "Create the action data.\n\n"
103 "Each action model (AM) has its own data that needs to be "
104 "allocated.\n"
105 "This function returns the allocated data for a predefined AM.\n"
106 ":return AM data.")
107
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def("quasiStatic", &ActionModelAbstract_wrap::quasiStatic_x,
108
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 ActionModel_quasiStatic_wraps(
109
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "x", "maxiter", "tol"),
110 "Compute the quasic-static control given a state.\n\n"
111 "It runs an iterative Newton step in order to compute the "
112 "quasic-static regime\n"
113 "given a state configuration.\n"
114 ":param data: action data\n"
115 ":param x: discrete-time state vector\n"
116 ":param maxiter: maximum allowed number of iterations\n"
117 ":param tol: stopping tolerance criteria (default 1e-9)\n"
118 ":return u: quasic-static control"))
119 20 .def("quasiStatic", &ActionModelAbstract_wrap::quasiStatic,
120
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 &ActionModelAbstract_wrap::default_quasiStatic,
121
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::args("self", "data", "u", "x", "maxiter", "tol"))
122
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
10 .add_property("nu", bp::make_function(&ActionModelAbstract_wrap::get_nu),
123
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionModelAbstract_wrap::nu_,
124 10 bp::return_internal_reference<>()),
125 "dimension of control vector")
126
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(&ActionModelAbstract_wrap::get_nr),
127 "dimension of cost-residual vector")
128
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
10 .add_property("ng", bp::make_function(&ActionModelAbstract_wrap::get_ng),
129
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionModelAbstract_wrap::ng_,
130 10 bp::return_internal_reference<>()),
131 "number of inequality constraints")
132
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
10 .add_property("nh", bp::make_function(&ActionModelAbstract_wrap::get_nh),
133
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionModelAbstract_wrap::nh_,
134 10 bp::return_internal_reference<>()),
135 "number of equality constraints")
136
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("ng_T",
137
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_ng_T),
138
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionModelAbstract_wrap::ng_T_,
139 10 bp::return_internal_reference<>()),
140 "number of inequality terminal constraints")
141
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("nh_T",
142
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_nh_T),
143
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionModelAbstract_wrap::nh_T_,
144 10 bp::return_internal_reference<>()),
145 "number of equality terminal constraints")
146
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
147 "state",
148
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_state,
149 10 bp::return_value_policy<bp::return_by_value>()),
150 "state")
151
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("g_lb",
152
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_g_lb,
153 10 bp::return_internal_reference<>()),
154 &ActionModelAbstract_wrap::set_g_lb,
155 "lower bound of the inequality constraints")
156
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("g_ub",
157
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_g_ub,
158 10 bp::return_internal_reference<>()),
159 &ActionModelAbstract_wrap::set_g_ub,
160 "upper bound of the inequality constraints")
161
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("u_lb",
162
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_u_lb,
163 10 bp::return_internal_reference<>()),
164 &ActionModelAbstract_wrap::set_u_lb, "lower control limits")
165
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("u_ub",
166
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_function(&ActionModelAbstract_wrap::get_u_ub,
167 10 bp::return_internal_reference<>()),
168 &ActionModelAbstract_wrap::set_u_ub, "upper control limits")
169
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
170 "has_control_limits",
171
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_function(&ActionModelAbstract_wrap::get_has_control_limits),
172 "indicates whether problem has finite control limits")
173
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(PrintableVisitor<ActionModelAbstract>());
174
175 10 bp::register_ptr_to_python<boost::shared_ptr<ActionDataAbstract> >();
176
177
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::class_<ActionDataAbstract>(
178 "ActionDataAbstract",
179 "Abstract class for action data.\n\n"
180 "In crocoddyl, an action data contains all the required information for "
181 "processing an\n"
182 "user-defined action model. The action data typically is allocated onces "
183 "by running\n"
184 "model.createData() and contains the first- and second- order "
185 "derivatives of the dynamics\n"
186 "and cost function, respectively.",
187
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::init<ActionModelAbstract*>(
188 20 bp::args("self", "model"),
189 "Create common data shared between AMs.\n\n"
190 "The action data uses the model in order to first process it.\n"
191 ":param model: action model"))
192
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property(
193 "cost",
194
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::cost,
195 bp::return_value_policy<bp::return_by_value>()),
196
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::cost), "cost value")
197
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("xnext",
198
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::xnext,
199 bp::return_internal_reference<>()),
200
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::xnext), "next state")
201
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("r",
202
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::r,
203 bp::return_internal_reference<>()),
204
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::r), "cost residual")
205
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Fx",
206
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Fx,
207 bp::return_internal_reference<>()),
208
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Fx),
209 "Jacobian of the dynamics w.r.t. the state")
210
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Fu",
211
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Fu,
212 bp::return_internal_reference<>()),
213
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Fu),
214 "Jacobian of the dynamics w.r.t. the control")
215
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Lx",
216
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Lx,
217 bp::return_internal_reference<>()),
218
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Lx),
219 "Jacobian of the cost w.r.t. the state")
220
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Lu",
221
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Lu,
222 bp::return_internal_reference<>()),
223
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Lu),
224 "Jacobian of the cost")
225
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Lxx",
226
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Lxx,
227 bp::return_internal_reference<>()),
228
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Lxx),
229 "Hessian of the cost w.r.t. the state")
230
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Lxu",
231
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Lxu,
232 bp::return_internal_reference<>()),
233
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Lxu),
234 "Hessian of the cost w.r.t. the state and control")
235
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Luu",
236
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Luu,
237 bp::return_internal_reference<>()),
238
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Luu),
239 "Hessian of the cost w.r.t. the control")
240
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("g",
241
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::g,
242 bp::return_internal_reference<>()),
243
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::g),
244 "inequality constraint values")
245
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Gx",
246
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Gx,
247 bp::return_internal_reference<>()),
248
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Gx),
249 "Jacobian of the inequality constraint w.r.t. the state")
250
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Gu",
251
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Gu,
252 bp::return_internal_reference<>()),
253
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Gu),
254 "Jacobian of the inequality constraint w.r.t. the control")
255
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("h",
256
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::h,
257 bp::return_internal_reference<>()),
258
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::h),
259 "equality constraint values")
260
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Hx",
261
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Hx,
262 bp::return_internal_reference<>()),
263
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Hx),
264 "Jacobian of the equality constraint w.r.t. the state")
265
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .add_property("Hu",
266
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 bp::make_getter(&ActionDataAbstract::Hu,
267 bp::return_internal_reference<>()),
268
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
20 bp::make_setter(&ActionDataAbstract::Hu),
269 "Jacobian of the equality constraint w.r.t. the control")
270
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 .def(CopyableVisitor<ActionDataAbstract>());
271 10 }
272
273 } // namespace python
274 } // namespace crocoddyl
275