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

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

20
          bp::args("self", "data", "x"))
70
      .def("createData", &DifferentialActionModelNumDiff::createData,
71
20
           bp::args("self"),
72
           "Create the action data.\n\n"
73
           "Each action model (AM) has its own data that needs to be "
74
           "allocated.\n"
75
           "This function returns the allocated data for a predefined AM.\n"
76

20
           ":return AM data.")
77
      .add_property(
78
          "model",
79
10
          bp::make_function(&DifferentialActionModelNumDiff::get_model,
80
10
                            bp::return_value_policy<bp::return_by_value>()),
81
10
          "action model")
82
      .add_property(
83
          "disturbance",
84
10
          bp::make_function(&DifferentialActionModelNumDiff::get_disturbance),
85
          &DifferentialActionModelNumDiff::set_disturbance,
86

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