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 |
|
✗ |
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 |
|
✗ |
bp::wrapper<ActionModel>() { |
50 |
|
✗ |
unone_ = VectorXs::Constant(nu, Scalar(NAN)); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
✗ |
void calc(const std::shared_ptr<ActionData>& data, |
54 |
|
|
const Eigen::Ref<const VectorXs>& x, |
55 |
|
|
const Eigen::Ref<const VectorXs>& u) override { |
56 |
|
✗ |
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 |
|
✗ |
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 |
|
✗ |
if (std::isnan( |
67 |
|
✗ |
scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
68 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, |
69 |
|
✗ |
(VectorXs)x); |
70 |
|
|
} else { |
71 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, (VectorXs)x, |
72 |
|
✗ |
(VectorXs)u); |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
✗ |
void calcDiff(const std::shared_ptr<ActionData>& data, |
77 |
|
|
const Eigen::Ref<const VectorXs>& x, |
78 |
|
|
const Eigen::Ref<const VectorXs>& u) override { |
79 |
|
✗ |
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 |
|
✗ |
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 |
|
✗ |
if (std::isnan( |
90 |
|
✗ |
scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
91 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
92 |
|
✗ |
(VectorXs)x); |
93 |
|
|
} else { |
94 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
95 |
|
✗ |
(VectorXs)x, (VectorXs)u); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
✗ |
std::shared_ptr<ActionData> createData() override { |
100 |
|
✗ |
enableMultithreading() = false; |
101 |
|
✗ |
if (boost::python::override createData = this->get_override("createData")) { |
102 |
|
✗ |
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 |
|
✗ |
std::size_t get_ng() const override { |
136 |
|
✗ |
if (boost::python::override get_ng = this->get_override("get_ng")) { |
137 |
|
✗ |
return bp::call<std::size_t>(get_ng.ptr()); |
138 |
|
|
} |
139 |
|
✗ |
return this->ActionModel::get_ng(); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
✗ |
std::size_t default_get_ng() const { return this->ActionModel::get_ng(); } |
143 |
|
|
|
144 |
|
✗ |
std::size_t get_nh() const override { |
145 |
|
✗ |
if (boost::python::override get_nh = this->get_override("get_nh")) { |
146 |
|
✗ |
return bp::call<std::size_t>(get_nh.ptr()); |
147 |
|
|
} |
148 |
|
✗ |
return this->ActionModel::get_nh(); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
✗ |
std::size_t default_get_nh() const { return this->ActionModel::get_nh(); } |
152 |
|
|
|
153 |
|
✗ |
std::size_t get_ng_T() const override { |
154 |
|
✗ |
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 |
|
✗ |
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 |
|
✗ |
std::size_t get_nh_T() const override { |
163 |
|
✗ |
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 |
|
✗ |
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 |
|
|
|