crocoddyl  1.4.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
quadratic-barrier.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2018-2020, LAAS-CNRS, University of Edinburgh
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_BARRIER_HPP_
10 #define CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_BARRIER_HPP_
11 
12 #include <stdexcept>
13 #include <math.h>
14 
15 #include "crocoddyl/core/fwd.hpp"
16 #include "crocoddyl/core/utils/exception.hpp"
17 #include "crocoddyl/core/activation-base.hpp"
18 
19 #include <pinocchio/utils/static-if.hpp>
20 
21 namespace crocoddyl {
22 
23 template <typename _Scalar>
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 
27  typedef _Scalar Scalar;
29  typedef typename MathBase::VectorXs VectorXs;
30  typedef typename MathBase::MatrixXs MatrixXs;
31 
32  ActivationBoundsTpl(const VectorXs& lower, const VectorXs& upper, const Scalar& b = (Scalar)1.)
33  : lb(lower), ub(upper), beta(b) {
34  if (lb.size() != ub.size()) {
35  throw_pretty("Invalid argument: "
36  << "The lower and upper bounds don't have the same dimension (lb,ub dimensions equal to " +
37  std::to_string(lb.size()) + "," + std::to_string(ub.size()) + ", respectively)");
38  }
39  if (beta < Scalar(0) || beta > Scalar(1.)) {
40  throw_pretty("Invalid argument: "
41  << "The range of beta is between 0 and 1");
42  }
43  using std::isfinite;
44  for (std::size_t i = 0; i < static_cast<std::size_t>(lb.size()); ++i) {
45  if (isfinite(lb(i)) && isfinite(ub(i))) {
46  if (lb(i) - ub(i) > 0) {
47  throw_pretty("Invalid argument: "
48  << "The lower and upper bounds are badly defined; ub has to be bigger / equals to lb");
49  }
50  }
51  }
52 
53  if (beta >= Scalar(0) && beta <= Scalar(1.)) {
54  VectorXs m = Scalar(0.5) * (lower + upper);
55  VectorXs d = Scalar(0.5) * (upper - lower);
56  lb = m - beta * d;
57  ub = m + beta * d;
58  } else {
59  beta = Scalar(1.);
60  }
61  }
62  ActivationBoundsTpl(const ActivationBoundsTpl& bounds) : lb(bounds.lb), ub(bounds.ub), beta(bounds.beta) {}
63  ActivationBoundsTpl() : beta(Scalar(1.)) {}
64 
65  VectorXs lb;
66  VectorXs ub;
67  Scalar beta;
68 };
69 
70 template <typename _Scalar>
72  public:
73  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
74 
75  typedef _Scalar Scalar;
81  typedef typename MathBase::VectorXs VectorXs;
82  typedef typename MathBase::MatrixXs MatrixXs;
83 
84  explicit ActivationModelQuadraticBarrierTpl(const ActivationBounds& bounds)
85  : Base(bounds.lb.size()), bounds_(bounds){};
87 
88  virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data, const Eigen::Ref<const VectorXs>& r) {
89  if (static_cast<std::size_t>(r.size()) != nr_) {
90  throw_pretty("Invalid argument: "
91  << "r has wrong dimension (it should be " + std::to_string(nr_) + ")");
92  }
93 
94  boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
95 
96  d->rlb_min_ = (r - bounds_.lb).array().min(Scalar(0.));
97  d->rub_max_ = (r - bounds_.ub).array().max(Scalar(0.));
98  data->a_value =
99  Scalar(0.5) * d->rlb_min_.matrix().squaredNorm() + Scalar(0.5) * d->rub_max_.matrix().squaredNorm();
100  };
101 
102  virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data, const Eigen::Ref<const VectorXs>& r) {
103  if (static_cast<std::size_t>(r.size()) != nr_) {
104  throw_pretty("Invalid argument: "
105  << "r has wrong dimension (it should be " + std::to_string(nr_) + ")");
106  }
107 
108  boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
109  data->Ar = (d->rlb_min_ + d->rub_max_).matrix();
110 
111  using pinocchio::internal::if_then_else;
112  for (Eigen::Index i = 0; i < data->Arr.cols(); i++) {
113  data->Arr.diagonal()[i] = if_then_else(
114  pinocchio::internal::LE, r[i] - bounds_.lb[i], Scalar(0.), Scalar(1.),
115  if_then_else(pinocchio::internal::GE, r[i] - bounds_.ub[i], Scalar(0.), Scalar(1.), Scalar(0.)));
116  }
117  };
118 
119  virtual boost::shared_ptr<ActivationDataAbstract> createData() {
120  return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
121  };
122 
123  const ActivationBounds& get_bounds() const { return bounds_; };
124  void set_bounds(const ActivationBounds& bounds) { bounds_ = bounds; };
125 
126  protected:
127  using Base::nr_;
128 
129  private:
130  ActivationBounds bounds_;
131 };
132 
133 template <typename _Scalar>
135  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
136 
137  typedef _Scalar Scalar;
139  typedef typename MathBase::ArrayXs ArrayXs;
141 
142  template <typename Activation>
143  explicit ActivationDataQuadraticBarrierTpl(Activation* const activation)
144  : Base(activation), rlb_min_(activation->get_nr()), rub_max_(activation->get_nr()) {
145  rlb_min_.setZero();
146  rub_max_.setZero();
147  }
148 
149  ArrayXs rlb_min_;
150  ArrayXs rub_max_;
151  using Base::a_value;
152  using Base::Ar;
153  using Base::Arr;
154 };
155 
156 } // namespace crocoddyl
157 
158 #endif // CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_BARRIER_HPP_