GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/activations/smooth-2norm.hpp
Date: 2025-01-30 11:01:55
Exec Total Coverage
Lines: 19 22 86.4%
Branches: 7 56 12.5%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2020, LAAS-CNRS
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
7 ///////////////////////////////////////////////////////////////////////////////
8
9 #ifndef CROCODDYL_CORE_ACTIVATIONS_SMOOTH_2NORM_HPP_
10 #define CROCODDYL_CORE_ACTIVATIONS_SMOOTH_2NORM_HPP_
11
12 #include <stdexcept>
13
14 #include "crocoddyl/core/activation-base.hpp"
15 #include "crocoddyl/core/fwd.hpp"
16 #include "crocoddyl/core/utils/exception.hpp"
17
18 namespace crocoddyl {
19
20 /**
21 * @brief Smooth-2Norm activation
22 *
23 * This activation function describes a smooth representation of a 2-norm of a
24 * residual vector, i.e. \f[ \begin{equation} \sqrt{\epsilon + sum^nr_{i=0}
25 * \|r_i\|^2} \end{equation} \f] where \f$\epsilon\f$ defines the smoothing
26 * factor, \f$r_i\f$ is the scalar residual for the \f$i\f$ constraints,
27 * \f$nr\f$ is the dimension of the residual vector.
28 *
29 * The computation of the function and it derivatives are carried out in
30 * `calc()` and `caldDiff()`, respectively.
31 *
32 * \sa `calc()`, `calcDiff()`, `createData()`
33 */
34 template <typename _Scalar>
35 class ActivationModelSmooth2NormTpl
36 : public ActivationModelAbstractTpl<_Scalar> {
37 public:
38 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
39
40 typedef _Scalar Scalar;
41 typedef MathBaseTpl<Scalar> MathBase;
42 typedef ActivationModelAbstractTpl<Scalar> Base;
43 typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract;
44 typedef typename MathBase::VectorXs VectorXs;
45 typedef typename MathBase::MatrixXs MatrixXs;
46
47 /**
48 * @brief Initialize the smooth-2Norm activation model
49 *
50 * The default `eps` value is defined as 1.
51 *
52 * @param[in] nr Dimension of the residual vector
53 * @param[in] eps Smoothing factor (default: 1.)
54 */
55 223 explicit ActivationModelSmooth2NormTpl(const std::size_t nr,
56 const Scalar eps = Scalar(1.))
57 223 : Base(nr), eps_(eps) {
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 223 times.
223 if (eps < Scalar(0.)) {
59 throw_pretty("Invalid argument: " << "eps should be a positive value");
60 }
61 223 };
62 450 virtual ~ActivationModelSmooth2NormTpl() {};
63
64 /**
65 * @brief Compute the smooth-2Norm function
66 *
67 * @param[in] data Smooth-2Norm activation data
68 * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
69 */
70 9675 virtual void calc(const std::shared_ptr<ActivationDataAbstract>& data,
71 const Eigen::Ref<const VectorXs>& r) {
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9675 times.
9675 if (static_cast<std::size_t>(r.size()) != nr_) {
73 throw_pretty(
74 "Invalid argument: " << "r has wrong dimension (it should be " +
75 std::to_string(nr_) + ")");
76 }
77 using std::sqrt;
78 9675 data->a_value = sqrt(r.squaredNorm() + eps_);
79 9675 };
80
81 /**
82 * @brief Compute the derivatives of the smooth-2Norm function
83 *
84 * @param[in] data Smooth-2Norm activation data
85 * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
86 */
87 304 virtual void calcDiff(const std::shared_ptr<ActivationDataAbstract>& data,
88 const Eigen::Ref<const VectorXs>& r) {
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
304 if (static_cast<std::size_t>(r.size()) != nr_) {
90 throw_pretty(
91 "Invalid argument: " << "r has wrong dimension (it should be " +
92 std::to_string(nr_) + ")");
93 }
94
95
1/2
✓ Branch 4 taken 304 times.
✗ Branch 5 not taken.
304 data->Ar = r / data->a_value;
96 using std::pow;
97
2/4
✓ Branch 5 taken 304 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 304 times.
✗ Branch 9 not taken.
304 data->Arr.diagonal().array() = Scalar(1) / pow(data->a_value, 3);
98 304 };
99
100 /**
101 * @brief Create the smooth-2Norm activation data
102 *
103 * @return the activation data
104 */
105 7837 virtual std::shared_ptr<ActivationDataAbstract> createData() {
106 return std::allocate_shared<ActivationDataAbstract>(
107
1/2
✓ Branch 2 taken 7837 times.
✗ Branch 3 not taken.
15674 Eigen::aligned_allocator<ActivationDataAbstract>(), this);
108 };
109
110 protected:
111 /**
112 * @brief Print relevant information of the smooth-1norm model
113 *
114 * @param[out] os Output stream object
115 */
116 37 virtual void print(std::ostream& os) const {
117 37 os << "ActivationModelSmooth2Norm {nr=" << nr_ << ", eps=" << eps_ << "}";
118 37 }
119
120 using Base::nr_; //!< Dimension of the residual vector
121 Scalar eps_; //!< Smoothing factor
122 };
123
124 } // namespace crocoddyl
125
126 #endif // CROCODDYL_CORE_ACTIVATIONS_SMOOTH_2NORM_HPP_
127