Directory: | ./ |
---|---|
File: | bindings/python/crocoddyl/core/action-base.hpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 37 | 66 | 56.1% |
Branches: | 42 | 194 | 21.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2025, 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 | #ifndef BINDINGS_PYTHON_CROCODDYL_CORE_ACTION_BASE_HPP_ | ||
11 | #define BINDINGS_PYTHON_CROCODDYL_CORE_ACTION_BASE_HPP_ | ||
12 | |||
13 | #include "crocoddyl/core/action-base.hpp" | ||
14 | #include "python/crocoddyl/core/core.hpp" | ||
15 | |||
16 | namespace crocoddyl { | ||
17 | namespace python { | ||
18 | |||
19 | template <typename _Scalar> | ||
20 | class ActionModelAbstractTpl_wrap | ||
21 | : public ActionModelAbstractTpl<_Scalar>, | ||
22 | public bp::wrapper<ActionModelAbstractTpl<_Scalar>> { | ||
23 | public: | ||
24 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
25 | ✗ | CROCODDYL_DERIVED_CAST(ActionModelBase, ActionModelAbstractTpl_wrap) | |
26 | |||
27 | typedef _Scalar Scalar; | ||
28 | typedef typename ScalarSelector<Scalar>::type ScalarType; | ||
29 | typedef typename crocoddyl::ActionModelAbstractTpl<Scalar> ActionModel; | ||
30 | typedef typename crocoddyl::ActionDataAbstractTpl<Scalar> ActionData; | ||
31 | typedef typename crocoddyl::StateAbstractTpl<Scalar> State; | ||
32 | typedef typename ActionModel::VectorXs VectorXs; | ||
33 | using ActionModel::ng_; | ||
34 | using ActionModel::ng_T_; | ||
35 | using ActionModel::nh_; | ||
36 | using ActionModel::nh_T_; | ||
37 | using ActionModel::nr_; | ||
38 | using ActionModel::nu_; | ||
39 | using ActionModel::state_; | ||
40 | using ActionModel::unone_; | ||
41 | |||
42 | 16 | ActionModelAbstractTpl_wrap(std::shared_ptr<State> state, | |
43 | const std::size_t nu, const std::size_t nr = 1, | ||
44 | const std::size_t ng = 0, | ||
45 | const std::size_t nh = 0, | ||
46 | const std::size_t ng_T = 0, | ||
47 | const std::size_t nh_T = 0) | ||
48 | : ActionModel(state, nu, nr, ng, nh, ng_T, nh_T), | ||
49 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
16 | bp::wrapper<ActionModel>() { |
50 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | unone_ = VectorXs::Constant(nu, Scalar(NAN)); |
51 | } | ||
52 | |||
53 | 360 | void calc(const std::shared_ptr<ActionData>& data, | |
54 | const Eigen::Ref<const VectorXs>& x, | ||
55 | const Eigen::Ref<const VectorXs>& u) override { | ||
56 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 180 times.
|
360 | if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
57 | ✗ | throw_pretty( | |
58 | "Invalid argument: " << "x has wrong dimension (it should be " + | ||
59 | std::to_string(state_->get_nx()) + ")"); | ||
60 | } | ||
61 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
|
360 | if (static_cast<std::size_t>(u.size()) != nu_) { |
62 | ✗ | throw_pretty( | |
63 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
64 | std::to_string(nu_) + ")"); | ||
65 | } | ||
66 | 360 | if (std::isnan( | |
67 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 177 times.
|
720 | scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
68 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
6 | return bp::call<void>(this->get_override("calc").ptr(), data, |
69 | 12 | (VectorXs)x); | |
70 | } else { | ||
71 |
3/6✓ Branch 1 taken 177 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 177 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 177 times.
✗ Branch 9 not taken.
|
354 | return bp::call<void>(this->get_override("calc").ptr(), data, (VectorXs)x, |
72 | 708 | (VectorXs)u); | |
73 | } | ||
74 | } | ||
75 | |||
76 | 112 | void calcDiff(const std::shared_ptr<ActionData>& data, | |
77 | const Eigen::Ref<const VectorXs>& x, | ||
78 | const Eigen::Ref<const VectorXs>& u) override { | ||
79 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
|
112 | if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
80 | ✗ | throw_pretty( | |
81 | "Invalid argument: " << "x has wrong dimension (it should be " + | ||
82 | std::to_string(state_->get_nx()) + ")"); | ||
83 | } | ||
84 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
|
112 | if (static_cast<std::size_t>(u.size()) != nu_) { |
85 | ✗ | throw_pretty( | |
86 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
87 | std::to_string(nu_) + ")"); | ||
88 | } | ||
89 | 112 | if (std::isnan( | |
90 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 55 times.
|
224 | scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
91 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
2 | return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
92 | 4 | (VectorXs)x); | |
93 | } else { | ||
94 |
2/4✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 55 times.
✗ Branch 6 not taken.
|
110 | return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
95 |
1/2✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
220 | (VectorXs)x, (VectorXs)u); |
96 | } | ||
97 | } | ||
98 | |||
99 | 444 | std::shared_ptr<ActionData> createData() override { | |
100 | 444 | enableMultithreading() = false; | |
101 |
4/8✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 222 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 222 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 222 times.
|
888 | if (boost::python::override createData = this->get_override("createData")) { |
102 |
1/2✓ Branch 2 taken 222 times.
✗ Branch 3 not taken.
|
444 | return bp::call<std::shared_ptr<ActionData>>(createData.ptr()); |
103 | } | ||
104 | ✗ | return ActionModel::createData(); | |
105 | } | ||
106 | |||
107 | ✗ | std::shared_ptr<ActionData> default_createData() { | |
108 | ✗ | return this->ActionModel::createData(); | |
109 | } | ||
110 | |||
111 | ✗ | void quasiStatic(const std::shared_ptr<ActionData>& data, | |
112 | Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x, | ||
113 | const std::size_t maxiter, const Scalar tol) override { | ||
114 | ✗ | if (boost::python::override quasiStatic = | |
115 | this->get_override("quasiStatic")) { | ||
116 | ✗ | u = bp::call<VectorXs>(quasiStatic.ptr(), data, (VectorXs)x, maxiter, | |
117 | tol); | ||
118 | ✗ | if (static_cast<std::size_t>(u.size()) != nu_) { | |
119 | ✗ | throw_pretty( | |
120 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
121 | std::to_string(nu_) + ")"); | ||
122 | } | ||
123 | ✗ | return; | |
124 | } | ||
125 | ✗ | return ActionModel::quasiStatic(data, u, x, maxiter, tol); | |
126 | } | ||
127 | |||
128 | ✗ | void default_quasiStatic(const std::shared_ptr<ActionData>& data, | |
129 | Eigen::Ref<VectorXs> u, | ||
130 | const Eigen::Ref<const VectorXs>& x, | ||
131 | const std::size_t maxiter, const Scalar tol) { | ||
132 | ✗ | return this->ActionModel::quasiStatic(data, u, x, maxiter, tol); | |
133 | } | ||
134 | |||
135 | 1500 | std::size_t get_ng() const override { | |
136 |
4/8✓ Branch 1 taken 750 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 750 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 750 times.
✓ Branch 9 taken 750 times.
✗ Branch 10 not taken.
|
1500 | if (boost::python::override get_ng = this->get_override("get_ng")) { |
137 | ✗ | return bp::call<std::size_t>(get_ng.ptr()); | |
138 | } | ||
139 | 1500 | return this->ActionModel::get_ng(); | |
140 | } | ||
141 | |||
142 | ✗ | std::size_t default_get_ng() const { return this->ActionModel::get_ng(); } | |
143 | |||
144 | 1500 | std::size_t get_nh() const override { | |
145 |
4/8✓ Branch 1 taken 750 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 750 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 750 times.
✓ Branch 9 taken 750 times.
✗ Branch 10 not taken.
|
1500 | if (boost::python::override get_nh = this->get_override("get_nh")) { |
146 | ✗ | return bp::call<std::size_t>(get_nh.ptr()); | |
147 | } | ||
148 | 1500 | return this->ActionModel::get_nh(); | |
149 | } | ||
150 | |||
151 | ✗ | std::size_t default_get_nh() const { return this->ActionModel::get_nh(); } | |
152 | |||
153 | 3000 | std::size_t get_ng_T() const override { | |
154 |
4/8✓ Branch 1 taken 1500 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1500 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1500 times.
✓ Branch 9 taken 1500 times.
✗ Branch 10 not taken.
|
3000 | if (boost::python::override get_ng_T = this->get_override("get_ng_T")) { |
155 | ✗ | return bp::call<std::size_t>(get_ng_T.ptr()); | |
156 | } | ||
157 | 3000 | return this->ActionModel::get_ng_T(); | |
158 | } | ||
159 | |||
160 | ✗ | std::size_t default_get_ng_T() const { return this->ActionModel::get_ng_T(); } | |
161 | |||
162 | 3000 | std::size_t get_nh_T() const override { | |
163 |
4/8✓ Branch 1 taken 1500 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1500 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1500 times.
✓ Branch 9 taken 1500 times.
✗ Branch 10 not taken.
|
3000 | if (boost::python::override get_nh_T = this->get_override("get_nh_T")) { |
164 | ✗ | return bp::call<std::size_t>(get_nh_T.ptr()); | |
165 | } | ||
166 | 3000 | return this->ActionModel::get_nh_T(); | |
167 | } | ||
168 | |||
169 | ✗ | std::size_t default_get_nh_T() const { return this->ActionModel::get_nh_T(); } | |
170 | |||
171 | template <typename NewScalar> | ||
172 | ✗ | ActionModelAbstractTpl_wrap<NewScalar> cast() const { | |
173 | typedef ActionModelAbstractTpl_wrap<NewScalar> ReturnType; | ||
174 | ✗ | ReturnType ret(state_->template cast<NewScalar>(), nu_, nr_, ng_, nh_, | |
175 | ✗ | ng_T_, nh_T_); | |
176 | ✗ | return ret; | |
177 | } | ||
178 | }; | ||
179 | |||
180 | } // namespace python | ||
181 | } // namespace crocoddyl | ||
182 | |||
183 | #endif // BINDINGS_PYTHON_CROCODDYL_CORE_ACTION_BASE_HPP_ | ||
184 |