Bayes Filters Library
TimeDecreasingDynamics.cpp
Go to the documentation of this file.
2 #include <BayesFilters/utils.h>
3 
4 #include <iostream>
5 #include <limits>
6 
7 using namespace bfl;
8 using namespace Eigen;
9 
10 
12 {
13  enum class Modality
14  {
15  Iteration,
16  Time
17  };
18 
20 
21  unsigned int iterations_;
22 
23  unsigned int current_iterations_ = 0;
24 
25  double seconds_;
26 
27  double current_seconds_ = 0.0;
28 
30 };
31 
32 
33 TimeDecreasingDynamics::TimeDecreasingDynamics(std::unique_ptr<StateModel> state_model, const unsigned int iterations) noexcept :
34  StateModelDecorator(std::move(state_model)),
35  pImpl_(std::unique_ptr<ImplData>(new ImplData))
36 {
37  ImplData& rImpl = *pImpl_;
38 
39 
40  rImpl.modality_ = ImplData::Modality::Iteration;
41 
42  rImpl.iterations_ = iterations;
43 
44  rImpl.seconds_ = std::numeric_limits<double>::infinity();
45 }
46 
47 
48 TimeDecreasingDynamics::TimeDecreasingDynamics(std::unique_ptr<StateModel> state_model, const double seconds) noexcept :
49  StateModelDecorator(std::move(state_model)),
50  pImpl_(std::unique_ptr<ImplData>(new ImplData))
51 {
52  ImplData& rImpl = *pImpl_;
53 
54 
55  rImpl.modality_ = ImplData::Modality::Time;
56 
57  rImpl.iterations_ = std::numeric_limits<unsigned int>::max();
58 
59  rImpl.seconds_ = std::abs(seconds);
60 
61  if (seconds < 0)
62  {
63  std::cerr << "WARNING::TIMEDECREASINGDYNAMICS::CTOR\n";
64  std::cerr << "WARNING::LOG:\n\tInput parameter `seconds` is negative. Used as positive.\n";
65  std::cerr << "WARNING::LOG:\n\tProvided: " << seconds << ". Used " << rImpl.seconds_ << "." << std::endl;
66  }
67 }
68 
69 
71  StateModelDecorator(std::move(state_model)),
72  pImpl_(std::move(state_model.pImpl_))
73 { }
74 
75 
77 { }
78 
79 
81 {
82  StateModelDecorator::operator=(std::move(state_model));
83 
84  pImpl_ = std::move(state_model.pImpl_);
85 
86  return *this;
87 }
88 
89 
90 MatrixXd TimeDecreasingDynamics::getNoiseSample(const std::size_t num)
91 {
92  ImplData& rImpl = *pImpl_;
93 
94 
95  double damper = 1.0;
96 
97  switch (rImpl.modality_)
98  {
99  case ImplData::Modality::Iteration:
100  {
101  damper = (rImpl.current_iterations_ <= rImpl.iterations_) ? std::exp(-rImpl.current_iterations_) : 0.0;
102 
103  break;
104  }
105 
106  case ImplData::Modality::Time:
107  {
108  damper = (rImpl.current_seconds_ <= rImpl.seconds_) ? std::exp(-rImpl.current_seconds_) : 0.0;
109 
110  break;
111  }
112 
113  default:
114  return StateModelDecorator::getNoiseSample(num);
115  }
116 
117  return StateModelDecorator::getNoiseSample(num) * damper;
118 }
119 
120 
121 bool TimeDecreasingDynamics::setProperty(const std::string& property)
122 {
123  ImplData& rImpl = *pImpl_;
124 
125 
126  if (property == "tdd_reset")
127  {
128  switch (rImpl.modality_)
129  {
130  case ImplData::Modality::Iteration:
131  {
132  rImpl.current_iterations_ = 0;
133 
134  break;
135  }
136 
137  case ImplData::Modality::Time:
138  {
139  rImpl.timer_.stop();
140 
141  break;
142  }
143 
144  default:
145  return false;
146  }
147 
148  return true;
149  }
150 
151 
152  if (property == "tdd_advance")
153  {
154  switch (rImpl.modality_)
155  {
156  case ImplData::Modality::Iteration:
157  {
158  ++rImpl.current_iterations_;
159 
160  break;
161  }
162 
163  case ImplData::Modality::Time:
164  {
165  rImpl.current_seconds_ = rImpl.timer_.elapsed() * 1000.0;
166 
167  if (!rImpl.timer_.is_running())
168  rImpl.timer_.start();
169 
170  break;
171  }
172 
173  default:
174  return false;
175  }
176 
177  return true;
178  }
179 
180 
181  return StateModelDecorator::setProperty(property);
182 }
bfl::utils::CpuTimer::elapsed
double elapsed()
Get the time passed between a start() and stop() call.
Definition: utils.h:494
bfl::TimeDecreasingDynamics::ImplData::modality_
Modality modality_
Definition: TimeDecreasingDynamics.cpp:19
bfl::TimeDecreasingDynamics::operator=
TimeDecreasingDynamics & operator=(TimeDecreasingDynamics &&state_model) noexcept
Definition: TimeDecreasingDynamics.cpp:80
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::utils::CpuTimer::start
void start()
Start the timer.
Definition: utils.h:469
bfl::utils::CpuTimer
This template class provides methods to keep track of time.
Definition: utils.h:463
bfl::TimeDecreasingDynamics::ImplData::current_seconds_
double current_seconds_
Definition: TimeDecreasingDynamics.cpp:27
bfl::TimeDecreasingDynamics::ImplData::Modality
Modality
Definition: TimeDecreasingDynamics.cpp:13
bfl::TimeDecreasingDynamics::getNoiseSample
Eigen::MatrixXd getNoiseSample(const std::size_t num) override
Definition: TimeDecreasingDynamics.cpp:90
bfl::TimeDecreasingDynamics::ImplData
Definition: TimeDecreasingDynamics.cpp:11
bfl::TimeDecreasingDynamics
Definition: TimeDecreasingDynamics.h:14
utils.h
bfl::TimeDecreasingDynamics::setProperty
bool setProperty(const std::string &property) override
Definition: TimeDecreasingDynamics.cpp:121
bfl::utils::CpuTimer::is_running
bool is_running()
Check if the timer is running or not.
Definition: utils.h:523
bfl::TimeDecreasingDynamics::ImplData::seconds_
double seconds_
Definition: TimeDecreasingDynamics.cpp:25
bfl::TimeDecreasingDynamics::~TimeDecreasingDynamics
virtual ~TimeDecreasingDynamics() noexcept
Definition: TimeDecreasingDynamics.cpp:76
bfl::TimeDecreasingDynamics::ImplData::timer_
utils::CpuTimer timer_
Definition: TimeDecreasingDynamics.cpp:29
bfl::TimeDecreasingDynamics::TimeDecreasingDynamics
TimeDecreasingDynamics(std::unique_ptr< StateModel > state_model, const unsigned int iterations) noexcept
Definition: TimeDecreasingDynamics.cpp:33
TimeDecreasingDynamics.h
bfl::utils::CpuTimer::stop
void stop()
Stop the timer.
Definition: utils.h:480
bfl::TimeDecreasingDynamics::ImplData::iterations_
unsigned int iterations_
Definition: TimeDecreasingDynamics.cpp:21
bfl::TimeDecreasingDynamics::ImplData::current_iterations_
unsigned int current_iterations_
Definition: TimeDecreasingDynamics.cpp:23