Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/actuation/squashing-base.cpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 21 | 21 | 100.0% |
Branches: | 47 | 94 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2025, University of Edinburgh, IRI: CSIC-UPC, | ||
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/actuation/squashing-base.hpp" | ||
11 | |||
12 | namespace crocoddyl { | ||
13 | namespace python { | ||
14 | |||
15 | template <typename Model> | ||
16 | struct SquashingModelAbstractVisitor | ||
17 | : public bp::def_visitor<SquashingModelAbstractVisitor<Model>> { | ||
18 | template <class PyClass> | ||
19 | 40 | void visit(PyClass& cl) const { | |
20 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
40 | cl.def("calc", pure_virtual(&Model::calc), bp::args("self", "data", "s"), |
21 | "Compute the squashing value for a given value of u, " | ||
22 | "component-wise. \n\n" | ||
23 | ":param data: squashing data\n" | ||
24 | ":param s: squashing input") | ||
25 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .def("calcDiff", pure_virtual(&Model::calcDiff), |
26 | bp::args("self", "data", "s"), | ||
27 | "Compute the derivative of the squashing function.\n\n" | ||
28 | "It assumes that calc has been run first.\n" | ||
29 | ":param data: squashing data\n" | ||
30 | ":param u: squashing input") | ||
31 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, bp::args("self"), |
32 | "Create the squashing data.\n\n") | ||
33 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
40 | .add_property( |
34 | "ns", | ||
35 | bp::make_function(&Model::get_ns, | ||
36 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
37 | "dimension of the squashing vector") | ||
38 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .add_property( |
39 | "s_lb", | ||
40 | bp::make_function(&Model::get_s_lb, | ||
41 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
42 | bp::make_function(&Model::set_s_lb), | ||
43 | "lower bound for the active zone of the squashing function") | ||
44 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .add_property( |
45 | "s_ub", | ||
46 | bp::make_function(&Model::get_s_ub, | ||
47 | 40 | bp::return_value_policy<bp::return_by_value>()), | |
48 | bp::make_function(&Model::set_s_ub), | ||
49 | "upper bound for the active zone of the squashing function"); | ||
50 | 40 | } | |
51 | }; | ||
52 | |||
53 | template <typename Data> | ||
54 | struct SquashingDataAbstractVisitor | ||
55 | : public bp::def_visitor<SquashingDataAbstractVisitor<Data>> { | ||
56 | template <class PyClass> | ||
57 | 40 | void visit(PyClass& cl) const { | |
58 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
40 | cl.add_property( |
59 | 40 | "u", bp::make_getter(&Data::u, bp::return_internal_reference<>()), | |
60 | bp::make_setter(&Data::u), "squashing-output") | ||
61 |
3/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
|
80 | .add_property( |
62 | "du_ds", | ||
63 | 40 | bp::make_getter(&Data::du_ds, bp::return_internal_reference<>()), | |
64 | bp::make_setter(&Data::du_ds), | ||
65 | "Jacobian of the squashing function"); | ||
66 | 40 | } | |
67 | }; | ||
68 | |||
69 | #define CROCODDYL_SQUASHING_MODEL_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
70 | typedef SquashingModelAbstractTpl<Scalar> Model; \ | ||
71 | typedef SquashingModelAbstractTpl_wrap<Scalar> Model_wrap; \ | ||
72 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
73 | bp::class_<Model_wrap, boost::noncopyable>( \ | ||
74 | "SquashingModelAbstract", \ | ||
75 | "Abstract class for squashing functions.\n\n" \ | ||
76 | "A squashing function is any sigmoid function that maps from R to a " \ | ||
77 | "bounded domain Its input can be any value and its output will be a " \ | ||
78 | "value between a lower bound and an upper bound. The computation of " \ | ||
79 | "the output value is done using calc() while its derivative is " \ | ||
80 | "computed using calcDiff(), respectively.", \ | ||
81 | bp::init<std::size_t>(bp::args("self", "ns"), \ | ||
82 | "Initialize the squashing model.\n\n" \ | ||
83 | ":param ns: dimension of the input vector")) \ | ||
84 | .def(SquashingModelAbstractVisitor<Model_wrap>()) \ | ||
85 | .def(CopyableVisitor<Model_wrap>()); | ||
86 | |||
87 | #define CROCODDYL_SQUASHING_DATA_ABSTRACT_PYTHON_BINDINGS(Scalar) \ | ||
88 | typedef SquashingDataAbstractTpl<Scalar> Data; \ | ||
89 | typedef SquashingModelAbstractTpl<Scalar> Model; \ | ||
90 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
91 | bp::class_<Data>( \ | ||
92 | "SquashingDataAbstract", \ | ||
93 | "Abstract class for squashing datas.\n\n" \ | ||
94 | "In crocoddyl, an squashing data contains all the required information " \ | ||
95 | "for processing a user-defined squashing model. The squashing data is " \ | ||
96 | "typically allocated once per running via model.createData().", \ | ||
97 | bp::init<Model*>( \ | ||
98 | bp::args("self", "model"), \ | ||
99 | "Create common data shared between squashing models.\n\n" \ | ||
100 | "The squashing data uses the model in order to first process it.\n" \ | ||
101 | ":param model: squashing model")) \ | ||
102 | .def(SquashingDataAbstractVisitor<Data>()) \ | ||
103 | .def(CopyableVisitor<Data>()); | ||
104 | |||
105 | 10 | void exposeSquashingAbstract() { | |
106 |
13/26✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 29 taken 10 times.
✗ Branch 30 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_SQUASHING_MODEL_ABSTRACT_PYTHON_BINDINGS) |
107 |
13/26✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 29 taken 10 times.
✗ Branch 30 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
|
20 | CROCODDYL_PYTHON_SCALARS(CROCODDYL_SQUASHING_DATA_ABSTRACT_PYTHON_BINDINGS) |
108 | 10 | } | |
109 | |||
110 | } // namespace python | ||
111 | } // namespace crocoddyl | ||
112 |