| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/core/numdiff/diff-action.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 53 | 53 | 100.0% |
| 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 | std::shared_ptr<DifferentialActionModelNumDiff> >(); | |
| 22 | |||
| 23 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<DifferentialActionModelNumDiff, |
| 24 | bp::bases<DifferentialActionModelAbstract> >( | ||
| 25 | "DifferentialActionModelNumDiff", | ||
| 26 | "Abstract class for computing calcDiff by using numerical " | ||
| 27 | "differentiation.\n\n", | ||
| 28 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<std::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 std::shared_ptr<DifferentialActionDataAbstract>&, | ||
| 38 | const Eigen::Ref<const Eigen::VectorXd>&, | ||
| 39 | 20 | const Eigen::Ref<const Eigen::VectorXd>&)>( | |
| 40 | "calc", &DifferentialActionModelNumDiff::calc, | ||
| 41 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
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 | ":param u: control input (dim. nu)") | ||
| 47 | .def<void (DifferentialActionModelNumDiff::*)( | ||
| 48 | const std::shared_ptr<DifferentialActionDataAbstract>&, | ||
| 49 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const Eigen::Ref<const Eigen::VectorXd>&)>( |
| 50 | "calc", &DifferentialActionModelAbstract::calc, | ||
| 51 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x")) |
| 52 | .def<void (DifferentialActionModelNumDiff::*)( | ||
| 53 | const std::shared_ptr<DifferentialActionDataAbstract>&, | ||
| 54 | const Eigen::Ref<const Eigen::VectorXd>&, | ||
| 55 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const Eigen::Ref<const Eigen::VectorXd>&)>( |
| 56 | "calcDiff", &DifferentialActionModelNumDiff::calcDiff, | ||
| 57 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
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 | ":param u: control input (dim. nu)") | ||
| 65 | .def<void (DifferentialActionModelNumDiff::*)( | ||
| 66 | const std::shared_ptr<DifferentialActionDataAbstract>&, | ||
| 67 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | const Eigen::Ref<const Eigen::VectorXd>&)>( |
| 68 | "calcDiff", &DifferentialActionModelAbstract::calcDiff, | ||
| 69 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::args("self", "data", "x")) |
| 70 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | .def("createData", &DifferentialActionModelNumDiff::createData, |
| 71 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
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 | ":return AM data.") | ||
| 77 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 78 | "model", | ||
| 79 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_function(&DifferentialActionModelNumDiff::get_model, |
| 80 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 81 | "action model") | ||
| 82 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 83 | "disturbance", | ||
| 84 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function(&DifferentialActionModelNumDiff::get_disturbance), |
| 85 | &DifferentialActionModelNumDiff::set_disturbance, | ||
| 86 | "disturbance constant used in the numerical differentiation") | ||
| 87 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("withGaussApprox", |
| 88 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | bp::make_function( |
| 89 | &DifferentialActionModelNumDiff::get_with_gauss_approx), | ||
| 90 | "Gauss approximation for computing the Hessians") | ||
| 91 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<DifferentialActionModelNumDiff>()); |
| 92 | |||
| 93 | 10 | bp::register_ptr_to_python<std::shared_ptr<DifferentialActionDataNumDiff> >(); | |
| 94 | |||
| 95 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::class_<DifferentialActionDataNumDiff, |
| 96 | bp::bases<DifferentialActionDataAbstract> >( | ||
| 97 | "DifferentialActionDataNumDiff", | ||
| 98 | "Numerical differentiation diff-action data.", | ||
| 99 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::init<DifferentialActionModelNumDiff*>( |
| 100 | 20 | bp::args("self", "model"), | |
| 101 | "Create numerical differentiation diff-action data.\n\n" | ||
| 102 | ":param model: numdiff diff-action model")) | ||
| 103 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Rx", |
| 104 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::Rx, |
| 105 | 10 | bp::return_internal_reference<>()), | |
| 106 | "Jacobian of the cost residual.") | ||
| 107 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("Ru", |
| 108 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::Ru, |
| 109 | 10 | bp::return_internal_reference<>()), | |
| 110 | "Jacobian of the cost residual.") | ||
| 111 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("dx", |
| 112 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::dx, |
| 113 | 10 | bp::return_internal_reference<>()), | |
| 114 | "state disturbance.") | ||
| 115 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("du", |
| 116 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::du, |
| 117 | 10 | bp::return_internal_reference<>()), | |
| 118 | "control disturbance.") | ||
| 119 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property("xp", |
| 120 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::xp, |
| 121 | 10 | bp::return_internal_reference<>()), | |
| 122 | "rate state after disturbance.") | ||
| 123 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 124 | "data_0", | ||
| 125 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::data_0, |
| 126 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 127 | "data that contains the final results") | ||
| 128 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 129 | "data_x", | ||
| 130 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::data_x, |
| 131 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 132 | "temporary data associated with the state variation") | ||
| 133 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .add_property( |
| 134 | "data_u", | ||
| 135 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | bp::make_getter(&DifferentialActionDataNumDiff::data_u, |
| 136 | 10 | bp::return_value_policy<bp::return_by_value>()), | |
| 137 | "temporary data associated with the control variation") | ||
| 138 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | .def(CopyableVisitor<DifferentialActionDataNumDiff>()); |
| 139 | 10 | } | |
| 140 | |||
| 141 | } // namespace python | ||
| 142 | } // namespace crocoddyl | ||
| 143 |