DMP_BBO library
ExponentialSystem.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 <boost/serialization/base_object.hpp>
35 
36 #include <cmath>
37 #include <vector>
38 #include <iostream>
39 #include <eigen3/Eigen/Core>
40 
43 
44 
45 using namespace std;
46 using namespace Eigen;
47 
48 namespace DmpBbo {
49 
50 ExponentialSystem::ExponentialSystem(double tau, Eigen::VectorXd y_init, Eigen::VectorXd y_attr, double alpha, std::string name)
51  : DynamicalSystem(1, tau, y_init, y_attr, name),
52  alpha_(alpha)
53 {
54  attractor_state_prealloc_ = VectorXd::Zero(dim_orig());
55 }
56 
58 {
59 }
60 
62 {
63  return new ExponentialSystem(tau(),initial_state(),attractor_state(),alpha_,name());
64 }
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  // xd = alpha_*(attractor_state()-x)/tau(); // Non-realtime version now commented out
73 
74  attractor_state(attractor_state_prealloc_);
75  xd.noalias() = alpha_*(attractor_state_prealloc_-x)/tau();
76 
77  EXITING_REAL_TIME_CRITICAL_CODE
78 }
79 
80 void ExponentialSystem::analyticalSolution(const VectorXd& ts, MatrixXd& xs, MatrixXd& xds) const
81 {
82  int T = ts.size();
83  assert(T>0);
84 
85  // Usually, we expect xs and xds to be of size T X dim(), so we resize to that. However, if the
86  // input matrices were of size dim() X T, we return the matrices of that size by doing a
87  // transposeInPlace at the end. That way, the user can also request dim() X T sized matrices.
88  bool caller_expects_transposed = (xs.rows()==dim() && xs.cols()==T);
89 
90  // Prepare output arguments to be of right size (Eigen does nothing if already the right size)
91  xs.resize(T,dim());
92  xds.resize(T,dim());
93 
94  VectorXd val_range = initial_state() - attractor_state();
95 
96  VectorXd exp_term = -alpha_*ts/tau();
97  exp_term = exp_term.array().exp().transpose();
98  VectorXd pos_scale = exp_term;
99  VectorXd vel_scale = -(alpha_/tau()) * exp_term;
100 
101  xs = val_range.transpose().replicate(T,1).array() * pos_scale.replicate(1,dim()).array();
102  xs += attractor_state().transpose().replicate(T,1);
103  xds = val_range.transpose().replicate(T,1).array() * vel_scale.replicate(1,dim()).array();
104 
105  if (caller_expects_transposed)
106  {
107  xs.transposeInPlace();
108  xds.transposeInPlace();
109  }
110 }
111 
112 template<class Archive>
113 void ExponentialSystem::serialize(Archive & ar, const unsigned int version)
114 {
115  // serialize base class information
116  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(DynamicalSystem);
117 
118  ar & BOOST_SERIALIZATION_NVP(alpha_);
119 }
120 
121 string ExponentialSystem::toString(void) const
122 {
123  RETURN_STRING_FROM_BOOST_SERIALIZATION_XML("ExponentialSystem");
124 }
125 
126 }
double tau(void) const
Accessor function for the time constant.
int dim_orig(void) const
Get the dimensionality of the dynamical system, i.e.
ExponentialSystem class header file.
Eigen::VectorXd initial_state(void) const
Accessor function for the initial state of the dynamical system.
int dim(void) const
Get the dimensionality of the dynamical system, i.e.
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
~ExponentialSystem(void)
Destructor.
Dynamical System modelling the evolution of an exponential system: .
std::string name(void) const
Accessor function for the name of the dynamical system.
BOOST_CLASS_EXPORT_IMPLEMENT(DmpBbo::ExponentialSystem)
For boost::serialization.
DynamicalSystem * clone(void) const
Return a pointer to a deep copy of the DynamicalSystem object.
Interface for implementing dynamical systems.
std::string toString(void) const
Returns a string representation of the object.
Eigen::VectorXd attractor_state(void) const
Accessor function for the attractor state of the dynamical system.
Header file to generate strings from boost serialized files.
void differentialEquation(const Eigen::Ref< const Eigen::VectorXd > &x, Eigen::Ref< Eigen::VectorXd > xd) const
The differential equation which defines the system.
void analyticalSolution(const Eigen::VectorXd &ts, Eigen::MatrixXd &xs, Eigen::MatrixXd &xds) const
Return analytical solution of the system at certain times.
Header file for serialization of Eigen matrices.