GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/residual-base.cpp Lines: 62 66 93.9 %
Date: 2024-02-13 11:12:33 Branches: 54 108 50.0 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2021-2023, University of Edinburgh, Heriot-Watt University
5
// Copyright note valid unless otherwise stated in individual files.
6
// All rights reserved.
7
///////////////////////////////////////////////////////////////////////////////
8
9
#include "python/crocoddyl/core/residual-base.hpp"
10
11
#include "python/crocoddyl/utils/copyable.hpp"
12
#include "python/crocoddyl/utils/printable.hpp"
13
14
namespace crocoddyl {
15
namespace python {
16
17
10
void exposeResidualAbstract() {
18
10
  bp::register_ptr_to_python<boost::shared_ptr<ResidualModelAbstract> >();
19
20
10
  bp::class_<ResidualModelAbstract_wrap, boost::noncopyable>(
21
      "ResidualModelAbstract",
22
      "Abstract class for residual models.\n\n"
23
      "A residual model defines a vector function r(x,u) in R^nr, where nr "
24
      "describes its dimension in the\n"
25
      "the Euclidean space. For each residual model, we need to provide ways "
26
      "of computing the residual\n"
27
      "vector and its Jacobians. These computations are mainly carried out "
28
      "inside calc() and calcDiff(),\n"
29
      "respectively.",
30
10
      bp::init<boost::shared_ptr<StateAbstract>, std::size_t, std::size_t,
31
               bp::optional<bool, bool, bool> >(
32
20
          bp::args("self", "state", "nr", "nu", "q_dependent", "v_dependent",
33
                   "u_dependent"),
34
          "Initialize the residual model.\n\n"
35
          ":param state: state description,\n"
36
          ":param nr: dimension of the residual vector\n"
37
          ":param nu: dimension of control vector (default state.nv)\n"
38
          ":param q_dependent: define if the residual function depends on q "
39
          "(default true)\n"
40
          ":param v_dependent: define if the residual function depends on v "
41
          "(default true)\n"
42
          ":param u_dependent: define if the residual function depends on u "
43
          "(default true)"))
44
10
      .def(bp::init<boost::shared_ptr<StateAbstract>, std::size_t,
45
                    bp::optional<bool, bool, bool> >(
46
20
          bp::args("self", "state", "nr", "q_dependent", "v_dependent",
47
                   "u_dependent"),
48
          "Initialize the cost model.\n\n"
49
          ":param state: state description\n"
50
          ":param nr: dimension of the residual vector\n"
51
          ":param q_dependent: define if the residual function depends on q "
52
          "(default true)\n"
53
          ":param v_dependent: define if the residual function depends on v "
54
          "(default true)\n"
55
          ":param u_dependent: define if the residual function depends on u "
56
10
          "(default true)"))
57
      .def("calc", pure_virtual(&ResidualModelAbstract_wrap::calc),
58
20
           bp::args("self", "data", "x", "u"),
59
           "Compute the residual vector.\n\n"
60
           ":param data: residual data\n"
61
           ":param x: state point (dim. state.nx)\n"
62

10
           ":param u: control input (dim. nu)")
63
      .def<void (ResidualModelAbstract::*)(
64
          const boost::shared_ptr<ResidualDataAbstract>&,
65
          const Eigen::Ref<const Eigen::VectorXd>&)>(
66
20
          "calc", &ResidualModelAbstract::calc, bp::args("self", "data", "x"),
67
          "Compute the residual vector for nodes that depends only on the "
68
          "state.\n\n"
69
          "It updates the residual vector based on the state only.\n"
70
          "This function is used in the terminal nodes of an optimal control "
71
          "problem.\n"
72
          ":param data: residual data\n"
73
20
          ":param x: state point (dim. state.nx)")
74
      .def("calcDiff", pure_virtual(&ResidualModelAbstract_wrap::calcDiff),
75
20
           bp::args("self", "data", "x", "u"),
76
           "Compute the Jacobians of the residual function.\n\n"
77
           ":param data: residual data\n"
78
           ":param x: state point (dim. state.nx)\n"
79

20
           ":param u: control input (dim. nu)")
80
      .def<void (ResidualModelAbstract::*)(
81
          const boost::shared_ptr<ResidualDataAbstract>&,
82
          const Eigen::Ref<const Eigen::VectorXd>&)>(
83
          "calcDiff", &ResidualModelAbstract::calcDiff,
84
20
          bp::args("self", "data", "x"),
85
          "Compute the Jacobian of the residual functions with respect to the "
86
          "state only.\n\n"
87
          "It updates the Jacobian of the residual function based on the state "
88
          "only.\n"
89
          "This function is used in the terminal nodes of an optimal control "
90
          "problem.\n"
91
          ":param data: residual data\n"
92
20
          ":param x: state point (dim. state.nx)")
93
      .def("createData", &ResidualModelAbstract_wrap::createData,
94
20
           &ResidualModelAbstract_wrap::default_createData, bp::args("self"),
95
           "Create the residual data.\n\n"
96
           "Each residual model might has its own data that needs to be "
97

20
           "allocated.")
98
      .def("calcCostDiff", &ResidualModelAbstract_wrap::calcCostDiff,
99
           &ResidualModelAbstract_wrap::default_calcCostDiff,
100
20
           bp::args("self", "cdata", "rdata", "adata", "update_u"),
101
           "Compute the derivative of the cost function.\n\n"
102
           "This function assumes that the derivatives of the activation and "
103
           "residual are\n"
104
           "computed via calcDiff functions.\n"
105
           ":param cdata: cost data\n"
106
           ":param rdata: residual data\n"
107
           ":param adata: activation data\n"
108
           ":param update_u: update the derivative of the cost function w.r.t. "
109
10
           "to the control if True.")
110
      .def("calcCostDiff",
111
10
           &ResidualModelAbstract_wrap::default_calcCostDiff_noupdate_u)
112
      .add_property(
113
          "state",
114
10
          bp::make_function(&ResidualModelAbstract_wrap::get_state,
115
10
                            bp::return_value_policy<bp::return_by_value>()),
116
10
          "state")
117
      .add_property("nr",
118
20
                    bp::make_function(&ResidualModelAbstract_wrap::get_nr),
119
10
                    "dimension of residual vector")
120
      .add_property("nu",
121
20
                    bp::make_function(&ResidualModelAbstract_wrap::get_nu),
122
10
                    "dimension of control vector")
123
      .add_property(
124
          "q_dependent",
125
20
          bp::make_function(&ResidualModelAbstract_wrap::get_q_dependent),
126
10
          "flag that indicates if the residual function depends on q")
127
      .add_property(
128
          "v_dependent",
129
20
          bp::make_function(&ResidualModelAbstract_wrap::get_v_dependent),
130
10
          "flag that indicates if the residual function depends on v")
131
      .add_property(
132
          "u_dependent",
133
20
          bp::make_function(&ResidualModelAbstract_wrap::get_u_dependent),
134
10
          "flag that indicates if the residual function depends on u")
135
10
      .def(CopyableVisitor<ResidualModelAbstract_wrap>())
136
10
      .def(PrintableVisitor<ResidualModelAbstract>());
137
138
10
  bp::register_ptr_to_python<boost::shared_ptr<ResidualDataAbstract> >();
139
140
10
  bp::class_<ResidualDataAbstract>(
141
      "ResidualDataAbstract",
142
      "Abstract class for residual data.\n\n"
143
      "In crocoddyl, a residual data contains all the required information for "
144
      "processing an\n"
145
      "user-defined residual models. The residual data typically is allocated "
146
      "once and containts\n"
147
      "the residual vector and its Jacobians.",
148
10
      bp::init<ResidualModelAbstract*, DataCollectorAbstract*>(
149
10
          bp::args("self", "model", "data"),
150
          "Create common data shared between residual models.\n\n"
151
          ":param model: residual model\n"
152
10
          ":param data: shared data")[bp::with_custodian_and_ward<
153
20
          1, 2, bp::with_custodian_and_ward<1, 3> >()])
154
      .add_property("shared",
155
10
                    bp::make_getter(&ResidualDataAbstract::shared,
156
10
                                    bp::return_internal_reference<>()),
157
10
                    "shared data")
158
      .add_property("r",
159
10
                    bp::make_getter(&ResidualDataAbstract::r,
160
                                    bp::return_internal_reference<>()),
161
20
                    bp::make_setter(&ResidualDataAbstract::r),
162
10
                    "residual vector")
163
      .add_property("Rx",
164
10
                    bp::make_getter(&ResidualDataAbstract::Rx,
165
                                    bp::return_internal_reference<>()),
166
20
                    bp::make_setter(&ResidualDataAbstract::Rx),
167
10
                    "Jacobian of the residual")
168
      .add_property("Ru",
169
10
                    bp::make_getter(&ResidualDataAbstract::Ru,
170
                                    bp::return_internal_reference<>()),
171
20
                    bp::make_setter(&ResidualDataAbstract::Ru),
172
10
                    "Jacobian of the residual")
173
      .add_property("Arr_Rx",
174
10
                    bp::make_getter(&ResidualDataAbstract::Arr_Rx,
175
10
                                    bp::return_internal_reference<>()),
176
                    "Intermediate product of Arr (2nd deriv of Activation) "
177
10
                    "with Rx (deriv of residue)")
178
      .add_property("Arr_Ru",
179
10
                    bp::make_getter(&ResidualDataAbstract::Arr_Ru,
180
10
                                    bp::return_internal_reference<>()),
181
                    "Intermediate product of Arr (2nd deriv of Activation) "
182
10
                    "with Ru (deriv of residue)")
183
10
      .def(CopyableVisitor<ResidualDataAbstract>());
184
10
}
185
186
}  // namespace python
187
}  // namespace crocoddyl