9 #ifndef CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_BARRIER_HPP_ 10 #define CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_BARRIER_HPP_ 15 #include "crocoddyl/core/fwd.hpp" 16 #include "crocoddyl/core/utils/exception.hpp" 17 #include "crocoddyl/core/activation-base.hpp" 21 template <
typename _Scalar>
23 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 typedef _Scalar Scalar;
27 typedef typename MathBase::VectorXs VectorXs;
28 typedef typename MathBase::MatrixXs MatrixXs;
30 ActivationBoundsTpl(
const VectorXs& lower,
const VectorXs& upper,
const Scalar& b = (Scalar)1.)
31 : lb(lower), ub(upper), beta(b) {
32 if (lb.size() != ub.size()) {
33 throw_pretty(
"Invalid argument: " 34 <<
"The lower and upper bounds don't have the same dimension (lb,ub dimensions equal to " +
35 std::to_string(lb.size()) +
"," + std::to_string(ub.size()) +
", respectively)");
37 if (beta < 0 || beta > 1.) {
38 throw_pretty(
"Invalid argument: " 39 <<
"The range of beta is between 0 and 1");
41 for (std::size_t i = 0; i < static_cast<std::size_t>(lb.size()); ++i) {
42 if (std::isfinite(lb(i)) && std::isfinite(ub(i))) {
43 if (lb(i) - ub(i) > 0) {
44 throw_pretty(
"Invalid argument: " 45 <<
"The lower and upper bounds are badly defined; ub has to be bigger / equals to lb");
50 if (beta >= 0 && beta <= 1.) {
51 VectorXs m = (Scalar)0.5 * (lower + upper);
52 VectorXs d = (Scalar)0.5 * (upper - lower);
67 template <
typename _Scalar>
70 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
72 typedef _Scalar Scalar;
78 typedef typename MathBase::VectorXs VectorXs;
79 typedef typename MathBase::MatrixXs MatrixXs;
82 : Base(bounds.lb.size()), bounds_(bounds){};
85 virtual void calc(
const boost::shared_ptr<ActivationDataAbstract>& data,
const Eigen::Ref<const VectorXs>& r) {
86 if (static_cast<std::size_t>(r.size()) != nr_) {
87 throw_pretty(
"Invalid argument: " 88 <<
"r has wrong dimension (it should be " + std::to_string(nr_) +
")");
91 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
93 d->rlb_min_ = (r - bounds_.lb).array().min(0.);
94 d->rub_max_ = (r - bounds_.ub).array().max(0.);
96 Scalar(0.5) * d->rlb_min_.matrix().squaredNorm() + Scalar(0.5) * d->rub_max_.matrix().squaredNorm();
99 virtual void calcDiff(
const boost::shared_ptr<ActivationDataAbstract>& data,
const Eigen::Ref<const VectorXs>& r) {
100 if (static_cast<std::size_t>(r.size()) != nr_) {
101 throw_pretty(
"Invalid argument: " 102 <<
"r has wrong dimension (it should be " + std::to_string(nr_) +
")");
105 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
106 data->Ar = (d->rlb_min_ + d->rub_max_).matrix();
107 data->Arr.diagonal() =
108 (((r - bounds_.lb).array() <= 0.) + ((r - bounds_.ub).array() >= 0.)).matrix().template cast<Scalar>();
111 virtual boost::shared_ptr<ActivationDataAbstract> createData() {
112 return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(),
this);
115 const ActivationBounds& get_bounds()
const {
return bounds_; };
116 void set_bounds(
const ActivationBounds& bounds) { bounds_ = bounds; };
122 ActivationBounds bounds_;
125 template <
typename _Scalar>
127 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
129 typedef _Scalar Scalar;
131 typedef typename MathBase::ArrayXs ArrayXs;
134 template <
typename Activation>
136 : Base(activation), rlb_min_(activation->get_nr()), rub_max_(activation->get_nr()) {
150 #endif // CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_BARRIER_HPP_