GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/cost-base.cpp Lines: 92 99 92.9 %
Date: 2024-02-13 11:12:33 Branches: 89 178 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 "python/crocoddyl/core/cost-base.hpp"
11
12
#include "python/crocoddyl/utils/copyable.hpp"
13
#include "python/crocoddyl/utils/deprecate.hpp"
14
#include "python/crocoddyl/utils/printable.hpp"
15
16
namespace crocoddyl {
17
namespace python {
18
19
10
void exposeCostAbstract() {
20
// TODO: Remove once the deprecated update call has been removed in a future
21
// release
22
#pragma GCC diagnostic push
23
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
24
25
10
  bp::register_ptr_to_python<boost::shared_ptr<CostModelAbstract> >();
26
27
10
  bp::class_<CostModelAbstract_wrap, boost::noncopyable>(
28
      "CostModelAbstract",
29
      "Abstract multibody cost models.\n\n"
30
      "In Crocoddyl, a cost model is defined by the scalar activation function "
31
      "a(.) and by the residual\n"
32
      " function r(.) as follows:\n"
33
      "    cost = a(r(x, u)),\n"
34
      "where the residual function depends on the state point x, which lies in "
35
      "the state manifold\n"
36
      "described with a nq-tuple, its velocity xd that belongs to the tangent "
37
      "space with nv dimension,\n"
38
      "and the control input u. The dimension of the residual vector is "
39
      "defined by nr, which belongs to\n"
40
      "the Euclidean space. On the other hand, the activation function builds "
41
      "a cost value based on the\n"
42
      "definition of the residual vector. The residual vector has to be "
43
      "specialized in a derived classes.",
44
10
      bp::init<boost::shared_ptr<StateAbstract>,
45
               boost::shared_ptr<ActivationModelAbstract>,
46
               boost::shared_ptr<ResidualModelAbstract> >(
47
20
          bp::args("self", "state", "activation", "residual"),
48
          "Initialize the cost model.\n\n"
49
          ":param state: state description\n"
50
          ":param activation: activation model\n"
51
          ":param residual: residual model"))
52
10
      .def(bp::init<boost::shared_ptr<StateAbstract>,
53
                    boost::shared_ptr<ActivationModelAbstract>, std::size_t>(
54
20
          bp::args("self", "state", "activation", "nu"),
55
          "Initialize the cost model.\n\n"
56
          ":param state: state description\n"
57
          ":param activation: activation model\n"
58
10
          ":param nu: dimension of control vector (default state.nv)"))
59
10
      .def(bp::init<boost::shared_ptr<StateAbstract>,
60
                    boost::shared_ptr<ActivationModelAbstract> >(
61
20
          bp::args("self", "state", "activation"),
62
          "Initialize the cost model.\n\n"
63
          ":param state: state description\n"
64
10
          ":param activation: activation model"))
65
10
      .def(bp::init<boost::shared_ptr<StateAbstract>,
66
                    boost::shared_ptr<ResidualModelAbstract> >(
67
20
          bp::args("self", "state", "residual"),
68
          "Initialize the cost model.\n\n"
69
          ":param state: state description\n"
70
10
          ":param residual: residual model"))
71
10
      .def(bp::init<boost::shared_ptr<StateAbstract>, std::size_t, std::size_t>(
72
20
          bp::args("self", "state", "nr", "nu"),
73
          "Initialize the cost model.\n\n"
74
          "We use ActivationModelQuad as a default activation model (i.e. "
75
          "a=0.5*||r||^2).\n"
76
          ":param state: state description\n"
77
          ":param nr: dimension of residual vector\n"
78
10
          ":param nu: dimension of control vector (default state.nv)"))
79
10
      .def(bp::init<boost::shared_ptr<StateAbstract>, std::size_t>(
80
20
          bp::args("self", "state", "nr"),
81
          "Initialize the cost model.\n\n"
82
          "We use ActivationModelQuad as a default activation model (i.e. "
83
          "a=0.5*||r||^2), and the default nu value is "
84
          "obtained from state.nv.\n"
85
          ":param state: state description\n"
86
10
          ":param nr: dimension of cost vector"))
87
      .def("calc", pure_virtual(&CostModelAbstract_wrap::calc),
88
20
           bp::args("self", "data", "x", "u"),
89
           "Compute the cost value and its residuals.\n\n"
90
           ":param data: cost data\n"
91
           ":param x: state point (dim. state.nx)\n"
92

10
           ":param u: control input (dim. nu)")
93
      .def<void (CostModelAbstract::*)(
94
          const boost::shared_ptr<CostDataAbstract>&,
95
          const Eigen::Ref<const Eigen::VectorXd>&)>(
96
20
          "calc", &CostModelAbstract::calc, bp::args("self", "data", "x"),
97
          "Compute the total cost value for nodes that depends only on the "
98
          "state.\n\n"
99
          "It updates the total cost based on the state only.\n"
100
          "This function is used in the terminal nodes of an optimal control "
101
          "problem.\n"
102
          ":param data: cost data\n"
103
20
          ":param x: state point (dim. state.nx)")
104
      .def("calcDiff", pure_virtual(&CostModelAbstract_wrap::calcDiff),
105
20
           bp::args("self", "data", "x", "u"),
106
           "Compute the derivatives of the cost function and its residuals.\n\n"
107
           "It computes the partial derivatives of the cost function.\n"
108
           "It assumes that calc has been run first.\n"
109
           ":param data: cost data\n"
110
           ":param x: state point (dim. state.nx)\n"
111

20
           ":param u: control input (dim. nu)")
112
      .def<void (CostModelAbstract::*)(
113
          const boost::shared_ptr<CostDataAbstract>&,
114
          const Eigen::Ref<const Eigen::VectorXd>&)>(
115
          "calcDiff", &CostModelAbstract::calcDiff,
116
20
          bp::args("self", "data", "x"),
117
          "Compute the Jacobian and Hessian of the cost functions with respect "
118
          "to the state only.\n\n"
119
          "It updates the Jacobian and Hessian of the cost function based on "
120
          "the state only.\n"
121
          "This function is used in the terminal nodes of an optimal control "
122
          "problem.\n"
123
          ":param data: cost data\n"
124
20
          ":param x: state point (dim. state.nx)")
125
      .def("createData", &CostModelAbstract_wrap::createData,
126
           bp::with_custodian_and_ward_postcall<0, 2>(),
127
20
           bp::args("self", "data"),
128
           "Create the cost data.\n\n"
129
           "Each cost model has its own data that needs to be allocated. This "
130
           "function\n"
131
           "returns the allocated data for a predefined cost.\n"
132
           ":param data: shared data\n"
133

20
           ":return cost data.")
134
      .def("createData", &CostModelAbstract_wrap::default_createData,
135
10
           bp::with_custodian_and_ward_postcall<0, 2>())
136
      .add_property(
137
          "state",
138
10
          bp::make_function(&CostModelAbstract_wrap::get_state,
139
10
                            bp::return_value_policy<bp::return_by_value>()),
140
10
          "state description")
141
      .add_property(
142
          "activation",
143
10
          bp::make_function(&CostModelAbstract_wrap::get_activation,
144
10
                            bp::return_value_policy<bp::return_by_value>()),
145
10
          "activation model")
146
      .add_property(
147
          "residual",
148
10
          bp::make_function(&CostModelAbstract_wrap::get_residual,
149
10
                            bp::return_value_policy<bp::return_by_value>()),
150
10
          "residual model")
151
20
      .add_property("nu", bp::make_function(&CostModelAbstract_wrap::get_nu),
152
10
                    "dimension of control vector")
153
10
      .def(PrintableVisitor<CostModelAbstract>());
154
155
10
  bp::register_ptr_to_python<boost::shared_ptr<CostDataAbstract> >();
156
157
10
  bp::class_<CostDataAbstract>(
158
      "CostDataAbstract", "Abstract class for cost data.\n\n",
159
10
      bp::init<CostModelAbstract*, DataCollectorAbstract*>(
160
10
          bp::args("self", "model", "data"),
161
          "Create common data shared between cost models.\n\n"
162
          ":param model: cost model\n"
163
10
          ":param data: shared data")[bp::with_custodian_and_ward<
164
20
          1, 2, bp::with_custodian_and_ward<1, 3> >()])
165
      .add_property("shared",
166
10
                    bp::make_getter(&CostDataAbstract::shared,
167
10
                                    bp::return_internal_reference<>()),
168
10
                    "shared data")
169
      .add_property(
170
          "activation",
171
10
          bp::make_getter(&CostDataAbstract::activation,
172
10
                          bp::return_value_policy<bp::return_by_value>()),
173
10
          "activation data")
174
      .add_property(
175
          "residual",
176
10
          bp::make_getter(&CostDataAbstract::residual,
177
10
                          bp::return_value_policy<bp::return_by_value>()),
178
10
          "residual data")
179
      .add_property(
180
          "cost",
181
10
          bp::make_getter(&CostDataAbstract::cost,
182
                          bp::return_value_policy<bp::return_by_value>()),
183

30
          bp::make_setter(&CostDataAbstract::cost), "cost value")
184
      .add_property("Lx",
185
10
                    bp::make_getter(&CostDataAbstract::Lx,
186
                                    bp::return_internal_reference<>()),
187
20
                    bp::make_setter(&CostDataAbstract::Lx),
188
10
                    "Jacobian of the cost")
189
      .add_property("Lu",
190
10
                    bp::make_getter(&CostDataAbstract::Lu,
191
                                    bp::return_internal_reference<>()),
192
20
                    bp::make_setter(&CostDataAbstract::Lu),
193
10
                    "Jacobian of the cost")
194
      .add_property("Lxx",
195
10
                    bp::make_getter(&CostDataAbstract::Lxx,
196
                                    bp::return_internal_reference<>()),
197
20
                    bp::make_setter(&CostDataAbstract::Lxx),
198
10
                    "Hessian of the cost")
199
      .add_property("Lxu",
200
10
                    bp::make_getter(&CostDataAbstract::Lxu,
201
                                    bp::return_internal_reference<>()),
202
20
                    bp::make_setter(&CostDataAbstract::Lxu),
203
10
                    "Hessian of the cost")
204
      .add_property("Luu",
205
10
                    bp::make_getter(&CostDataAbstract::Luu,
206
                                    bp::return_internal_reference<>()),
207
20
                    bp::make_setter(&CostDataAbstract::Luu),
208
10
                    "Hessian of the cost")
209
      .add_property(
210
          "r",
211
10
          bp::make_function(&CostDataAbstract::get_r,
212

20
                            deprecated<bp::return_internal_reference<> >(
213
                                "Deprecated. Use residual.r.")),
214
20
          bp::make_function(&CostDataAbstract::set_r,
215

20
                            deprecated<>("Deprecated. Use residual.r.")),
216
10
          "cost residual")
217
      .add_property(
218
          "Rx",
219
10
          bp::make_function(&CostDataAbstract::get_Rx,
220

20
                            deprecated<bp::return_internal_reference<> >(
221
                                "Deprecated. Use residual.Rx.")),
222
20
          bp::make_function(&CostDataAbstract::set_Rx,
223

20
                            deprecated<>("Deprecated. Use residual.Rx.")),
224
10
          "Jacobian of the cost residual")
225
      .add_property(
226
          "Ru",
227
10
          bp::make_function(&CostDataAbstract::get_Ru,
228

20
                            deprecated<bp::return_internal_reference<> >(
229
                                "Deprecated. Use residual.Ru.")),
230
20
          bp::make_function(&CostDataAbstract::set_Ru,
231

20
                            deprecated<>("Deprecated. Use residual.Ru.")),
232
10
          "Jacobian of the cost residual")
233
10
      .def(CopyableVisitor<CostDataAbstract>());
234
235
#pragma GCC diagnostic pop
236
10
}
237
238
}  // namespace python
239
}  // namespace crocoddyl