DMP_BBO library
ModelParametersLWPR.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 
36 
38 
39 
40 #include "lwpr.hh"
41 
42 #include <iostream>
43 #include <fstream>
44 
45 using namespace Eigen;
46 using namespace std;
47 
48 namespace DmpBbo {
49 
50 ModelParametersLWPR::ModelParametersLWPR(LWPR_Object* lwpr_object)
51 :
52  lwpr_object_(lwpr_object)
53 {
54  countLengths();
55 }
56 
57 void ModelParametersLWPR::countLengths(void)
58 {
59 
60  // Determine the lengths of different vectors
61  n_centers_ = 0;
62  n_slopes_ = 0;
63  n_offsets_ = 0;
64  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
65  {
66  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
67  {
68  n_centers_ += lwpr_object_->nIn();
69  n_slopes_ += lwpr_object_->model.sub[iDim].rf[iRF]->nReg;
70  n_offsets_ += 1;
71  }
72  }
73  n_widths_ = n_centers_;
74 
75 }
76 
77 ModelParametersLWPR::~ModelParametersLWPR(void)
78 {
79  delete lwpr_object_;
80 }
81 
83 {
84  LWPR_Object* lwpr_object_clone = new LWPR_Object(*lwpr_object_);
85  return new ModelParametersLWPR(lwpr_object_clone);
86 }
87 
88 string ModelParametersLWPR::toString(void) const
89 {
90  RETURN_STRING_FROM_BOOST_SERIALIZATION_XML("ModelParametersLWPR");
91 
92  /*
93  output << "{ \"ModelParametersLWPR\": { \"lwpr_object_xml_string\": \"";
94 
95  // Write file
96  string filename("/tmp/lpwrfile_serialize.xml");
97  lwpr_object_->writeXML(filename.c_str());
98 
99  // Read file into string stream (through file stream)
100  ifstream t(filename);
101  stringstream strstream;
102  strstream << t.rdbuf();
103 
104  // Get the string and remove newlines
105  string str = strstream.str();
106  str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
107 
108  // Output the XML string to the current output stream
109  output << str;
110 
111  output << " \" } }";
112  */
113 };
114 
115 
116 
118 {
119  return lwpr_object_->nIn();
120 }
121 
122 void ModelParametersLWPR::getSelectableParameters(set<string>& selected_values_labels) const
123 {
124  selected_values_labels = set<string>();
125  selected_values_labels.insert("centers");
126  selected_values_labels.insert("widths");
127  selected_values_labels.insert("slopes");
128  selected_values_labels.insert("offsets");
129 }
130 
131 
132 
133 
134 void ModelParametersLWPR::getParameterVectorMask(const std::set<std::string> selected_values_labels, VectorXi& selected_mask) const
135 {
136  selected_mask.resize(getParameterVectorAllSize());
137  selected_mask.fill(0);
138 
139  int offset = 0;
140  int size;
141 
142  // Centers
143  size = n_centers_;
144  if (selected_values_labels.find("centers")!=selected_values_labels.end())
145  selected_mask.segment(offset,size).fill(1);
146  offset += size;
147 
148  // Widths
149  size = n_widths_;
150  if (selected_values_labels.find("widths")!=selected_values_labels.end())
151  selected_mask.segment(offset,size).fill(2);
152  offset += size;
153 
154  // Offsets
155  size = n_offsets_;
156  if (selected_values_labels.find("offsets")!=selected_values_labels.end())
157  selected_mask.segment(offset,size).fill(3);
158  offset += size;
159 
160  // Slopes
161  size = n_slopes_;
162  if (selected_values_labels.find("slopes")!=selected_values_labels.end())
163  selected_mask.segment(offset,size).fill(4);
164  offset += size;
165 
166  assert(offset == getParameterVectorAllSize());
167 }
168 
169 void ModelParametersLWPR::getParameterVectorAll(VectorXd& values) const
170 {
171  values.resize(getParameterVectorAllSize());
172  int ii=0;
173 
174  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
175  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
176  for (int j = 0; j < lwpr_object_->nIn(); j++)
177  values[ii++] = lwpr_object_->model.sub[iDim].rf[iRF]->c[j];
178 
179  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
180  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
181  for (int j = 0; j < lwpr_object_->nIn(); j++)
182  values[ii++] = lwpr_object_->model.sub[iDim].rf[iRF]->D[j*lwpr_object_->model.nInStore+j];
183 
184  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
185  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
186  for (int j = 0; j < lwpr_object_->model.sub[iDim].rf[iRF]->nReg; j++)
187  values[ii++] = lwpr_object_->model.sub[iDim].rf[iRF]->beta[j];
188 
189  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
190  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
191  values[ii++] = lwpr_object_->model.sub[iDim].rf[iRF]->beta0;
192 
193  assert(ii == getParameterVectorAllSize());
194 
195 };
196 
197 void ModelParametersLWPR::setParameterVectorAll(const VectorXd& values) {
198  if (getParameterVectorAllSize() != values.size())
199  {
200  cerr << __FILE__ << ":" << __LINE__ << ": values is of wrong size." << endl;
201  return;
202  }
203 
204  int ii=0;
205  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
206  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
207  for (int j = 0; j < lwpr_object_->nIn(); j++)
208  lwpr_object_->model.sub[iDim].rf[iRF]->c[j] = values[ii++];
209 
210  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
211  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
212  for (int j = 0; j < lwpr_object_->nIn(); j++)
213  lwpr_object_->model.sub[iDim].rf[iRF]->D[j*lwpr_object_->model.nInStore+j] = values[ii++];
214 
215  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
216  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
217  for (int j = 0; j < lwpr_object_->model.sub[iDim].rf[iRF]->nReg; j++)
218  lwpr_object_->model.sub[iDim].rf[iRF]->beta[j] = values[ii++];
219 
220  for (int iDim = 0; iDim < lwpr_object_->model.nOut; iDim++)
221  for (int iRF = 0; iRF < lwpr_object_->model.sub[iDim].numRFS; iRF++)
222  lwpr_object_->model.sub[iDim].rf[iRF]->beta0 = values[ii++];
223 
224  assert(ii == getParameterVectorAllSize());
225 };
226 
227 
229 {
230  if (lwpr_object_->nIn()!=1)
231  {
232  //cout << "Warning: Can only call toUnifiedModel() when input dim of LWPR is 1" << endl;
233  return NULL;
234  }
235  if (lwpr_object_->model.nOut!=1)
236  {
237  //cout << "Warning: Can only call toUnifiedModel() when output dim of LWPR is 1" << endl;
238  return NULL;
239  }
240 
241  int i_in = 0;
242  int i_out = 0;
243 
244  int n_basis_functions = lwpr_object_->model.sub[i_out].numRFS;
245  VectorXd centers(n_basis_functions);
246  VectorXd widths(n_basis_functions);
247  VectorXd offsets(n_basis_functions);
248  VectorXd slopes(n_basis_functions);
249 
250  vector<double> tmp;
251 
252  for (int iRF = 0; iRF < lwpr_object_->model.sub[i_out].numRFS; iRF++)
253  {
254  LWPR_ReceptiveFieldObject rf = lwpr_object_->getRF(i_out,iRF);
255 
256  tmp = rf.center();
257  centers[iRF] = tmp[i_in];
258 
259  offsets[iRF] = rf.beta0();
260 
261  tmp = rf.slope();
262  slopes[iRF] = tmp[i_in];
263  tmp = rf.beta();
264 
265  vector<vector<double> > D = rf.D();
266  widths[iRF] = sqrt(1.0/D[i_in][i_in]);
267 
268  }
269 
270 
271  vector<double> norm_out = lwpr_object_->normOut();
272  vector<double> norm_in = lwpr_object_->normIn();
273  //cout << " centers=" << centers.transpose() << endl;
274  //cout << " widths=" << widths.transpose() << endl;
275  //cout << " offsets=" << offsets.transpose() << endl;
276  //cout << " slopes=" << slopes.transpose() << endl;
277  //cout << " norm_out[i_out]=" << norm_out[i_out] << endl;
278  //cout << " norm_in[i_in]=" << norm_out[i_in] << endl;
279 
280  offsets /= norm_out[i_out];
281  slopes /= norm_out[i_out];
282  centers /= norm_in[i_in];
283  widths /= norm_in[i_in];
284 
285  //cout << " centers=" << centers.transpose() << endl;
286  //cout << " widths=" << widths.transpose() << endl;
287  //cout << " offsets=" << offsets.transpose() << endl;
288  //cout << " slopes=" << slopes.transpose() << endl;
289 
290  //bool asymmetric_kernels=false;
291  bool normalized_basis_functions=true;
292  bool lines_pivot_at_max_activation=true;
293 
294  return new UnifiedModel(centers,widths,slopes,offsets,
295  normalized_basis_functions, lines_pivot_at_max_activation);
296 }
297 
298 template<class Archive>
299 void ModelParametersLWPR::serialize(Archive & ar, const unsigned int version)
300 {
301  // serialize base class information
302  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(ModelParameters);
303 
304  std::cerr << "ERROR: Don't know how to serialize ModelParametersLWPR yet..." << std::endl;
305 
306  /*
307  ar & BOOST_SERIALIZATION_NVP(centers_);
308  ar & BOOST_SERIALIZATION_NVP(widths_);
309  ar & BOOST_SERIALIZATION_NVP(slopes_);
310  ar & BOOST_SERIALIZATION_NVP(offsets_);
311  ar & BOOST_SERIALIZATION_NVP(asymmetric_kernels_);
312  ar & BOOST_SERIALIZATION_NVP(lines_pivot_at_max_activation_);
313  ar & BOOST_SERIALIZATION_NVP(slopes_as_angles_);
314  ar & BOOST_SERIALIZATION_NVP(all_values_vector_size_);
315  ar & BOOST_SERIALIZATION_NVP(caching_);
316  */
317 }
318 
319 /*
320 // void FunctionApproximatorLWPR::save(const char* file)
321 // {
322 // lwpr_object_->writeBinary(file);
323 // }
324 */
325 
326 }
UnifiedModel class header file.
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.
void setParameterVectorAll(const Eigen::VectorXd &values)
Set all available parameter values with one vector.
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...
ModelParametersLWPR class header file.
void getParameterVectorAll(Eigen::VectorXd &all_values) const
Return a vector that returns all available parameter values.
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
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.
BOOST_CLASS_EXPORT_IMPLEMENT(DmpBbo::ModelParametersLWPR)
For boost::serialization.
UnifiedModel * toUnifiedModel(void) const
Convert these LWPR model parameters to unified model parameters.
std::string toString(void) const
Returns a string representation of the object.
Model parameters for the Locally Weighted Projection Regression (LWPR) function approximator.
int getExpectedInputDim(void) const
The expected dimensionality of the input data.
Header file to generate strings from boost serialized files.
Header file for serialization of Eigen matrices.