GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/activations/smooth-1norm.hpp
Date: 2025-05-13 10:30:51
Exec Total Coverage
Lines: 0 35 0.0%
Branches: 0 88 0.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
11 #define CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
12
13 #include "crocoddyl/core/activation-base.hpp"
14 #include "crocoddyl/core/fwd.hpp"
15
16 namespace crocoddyl {
17
18 /**
19 * @brief Smooth-abs activation
20 *
21 * This activation function describes a smooth representation of an absolute
22 * activation (1-norm) for each element of a residual vector, i.e. \f[
23 * \begin{equation} sum^nr_{i=0} \sqrt{\epsilon + \|r_i\|^2} \end{equation} \f]
24 * where \f$\epsilon\f$ defines the smoothing factor, \f$r_i\f$ is the scalar
25 * residual for the \f$i\f$ constraints, \f$nr\f$ is the dimension of the
26 * residual vector.
27 *
28 * The computation of the function and it derivatives are carried out in
29 * `calc()` and `caldDiff()`, respectively.
30 *
31 * \sa `calc()`, `calcDiff()`, `createData()`
32 */
33 template <typename _Scalar>
34 class ActivationModelSmooth1NormTpl
35 : public ActivationModelAbstractTpl<_Scalar> {
36 public:
37 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
38 CROCODDYL_DERIVED_CAST(ActivationModelBase, ActivationModelSmooth1NormTpl)
39
40 typedef _Scalar Scalar;
41 typedef MathBaseTpl<Scalar> MathBase;
42 typedef ActivationModelAbstractTpl<Scalar> Base;
43 typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract;
44 typedef ActivationDataSmooth1NormTpl<Scalar> Data;
45 typedef typename MathBase::VectorXs VectorXs;
46 typedef typename MathBase::MatrixXs MatrixXs;
47
48 /**
49 * @brief Initialize the smooth-abs activation model
50 *
51 * The default `eps` value is defined as 1.
52 *
53 * @param[in] nr Dimension of the residual vector
54 * @param[in] eps Smoothing factor (default: 1.)
55 */
56 explicit ActivationModelSmooth1NormTpl(const std::size_t nr,
57 const Scalar eps = Scalar(1.))
58 : Base(nr), eps_(eps) {
59 if (eps < Scalar(0.)) {
60 throw_pretty("Invalid argument: " << "eps should be a positive value");
61 }
62 if (eps == Scalar(0.)) {
63 std::cerr << "Warning: eps=0 leads to derivatives discontinuities in the "
64 "origin, it becomes the absolute function"
65 << std::endl;
66 }
67 };
68 virtual ~ActivationModelSmooth1NormTpl() = default;
69
70 /**
71 * @brief Compute the smooth-abs function
72 *
73 * @param[in] data Smooth-abs activation data
74 * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
75 */
76 virtual void calc(const std::shared_ptr<ActivationDataAbstract>& data,
77 const Eigen::Ref<const VectorXs>& r) override {
78 if (static_cast<std::size_t>(r.size()) != nr_) {
79 throw_pretty(
80 "Invalid argument: " << "r has wrong dimension (it should be " +
81 std::to_string(nr_) + ")");
82 }
83 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
84
85 d->a = (r.array().cwiseAbs2().array() + eps_).array().cwiseSqrt();
86 data->a_value = d->a.sum();
87 };
88
89 /**
90 * @brief Compute the derivatives of the smooth-abs function
91 *
92 * @param[in] data Smooth-abs activation data
93 * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
94 */
95 virtual void calcDiff(const std::shared_ptr<ActivationDataAbstract>& data,
96 const Eigen::Ref<const VectorXs>& r) override {
97 if (static_cast<std::size_t>(r.size()) != nr_) {
98 throw_pretty(
99 "Invalid argument: " << "r has wrong dimension (it should be " +
100 std::to_string(nr_) + ")");
101 }
102
103 std::shared_ptr<Data> d = std::static_pointer_cast<Data>(data);
104 data->Ar = r.cwiseProduct(d->a.cwiseInverse());
105 data->Arr.diagonal() =
106 d->a.cwiseProduct(d->a).cwiseProduct(d->a).cwiseInverse();
107 };
108
109 /**
110 * @brief Create the smooth-abs activation data
111 *
112 * @return the activation data
113 */
114 virtual std::shared_ptr<ActivationDataAbstract> createData() override {
115 return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
116 };
117
118 template <typename NewScalar>
119 ActivationModelSmooth1NormTpl<NewScalar> cast() const {
120 typedef ActivationModelSmooth1NormTpl<NewScalar> ReturnType;
121 ReturnType res(nr_, scalar_cast<NewScalar>(eps_));
122 return res;
123 }
124
125 /**
126 * @brief Print relevant information of the smooth-1norm model
127 *
128 * @param[out] os Output stream object
129 */
130 virtual void print(std::ostream& os) const override {
131 os << "ActivationModelSmooth1Norm {nr=" << nr_ << ", eps=" << eps_ << "}";
132 }
133
134 protected:
135 using Base::nr_; //!< Dimension of the residual vector
136 Scalar eps_; //!< Smoothing factor
137 };
138
139 template <typename _Scalar>
140 struct ActivationDataSmooth1NormTpl
141 : public ActivationDataAbstractTpl<_Scalar> {
142 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
143
144 typedef _Scalar Scalar;
145 typedef ActivationDataAbstractTpl<Scalar> Base;
146 typedef MathBaseTpl<Scalar> MathBase;
147 typedef typename MathBase::VectorXs VectorXs;
148 typedef typename MathBase::MatrixXs MatrixXs;
149 typedef typename MathBase::DiagonalMatrixXs DiagonalMatrixXs;
150
151 template <typename Activation>
152 explicit ActivationDataSmooth1NormTpl(Activation* const activation)
153 : Base(activation), a(VectorXs::Zero(activation->get_nr())) {}
154 virtual ~ActivationDataSmooth1NormTpl() = default;
155
156 VectorXs a;
157
158 using Base::a_value;
159 using Base::Ar;
160 using Base::Arr;
161 };
162
163 } // namespace crocoddyl
164
165 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
166 crocoddyl::ActivationModelSmooth1NormTpl)
167 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(
168 crocoddyl::ActivationDataSmooth1NormTpl)
169
170 #endif // CROCODDYL_CORE_ACTIVATIONS_SMOOTH_1NORM_HPP_
171