Crocoddyl
smooth-1norm.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-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_SMOOTH_1NORM_HPP_
10 #define CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
11 
12 #include <iostream>
13 #include <stdexcept>
14 
15 #include "crocoddyl/core/activation-base.hpp"
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/utils/exception.hpp"
18 
19 namespace crocoddyl {
20 
36 template <typename _Scalar>
38  : public ActivationModelAbstractTpl<_Scalar> {
39  public:
40  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
41 
42  typedef _Scalar Scalar;
47  typedef typename MathBase::VectorXs VectorXs;
48  typedef typename MathBase::MatrixXs MatrixXs;
49 
58  explicit ActivationModelSmooth1NormTpl(const std::size_t nr,
59  const Scalar eps = Scalar(1.))
60  : Base(nr), eps_(eps) {
61  if (eps < Scalar(0.)) {
62  throw_pretty("Invalid argument: " << "eps should be a positive value");
63  }
64  if (eps == Scalar(0.)) {
65  std::cerr << "Warning: eps=0 leads to derivatives discontinuities in the "
66  "origin, it becomes the absolute function"
67  << std::endl;
68  }
69  };
70  virtual ~ActivationModelSmooth1NormTpl() {};
71 
78  virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data,
79  const Eigen::Ref<const VectorXs>& r) {
80  if (static_cast<std::size_t>(r.size()) != nr_) {
81  throw_pretty(
82  "Invalid argument: " << "r has wrong dimension (it should be " +
83  std::to_string(nr_) + ")");
84  }
85  boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
86 
87  d->a = (r.array().cwiseAbs2().array() + eps_).array().cwiseSqrt();
88  data->a_value = d->a.sum();
89  };
90 
97  virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data,
98  const Eigen::Ref<const VectorXs>& r) {
99  if (static_cast<std::size_t>(r.size()) != nr_) {
100  throw_pretty(
101  "Invalid argument: " << "r has wrong dimension (it should be " +
102  std::to_string(nr_) + ")");
103  }
104 
105  boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
106  data->Ar = r.cwiseProduct(d->a.cwiseInverse());
107  data->Arr.diagonal() =
108  d->a.cwiseProduct(d->a).cwiseProduct(d->a).cwiseInverse();
109  };
110 
116  virtual boost::shared_ptr<ActivationDataAbstract> createData() {
117  return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
118  };
119 
125  virtual void print(std::ostream& os) const {
126  os << "ActivationModelSmooth1Norm {nr=" << nr_ << ", eps=" << eps_ << "}";
127  }
128 
129  protected:
130  using Base::nr_;
131  Scalar eps_;
132 };
133 
134 template <typename _Scalar>
136  : public ActivationDataAbstractTpl<_Scalar> {
137  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
138 
139  typedef _Scalar Scalar;
142  typedef typename MathBase::VectorXs VectorXs;
143  typedef typename MathBase::MatrixXs MatrixXs;
144 
145  template <typename Activation>
146  explicit ActivationDataSmooth1NormTpl(Activation* const activation)
147  : Base(activation), a(VectorXs::Zero(activation->get_nr())) {}
148 
149  VectorXs a;
150  using Base::Arr;
151 };
152 
153 } // namespace crocoddyl
154 
155 #endif // CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
virtual boost::shared_ptr< ActivationDataAbstract > createData()
Create the smooth-abs activation data.
virtual void calc(const boost::shared_ptr< ActivationDataAbstract > &data, const Eigen::Ref< const VectorXs > &r)
Compute the smooth-abs function.
virtual void print(std::ostream &os) const
Print relevant information of the smooth-1norm model.
Scalar eps_
< Dimension of the residual vector
virtual void calcDiff(const boost::shared_ptr< ActivationDataAbstract > &data, const Eigen::Ref< const VectorXs > &r)
Compute the derivatives of the smooth-abs function.
ActivationModelSmooth1NormTpl(const std::size_t nr, const Scalar eps=Scalar(1.))
Initialize the smooth-abs activation model.