crocoddyl 1.9.0
Contact RObot COntrol by Differential DYnamic programming Library (Crocoddyl)
 
Loading...
Searching...
No Matches
quadratic-flat-exp.hpp
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.
8
9#ifndef CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_FLAT_EXP_HPP_
10#define CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_FLAT_EXP_HPP_
11
12#include "crocoddyl/core/fwd.hpp"
13#include "crocoddyl/core/activation-base.hpp"
14#include "crocoddyl/core/utils/exception.hpp"
15
16namespace 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 */
33template <typename _Scalar>
35 public:
36 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
37
38 typedef _Scalar Scalar;
43 typedef typename MathBase::VectorXs VectorXs;
44 typedef typename MathBase::MatrixXs MatrixXs;
45
46 /*
47 * @brief Initialize the quadratic-flat-exp activation model
48 *
49 * The default `alpha` value is defined as 1.
50 *
51 * @param[in] nr Dimension of the residual vector
52 * @param[in] alpha Width of quadratic basin (default: 1.)
53 */
54
55 explicit ActivationModelQuadFlatExpTpl(const std::size_t &nr, const Scalar &alpha = Scalar(1.))
56 : Base(nr), alpha_(alpha) {
57 if (alpha < Scalar(0.)) {
58 throw_pretty("Invalid argument: "
59 << "alpha should be a positive value");
60 }
61 };
63
64 /*
65 * @brief Compute the quadratic-flat-exp function
66 *
67 * @param[in] data Quadratic-flat activation data
68 * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
69 */
70 virtual void calc(const boost::shared_ptr<ActivationDataAbstract> &data, const Eigen::Ref<const VectorXs> &r) {
71 if (static_cast<std::size_t>(r.size()) != nr_) {
72 throw_pretty("Invalid argument: "
73 << "r has wrong dimension (it should be " + std::to_string(nr_) + ")");
74 }
75 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
76
77 d->a0 = exp(-r.squaredNorm() / alpha_);
78 data->a_value = Scalar(1.0) - d->a0;
79 };
80
81 /*
82 * @brief Compute the derivatives of the quadratic-flat-exp function
83 *
84 * @param[in] data Quadratic-flat activation data
85 * @param[in] r Residual vector \f$\mathbf{r}\in\mathbb{R}^{nr}\f$
86 */
87 virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract> &data, const Eigen::Ref<const VectorXs> &r) {
88 if (static_cast<std::size_t>(r.size()) != nr_) {
89 throw_pretty("Invalid argument: "
90 << "r has wrong dimension (it should be " + std::to_string(nr_) + ")");
91 }
92 boost::shared_ptr<Data> d = boost::static_pointer_cast<Data>(data);
93
94 d->a1 = Scalar(2.0) / alpha_ * d->a0;
95 data->Ar = d->a1 * r;
96 data->Arr.diagonal() = -Scalar(2.0) * d->a1 * r.array().square() / alpha_;
97 data->Arr.diagonal().array() += d->a1;
98 };
99
105 virtual boost::shared_ptr<ActivationDataAbstract> createData() {
106 boost::shared_ptr<Data> data = boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
107 return data;
108 };
109
110 Scalar get_alpha() const { return alpha_; };
111 void set_alpha(const Scalar alpha) { alpha_ = alpha; };
112
118 virtual void print(std::ostream &os) const {
119 os << "ActivationModelQuadFlatExp {nr=" << nr_ << ", a=" << alpha_ << "}";
120 }
121
122 protected:
123 using Base::nr_;
124
125 private:
126 Scalar alpha_;
127};
128
129/*
130 * @brief Data structure of the quadratic-flat-exp activation
131 *
132 * @param[in] a0 computed in calc to avoid recomputation
133 * @param[in] a1 computed in calcDiff to avoid recomputation
134 */
135template <typename _Scalar>
137 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
138
139 typedef _Scalar Scalar;
142
143 template <typename Activation>
144 explicit ActivationDataQuadFlatExpTpl(Activation *const activation) : Base(activation), a0(0), a1(0) {}
145
146 Scalar a0;
147 Scalar a1;
148};
149
150} // namespace crocoddyl
151
152#endif // CROCODDYL_CORE_ACTIVATIONS_QUADRATIC_FLAT_EXP_HPP_
virtual boost::shared_ptr< ActivationDataAbstract > createData()
Create the quadratic-flat-exp activation data.
virtual void print(std::ostream &os) const
Print relevant information of the quadratic flat-exp model.