GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/activations/smooth-2norm.hpp Lines: 19 23 82.6 %
Date: 2024-02-13 11:12:33 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
223
    if (eps < Scalar(0.)) {
59
      throw_pretty("Invalid argument: "
60
                   << "eps should be a positive value");
61
    }
62
223
  };
63
450
  virtual ~ActivationModelSmooth2NormTpl(){};
64
65
  /**
66
   * @brief Compute the smooth-2Norm function
67
   *
68
   * @param[in] data  Smooth-2Norm activation data
69
   * @param[in] r     Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
70
   */
71
9675
  virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data,
72
                    const Eigen::Ref<const VectorXs>& r) {
73
9675
    if (static_cast<std::size_t>(r.size()) != nr_) {
74
      throw_pretty("Invalid argument: "
75
                   << "r has wrong dimension (it should be " +
76
                          std::to_string(nr_) + ")");
77
    }
78
    using std::sqrt;
79
9675
    data->a_value = sqrt(r.squaredNorm() + eps_);
80
9675
  };
81
82
  /**
83
   * @brief Compute the derivatives of the smooth-2Norm function
84
   *
85
   * @param[in] data  Smooth-2Norm activation data
86
   * @param[in] r     Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
87
   */
88
304
  virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data,
89
                        const Eigen::Ref<const VectorXs>& r) {
90
304
    if (static_cast<std::size_t>(r.size()) != nr_) {
91
      throw_pretty("Invalid argument: "
92
                   << "r has wrong dimension (it should be " +
93
                          std::to_string(nr_) + ")");
94
    }
95
96
304
    data->Ar = r / data->a_value;
97
    using std::pow;
98

304
    data->Arr.diagonal().array() = Scalar(1) / pow(data->a_value, 3);
99
304
  };
100
101
  /**
102
   * @brief Create the smooth-2Norm activation data
103
   *
104
   * @return the activation data
105
   */
106
7837
  virtual boost::shared_ptr<ActivationDataAbstract> createData() {
107
    return boost::allocate_shared<ActivationDataAbstract>(
108
7837
        Eigen::aligned_allocator<ActivationDataAbstract>(), this);
109
  };
110
111
 protected:
112
  /**
113
   * @brief Print relevant information of the smooth-1norm model
114
   *
115
   * @param[out] os  Output stream object
116
   */
117
37
  virtual void print(std::ostream& os) const {
118
37
    os << "ActivationModelSmooth2Norm {nr=" << nr_ << ", eps=" << eps_ << "}";
119
37
  }
120
121
  using Base::nr_;  //!< Dimension of the residual vector
122
  Scalar eps_;      //!< Smoothing factor
123
};
124
125
}  // namespace crocoddyl
126
127
#endif  // CROCODDYL_CORE_ACTIVATIONS_SMOOTH_2NORM_HPP_