- 3.0.2 core module.
PeriodicActivation.hpp
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <cmath>
5 
6 #include "../activations/ActivationBase.hpp"
8 
9 namespace ct {
10 namespace core {
11 namespace tpl {
12 
13 template <typename SCALAR, typename TRAIT = typename ct::core::tpl::TraitSelector<SCALAR>::Trait>
14 class PeriodicActivation : public ActivationBase<SCALAR>
15 {
16 public:
18  PeriodicActivation(const SCALAR active_percentage,
19  const SCALAR period,
20  const SCALAR activation_offset,
21  const SCALAR period_offset)
22  : active_percentage_(active_percentage),
23  period_(period),
24  activation_offset_(activation_offset),
25  period_offset_(period_offset)
26  {
27  }
28  virtual ~PeriodicActivation() {}
30  : active_percentage_(arg.active_percentage_),
31  period_(arg.period_),
32  activation_offset_(arg.activation_offset_),
33  period_offset_(arg.period_offset_),
34  t_end_(arg.t_end_)
35  {
36  }
37 
38  virtual void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false) override
39  {
40  boost::property_tree::ptree pt;
41  boost::property_tree::read_info(filename, pt);
42 
43  active_percentage_ = pt.get<SCALAR>(termName + ".active_percentage");
44  period_ = pt.get<SCALAR>(termName + ".period");
45  activation_offset_ = pt.get<SCALAR>(termName + ".activation_offset");
46  period_offset_ = pt.get<SCALAR>(termName + ".period_offset");
47  t_end_ = pt.get<SCALAR>(termName + ".t_end");
48 
49  if (activation_offset_ + active_percentage_ * period_ > period_)
50  {
51  throw std::runtime_error(
52  "Activation offset plus active period percentage exceed period time. Adjust settings");
53  }
54 
55  if (verbose)
56  printInfo();
57  }
58 
59  // to verify
60  virtual bool isActive(const SCALAR t) override { return isActiveSpecialized(t); }
61  template <typename S = SCALAR>
62  typename std::enable_if<std::is_same<S, double>::value, bool>::type isActiveSpecialized(const SCALAR t)
63  {
64  bool active = false;
65  if (t >= period_offset_ && t < t_end_)
66  {
67  SCALAR t0 = t - period_offset_;
68  SCALAR t0norm = fmod(t0, period_);
69  if (t0norm >= activation_offset_ && t0norm < (activation_offset_ + active_percentage_ * period_))
70  active = true;
71  }
72  return active;
73  }
74 
75  template <typename S = SCALAR>
76  typename std::enable_if<!std::is_same<S, double>::value, bool>::type isActiveSpecialized(const SCALAR t)
77  {
78  return true;
79  }
80 
81  virtual SCALAR computeActivation(const SCALAR t) override { return SCALAR(1.0); }
82  virtual void printInfo() override
83  {
84  std::cout << "Cost function active at periodic times: " << std::endl;
85  std::cout << "Period: " << period_ << "\nOffset after period start: " << activation_offset_ << "s"
86  << std::endl;
87  std::cout << "Offset between t0: " << period_offset_ << "s" << std::endl;
88  std::cout << "Active for " << 100 * active_percentage_ << "% of the period" << std::endl;
89  }
90 
91 private:
92  SCALAR
93  active_percentage_; // how much of the cycle is the time active TODO: misleading name. should be called fraction
94  SCALAR period_; // what is the period
95  SCALAR activation_offset_; // how much is the activation offset WITHIN the period
96  SCALAR period_offset_; // how much is the period offset to t=0?
97  SCALAR t_end_;
98 };
99 } // namespace tpl
100 
102 } // namespace core
103 } // namespace ct
std::enable_if<!std::is_same< S, double >::value, bool >::type isActiveSpecialized(const SCALAR t)
Definition: PeriodicActivation.hpp:76
virtual void printInfo() override
print to console
Definition: PeriodicActivation.hpp:82
PeriodicActivation(const PeriodicActivation &arg)
Definition: PeriodicActivation.hpp:29
virtual bool isActive(const SCALAR t) override
return if term is active
Definition: PeriodicActivation.hpp:60
PeriodicActivation()
Definition: PeriodicActivation.hpp:17
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Definition: PeriodicActivation.hpp:14
Definition: ActivationBase.hpp:18
std::enable_if< std::is_same< S, double >::value, bool >::type isActiveSpecialized(const SCALAR t)
Definition: PeriodicActivation.hpp:62
const bool verbose
Definition: JacobianCGTest.h:19
virtual ~PeriodicActivation()
Definition: PeriodicActivation.hpp:28
virtual SCALAR computeActivation(const SCALAR t) override
compute activation multiplier based on scalar input
Definition: PeriodicActivation.hpp:81
virtual void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false) override
load activations from file
Definition: PeriodicActivation.hpp:38
PeriodicActivation(const SCALAR active_percentage, const SCALAR period, const SCALAR activation_offset, const SCALAR period_offset)
Definition: PeriodicActivation.hpp:18