DMP_BBO library
TimeSystem.cpp
Go to the documentation of this file.
1 
24 #include <boost/serialization/export.hpp>
25 #include <boost/archive/text_iarchive.hpp>
26 #include <boost/archive/text_oarchive.hpp>
27 #include <boost/archive/xml_iarchive.hpp>
28 #include <boost/archive/xml_oarchive.hpp>
30 
33 
34 #include <cmath>
35 #include <vector>
36 #include <iostream>
37 #include <eigen3/Eigen/Core>
38 
40 
41 using namespace std;
42 using namespace Eigen;
43 
44 namespace DmpBbo {
45 
46 TimeSystem::TimeSystem(double tau, bool count_down, std::string name)
47 :
48  DynamicalSystem(1, tau, VectorXd::Zero(1), VectorXd::Ones(1), name),
49  count_down_(count_down)
50 {
51  if (count_down_)
52  {
53  set_initial_state(VectorXd::Ones(1));
54  set_attractor_state(VectorXd::Zero(1));
55  }
56 }
57 
59 {
60 }
61 
63 {
64  return new TimeSystem(tau(),count_down(),name());
65 }
66 
68  const Eigen::Ref<const Eigen::VectorXd>& x,
69  Eigen::Ref<Eigen::VectorXd> xd) const
70 {
71  ENTERING_REAL_TIME_CRITICAL_CODE
72 
73  // if state<1: xd = 1/obj.tau (or for count_down=true, if state>0: xd = -1/obj.tau
74  // else xd = 0
75  xd.resize(1);
76  xd[0] = 0;
77 
78  if (count_down_)
79  {
80  if (x[0]>0)
81  xd[0] = -1.0/tau();
82  }
83  else
84  {
85  if (x[0]<1)
86  xd[0] = 1.0/tau();
87  }
88 
89  EXITING_REAL_TIME_CRITICAL_CODE
90 }
91 
92 void TimeSystem::analyticalSolution(const VectorXd& ts, MatrixXd& xs, MatrixXd& xds) const
93 {
94  int T = ts.size();
95  assert(T>0);
96 
97  // Usually, we expect xs and xds to be of size T X dim(), so we resize to that. However, if the
98  // input matrices were of size dim() X T, we return the matrices of that size by doing a
99  // transposeInPlace at the end. That way, the user can also request dim() X T sized matrices.
100  bool caller_expects_transposed = (xs.rows()==dim() && xs.cols()==T);
101 
102  // Prepare output arguments to be of right size (Eigen does nothing if already the right size)
103  xs.resize(T,dim());
104  xds.resize(T,dim());
105 
106  // Find first index at which the time is larger than tau. Then velocities should be set to zero.
107  int velocity_stop_index = -1;
108  int i=0;
109  while (velocity_stop_index<0 && i<ts.size())
110  if (ts[i++]>tau())
111  velocity_stop_index = i-1;
112 
113  if (velocity_stop_index<0)
114  velocity_stop_index = ts.size();
115 
116  if (count_down_)
117  {
118  xs.topRows(velocity_stop_index) = (-ts.segment(0,velocity_stop_index).array()/tau()).array()+1.0;
119  xs.bottomRows(xs.size()-velocity_stop_index).fill(0.0);
120 
121  xds.topRows(velocity_stop_index).fill(-1.0/tau());
122  xds.bottomRows(xds.size()-velocity_stop_index).fill(0.0);
123  }
124  else
125  {
126  xs.topRows(velocity_stop_index) = ts.segment(0,velocity_stop_index).array()/tau();
127  xs.bottomRows(xs.size()-velocity_stop_index).fill(1.0);
128 
129  xds.topRows(velocity_stop_index).fill(1.0/tau());
130  xds.bottomRows(xds.size()-velocity_stop_index).fill(0.0);
131  }
132 
133  if (caller_expects_transposed)
134  {
135  xs.transposeInPlace();
136  xds.transposeInPlace();
137  }
138 }
139 
140 template<class Archive>
141 void TimeSystem::serialize(Archive & ar, const unsigned int version)
142 {
143  // serialize base class information
144  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(DynamicalSystem);
145 
146  ar & BOOST_SERIALIZATION_NVP(count_down_);
147 }
148 
149 string TimeSystem::toString(void) const
150 {
152 }
153 
154 }
double tau(void) const
Accessor function for the time constant.
virtual void set_attractor_state(const Eigen::Ref< const Eigen::VectorXd > &attractor_state)
Mutator function for the attractor state of the dynamical system.
BOOST_CLASS_EXPORT_IMPLEMENT(DmpBbo::TimeSystem)
For boost::serialization.
Dynamical System modelling the evolution of a time: .
Definition: TimeSystem.hpp:40
virtual void set_initial_state(const Eigen::VectorXd &initial_state)
Mutator function for the initial state of the dynamical system.
int dim(void) const
Get the dimensionality of the dynamical system, i.e.
TimeSystem class header file.
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
DynamicalSystem * clone(void) const
Return a pointer to a deep copy of the DynamicalSystem object.
Definition: TimeSystem.cpp:62
bool count_down(void) const
Accessor function for count_down.
Definition: TimeSystem.hpp:69
void differentialEquation(const Eigen::Ref< const Eigen::VectorXd > &x, Eigen::Ref< Eigen::VectorXd > xd) const
The differential equation which defines the system.
Definition: TimeSystem.cpp:67
std::string name(void) const
Accessor function for the name of the dynamical system.
void analyticalSolution(const Eigen::VectorXd &ts, Eigen::MatrixXd &xs, Eigen::MatrixXd &xds) const
Return analytical solution of the system at certain times.
Definition: TimeSystem.cpp:92
Interface for implementing dynamical systems.
Header file to generate strings from boost serialized files.
~TimeSystem(void)
Destructor.
Definition: TimeSystem.cpp:58
std::string toString(void) const
Returns a string representation of the object.
Definition: TimeSystem.cpp:149