Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2021-2024, University of Edinburgh, Heriot-Watt University |
5 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
6 |
|
|
// All rights reserved. |
7 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
8 |
|
|
|
9 |
|
|
#ifndef BINDINGS_PYTHON_CROCODDYL_CORE_RESIDUAL_BASE_HPP_ |
10 |
|
|
#define BINDINGS_PYTHON_CROCODDYL_CORE_RESIDUAL_BASE_HPP_ |
11 |
|
|
|
12 |
|
|
#include "crocoddyl/core/residual-base.hpp" |
13 |
|
|
#include "crocoddyl/core/utils/exception.hpp" |
14 |
|
|
#include "python/crocoddyl/core/core.hpp" |
15 |
|
|
|
16 |
|
|
namespace crocoddyl { |
17 |
|
|
namespace python { |
18 |
|
|
|
19 |
|
|
class ResidualModelAbstract_wrap : public ResidualModelAbstract, |
20 |
|
|
public bp::wrapper<ResidualModelAbstract> { |
21 |
|
|
public: |
22 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
23 |
|
|
using ResidualModelAbstract::nu_; |
24 |
|
|
using ResidualModelAbstract::unone_; |
25 |
|
|
|
26 |
|
✗ |
ResidualModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
27 |
|
|
const std::size_t nr, const std::size_t nu, |
28 |
|
|
const bool q_dependent = true, |
29 |
|
|
const bool v_dependent = true, |
30 |
|
|
const bool u_dependent = true) |
31 |
|
✗ |
: ResidualModelAbstract(state, nr, nu, q_dependent, v_dependent, |
32 |
|
|
u_dependent), |
33 |
|
✗ |
bp::wrapper<ResidualModelAbstract>() { |
34 |
|
✗ |
unone_ = NAN * MathBase::VectorXs::Ones(nu); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
✗ |
ResidualModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
38 |
|
|
const std::size_t nr, |
39 |
|
|
const bool q_dependent = true, |
40 |
|
|
const bool v_dependent = true, |
41 |
|
|
const bool u_dependent = true) |
42 |
|
✗ |
: ResidualModelAbstract(state, nr, q_dependent, v_dependent, u_dependent), |
43 |
|
✗ |
bp::wrapper<ResidualModelAbstract>() { |
44 |
|
✗ |
unone_ = NAN * MathBase::VectorXs::Ones(nu_); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
✗ |
void calc(const boost::shared_ptr<ResidualDataAbstract>& data, |
48 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& x, |
49 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& u) { |
50 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
51 |
|
✗ |
throw_pretty( |
52 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
53 |
|
|
std::to_string(state_->get_nx()) + ")"); |
54 |
|
|
} |
55 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
56 |
|
✗ |
throw_pretty( |
57 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
58 |
|
|
std::to_string(nu_) + ")"); |
59 |
|
|
} |
60 |
|
✗ |
if (std::isnan(u.lpNorm<Eigen::Infinity>())) { |
61 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, |
62 |
|
✗ |
(Eigen::VectorXd)x); |
63 |
|
|
} else { |
64 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, |
65 |
|
✗ |
(Eigen::VectorXd)x, (Eigen::VectorXd)u); |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
|
69 |
|
✗ |
void calcDiff(const boost::shared_ptr<ResidualDataAbstract>& data, |
70 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& x, |
71 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& u) { |
72 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
73 |
|
✗ |
throw_pretty( |
74 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
75 |
|
|
std::to_string(state_->get_nx()) + ")"); |
76 |
|
|
} |
77 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
78 |
|
✗ |
throw_pretty( |
79 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
80 |
|
|
std::to_string(nu_) + ")"); |
81 |
|
|
} |
82 |
|
✗ |
if (std::isnan(u.lpNorm<Eigen::Infinity>())) { |
83 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
84 |
|
✗ |
(Eigen::VectorXd)x); |
85 |
|
|
} else { |
86 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
87 |
|
✗ |
(Eigen::VectorXd)x, (Eigen::VectorXd)u); |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
✗ |
boost::shared_ptr<ResidualDataAbstract> createData( |
92 |
|
|
DataCollectorAbstract* const data) { |
93 |
|
✗ |
enableMultithreading() = false; |
94 |
|
✗ |
if (boost::python::override createData = this->get_override("createData")) { |
95 |
|
|
return bp::call<boost::shared_ptr<ResidualDataAbstract> >( |
96 |
|
✗ |
createData.ptr(), boost::ref(data)); |
97 |
|
|
} |
98 |
|
✗ |
return ResidualModelAbstract::createData(data); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
✗ |
boost::shared_ptr<ResidualDataAbstract> default_createData( |
102 |
|
|
DataCollectorAbstract* const data) { |
103 |
|
✗ |
return this->ResidualModelAbstract::createData(data); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
✗ |
void calcCostDiff(const boost::shared_ptr<CostDataAbstract>& cdata, |
107 |
|
|
const boost::shared_ptr<ResidualDataAbstract>& rdata, |
108 |
|
|
const boost::shared_ptr<ActivationDataAbstract>& adata, |
109 |
|
|
const bool update_u = true) { |
110 |
|
✗ |
if (boost::python::override calcCostDiff = |
111 |
|
✗ |
this->get_override("calcCostDiff")) { |
112 |
|
✗ |
return bp::call<void>(calcCostDiff.ptr(), boost::ref(cdata), |
113 |
|
✗ |
boost::ref(rdata), boost::ref(adata), update_u); |
114 |
|
|
} |
115 |
|
✗ |
return ResidualModelAbstract::calcCostDiff(cdata, rdata, adata, update_u); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
✗ |
void default_calcCostDiff( |
119 |
|
|
const boost::shared_ptr<CostDataAbstract>& cdata, |
120 |
|
|
const boost::shared_ptr<ResidualDataAbstract>& rdata, |
121 |
|
|
const boost::shared_ptr<ActivationDataAbstract>& adata, |
122 |
|
|
const bool update_u) { |
123 |
|
✗ |
return this->ResidualModelAbstract::calcCostDiff(cdata, rdata, adata, |
124 |
|
✗ |
update_u); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
✗ |
void default_calcCostDiff_noupdate_u( |
128 |
|
|
const boost::shared_ptr<CostDataAbstract>& cdata, |
129 |
|
|
const boost::shared_ptr<ResidualDataAbstract>& rdata, |
130 |
|
|
const boost::shared_ptr<ActivationDataAbstract>& adata) { |
131 |
|
✗ |
return this->ResidualModelAbstract::calcCostDiff(cdata, rdata, adata); |
132 |
|
|
} |
133 |
|
|
}; |
134 |
|
|
|
135 |
|
|
} // namespace python |
136 |
|
|
} // namespace crocoddyl |
137 |
|
|
|
138 |
|
|
#endif // BINDINGS_PYTHON_CROCODDYL_CORE_RESIDUAL_BASE_HPP_ |
139 |
|
|
|