GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/costs/residual.cpp Lines: 27 28 96.4 %
Date: 2024-02-13 11:12:33 Branches: 21 42 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 "crocoddyl/core/costs/residual.hpp"
10
11
#include "python/crocoddyl/core/core.hpp"
12
#include "python/crocoddyl/utils/copyable.hpp"
13
14
namespace crocoddyl {
15
namespace python {
16
17
10
void exposeCostResidual() {
18
10
  bp::register_ptr_to_python<boost::shared_ptr<CostModelResidual> >();
19
20
10
  bp::class_<CostModelResidual, bp::bases<CostModelAbstract> >(
21
      "CostModelResidual",
22
      "This cost function uses a residual vector with a Gauss-Newton "
23
      "assumption to define a cost term.",
24
10
      bp::init<boost::shared_ptr<StateAbstract>,
25
               boost::shared_ptr<ActivationModelAbstract>,
26
               boost::shared_ptr<ResidualModelAbstract> >(
27
20
          bp::args("self", "state", "activation", "residual"),
28
          "Initialize the residual cost model.\n\n"
29
          ":param state: state description\n"
30
          ":param activation: activation model\n"
31
          ":param residual: residual model"))
32
10
      .def(bp::init<boost::shared_ptr<StateAbstract>,
33
                    boost::shared_ptr<ResidualModelAbstract> >(
34
20
          bp::args("self", "state", "residual"),
35
          "Initialize the residual cost model.\n\n"
36
          ":param state: state description\n"
37
10
          ":param residual: residual model"))
38
      .def<void (CostModelResidual::*)(
39
          const boost::shared_ptr<CostDataAbstract>&,
40
          const Eigen::Ref<const Eigen::VectorXd>&,
41
          const Eigen::Ref<const Eigen::VectorXd>&)>(
42
20
          "calc", &CostModelResidual::calc, bp::args("self", "data", "x", "u"),
43
          "Compute the residual cost.\n\n"
44
          ":param data: cost residual data\n"
45
          ":param x: state point (dim. state.nx)\n"
46
20
          ":param u: control input (dim. nu)")
47
      .def<void (CostModelResidual::*)(
48
          const boost::shared_ptr<CostDataAbstract>&,
49
          const Eigen::Ref<const Eigen::VectorXd>&)>(
50
20
          "calc", &CostModelAbstract::calc, bp::args("self", "data", "x"),
51
          "Compute the residual cost based on state only.\n\n"
52
          "It updates the total cost based on the state only.\n"
53
          "This function is used in the terminal nodes of an optimal control "
54
          "problem.\n"
55
          ":param data: cost data\n"
56
10
          ":param x: state point (dim. state.nx)")
57
      .def<void (CostModelResidual::*)(
58
          const boost::shared_ptr<CostDataAbstract>&,
59
          const Eigen::Ref<const Eigen::VectorXd>&,
60
          const Eigen::Ref<const Eigen::VectorXd>&)>(
61
          "calcDiff", &CostModelResidual::calcDiff,
62
20
          bp::args("self", "data", "x", "u"),
63
          "Compute the derivatives of the residual cost.\n\n"
64
          "It assumes that calc has been run first.\n"
65
          ":param data: cost residual data\n"
66
          ":param x: state point (dim. state.nx)\n"
67
10
          ":param u: control input (dim. nu)")
68
      .def<void (CostModelResidual::*)(
69
          const boost::shared_ptr<CostDataAbstract>&,
70
          const Eigen::Ref<const Eigen::VectorXd>&)>(
71
          "calcDiff", &CostModelAbstract::calcDiff,
72
20
          bp::args("self", "data", "x"),
73
          "Compute the derivatives of the residual cost with respect to the "
74
          "state only.\n\n"
75
          "It updates the Jacobian and Hessian of the cost function based on "
76
          "the state only.\n"
77
          "This function is used in the terminal nodes of an optimal control "
78
          "problem.\n"
79
          ":param data: cost residual data\n"
80
10
          ":param x: state point (dim. state.nx)")
81
      .def("createData", &CostModelResidual::createData,
82
           bp::with_custodian_and_ward_postcall<0, 2>(),
83
20
           bp::args("self", "data"),
84
           "Create the residual cost data.\n\n"
85
           "Each cost model has its own data that needs to be allocated. This "
86
           "function\n"
87
           "returns the allocated data for a predefined cost.\n"
88
           ":param data: shared data\n"
89

20
           ":return cost data.")
90
10
      .def(CopyableVisitor<CostModelResidual>());
91
92
10
  bp::register_ptr_to_python<boost::shared_ptr<CostDataResidual> >();
93
94
10
  bp::class_<CostDataResidual, bp::bases<CostDataAbstract> >(
95
      "CostDataResidual", "Data for residual cost.\n\n",
96
10
      bp::init<CostModelResidual*, DataCollectorAbstract*>(
97
10
          bp::args("self", "model", "data"),
98
          "Create residual cost data.\n\n"
99
          ":param model: residual cost model\n"
100
10
          ":param data: shared data")[bp::with_custodian_and_ward<
101
20
          1, 2, bp::with_custodian_and_ward<1, 3> >()])
102
10
      .def(CopyableVisitor<CostDataResidual>());
103
10
}
104
105
}  // namespace python
106
}  // namespace crocoddyl