GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/numdiff/action.cpp Lines: 49 49 100.0 %
Date: 2024-02-13 11:12:33 Branches: 36 72 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 "crocoddyl/core/numdiff/action.hpp"
11
12
#include "python/crocoddyl/core/action-base.hpp"
13
#include "python/crocoddyl/core/core.hpp"
14
#include "python/crocoddyl/utils/copyable.hpp"
15
16
namespace crocoddyl {
17
namespace python {
18
19
10
void exposeActionNumDiff() {
20
10
  bp::register_ptr_to_python<boost::shared_ptr<ActionModelNumDiff> >();
21
22
10
  bp::class_<ActionModelNumDiff, bp::bases<ActionModelAbstract> >(
23
      "ActionModelNumDiff",
24
      "Abstract class for computing calcDiff by using numerical "
25
      "differentiation.\n\n",
26
10
      bp::init<boost::shared_ptr<ActionModelAbstract>, bp::optional<bool> >(
27
20
          bp::args("self", "model", "gaussApprox"),
28
          "Initialize the action model NumDiff.\n\n"
29
          ":param model: action model where we compute the derivatives through "
30
          "NumDiff\n"
31
          ":param gaussApprox: compute the Hessian using Gauss approximation "
32
          "(default False)"))
33
      .def<void (ActionModelNumDiff::*)(
34
          const boost::shared_ptr<ActionDataAbstract>&,
35
          const Eigen::Ref<const Eigen::VectorXd>&,
36
          const Eigen::Ref<const Eigen::VectorXd>&)>(
37
20
          "calc", &ActionModelNumDiff::calc, bp::args("self", "data", "x", "u"),
38
          "Compute the next state and cost value.\n\n"
39
          "The system evolution is described in model.\n"
40
          ":param data: NumDiff action data\n"
41
          ":param x: state point (dim. state.nx)\n"
42
20
          ":param u: control input (dim. nu)")
43
      .def<void (ActionModelNumDiff::*)(
44
          const boost::shared_ptr<ActionDataAbstract>&,
45
          const Eigen::Ref<const Eigen::VectorXd>&)>(
46

20
          "calc", &ActionModelAbstract::calc, bp::args("self", "data", "x"))
47
      .def<void (ActionModelNumDiff::*)(
48
          const boost::shared_ptr<ActionDataAbstract>&,
49
          const Eigen::Ref<const Eigen::VectorXd>&,
50
          const Eigen::Ref<const Eigen::VectorXd>&)>(
51
          "calcDiff", &ActionModelNumDiff::calcDiff,
52
20
          bp::args("self", "data", "x", "u"),
53
          "Compute the derivatives of the dynamics and cost functions.\n\n"
54
          "It computes the Jacobian and Hessian using numerical "
55
          "differentiation.\n"
56
          "It assumes that calc has been run first.\n"
57
          ":param data: NumDiff action data\n"
58
          ":param x: state point (dim. state.nx)\n"
59
10
          ":param u: control input (dim. nu)")
60
      .def<void (ActionModelNumDiff::*)(
61
          const boost::shared_ptr<ActionDataAbstract>&,
62
          const Eigen::Ref<const Eigen::VectorXd>&)>(
63
          "calcDiff", &ActionModelAbstract::calcDiff,
64

20
          bp::args("self", "data", "x"))
65
20
      .def("createData", &ActionModelNumDiff::createData, bp::args("self"),
66
           "Create the action data.\n\n"
67
           "Each action model (AM) has its own data that needs to be "
68
           "allocated.\n"
69
           "This function returns the allocated data for a predefined AM.\n"
70

20
           ":return AM data.")
71
      .add_property(
72
          "model",
73
10
          bp::make_function(&ActionModelNumDiff::get_model,
74
10
                            bp::return_value_policy<bp::return_by_value>()),
75
10
          "action model")
76
      .add_property(
77
          "disturbance",
78
10
          bp::make_function(&ActionModelNumDiff::get_disturbance),
79
          &ActionModelNumDiff::set_disturbance,
80

10
          "disturbance constant used in the numerical differentiation")
81
      .add_property(
82
          "withGaussApprox",
83
10
          bp::make_function(&ActionModelNumDiff::get_with_gauss_approx,
84
10
                            bp::return_value_policy<bp::return_by_value>()),
85
10
          "Gauss approximation for computing the Hessians")
86
10
      .def(CopyableVisitor<ActionModelNumDiff>());
87
88
10
  bp::register_ptr_to_python<boost::shared_ptr<ActionDataNumDiff> >();
89
90
10
  bp::class_<ActionDataNumDiff, bp::bases<ActionDataAbstract> >(
91
      "ActionDataNumDiff", "Numerical differentiation action data.",
92
10
      bp::init<ActionModelNumDiff*>(
93
20
          bp::args("self", "model"),
94
          "Create numerical differentiation action data.\n\n"
95
          ":param model: numdiff action model"))
96
      .add_property("Rx",
97
10
                    bp::make_getter(&ActionDataNumDiff::Rx,
98
10
                                    bp::return_internal_reference<>()),
99
10
                    "Jacobian of the cost residual.")
100
      .add_property("Ru",
101
10
                    bp::make_getter(&ActionDataNumDiff::Ru,
102
10
                                    bp::return_internal_reference<>()),
103
10
                    "Jacobian of the cost residual.")
104
      .add_property("dx",
105
10
                    bp::make_getter(&ActionDataNumDiff::dx,
106
10
                                    bp::return_internal_reference<>()),
107
10
                    "state disturbance.")
108
      .add_property("du",
109
10
                    bp::make_getter(&ActionDataNumDiff::du,
110
10
                                    bp::return_internal_reference<>()),
111
10
                    "control disturbance.")
112
      .add_property(
113
          "data_0",
114
10
          bp::make_getter(&ActionDataNumDiff::data_0,
115
10
                          bp::return_value_policy<bp::return_by_value>()),
116
10
          "data that contains the final results")
117
      .add_property(
118
          "data_x",
119
10
          bp::make_getter(&ActionDataNumDiff::data_x,
120
10
                          bp::return_value_policy<bp::return_by_value>()),
121
10
          "temporary data associated with the state variation")
122
      .add_property(
123
          "data_u",
124
10
          bp::make_getter(&ActionDataNumDiff::data_u,
125
10
                          bp::return_value_policy<bp::return_by_value>()),
126
10
          "temporary data associated with the control variation")
127
10
      .def(CopyableVisitor<ActionDataNumDiff>());
128
10
}
129
130
}  // namespace python
131
}  // namespace crocoddyl