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