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