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_DIFF_ACTION_BASE_HPP_ |
11 |
|
|
#define BINDINGS_PYTHON_CROCODDYL_CORE_DIFF_ACTION_BASE_HPP_ |
12 |
|
|
|
13 |
|
|
#include "crocoddyl/core/diff-action-base.hpp" |
14 |
|
|
#include "python/crocoddyl/core/core.hpp" |
15 |
|
|
|
16 |
|
|
namespace crocoddyl { |
17 |
|
|
namespace python { |
18 |
|
|
|
19 |
|
|
template <typename _Scalar> |
20 |
|
|
class DifferentialActionModelAbstractTpl_wrap |
21 |
|
|
: public DifferentialActionModelAbstractTpl<_Scalar>, |
22 |
|
|
public bp::wrapper<DifferentialActionModelAbstractTpl<_Scalar>> { |
23 |
|
|
public: |
24 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
25 |
|
✗ |
CROCODDYL_DERIVED_CAST(DifferentialActionModelBase, |
26 |
|
|
DifferentialActionModelAbstractTpl_wrap) |
27 |
|
|
|
28 |
|
|
typedef _Scalar Scalar; |
29 |
|
|
typedef typename ScalarSelector<Scalar>::type ScalarType; |
30 |
|
|
typedef typename crocoddyl::DifferentialActionModelAbstractTpl<Scalar> |
31 |
|
|
DifferentialActionModel; |
32 |
|
|
typedef typename crocoddyl::DifferentialActionDataAbstractTpl<Scalar> |
33 |
|
|
DifferentialActionData; |
34 |
|
|
typedef typename crocoddyl::StateAbstractTpl<Scalar> State; |
35 |
|
|
typedef typename DifferentialActionModel::VectorXs VectorXs; |
36 |
|
|
using DifferentialActionModel::ng_; |
37 |
|
|
using DifferentialActionModel::ng_T_; |
38 |
|
|
using DifferentialActionModel::nh_; |
39 |
|
|
using DifferentialActionModel::nh_T_; |
40 |
|
|
using DifferentialActionModel::nr_; |
41 |
|
|
using DifferentialActionModel::nu_; |
42 |
|
|
using DifferentialActionModel::state_; |
43 |
|
|
using DifferentialActionModel::unone_; |
44 |
|
|
|
45 |
|
✗ |
DifferentialActionModelAbstractTpl_wrap(std::shared_ptr<State> state, |
46 |
|
|
const std::size_t nu, |
47 |
|
|
const std::size_t nr = 1, |
48 |
|
|
const std::size_t ng = 0, |
49 |
|
|
const std::size_t nh = 0, |
50 |
|
|
const std::size_t ng_T = 0, |
51 |
|
|
const std::size_t nh_T = 0) |
52 |
|
|
: DifferentialActionModel(state, nu, nr, ng, nh, ng_T, nh_T), |
53 |
|
✗ |
bp::wrapper<DifferentialActionModel>() { |
54 |
|
✗ |
unone_ = VectorXs::Constant(nu_, Scalar(NAN)); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
✗ |
void calc(const std::shared_ptr<DifferentialActionData>& data, |
58 |
|
|
const Eigen::Ref<const VectorXs>& x, |
59 |
|
|
const Eigen::Ref<const VectorXs>& u) override { |
60 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
61 |
|
✗ |
throw_pretty( |
62 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
63 |
|
|
std::to_string(state_->get_nx()) + ")"); |
64 |
|
|
} |
65 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
66 |
|
✗ |
throw_pretty( |
67 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
68 |
|
|
std::to_string(nu_) + ")"); |
69 |
|
|
} |
70 |
|
✗ |
if (std::isnan( |
71 |
|
✗ |
scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
72 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, |
73 |
|
✗ |
(VectorXs)x); |
74 |
|
|
} else { |
75 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, (VectorXs)x, |
76 |
|
✗ |
(VectorXs)u); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
✗ |
void calcDiff(const std::shared_ptr<DifferentialActionData>& data, |
81 |
|
|
const Eigen::Ref<const VectorXs>& x, |
82 |
|
|
const Eigen::Ref<const VectorXs>& u) override { |
83 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
84 |
|
✗ |
throw_pretty( |
85 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
86 |
|
|
std::to_string(state_->get_nx()) + ")"); |
87 |
|
|
} |
88 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
89 |
|
✗ |
throw_pretty( |
90 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
91 |
|
|
std::to_string(nu_) + ")"); |
92 |
|
|
} |
93 |
|
✗ |
if (std::isnan( |
94 |
|
✗ |
scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
95 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
96 |
|
✗ |
(VectorXs)x); |
97 |
|
|
} else { |
98 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
99 |
|
✗ |
(VectorXs)x, (VectorXs)u); |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
✗ |
std::shared_ptr<DifferentialActionData> createData() override { |
104 |
|
✗ |
enableMultithreading() = false; |
105 |
|
✗ |
if (boost::python::override createData = this->get_override("createData")) { |
106 |
|
|
return bp::call<std::shared_ptr<DifferentialActionData>>( |
107 |
|
✗ |
createData.ptr()); |
108 |
|
|
} |
109 |
|
✗ |
return DifferentialActionModel::createData(); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
✗ |
std::shared_ptr<DifferentialActionData> default_createData() { |
113 |
|
✗ |
return this->DifferentialActionModel::createData(); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
✗ |
void quasiStatic(const std::shared_ptr<DifferentialActionData>& data, |
117 |
|
|
Eigen::Ref<VectorXs> u, const Eigen::Ref<const VectorXs>& x, |
118 |
|
|
const std::size_t maxiter, const Scalar tol) override { |
119 |
|
✗ |
if (boost::python::override quasiStatic = |
120 |
|
|
this->get_override("quasiStatic")) { |
121 |
|
✗ |
u = bp::call<VectorXs>(quasiStatic.ptr(), data, (VectorXs)x, maxiter, |
122 |
|
|
tol); |
123 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
124 |
|
✗ |
throw_pretty( |
125 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
126 |
|
|
std::to_string(nu_) + ")"); |
127 |
|
|
} |
128 |
|
✗ |
return; |
129 |
|
|
} |
130 |
|
✗ |
return DifferentialActionModel::quasiStatic(data, u, x, maxiter, tol); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
✗ |
void default_quasiStatic(const std::shared_ptr<DifferentialActionData>& data, |
134 |
|
|
Eigen::Ref<VectorXs> u, |
135 |
|
|
const Eigen::Ref<const VectorXs>& x, |
136 |
|
|
const std::size_t maxiter, const Scalar tol) { |
137 |
|
✗ |
return this->DifferentialActionModel::quasiStatic(data, u, x, maxiter, tol); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
template <typename NewScalar> |
141 |
|
✗ |
DifferentialActionModelAbstractTpl_wrap<NewScalar> cast() const { |
142 |
|
|
typedef DifferentialActionModelAbstractTpl_wrap<NewScalar> ReturnType; |
143 |
|
✗ |
ReturnType ret(state_->template cast<NewScalar>(), nr_, nu_, ng_, nh_, |
144 |
|
✗ |
ng_T_, nh_T_); |
145 |
|
✗ |
return ret; |
146 |
|
|
} |
147 |
|
|
}; |
148 |
|
|
|
149 |
|
|
} // namespace python |
150 |
|
|
} // namespace crocoddyl |
151 |
|
|
|
152 |
|
|
#endif // BINDINGS_PYTHON_CROCODDYL_CORE_DIFF_ACTION_BASE_HPP_ |
153 |
|
|
|