DMP_BBO library
ModelParametersRRRFF.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 
31 
34 
35 #include <iostream>
36 
41 
42 
43 using namespace Eigen;
44 using namespace std;
45 
46 namespace DmpBbo {
47 
48 ModelParametersRRRFF::ModelParametersRRRFF(Eigen::VectorXd weights, Eigen::MatrixXd cosines_periodes, Eigen::VectorXd cosines_phase)
49 :
50  weights_(weights),
51  cosines_periodes_(cosines_periodes),
52  cosines_phase_(cosines_phase)
53 {
54 
55  assert(cosines_phase.size() == cosines_periodes.rows());
56  assert(weights.rows() == cosines_periodes.rows());
57 
58  nb_in_dim_ = cosines_periodes.cols();
59  // int nb_output_dim = weights.cols();
60 
61  all_values_vector_size_ = 0;
62  all_values_vector_size_ += weights_.rows() * weights_.cols();
63  all_values_vector_size_ += cosines_phase_.size();
64  all_values_vector_size_ += cosines_periodes_.rows() * cosines_periodes_.cols();
65 };
66 
68 {
69  return new ModelParametersRRRFF(weights_, cosines_periodes_, cosines_phase_);
70 }
71 
72 void ModelParametersRRRFF::cosineActivations(const Eigen::Ref<const Eigen::MatrixXd>& inputs, Eigen::MatrixXd& cosine_activations) const
73 {
74  if (caching_)
75  {
76  // If the cached inputs matrix has the same size as the one now requested
77  // (this also takes care of the case when inputs_cached is empty and need to be initialized)
78  if ( inputs.rows()==inputs_cached_.rows() && inputs.cols()==inputs_cached_.cols() )
79  {
80  // And they have the same values
81  if ( (inputs.array()==inputs_cached_.array()).all() )
82  {
83  // Then you can return the cached values
84  cosine_activations = cosine_activations_cached_;
85  return;
86  }
87  }
88  }
89 
90  BasisFunction::Cosine::activations(cosines_periodes_,cosines_phase_,inputs,cosine_activations);
91 
92  if (caching_)
93  {
94  // Cache the current results now.
95  inputs_cached_ = inputs;
96  cosine_activations_cached_ = cosine_activations;
97  }
98 
99 }
100 
102 {
103  return nb_in_dim_;
104 };
105 
106 template<class Archive>
107 void ModelParametersRRRFF::serialize(Archive & ar, const unsigned int version)
108 {
109  // serialize base class information
110  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(ModelParameters);
111 
112  ar & BOOST_SERIALIZATION_NVP(weights_);
113  ar & BOOST_SERIALIZATION_NVP(cosines_periodes_);
114  ar & BOOST_SERIALIZATION_NVP(cosines_phase_);
115  ar & BOOST_SERIALIZATION_NVP(nb_in_dim_);
116  ar & BOOST_SERIALIZATION_NVP(all_values_vector_size_);
117 }
118 
120 {
121  RETURN_STRING_FROM_BOOST_SERIALIZATION_XML("ModelParametersRRRFF");
122 };
123 
124 
125 void ModelParametersRRRFF::getSelectableParameters(set<string>& selected_values_labels) const
126 {
127  selected_values_labels = set<string>();
128  selected_values_labels.insert("weights");
129  selected_values_labels.insert("phases");
130  selected_values_labels.insert("periods");
131 }
132 
133 void ModelParametersRRRFF::getParameterVectorMask(const std::set<std::string> selected_values_labels, VectorXi& selected_mask) const
134 {
135  selected_mask.resize(getParameterVectorAllSize());
136  selected_mask.fill(0);
137 
138  int offset = 0;
139  int size;
140 
141  size = weights_.rows() * weights_.cols();
142  if (selected_values_labels.find("weights")!=selected_values_labels.end())
143  selected_mask.segment(offset,size).fill(1);
144  offset += size;
145 
146  size = cosines_phase_.size();
147  if (selected_values_labels.find("phases")!=selected_values_labels.end())
148  selected_mask.segment(offset,size).fill(2);
149  offset += size;
150 
151  size = cosines_periodes_.rows() * cosines_periodes_.cols();
152  if (selected_values_labels.find("periods")!=selected_values_labels.end())
153  selected_mask.segment(offset,size).fill(3);
154  offset += size;
155 
156  assert(offset == getParameterVectorAllSize());
157 }
158 
160 {
161  values.resize(getParameterVectorAllSize());
162  int offset = 0;
163 
164  for (int c = 0; c < weights_.cols(); c++)
165  {
166  values.segment(offset, weights_.rows()) = weights_.col(c);
167  offset += weights_.rows();
168  }
169 
170  values.segment(offset, cosines_phase_.size()) = cosines_phase_;
171  offset += cosines_phase_.size();
172 
173  for (int c = 0; c < cosines_periodes_.cols(); c++)
174  {
175  values.segment(offset, cosines_periodes_.rows()) = cosines_periodes_.col(c);
176  offset += cosines_periodes_.rows();
177  }
178 
179  assert(offset == getParameterVectorAllSize());
180 
181 };
182 
184 {
185  if (all_values_vector_size_ != values.size())
186  {
187  cerr << __FILE__ << ":" << __LINE__ << ": values is of wrong size." << endl;
188  return;
189  }
190 
191  int offset = 0;
192  for (int c = 0; c < weights_.cols(); c++)
193  {
194  weights_.col(c) = values.segment(offset, weights_.rows());
195  offset += weights_.rows();
196  }
197 
198  cosines_phase_ = values.segment(offset, cosines_phase_.size());
199  offset += cosines_phase_.size();
200 
201  for (int c = 0; c < cosines_periodes_.cols(); c++)
202  {
203  cosines_periodes_.col(c) = values.segment(offset, cosines_periodes_.rows());
204  offset += cosines_periodes_.rows();
205  }
206 
207  assert(offset == getParameterVectorAllSize());
208 };
209 
211 {
212  return new UnifiedModel(cosines_periodes_, cosines_phase_, weights_);
213 }
214 
215 }
216 
217 
void getSelectableParameters(std::set< std::string > &selected_values_labels) const
Return all the names of the parameter types that can be selected.
UnifiedModel class header file.
std::string toString(void) const
Returns a string representation of the object.
void getParameterVectorMask(const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const
Get a mask for selecting parameters.
ModelParameters * clone(void) const
Return a pointer to a deep copy of the ModelParameters object.
int getExpectedInputDim(void) const
The expected dimensionality of the input data.
BasisFunction header file.
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
UnifiedModel * toUnifiedModel(void) const
Convert these model parameters to unified model parameters.
The unified model, which can be used to represent the model of all other function approximators...
Base class for all model parameters of function approximators.
void setParameterVectorAll(const Eigen::VectorXd &values)
Set all available parameter values with one vector.
ModelParametersRRRFF class header file.
Model parameters for the RRRFF function approximator.
Header file to generate strings from boost serialized files.
void getParameterVectorAll(Eigen::VectorXd &all_values) const
Return a vector that returns all available parameter values.
int getParameterVectorAllSize(void) const
Get the size of the parameter values vector when it contains all available parameter values...
void cosineActivations(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &cosine_activations) const
Get the values of the cosine basis functions for given inputs.
Header file for serialization of Eigen matrices.
BOOST_CLASS_EXPORT_IMPLEMENT(DmpBbo::ModelParametersRRRFF)
For boost::serialization.