1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2019-2024, 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_COST_BASE_HPP_ |
11 |
|
|
#define BINDINGS_PYTHON_CROCODDYL_CORE_COST_BASE_HPP_ |
12 |
|
|
|
13 |
|
|
#include "crocoddyl/core/cost-base.hpp" |
14 |
|
|
#include "crocoddyl/core/utils/exception.hpp" |
15 |
|
|
#include "python/crocoddyl/core/core.hpp" |
16 |
|
|
|
17 |
|
|
namespace crocoddyl { |
18 |
|
|
namespace python { |
19 |
|
|
|
20 |
|
|
class CostModelAbstract_wrap : public CostModelAbstract, |
21 |
|
|
public bp::wrapper<CostModelAbstract> { |
22 |
|
|
public: |
23 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
24 |
|
|
using CostModelAbstract::nu_; |
25 |
|
|
using CostModelAbstract::unone_; |
26 |
|
|
|
27 |
|
|
CostModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
28 |
|
|
boost::shared_ptr<ActivationModelAbstract> activation, |
29 |
|
|
boost::shared_ptr<ResidualModelAbstract> residual) |
30 |
|
|
: CostModelAbstract(state, activation, residual) { |
31 |
|
|
unone_ = NAN * MathBase::VectorXs::Ones(nu_); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
1 |
CostModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
35 |
|
|
boost::shared_ptr<ActivationModelAbstract> activation, |
36 |
|
|
const std::size_t nu) |
37 |
✓✗ |
1 |
: CostModelAbstract(state, activation, nu) { |
38 |
✓✗✓✗ ✓✗ |
1 |
unone_ = NAN * MathBase::VectorXs::Ones(nu); |
39 |
|
1 |
} |
40 |
|
|
|
41 |
|
6 |
CostModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
42 |
|
|
boost::shared_ptr<ActivationModelAbstract> activation) |
43 |
✓✗ |
6 |
: CostModelAbstract(state, activation) { |
44 |
✓✗✓✗ ✓✗ |
6 |
unone_ = NAN * MathBase::VectorXs::Ones(nu_); |
45 |
|
6 |
} |
46 |
|
|
|
47 |
|
|
CostModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
48 |
|
|
boost::shared_ptr<ResidualModelAbstract> residual) |
49 |
|
|
: CostModelAbstract(state, residual) { |
50 |
|
|
unone_ = NAN * MathBase::VectorXs::Ones(nu_); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
CostModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
54 |
|
|
const std::size_t nr, const std::size_t nu) |
55 |
|
|
: CostModelAbstract(state, nr, nu), bp::wrapper<CostModelAbstract>() { |
56 |
|
|
unone_ = NAN * MathBase::VectorXs::Ones(nu); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
CostModelAbstract_wrap(boost::shared_ptr<StateAbstract> state, |
60 |
|
|
const std::size_t nr) |
61 |
|
|
: CostModelAbstract(state, nr), bp::wrapper<CostModelAbstract>() { |
62 |
|
|
unone_ = NAN * MathBase::VectorXs::Ones(nu_); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
void calc(const boost::shared_ptr<CostDataAbstract>& data, |
66 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& x, |
67 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& u) { |
68 |
|
|
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
69 |
|
|
throw_pretty("Invalid argument: " |
70 |
|
|
<< "x has wrong dimension (it should be " + |
71 |
|
|
std::to_string(state_->get_nx()) + ")"); |
72 |
|
|
} |
73 |
|
|
if (static_cast<std::size_t>(u.size()) != nu_) { |
74 |
|
|
throw_pretty("Invalid argument: " |
75 |
|
|
<< "u has wrong dimension (it should be " + |
76 |
|
|
std::to_string(nu_) + ")"); |
77 |
|
|
} |
78 |
|
|
if (std::isnan(u.lpNorm<Eigen::Infinity>())) { |
79 |
|
|
return bp::call<void>(this->get_override("calc").ptr(), data, |
80 |
|
|
(Eigen::VectorXd)x); |
81 |
|
|
} else { |
82 |
|
|
return bp::call<void>(this->get_override("calc").ptr(), data, |
83 |
|
|
(Eigen::VectorXd)x, (Eigen::VectorXd)u); |
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
void calcDiff(const boost::shared_ptr<CostDataAbstract>& data, |
88 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& x, |
89 |
|
|
const Eigen::Ref<const Eigen::VectorXd>& u) { |
90 |
|
|
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
91 |
|
|
throw_pretty("Invalid argument: " |
92 |
|
|
<< "x has wrong dimension (it should be " + |
93 |
|
|
std::to_string(state_->get_nx()) + ")"); |
94 |
|
|
} |
95 |
|
|
if (static_cast<std::size_t>(u.size()) != nu_) { |
96 |
|
|
throw_pretty("Invalid argument: " |
97 |
|
|
<< "u has wrong dimension (it should be " + |
98 |
|
|
std::to_string(nu_) + ")"); |
99 |
|
|
} |
100 |
|
|
if (std::isnan(u.lpNorm<Eigen::Infinity>())) { |
101 |
|
|
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
102 |
|
|
(Eigen::VectorXd)x); |
103 |
|
|
} else { |
104 |
|
|
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
105 |
|
|
(Eigen::VectorXd)x, (Eigen::VectorXd)u); |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
boost::shared_ptr<CostDataAbstract> createData( |
110 |
|
|
DataCollectorAbstract* const data) { |
111 |
|
|
enableMultithreading() = false; |
112 |
|
|
if (boost::python::override createData = this->get_override("createData")) { |
113 |
|
|
return bp::call<boost::shared_ptr<CostDataAbstract> >(createData.ptr(), |
114 |
|
|
boost::ref(data)); |
115 |
|
|
} |
116 |
|
|
return CostModelAbstract::createData(data); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
15 |
boost::shared_ptr<CostDataAbstract> default_createData( |
120 |
|
|
DataCollectorAbstract* const data) { |
121 |
|
15 |
return this->CostModelAbstract::createData(data); |
122 |
|
|
} |
123 |
|
|
}; |
124 |
|
|
|
125 |
|
|
} // namespace python |
126 |
|
|
} // namespace crocoddyl |
127 |
|
|
|
128 |
|
|
#endif // BINDINGS_PYTHON_CROCODDYL_MULTIBODY_COST_BASE_HPP_ |