Crocoddyl
activation-base.hpp
1 // 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.
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>
25  public:
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  typedef _Scalar Scalar;
31  typedef typename MathBase::VectorXs VectorXs;
32  typedef typename MathBase::MatrixXs MatrixXs;
33 
34  explicit ActivationModelAbstractTpl(const std::size_t nr) : nr_(nr) {};
35  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  std::size_t get_nr() const { return nr_; };
47 
51  friend std::ostream& operator<<(
52  std::ostream& os, const ActivationModelAbstractTpl<Scalar>& model) {
53  model.print(os);
54  return os;
55  }
56 
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>
72  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
73 
74  typedef _Scalar Scalar;
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  explicit ActivationDataAbstractTpl(Activation<Scalar>* const activation)
82  : a_value(Scalar(0.)),
83  Ar(VectorXs::Zero(activation->get_nr())),
84  Arr(DiagonalMatrixXs(activation->get_nr())) {
85  Arr.setZero();
86  }
87  virtual ~ActivationDataAbstractTpl() {}
88 
89  Scalar a_value;
90  VectorXs Ar;
91  DiagonalMatrixXs Arr;
92 
93  static MatrixXs getHessianMatrix(
95  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_
friend std::ostream & operator<<(std::ostream &os, const ActivationModelAbstractTpl< Scalar > &model)
Print information on the activation model.
virtual void print(std::ostream &os) const
Print relevant information of the activation model.