9 #ifndef CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
10 #define CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
15 #include "crocoddyl/core/activation-base.hpp"
16 #include "crocoddyl/core/fwd.hpp"
17 #include "crocoddyl/core/utils/exception.hpp"
36 template <
typename _Scalar>
40 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42 typedef _Scalar Scalar;
47 typedef typename MathBase::VectorXs VectorXs;
48 typedef typename MathBase::MatrixXs MatrixXs;
59 const Scalar eps = Scalar(1.))
61 if (eps < Scalar(0.)) {
62 throw_pretty(
"Invalid argument: " <<
"eps should be a positive value");
64 if (eps == Scalar(0.)) {
65 std::cerr <<
"Warning: eps=0 leads to derivatives discontinuities in the "
66 "origin, it becomes the absolute function"
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_) {
82 "Invalid argument: " <<
"r has wrong dimension (it should be " +
83 std::to_string(nr_) +
")");
85 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
87 d->a = (r.array().cwiseAbs2().array() +
eps_).array().cwiseSqrt();
88 data->a_value = d->a.sum();
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_) {
101 "Invalid argument: " <<
"r has wrong dimension (it should be " +
102 std::to_string(nr_) +
")");
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();
116 virtual boost::shared_ptr<ActivationDataAbstract>
createData() {
117 return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(),
this);
125 virtual void print(std::ostream& os)
const {
126 os <<
"ActivationModelSmooth1Norm {nr=" << nr_ <<
", eps=" <<
eps_ <<
"}";
134 template <
typename _Scalar>
137 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
139 typedef _Scalar Scalar;
142 typedef typename MathBase::VectorXs VectorXs;
143 typedef typename MathBase::MatrixXs MatrixXs;
145 template <
typename Activation>
147 :
Base(activation), a(VectorXs::Zero(activation->get_nr())) {}
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.