DMP_BBO library
ModelParametersRBFN.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>
31 
34 
39 
40 #include <iostream>
41 #include <fstream>
42 
43 #include <eigen3/Eigen/Core>
44 
45 
46 using namespace std;
47 using namespace Eigen;
48 
49 namespace DmpBbo {
50 
51 ModelParametersRBFN::ModelParametersRBFN(const Eigen::MatrixXd& centers, const Eigen::MatrixXd& widths, const Eigen::MatrixXd& weights)
52 :
53  centers_(centers),
54  widths_(widths),
55  weights_(weights),
56  caching_(false)
57 {
58 #ifndef NDEBUG // Variables below are only required for asserts; check for NDEBUG to avoid warnings.
59  int n_basis_functions = centers.rows();
60  int n_dims = centers.cols();
61 #endif
62  assert(n_basis_functions==widths_.rows());
63  assert(n_dims ==widths_.cols());
64  assert(n_basis_functions==weights_.rows());
65  assert(1 ==weights_.cols());
66 
67  all_values_vector_size_ = 0;
68  all_values_vector_size_ += centers_.rows()*centers_.cols();
69  all_values_vector_size_ += widths_.rows() *widths_.cols();
70  all_values_vector_size_ += weights_.rows()*weights_.cols();
71 
72 };
73 
75  return new ModelParametersRBFN(centers_,widths_,weights_);
76 }
77 
78 void ModelParametersRBFN::kernelActivations(const Eigen::Ref<const Eigen::MatrixXd>& inputs, Eigen::MatrixXd& kernel_activations) const
79 {
80  if (caching_)
81  {
82  // If the cached inputs matrix has the same size as the one now requested
83  // (this also takes care of the case when inputs_cached is empty and need to be initialized)
84  if ( inputs.rows()==inputs_cached_.rows() && inputs.cols()==inputs_cached_.cols() )
85  {
86  // And they have the same values
87  if ( (inputs.array()==inputs_cached_.array()).all() )
88  {
89  // Then you can return the cached values
90  kernel_activations = kernel_activations_cached_;
91  return;
92  }
93  }
94  }
95 
96  ENTERING_REAL_TIME_CRITICAL_CODE
97 
98  // Cache could not be used, actually do the work
99  bool normalized_basis_functions=false;
100  bool asymmetric_kernels=false;
101  BasisFunction::Gaussian::activations(centers_,widths_,inputs,kernel_activations,
102  normalized_basis_functions,asymmetric_kernels);
103 
104  EXITING_REAL_TIME_CRITICAL_CODE
105 
106  if (caching_)
107  {
108  // Cache the current results now.
109  inputs_cached_ = inputs;
110  kernel_activations_cached_ = kernel_activations;
111  }
112 
113 }
114 
115 template<class Archive>
116 void ModelParametersRBFN::serialize(Archive & ar, const unsigned int version)
117 {
118  // serialize base class information
119  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(ModelParameters);
120 
121  ar & BOOST_SERIALIZATION_NVP(centers_);
122  ar & BOOST_SERIALIZATION_NVP(widths_);
123  ar & BOOST_SERIALIZATION_NVP(weights_);
124  ar & BOOST_SERIALIZATION_NVP(all_values_vector_size_);
125  ar & BOOST_SERIALIZATION_NVP(caching_);
126 }
127 
129 {
130  RETURN_STRING_FROM_BOOST_SERIALIZATION_XML("ModelParametersRBFN");
131 }
132 
133 void ModelParametersRBFN::getSelectableParameters(set<string>& selected_values_labels) const
134 {
135  selected_values_labels = set<string>();
136  selected_values_labels.insert("centers");
137  selected_values_labels.insert("widths");
138  selected_values_labels.insert("weights");
139 }
140 
141 
142 void ModelParametersRBFN::getParameterVectorMask(const std::set<std::string> selected_values_labels, VectorXi& selected_mask) const
143 {
144 
145  selected_mask.resize(getParameterVectorAllSize());
146  selected_mask.fill(0);
147 
148  int offset = 0;
149  int size;
150 
151  // Centers
152  size = centers_.rows()*centers_.cols();
153  if (selected_values_labels.find("centers")!=selected_values_labels.end())
154  selected_mask.segment(offset,size).fill(1);
155  offset += size;
156 
157  // Widths
158  size = widths_.rows()*widths_.cols();
159  if (selected_values_labels.find("widths")!=selected_values_labels.end())
160  selected_mask.segment(offset,size).fill(2);
161  offset += size;
162 
163  // Weights
164  size = weights_.rows()*weights_.cols();
165  if (selected_values_labels.find("weights")!=selected_values_labels.end())
166  selected_mask.segment(offset,size).fill(3);
167  offset += size;
168 
169  assert(offset == getParameterVectorAllSize());
170 }
171 
172 void ModelParametersRBFN::getParameterVectorAll(VectorXd& values) const
173 {
174  values.resize(getParameterVectorAllSize());
175  int offset = 0;
176 
177  for (int i_dim=0; i_dim<centers_.cols(); i_dim++)
178  {
179  values.segment(offset,centers_.rows()) = centers_.col(i_dim);
180  offset += centers_.rows();
181  }
182 
183  for (int i_dim=0; i_dim<widths_.cols(); i_dim++)
184  {
185  values.segment(offset,widths_.rows()) = widths_.col(i_dim);
186  offset += widths_.rows();
187  }
188 
189  values.segment(offset,weights_.rows()) = weights_;
190  offset += weights_.rows();
191 
192  assert(offset == getParameterVectorAllSize());
193 };
194 
195 void ModelParametersRBFN::setParameterVectorAll(const VectorXd& values) {
196 
197  if (all_values_vector_size_ != values.size())
198  {
199  cerr << __FILE__ << ":" << __LINE__ << ": values is of wrong size." << endl;
200  return;
201  }
202 
203  int offset = 0;
204  int size = centers_.rows();
205  int n_dims = centers_.cols();
206  for (int i_dim=0; i_dim<n_dims; i_dim++)
207  {
208  // If the centers change, the cache for kernelActivations() must be cleared,
209  // because this function will return different values for different centers
210  if ( !(centers_.col(i_dim).array() == values.segment(offset,size).array()).all() )
211  clearCache();
212 
213  centers_.col(i_dim) = values.segment(offset,size);
214  offset += size;
215  }
216  for (int i_dim=0; i_dim<n_dims; i_dim++)
217  {
218  // If the centers change, the cache for kernelActivations() must be cleared,
219  // because this function will return different values for different centers
220  if ( !(widths_.col(i_dim).array() == values.segment(offset,size).array()).all() )
221  clearCache();
222 
223  widths_.col(i_dim) = values.segment(offset,size);
224  offset += size;
225  }
226 
227  weights_ = values.segment(offset,size);
228  offset += size;
229  // Cache must not be cleared, because kernelActivations() returns the same values.
230 
231  assert(offset == getParameterVectorAllSize());
232 };
233 
234 void ModelParametersRBFN::setParameterVectorModifierPrivate(std::string modifier, bool new_value)
235 {
236 }
237 
239 {
240  // RBFN does not use normalized basis functions
241  bool normalized_basis_functions = false;
242  return new UnifiedModel(centers_, widths_, weights_,normalized_basis_functions);
243 }
244 
245 }
246 
247 
UnifiedModel class header file.
UnifiedModel * toUnifiedModel(void) const
Convert these model parameters to unified model parameters.
FunctionApproximator class header file.
void kernelActivations(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &kernel_activations) const
Get the kernel activations for given inputs.
ModelParameters * clone(void) const
Return a pointer to a deep copy of the ModelParameters object.
int getParameterVectorAllSize(void) const
Get the size of the parameter values vector when it contains all available parameter values...
std::string toString(void) const
Returns a string representation of the object.
BasisFunction header file.
BOOST_CLASS_EXPORT_IMPLEMENT(DmpBbo::ModelParametersRBFN)
For boost::serialization.
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
ModelParametersRBFN class header file.
The unified model, which can be used to represent the model of all other function approximators...
void setParameterVectorAll(const Eigen::VectorXd &values)
Set all available parameter values with one vector.
void getParameterVectorAll(Eigen::VectorXd &all_values) const
Return a vector that returns all available parameter values.
Base class for all model parameters of function approximators.
void setParameterVectorModifierPrivate(std::string modifier, bool new_value)
Turn certain modifiers on or off, see Parameterizable::setParameterVectorModifier().
Model parameters for the Radial Basis Function Network (RBFN) function approximator.
Header file to generate strings from boost serialized files.
void getParameterVectorMask(const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const
Get a mask for selecting parameters.
void getSelectableParameters(std::set< std::string > &selected_values_labels) const
Return all the names of the parameter types that can be selected.
Header file for serialization of Eigen matrices.