9#ifndef CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_SMOOTH_1NORM_HPP_ 10#define CROCODDYL_CORE_ACTIVATIONS_WEIGHTED_SMOOTH_1NORM_HPP_ 12#include "crocoddyl/core/activation-base.hpp" 13#include "crocoddyl/core/fwd.hpp" 39template <
typename _Scalar>
41 :
public ActivationModelAbstractTpl<_Scalar> {
43 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
47 typedef _Scalar Scalar;
49 typedef ActivationModelAbstractTpl<Scalar> Base;
52 typedef typename MathBase::VectorXs VectorXs;
61 const VectorXs& weights,
const Scalar delta = Scalar(1.))
62 : Base(weights.size()), weights_(weights), delta_(delta) {
63 check_weights(weights_);
64 if (delta_ <= Scalar(0.)) {
66 "Invalid argument: delta should be a strictly positive value");
77 virtual void calc(
const std::shared_ptr<ActivationDataAbstract>& data,
78 const Eigen::Ref<const VectorXs>& r)
override {
79 if (
static_cast<std::size_t
>(r.size()) != nr_) {
81 "Invalid argument: " <<
"r has wrong dimension (it should be " +
82 std::to_string(nr_) +
")");
84 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
86 d->a = (r.array().square() + delta_ * delta_).sqrt().matrix();
89 (weights_.array() * r.array().square() / (d->a.array() + delta_)).sum();
98 virtual void calcDiff(
const std::shared_ptr<ActivationDataAbstract>& data,
99 const Eigen::Ref<const VectorXs>& r)
override {
100 if (
static_cast<std::size_t
>(r.size()) != nr_) {
102 "Invalid argument: " <<
"r has wrong dimension (it should be " +
103 std::to_string(nr_) +
")");
105 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
106 const VectorXs a_inv = d->a.cwiseInverse();
107 data->Ar = delta_ * weights_.cwiseProduct(r).cwiseProduct(a_inv);
108 data->Arr.diagonal() =
109 delta_ * delta_ * delta_ *
110 weights_.cwiseProduct(a_inv.cwiseProduct(a_inv).cwiseProduct(a_inv));
118 virtual std::shared_ptr<ActivationDataAbstract>
createData()
override {
119 return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(),
this);
122 template <
typename NewScalar>
125 return ReturnType(weights_.template cast<NewScalar>(),
126 scalar_cast<NewScalar>(delta_));
129 const VectorXs& get_weights()
const {
return weights_; }
130 Scalar get_delta()
const {
return delta_; }
132 void set_weights(
const VectorXs& weights) {
133 if (weights.size() != weights_.size()) {
134 throw_pretty(
"Invalid argument: " 135 <<
"weight vector has wrong dimension (it should be " +
136 std::to_string(weights_.size()) +
")");
138 check_weights(weights);
147 virtual void print(std::ostream& os)
const override {
148 os <<
"ActivationModelWeightedSmooth1Norm {nr=" << nr_
149 <<
", delta=" << delta_ <<
"}";
156 static void check_weights(
const VectorXs& weights) {
157 for (Eigen::Index i = 0; i < weights.size(); ++i) {
158 if (weights[i] < Scalar(0.)) {
160 "Invalid argument: weights should contain nonnegative values");
169template <
typename _Scalar>
170struct ActivationDataWeightedSmooth1NormTpl
171 :
public ActivationDataAbstractTpl<_Scalar> {
172 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
174 typedef _Scalar Scalar;
175 typedef ActivationDataAbstractTpl<Scalar> Base;
177 typedef typename MathBase::VectorXs VectorXs;
179 template <
typename Activation>
180 explicit ActivationDataWeightedSmooth1NormTpl(Activation*
const activation)
181 : Base(activation),
a(VectorXs::Zero(activation->get_nr())) {}
182 virtual ~ActivationDataWeightedSmooth1NormTpl() =
default;
193CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
195CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(
Weighted smooth-1norm activation.
virtual void calcDiff(const std::shared_ptr< ActivationDataAbstract > &data, const Eigen::Ref< const VectorXs > &r) override
Compute the activation derivatives.
ActivationModelWeightedSmooth1NormTpl(const VectorXs &weights, const Scalar delta=Scalar(1.))
Initialize the weighted smooth-1norm activation model.
virtual std::shared_ptr< ActivationDataAbstract > createData() override
Create the weighted smooth-1norm activation data.
virtual void calc(const std::shared_ptr< ActivationDataAbstract > &data, const Eigen::Ref< const VectorXs > &r) override
Compute the weighted smooth-1norm activation.
virtual void print(std::ostream &os) const override
Print relevant information of the weighted smooth-1norm model.
VectorXs a
Element-wise values .