Directory: | ./ |
---|---|
File: | include/crocoddyl/core/activations/quadratic-flat-exp.hpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 29 | 34 | 85.3% |
Branches: | 14 | 70 | 20.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2020, LAAS-CNRS, University of Edinburgh | ||
5 | // Copyright note valid unless otherwise stated in individual files. | ||
6 | // All rights reserved. | ||
7 | /////////////////////////////////////////////////////////////////////////////// | ||
8 | |||
9 | #ifndef CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_FLAT_EXP_HPP_ | ||
10 | #define CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_FLAT_EXP_HPP_ | ||
11 | |||
12 | #include "crocoddyl/core/activation-base.hpp" | ||
13 | #include "crocoddyl/core/fwd.hpp" | ||
14 | #include "crocoddyl/core/utils/exception.hpp" | ||
15 | |||
16 | namespace crocoddyl { | ||
17 | |||
18 | /* | ||
19 | * @brief Quadratic-flat-exp activation | ||
20 | * | ||
21 | * This activation function describes a quadratic exponential activation | ||
22 | * depending on the square norm of a residual vector, i.e. \f[ \begin{equation} | ||
23 | * 1 - exp(\|\mathbf{r}\|^2 / \alpha) \end{equation} \f] where \f$\alpha\f$ | ||
24 | * defines the width of the quadratic basin, \f$r\f$ is the scalar residual, | ||
25 | * \f$nr\f$ is the dimension of the residual vector. Far | ||
26 | * away from zero, the quadFlat activation is nearly flat. | ||
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 ActivationModelQuadFlatExpTpl | ||
35 | : public ActivationModelAbstractTpl<_Scalar> { | ||
36 | public: | ||
37 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
38 | |||
39 | typedef _Scalar Scalar; | ||
40 | typedef MathBaseTpl<Scalar> MathBase; | ||
41 | typedef ActivationModelAbstractTpl<Scalar> Base; | ||
42 | typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract; | ||
43 | typedef ActivationDataQuadFlatExpTpl<Scalar> Data; | ||
44 | typedef typename MathBase::VectorXs VectorXs; | ||
45 | typedef typename MathBase::MatrixXs MatrixXs; | ||
46 | |||
47 | /* | ||
48 | * @brief Initialize the quadratic-flat-exp activation model | ||
49 | * | ||
50 | * The default `alpha` value is defined as 1. | ||
51 | * | ||
52 | * @param[in] nr Dimension of the residual vector | ||
53 | * @param[in] alpha Width of quadratic basin (default: 1.) | ||
54 | */ | ||
55 | |||
56 | 223 | explicit ActivationModelQuadFlatExpTpl(const std::size_t &nr, | |
57 | const Scalar &alpha = Scalar(1.)) | ||
58 | 223 | : Base(nr), alpha_(alpha) { | |
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 223 times.
|
223 | if (alpha < Scalar(0.)) { |
60 | ✗ | throw_pretty("Invalid argument: " << "alpha should be a positive value"); | |
61 | } | ||
62 | 223 | }; | |
63 | 450 | virtual ~ActivationModelQuadFlatExpTpl() {}; | |
64 | |||
65 | /* | ||
66 | * @brief Compute the quadratic-flat-exp function | ||
67 | * | ||
68 | * @param[in] data Quadratic-flat 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 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9675 times.
|
9675 | if (static_cast<std::size_t>(r.size()) != nr_) { |
74 | ✗ | throw_pretty( | |
75 | "Invalid argument: " << "r has wrong dimension (it should be " + | ||
76 | std::to_string(nr_) + ")"); | ||
77 | } | ||
78 | 9675 | boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data); | |
79 | |||
80 |
1/2✓ Branch 1 taken 9675 times.
✗ Branch 2 not taken.
|
9675 | d->a0 = exp(-r.squaredNorm() / alpha_); |
81 | 9675 | data->a_value = Scalar(1.0) - d->a0; | |
82 | 9675 | }; | |
83 | |||
84 | /* | ||
85 | * @brief Compute the derivatives of the quadratic-flat-exp function | ||
86 | * | ||
87 | * @param[in] data Quadratic-flat activation data | ||
88 | * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$ | ||
89 | */ | ||
90 | 304 | virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract> &data, | |
91 | const Eigen::Ref<const VectorXs> &r) { | ||
92 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 304 times.
|
304 | if (static_cast<std::size_t>(r.size()) != nr_) { |
93 | ✗ | throw_pretty( | |
94 | "Invalid argument: " << "r has wrong dimension (it should be " + | ||
95 | std::to_string(nr_) + ")"); | ||
96 | } | ||
97 | 304 | boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data); | |
98 | |||
99 | 304 | d->a1 = Scalar(2.0) / alpha_ * d->a0; | |
100 |
2/4✓ Branch 2 taken 304 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 304 times.
✗ Branch 7 not taken.
|
304 | data->Ar = d->a1 * r; |
101 |
5/10✓ Branch 1 taken 304 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 304 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 304 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 304 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 304 times.
✗ Branch 17 not taken.
|
304 | data->Arr.diagonal() = -Scalar(2.0) * d->a1 * r.array().square() / alpha_; |
102 |
2/4✓ Branch 4 taken 304 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 304 times.
✗ Branch 8 not taken.
|
304 | data->Arr.diagonal().array() += d->a1; |
103 | 304 | }; | |
104 | |||
105 | /** | ||
106 | * @brief Create the quadratic-flat-exp activation data | ||
107 | * | ||
108 | * @return the activation data | ||
109 | */ | ||
110 | 7837 | virtual boost::shared_ptr<ActivationDataAbstract> createData() { | |
111 |
1/2✓ Branch 1 taken 7837 times.
✗ Branch 2 not taken.
|
7837 | boost::shared_ptr<Data> data = |
112 | 7837 | boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); | |
113 | 15674 | return data; | |
114 | 7837 | }; | |
115 | |||
116 | ✗ | Scalar get_alpha() const { return alpha_; }; | |
117 | ✗ | void set_alpha(const Scalar alpha) { alpha_ = alpha; }; | |
118 | |||
119 | /** | ||
120 | * @brief Print relevant information of the quadratic flat-exp model | ||
121 | * | ||
122 | * @param[out] os Output stream object | ||
123 | */ | ||
124 | 37 | virtual void print(std::ostream &os) const { | |
125 | 37 | os << "ActivationModelQuadFlatExp {nr=" << nr_ << ", a=" << alpha_ << "}"; | |
126 | 37 | } | |
127 | |||
128 | protected: | ||
129 | using Base::nr_; //!< Dimension of the residual vector | ||
130 | |||
131 | private: | ||
132 | Scalar alpha_; //!< Width of quadratic basin | ||
133 | }; | ||
134 | |||
135 | /* | ||
136 | * @brief Data structure of the quadratic-flat-exp activation | ||
137 | * | ||
138 | * @param[in] a0 computed in calc to avoid recomputation | ||
139 | * @param[in] a1 computed in calcDiff to avoid recomputation | ||
140 | */ | ||
141 | template <typename _Scalar> | ||
142 | struct ActivationDataQuadFlatExpTpl | ||
143 | : public ActivationDataAbstractTpl<_Scalar> { | ||
144 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
145 | |||
146 | typedef _Scalar Scalar; | ||
147 | typedef MathBaseTpl<Scalar> MathBase; | ||
148 | typedef ActivationDataAbstractTpl<Scalar> Base; | ||
149 | |||
150 | template <typename Activation> | ||
151 | 7837 | explicit ActivationDataQuadFlatExpTpl(Activation *const activation) | |
152 | 7837 | : Base(activation), a0(0), a1(0) {} | |
153 | |||
154 | Scalar a0; | ||
155 | Scalar a1; | ||
156 | }; | ||
157 | |||
158 | } // namespace crocoddyl | ||
159 | |||
160 | #endif // CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_FLAT_EXP_HPP_ | ||
161 |