Directory: | ./ |
---|---|
File: | include/crocoddyl/core/activation-base.hpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 14 | 20 | 70.0% |
Branches: | 4 | 14 | 28.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh, | ||
5 | // Heriot-Watt University | ||
6 | // Copyright note valid unless otherwise stated in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #ifndef CROCODDYL_CORE_ACTIVATION_BASE_HPP_ | ||
11 | #define CROCODDYL_CORE_ACTIVATION_BASE_HPP_ | ||
12 | |||
13 | #include <boost/core/demangle.hpp> | ||
14 | #include <boost/make_shared.hpp> | ||
15 | #include <boost/shared_ptr.hpp> | ||
16 | #include <stdexcept> | ||
17 | |||
18 | #include "crocoddyl/core/fwd.hpp" | ||
19 | #include "crocoddyl/core/mathbase.hpp" | ||
20 | |||
21 | namespace crocoddyl { | ||
22 | |||
23 | template <typename _Scalar> | ||
24 | class ActivationModelAbstractTpl { | ||
25 | public: | ||
26 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
27 | |||
28 | typedef _Scalar Scalar; | ||
29 | typedef MathBaseTpl<Scalar> MathBase; | ||
30 | typedef ActivationDataAbstractTpl<Scalar> ActivationDataAbstract; | ||
31 | typedef typename MathBase::VectorXs VectorXs; | ||
32 | typedef typename MathBase::MatrixXs MatrixXs; | ||
33 | |||
34 | 7802 | explicit ActivationModelAbstractTpl(const std::size_t nr) : nr_(nr) {}; | |
35 | 15644 | virtual ~ActivationModelAbstractTpl() {}; | |
36 | |||
37 | virtual void calc(const boost::shared_ptr<ActivationDataAbstract>& data, | ||
38 | const Eigen::Ref<const VectorXs>& r) = 0; | ||
39 | virtual void calcDiff(const boost::shared_ptr<ActivationDataAbstract>& data, | ||
40 | const Eigen::Ref<const VectorXs>& r) = 0; | ||
41 | ✗ | virtual boost::shared_ptr<ActivationDataAbstract> createData() { | |
42 | return boost::allocate_shared<ActivationDataAbstract>( | ||
43 | ✗ | Eigen::aligned_allocator<ActivationDataAbstract>(), this); | |
44 | }; | ||
45 | |||
46 | 1208462 | std::size_t get_nr() const { return nr_; }; | |
47 | |||
48 | /** | ||
49 | * @brief Print information on the activation model | ||
50 | */ | ||
51 | 335 | friend std::ostream& operator<<( | |
52 | std::ostream& os, const ActivationModelAbstractTpl<Scalar>& model) { | ||
53 | 335 | model.print(os); | |
54 | 335 | return os; | |
55 | } | ||
56 | |||
57 | /** | ||
58 | * @brief Print relevant information of the activation model | ||
59 | * | ||
60 | * @param[out] os Output stream object | ||
61 | */ | ||
62 | ✗ | virtual void print(std::ostream& os) const { | |
63 | ✗ | os << boost::core::demangle(typeid(*this).name()); | |
64 | } | ||
65 | |||
66 | protected: | ||
67 | std::size_t nr_; | ||
68 | }; | ||
69 | |||
70 | template <typename _Scalar> | ||
71 | struct ActivationDataAbstractTpl { | ||
72 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
73 | |||
74 | typedef _Scalar Scalar; | ||
75 | typedef MathBaseTpl<Scalar> MathBase; | ||
76 | typedef typename MathBase::VectorXs VectorXs; | ||
77 | typedef typename MathBase::MatrixXs MatrixXs; | ||
78 | typedef typename MathBase::DiagonalMatrixXs DiagonalMatrixXs; | ||
79 | |||
80 | template <template <typename Scalar> class Activation> | ||
81 | 955628 | explicit ActivationDataAbstractTpl(Activation<Scalar>* const activation) | |
82 | 955628 | : a_value(Scalar(0.)), | |
83 |
1/2✓ Branch 2 taken 477814 times.
✗ Branch 3 not taken.
|
955628 | Ar(VectorXs::Zero(activation->get_nr())), |
84 |
1/2✓ Branch 3 taken 477814 times.
✗ Branch 4 not taken.
|
1911256 | Arr(DiagonalMatrixXs(activation->get_nr())) { |
85 |
1/2✓ Branch 1 taken 477814 times.
✗ Branch 2 not taken.
|
955628 | Arr.setZero(); |
86 | } | ||
87 | 480654 | virtual ~ActivationDataAbstractTpl() {} | |
88 | |||
89 | Scalar a_value; | ||
90 | VectorXs Ar; | ||
91 | DiagonalMatrixXs Arr; | ||
92 | |||
93 | 13 | static MatrixXs getHessianMatrix( | |
94 | const ActivationDataAbstractTpl<Scalar>& data) { | ||
95 |
1/2✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
|
26 | return data.Arr.diagonal().asDiagonal(); |
96 | } | ||
97 | ✗ | static void setHessianMatrix(ActivationDataAbstractTpl<Scalar>& data, | |
98 | const MatrixXs& Arr) { | ||
99 | ✗ | data.Arr.diagonal() = Arr.diagonal(); | |
100 | } | ||
101 | }; | ||
102 | |||
103 | } // namespace crocoddyl | ||
104 | |||
105 | #endif // CROCODDYL_CORE_ACTIVATION_BASE_HPP_ | ||
106 |