DMP_BBO library
Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends | List of all members
FunctionApproximator Class Referenceabstract

Base class for all function approximators. More...

#include <FunctionApproximator.hpp>

Inheritance diagram for FunctionApproximator:
Inheritance graph
[legend]
Collaboration diagram for FunctionApproximator:
Collaboration graph
[legend]

Public Member Functions

 FunctionApproximator (const MetaParameters *const meta_parameters, const ModelParameters *const model_parameters=NULL)
 Initialize a function approximator with meta- and optionally model-parameters. More...
 
 FunctionApproximator (const ModelParameters *const model_parameters)
 Initialize a function approximator with model-parameters. More...
 
virtual FunctionApproximatorclone (void) const =0
 Return a pointer to a deep copy of the FunctionApproximator object. More...
 
virtual void train (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)=0
 Train the function approximator with corresponding input and target examples. More...
 
void train (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets, std::string save_directory, bool overwrite=false)
 Train the function approximator with corresponding input and target examples (and write results to file). More...
 
void reTrain (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)
 Re-train the function approximator with corresponding input and target examples. More...
 
void reTrain (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets, std::string save_directory, bool overwrite=false)
 Re-train the function approximator with corresponding input and target examples (and write results to file). More...
 
virtual void predict (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs)=0
 Query the function approximator to make a prediction. More...
 
virtual void predict (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs, Eigen::MatrixXd &variances)
 Query the function approximator to make a prediction, and also to predict its variance. More...
 
virtual void predict (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs, std::vector< Eigen::MatrixXd > &variances)
 Query the function approximator to make a prediction, and also to predict its variance. More...
 
virtual void predictVariance (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &variances)
 Query the function approximator to get the variance of a prediction This function is not implemented by all function approximators. More...
 
bool isTrained (void) const
 Determine whether the function approximator has already been trained with data or not. More...
 
int getExpectedInputDim (void) const
 The expected dimensionality of the input data. More...
 
int getExpectedOutputDim (void) const
 The expected dimensionality of the output data. More...
 
virtual std::string getName (void) const =0
 Get the name of this function approximator. More...
 
void getSelectableParameters (std::set< std::string > &selected_values_labels) const
 Return all the names of the parameter types that can be selected. More...
 
void setSelectedParameters (const std::set< std::string > &selected_values_labels)
 Determine which subset of parameters is represented in the vector returned by Parameterizable::getParameterVectorSelected. More...
 
void getParameterVectorSelectedMinMax (Eigen::VectorXd &min, Eigen::VectorXd &max) const
 Get the minimum and maximum of the selected parameters in one vector. More...
 
int getParameterVectorSelectedSize (void) const
 Get the size of the vector of selected parameters, as returned by getParameterVectorSelected(. More...
 
void setParameterVectorSelected (const Eigen::VectorXd &values, bool normalized=false)
 Set all the values of the selected parameters with one vector. More...
 
void getParameterVectorSelected (Eigen::VectorXd &values, bool normalized=false) const
 Get the values of the selected parameters in one vector. More...
 
void getParameterVectorMask (const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const
 Get a mask for selecting parameters. More...
 
int getParameterVectorAllSize (void) const
 Get the size of the parameter values vector when it contains all available parameter values. More...
 
void getParameterVectorAll (Eigen::VectorXd &values) const
 Return a vector that returns all available parameter values. More...
 
void setParameterVectorAll (const Eigen::VectorXd &values)
 Set all available parameter values with one vector. More...
 
UnifiedModelgetUnifiedModel (void) const
 Return a representation of this function approximator's model as a unified model. More...
 
std::string toString (void) const
 Returns a string representation of the object. More...
 
const MetaParametersgetMetaParameters (void) const
 Accessor for FunctionApproximator::meta_parameters_. More...
 
const ModelParametersgetModelParameters (void) const
 Accessor for FunctionApproximator::model_parameters_. More...
 
void setParameterVectorModifierPrivate (std::string modifier, bool new_value)
 Turn certain modifiers on or off, see Parameterizable::setParameterVectorModifier(). More...
 
virtual bool saveGridData (const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, std::string directory, bool overwrite=false) const
 Generate a grid of inputs, and output the response of the basis functions and line segments for these inputs. More...
 
- Public Member Functions inherited from Parameterizable
virtual ~Parameterizable (void)
 Destructor.
 
virtual void getParameterVectorSelectedNormalized (Eigen::VectorXd &values) const
 Get the normalized values of the selected parameters in one vector. More...
 
void getParameterVectorSelectedMinMax (Eigen::VectorXd &min, Eigen::VectorXd &max) const
 Get the minimum and maximum of the selected parameters in one vector. More...
 
void getParameterVectorAllMinMax (Eigen::VectorXd &min, Eigen::VectorXd &max) const
 Get the minimum and maximum values of the current parameter vector. More...
 
void getParameterVectorSelectedRanges (Eigen::VectorXd &ranges) const
 Get the ranges of the selected parameters, i.e. More...
 
virtual void setParameterVectorSelectedNormalized (const Eigen::VectorXd &values)
 Set all the values of the selected parameters with one vector of normalized values. More...
 
void setSelectedParametersOne (std::string selected)
 Set the parameters that are currently selected. More...
 
void setParameterVectorModifier (std::string modifier, bool new_value)
 Turn certain modifiers on or off. More...
 
void setVectorLengthsPerDimension (const Eigen::VectorXi &lengths_per_dimension)
 The vector (VectorXd) with parameter values can be split into different parts (as vector<VectorXd>; this function specifices the length of each sub-vector. More...
 
Eigen::VectorXi getVectorLengthsPerDimension (void) const
 Get the specified length of each vector in each dimension. More...
 
void getParameterVectorSelected (std::vector< Eigen::VectorXd > &values, bool normalized=false) const
 Get the values of the selected parameters in one vector. More...
 
void setParameterVectorSelected (const std::vector< Eigen::VectorXd > &values, bool normalized=false)
 Set all the values of the selected parameters with a vector of vectors. More...
 

Static Public Member Functions

static void generateInputsGrid (const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, Eigen::MatrixXd &inputs_grid)
 Generate a input samples that lie on a grid (much like Matlab's meshgrid) For instance, if min = [2 6], and max = [3 8], and n_samples_per_dim = [3 5] then this function first makes linearly spaces samples along each dimension, e.g. More...
 

Protected Member Functions

void setModelParameters (ModelParameters *model_parameters)
 Accessor for FunctionApproximator::model_parameters_. More...
 
 FunctionApproximator (void)
 Default constructor. More...
 

Friends

class boost::serialization::access
 Give boost serialization access to private members. More...
 
std::ostream & operator<< (std::ostream &output, const FunctionApproximator &function_approximator)
 Print to output stream. More...
 

Detailed Description

Base class for all function approximators.

Definition at line 48 of file FunctionApproximator.hpp.

Constructor & Destructor Documentation

FunctionApproximator ( const MetaParameters *const  meta_parameters,
const ModelParameters *const  model_parameters = NULL 
)

Initialize a function approximator with meta- and optionally model-parameters.

Parameters
[in]meta_parametersThe training algorithm meta-parameters
[in]model_parametersThe parameters of the trained model. If this parameter is not passed, the function approximator is initialized as untrained. In this case, you must call FunctionApproximator::train() before being able to call FunctionApproximator::predict(). Either meta_parameters XOR model-parameters can passed as NULL, but not both.

Definition at line 49 of file FunctionApproximator.cpp.

50 {
51  // At least one of them should not be NULL
52  assert(meta_parameters!=NULL || model_parameters!=NULL);
53 
54  if (meta_parameters==NULL)
55  meta_parameters_ = NULL;
56  else
57  meta_parameters_ = meta_parameters->clone();
58 
59  if (model_parameters==NULL)
60  model_parameters_ = NULL;
61  else
62  model_parameters_ = model_parameters->clone();
63 
64  // If both meta- and model-parameters were set, check if they have the same expected input dim.
65  if (meta_parameters_!=NULL && model_parameters_!=NULL)
66  assert(model_parameters_->getExpectedInputDim()==meta_parameters_->getExpectedInputDim());
67 
68 }
virtual MetaParameters * clone(void) const =0
Return a pointer to a deep copy of the MetaParameters object.
virtual int getExpectedInputDim(void) const =0
The expected dimensionality of the input data.
int getExpectedInputDim(void) const
The expected dimensionality of the input data.
virtual ModelParameters * clone(void) const =0
Return a pointer to a deep copy of the ModelParameters object.

Here is the call graph for this function:

FunctionApproximator ( const ModelParameters *const  model_parameters)

Initialize a function approximator with model-parameters.

Parameters
[in]model_parametersThe parameters of the trained model.

Definition at line 70 of file FunctionApproximator.cpp.

71 {
72  assert(model_parameters!=NULL);
73  meta_parameters_ = NULL;
74  model_parameters_ = model_parameters->clone();
75 }
virtual ModelParameters * clone(void) const =0
Return a pointer to a deep copy of the ModelParameters object.

Here is the call graph for this function:

FunctionApproximator ( void  )
inlineprotected

Default constructor.

Remarks
This default constuctor is required for boost::serialization to work. Since this constructor should not be called by other classes, it is private (boost::serialization is a friend)

Definition at line 308 of file FunctionApproximator.hpp.

308 {};

Member Function Documentation

virtual FunctionApproximator* clone ( void  ) const
pure virtual

Return a pointer to a deep copy of the FunctionApproximator object.

Returns
Pointer to a deep copy

Implemented in FunctionApproximatorRRRFF, FunctionApproximatorLWPR, FunctionApproximatorGMR, FunctionApproximatorGPR, FunctionApproximatorLWR, and FunctionApproximatorRBFN.

virtual void train ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
const Eigen::Ref< const Eigen::MatrixXd > &  targets 
)
pure virtual

Train the function approximator with corresponding input and target examples.

Parameters
[in]inputsInput values of the training examples
[in]targetsTarget values of the training examples

Implemented in FunctionApproximatorRRRFF, FunctionApproximatorLWPR, FunctionApproximatorGMR, FunctionApproximatorGPR, FunctionApproximatorLWR, and FunctionApproximatorRBFN.

void train ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
const Eigen::Ref< const Eigen::MatrixXd > &  targets,
std::string  save_directory,
bool  overwrite = false 
)

Train the function approximator with corresponding input and target examples (and write results to file).

Parameters
[in]inputsInput values of the training examples
[in]targetsTarget values of the training examples
[in]save_directoryDirectory to which to write results.
[in]overwriteWhether to overwrite existing files. true=do overwrite, false=don't overwrite and give a warning.

Definition at line 288 of file FunctionApproximator.cpp.

289 {
290  train(inputs,targets);
291 
292  if (save_directory.empty())
293  return;
294 
295  if (!isTrained())
296  return;
297 
298  if (getExpectedInputDim()<3)
299  {
300 
301  VectorXd min = inputs.colwise().minCoeff();
302  VectorXd max = inputs.colwise().maxCoeff();
303 
304  int n_samples_per_dim = 100;
305  if (getExpectedInputDim()==2) n_samples_per_dim = 40;
306  VectorXi n_samples_per_dim_vec = VectorXi::Constant(getExpectedInputDim(),n_samples_per_dim);
307 
308  MatrixXd inputs_grid;
309  FunctionApproximator::generateInputsGrid(min, max, n_samples_per_dim_vec, inputs_grid);
310 
311  MatrixXd outputs_grid(inputs_grid.rows(),getExpectedOutputDim());
312  predict(inputs_grid,outputs_grid);
313 
314  saveMatrix(save_directory,"n_samples_per_dim.txt",n_samples_per_dim_vec,overwrite);
315  saveMatrix(save_directory,"inputs_grid.txt",inputs_grid,overwrite);
316  saveMatrix(save_directory,"outputs_grid.txt",outputs_grid,overwrite);
317 
318 
319  MatrixXd variances_grid(inputs_grid.rows(),getExpectedOutputDim());
320  predictVariance(inputs_grid,variances_grid);
321  if (!variances_grid.size()==0)
322  {
323  variances_grid = variances_grid.array().sqrt();
324  saveMatrix(save_directory,"variances_grid.txt",variances_grid,overwrite);
325  }
326 
327  saveGridData(min, max, n_samples_per_dim_vec, save_directory, overwrite);
328 
329  }
330 
331  MatrixXd outputs(inputs.rows(),getExpectedOutputDim());
332  predict(inputs,outputs);
333 
334 
335  // saveMatrix does not accept Ref, but only Matrix
336  MatrixXd save_matrix;
337  save_matrix = inputs;
338  saveMatrix(save_directory,"inputs.txt",save_matrix,overwrite);
339  save_matrix = targets;
340  saveMatrix(save_directory,"targets.txt",save_matrix,overwrite);
341  save_matrix = outputs;
342  saveMatrix(save_directory,"outputs.txt",save_matrix,overwrite);
343 
344 
345 
346  string filename = save_directory+"/plotdata.py";
347  ofstream outfile;
348  outfile.open(filename.c_str());
349  if (!outfile.is_open())
350  {
351  cerr << __FILE__ << ":" << __LINE__ << ":";
352  cerr << "Could not open file " << filename << " for writing." << endl;
353  }
354  else
355  {
356  // Python code generation in C++. Rock 'n' roll! ;-)
357  if (inputs.cols()==2) {
358  outfile << "from mpl_toolkits.mplot3d import Axes3D \n";
359  }
360  outfile << "import numpy \n";
361  outfile << "import matplotlib.pyplot as plt \n";
362  outfile << "directory = '" << save_directory << "' \n";
363  outfile << "inputs = numpy.loadtxt(directory+'/inputs.txt') \n";
364  outfile << "targets = numpy.loadtxt(directory+'/targets.txt') \n";
365  outfile << "outputs = numpy.loadtxt(directory+'/outputs.txt') \n";
366  outfile << "fig = plt.figure() \n";
367  if (inputs.cols()==2) {
368  outfile << "ax = Axes3D(fig) \n";
369  outfile << "ax.plot(inputs[:,0],inputs[:,1],targets, '.', label='targets',color='black') \n";
370  outfile << "ax.plot(inputs[:,0],inputs[:,1],outputs, '.', label='predictions',color='red')\n";
371  outfile << "ax.set_xlabel('input_1'); ax.set_ylabel('input_2'); ax.set_zlabel('output') \n";
372  outfile << "ax.legend(loc='lower right') \n";
373  } else {
374  outfile << "plt.plot(inputs,targets, '.', label='targets',color='black') \n";
375  outfile << "plt.plot(inputs,outputs, '.', label='predictions',color='red') \n";
376  outfile << "plt.xlabel('input'); plt.ylabel('output'); \n";
377  outfile << "plt.legend(loc='lower right') \n";
378  }
379  outfile << "plt.show() \n";
380  outfile << endl;
381 
382  outfile.close();
383  //cout << " ______________________________________________________________" << endl;
384  //cout << " | Plot saved data with:" << " 'python " << filename << "'." << endl;
385  //cout << " |______________________________________________________________" << endl;
386  }
387 
388 }
virtual bool saveGridData(const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, std::string directory, bool overwrite=false) const
Generate a grid of inputs, and output the response of the basis functions and line segments for these...
virtual void train(const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)=0
Train the function approximator with corresponding input and target examples.
static void generateInputsGrid(const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, Eigen::MatrixXd &inputs_grid)
Generate a input samples that lie on a grid (much like Matlab&#39;s meshgrid) For instance, if min = [2 6], and max = [3 8], and n_samples_per_dim = [3 5] then this function first makes linearly spaces samples along each dimension, e.g.
bool saveMatrix(std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > matrix, bool overwrite=false)
Save an Eigen matrix to an ASCII file.
bool isTrained(void) const
Determine whether the function approximator has already been trained with data or not...
virtual void predictVariance(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &variances)
Query the function approximator to get the variance of a prediction This function is not implemented ...
int getExpectedOutputDim(void) const
The expected dimensionality of the output data.
virtual void predict(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs)=0
Query the function approximator to make a prediction.
int getExpectedInputDim(void) const
The expected dimensionality of the input data.

Here is the call graph for this function:

void reTrain ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
const Eigen::Ref< const Eigen::MatrixXd > &  targets 
)

Re-train the function approximator with corresponding input and target examples.

Parameters
[in]inputsInput values of the training examples
[in]targetsTarget values of the training examples Re-training could in principle have been enabled through FunctionApproximator::train, but we wanted to keep a clear disctinction between training (which must be done at least once before FunctionApproximator::predict) can be called and re-training.

Definition at line 130 of file FunctionApproximator.cpp.

131 {
132  delete model_parameters_;
133  model_parameters_ = NULL;
134  train(inputs,targets);
135 }
virtual void train(const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)=0
Train the function approximator with corresponding input and target examples.
void reTrain ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
const Eigen::Ref< const Eigen::MatrixXd > &  targets,
std::string  save_directory,
bool  overwrite = false 
)

Re-train the function approximator with corresponding input and target examples (and write results to file).

Parameters
[in]inputsInput values of the training examples
[in]targetsTarget values of the training examples
[in]save_directoryDirectory to which to write results.
[in]overwriteOverwrite existing files in the directory above (default: false) Re-training could in principle have been enabled through FunctionApproximator::train, but we wanted to keep a clear disctinction between training (which must be done at least once before FunctionApproximator::predict) can be called and re-training.

Definition at line 137 of file FunctionApproximator.cpp.

138 {
139  delete model_parameters_;
140  model_parameters_ = NULL;
141  train(inputs,targets,save_directory,overwrite);
142 }
virtual void train(const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)=0
Train the function approximator with corresponding input and target examples.
virtual void predict ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
Eigen::MatrixXd &  outputs 
)
pure virtual

Query the function approximator to make a prediction.

Parameters
[in]inputsInput values of the query
[out]outputsPredicted output values
Remarks
This method should be const. But third party functions which is called in this function have not always been implemented as const (Examples: LWPRObject::predict or RRRFF::predict ). Therefore, this function cannot be const.

Implemented in FunctionApproximatorLWR, FunctionApproximatorRRRFF, FunctionApproximatorGMR, FunctionApproximatorGPR, FunctionApproximatorLWPR, and FunctionApproximatorRBFN.

virtual void predict ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
Eigen::MatrixXd &  outputs,
Eigen::MatrixXd &  variances 
)
inlinevirtual

Query the function approximator to make a prediction, and also to predict its variance.

Parameters
[in]inputsInput values of the query (n_samples X n_dims_in)
[out]outputsPredicted output values (n_samples X n_dims_out)
[out]variancesPredicted variances for the output values (n_samples X n_dims_out). Note that if the output has a dimensionality>1, these variances should actuall be covariance matrices (use function predict(const Eigen::Ref<const Eigen::MatrixXd>& inputs, Eigen::MatrixXd& outputs, std::vector<Eigen::MatrixXd>& variances) to get the full covariance matrices). So for an output dimensionality of 1 this function works fine. For dimensionality>1 we return only the diagional of the covariance matrix, which may not always be what you want.
Remarks
This method should be const. But third party functions which is called in this function have not always been implemented as const (Examples: LWPRObject::predict or RRRFF::predict ). Therefore, this function cannot be const.

Reimplemented in FunctionApproximatorGMR.

Definition at line 142 of file FunctionApproximator.hpp.

146  {
147  predict(inputs, outputs);
148  variances.fill(0);
149  }
virtual void predict(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs)=0
Query the function approximator to make a prediction.

Here is the call graph for this function:

virtual void predict ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
Eigen::MatrixXd &  outputs,
std::vector< Eigen::MatrixXd > &  variances 
)
inlinevirtual

Query the function approximator to make a prediction, and also to predict its variance.

Parameters
[in]inputsInput values of the query (n_samples X n_dims_in)
[out]outputsPredicted output values (n_samples X n_dims_out)
[out]variancesPredicted covariance matrices for the output values. It is is of size (n_samples X n_dims_out X n_dims_out), which has been implemented as a std::vector of Eigen::MatrixXd.
Remarks
This method should be const. But third party functions which is called in this function have not always been implemented as const (Examples: LWPRObject::predict or RRRFF::predict ). Therefore, this function cannot be const.

Definition at line 160 of file FunctionApproximator.hpp.

164  {
165  predict(inputs, outputs);
166  for (unsigned int i=0; i<variances.size(); i++)
167  variances[i].fill(0);
168  }
virtual void predict(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs)=0
Query the function approximator to make a prediction.

Here is the call graph for this function:

virtual void predictVariance ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
Eigen::MatrixXd &  variances 
)
inlinevirtual

Query the function approximator to get the variance of a prediction This function is not implemented by all function approximators.

Therefore, the default implementation fills outputs with 0s.

Parameters
[in]inputsInput values of the query (n_samples X n_dims_in)
[out]variancesPredicted variances for the output values (n_samples X n_dims_out). Note that if the output has a dimensionality>1, these variances should actuall be covariance matrices (use function predict(const Eigen::Ref<const Eigen::MatrixXd>& inputs, Eigen::MatrixXd& outputs, std::vector<Eigen::MatrixXd>& variances) to get the full covariance matrices). So for an output dimensionality of 1 this function works fine. For dimensionality>1 we return only the diagional of the covariance matrix, which may not always be what you want.
Remarks
This method should be const. But third party functions which is called in this function have not always been implemented as const (Examples: LWPRObject::predict). Therefore, this function cannot be const.

Reimplemented in FunctionApproximatorGMR, and FunctionApproximatorGPR.

Definition at line 181 of file FunctionApproximator.hpp.

184  {
185  variances.fill(0);
186  }
bool isTrained ( void  ) const
inline

Determine whether the function approximator has already been trained with data or not.

Returns
true if the function approximator has already been trained, false otherwise.

Definition at line 191 of file FunctionApproximator.hpp.

192  {
193  return (model_parameters_!=NULL);
194  }

Here is the call graph for this function:

int getExpectedInputDim ( void  ) const

The expected dimensionality of the input data.

Returns
Expected dimensionality of the input data

Definition at line 114 of file FunctionApproximator.cpp.

115 {
116  if (model_parameters_!=NULL)
117  return model_parameters_->getExpectedInputDim();
118  else
119  return meta_parameters_->getExpectedInputDim();
120 }
virtual int getExpectedInputDim(void) const =0
The expected dimensionality of the input data.
int getExpectedInputDim(void) const
The expected dimensionality of the input data.

Here is the call graph for this function:

int getExpectedOutputDim ( void  ) const

The expected dimensionality of the output data.

Returns
Expected dimensionality of the output data
Todo:
"int getExpectedOutputDim(void) const" should be pure virtual

Definition at line 122 of file FunctionApproximator.cpp.

123 {
124  if (model_parameters_!=NULL)
125  return model_parameters_->getExpectedOutputDim();
126  else
127  return meta_parameters_->getExpectedOutputDim();
128 }
virtual int getExpectedOutputDim(void) const
The expected dimensionality of the output data.
virtual int getExpectedOutputDim(void) const
The expected dimensionality of the output data.
virtual std::string getName ( void  ) const
pure virtual

Get the name of this function approximator.

Returns
Name of this function approximator

Implemented in FunctionApproximatorLWR, FunctionApproximatorRBFN, FunctionApproximatorGMR, FunctionApproximatorGPR, FunctionApproximatorRRRFF, and FunctionApproximatorLWPR.

void getSelectableParameters ( std::set< std::string > &  selected_values_labels) const
virtual

Return all the names of the parameter types that can be selected.

Parameters
[out]selected_values_labelsNames of the parameter types that can be selected

Implements Parameterizable.

Definition at line 200 of file FunctionApproximator.cpp.

201 {
202  if (checkModelParametersInitialized())
203  model_parameters_->getSelectableParameters(labels);
204  else
205  labels.clear();
206 }
virtual void getSelectableParameters(std::set< std::string > &selected_values_labels) const =0
Return all the names of the parameter types that can be selected.
void setSelectedParameters ( const std::set< std::string > &  selected_values_labels)
virtual

Determine which subset of parameters is represented in the vector returned by Parameterizable::getParameterVectorSelected.

Different function approximators have different types of model parameters. For instance, LWR has the centers and widths of basis functions, along with the slopes of each line segment. Parameterizable::setSelectedParameters provides a means to determine which parameters should be returned by Parameterizable::getParameterVectorSelected, i.e. by calling: std::set<std::string> selected; selected.insert("slopes"); model_parameters.setSelectedParameters(selected)

Parameters
[in]selected_values_labelsThe names of the parameters that are selected

Reimplemented from Parameterizable.

Definition at line 194 of file FunctionApproximator.cpp.

195 {
196  if (checkModelParametersInitialized())
197  model_parameters_->setSelectedParameters(selected_values_labels);
198 }
virtual void setSelectedParameters(const std::set< std::string > &selected_values_labels)
Determine which subset of parameters is represented in the vector returned by Parameterizable::getPar...
void getParameterVectorSelectedMinMax ( Eigen::VectorXd &  min,
Eigen::VectorXd &  max 
) const

Get the minimum and maximum of the selected parameters in one vector.

Parameters
[out]minThe minimum of the selected parameters concatenated in one vector
[out]maxThe minimum of the selected parameters concatenated in one vector

Definition at line 145 of file FunctionApproximator.cpp.

146 {
147  if (model_parameters_==NULL)
148  {
149  cerr << __FILE__ << ":" << __LINE__ << ": Warning: Trying to access model parameters of the function approximator, but it has not been trained yet. Returning empty parameter vector." << endl;
150  min.resize(0);
151  max.resize(0);
152  return;
153  }
154 
155  model_parameters_->getParameterVectorSelectedMinMax(min,max);
156 }
void getParameterVectorSelectedMinMax(Eigen::VectorXd &min, Eigen::VectorXd &max) const
Get the minimum and maximum of the selected parameters in one vector.
int getParameterVectorSelectedSize ( void  ) const
virtual

Get the size of the vector of selected parameters, as returned by getParameterVectorSelected(.

Returns
The size of the vector representation of the selected parameters.

Reimplemented from Parameterizable.

Definition at line 180 of file FunctionApproximator.cpp.

181 {
182  if (checkModelParametersInitialized())
183  return model_parameters_->getParameterVectorSelectedSize();
184  else
185  return 0;
186 }
virtual int getParameterVectorSelectedSize(void) const
Get the size of the vector of selected parameters, as returned by getParameterVectorSelected(.
void setParameterVectorSelected ( const Eigen::VectorXd &  values,
bool  normalized = false 
)
virtual

Set all the values of the selected parameters with one vector.

Parameters
[in]valuesThe new values of the selected parameters in one vector
[in]normalizedWhether the data is normalized or not

Reimplemented from Parameterizable.

Definition at line 188 of file FunctionApproximator.cpp.

189 {
190  if (checkModelParametersInitialized())
191  model_parameters_->setParameterVectorSelected(values, normalized);
192 }
virtual void setParameterVectorSelected(const Eigen::VectorXd &values, bool normalized=false)
Set all the values of the selected parameters with one vector.
void getParameterVectorSelected ( Eigen::VectorXd &  values,
bool  normalized = false 
) const
virtual

Get the values of the selected parameters in one vector.

Parameters
[out]valuesThe selected parameters concatenated in one vector
[in]normalizedWhether to normalize the data or not

Reimplemented from Parameterizable.

Definition at line 171 of file FunctionApproximator.cpp.

172 {
173  if (checkModelParametersInitialized())
174  model_parameters_->getParameterVectorSelected(values, normalized);
175  else
176  values.resize(0);
177 }
virtual void getParameterVectorSelected(Eigen::VectorXd &values, bool normalized=false) const
Get the values of the selected parameters in one vector.
void getParameterVectorMask ( const std::set< std::string >  selected_values_labels,
Eigen::VectorXi &  selected_mask 
) const
virtual

Get a mask for selecting parameters.

Parameters
[in]selected_values_labelsLabels of the selected parameter values
[out]selected_maskA mask indicating indices of selected parameters. 0 indicates not selected, >0 indicates selected.

For instance, if the parameters consists of centers, widths and slopes the parameter values vector will be something like

    centers     widths    slopes
[ 100 110 120 10 10 10 0.4 0.7 0.4 ]

In this case, if selected_values_labels contains "centers" and "slopes", the mask will be:

    centers     widths    slopes
[   1   1   1  0  0  0   3   3   3 ]

The '0' indicates that these parameters are not selected. The other ones have different numbers so that they may be discerned from one another (as required in Parameterizable::getParameterVectorSelectedMinMax for instance.

Implements Parameterizable.

Definition at line 208 of file FunctionApproximator.cpp.

208  {
209  if (checkModelParametersInitialized())
210  model_parameters_->getParameterVectorMask(selected_values_labels,selected_mask);
211  else
212  selected_mask.resize(0);
213 
214 };
virtual void getParameterVectorMask(const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const =0
Get a mask for selecting parameters.
int getParameterVectorAllSize ( void  ) const
virtual

Get the size of the parameter values vector when it contains all available parameter values.

Returns
The size of the parameter vector

For instance, if the parameters consists of centers, widths and slopes the parameter values vector will be something like

    centers     widths    slopes
[ 100 110 120 10 10 10 0.4 0.7 0.4 ]

then getParameterVectorAllSize() will return 9

Implements Parameterizable.

Definition at line 215 of file FunctionApproximator.cpp.

215  {
216  if (checkModelParametersInitialized())
217  return model_parameters_->getParameterVectorAllSize();
218  else
219  return 0;
220 };
virtual int getParameterVectorAllSize(void) const =0
Get the size of the parameter values vector when it contains all available parameter values...
void getParameterVectorAll ( Eigen::VectorXd &  values) const
virtual

Return a vector that returns all available parameter values.

Parameters
[out]valuesAll available parameter values in one vector.
Remarks
Contrast this with Parameterizable::getParameterVectorSelected, which return only the SELECTED parameter values. Selecting parameters is done with Parameterizable::setSelectedParameters

Implements Parameterizable.

Definition at line 221 of file FunctionApproximator.cpp.

221  {
222  if (checkModelParametersInitialized())
223  model_parameters_->getParameterVectorAll(values);
224  else
225  values.resize(0);
226 };
virtual void getParameterVectorAll(Eigen::VectorXd &values) const =0
Return a vector that returns all available parameter values.
void setParameterVectorAll ( const Eigen::VectorXd &  values)
virtual

Set all available parameter values with one vector.

Parameters
[in]valuesAll available parameter values in one vector.
Remarks
Contrast this with Parameterizable::setParameterVectorSelected, which sets only the SELECTED parameter values. Selecting parameters is done with Parameterizable::setSelectedParameters

Implements Parameterizable.

Definition at line 227 of file FunctionApproximator.cpp.

227  {
228  if (checkModelParametersInitialized())
229  model_parameters_->setParameterVectorAll(values);
230 };
virtual void setParameterVectorAll(const Eigen::VectorXd &values)=0
Set all available parameter values with one vector.
UnifiedModel * getUnifiedModel ( void  ) const

Return a representation of this function approximator's model as a unified model.

See also the page on Unified Model for Function Approximators

Returns
Unified model representation of this function approximator's model.

Definition at line 108 of file FunctionApproximator.cpp.

109 {
110  return model_parameters_->toUnifiedModel();
111 }
virtual UnifiedModel * toUnifiedModel(void) const =0
Convert these model parameters to unified model parameters.
string toString ( void  ) const

Returns a string representation of the object.

Returns
A string representation of the object.

Definition at line 232 of file FunctionApproximator.cpp.

233 {
234  string name = "FunctionApproximator"+getName();
236 }
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
virtual std::string getName(void) const =0
Get the name of this function approximator.
const MetaParameters * getMetaParameters ( void  ) const

Accessor for FunctionApproximator::meta_parameters_.

Returns
The meta-parameters of the algorithm

Definition at line 85 of file FunctionApproximator.cpp.

86 {
87  return meta_parameters_;
88 };
const ModelParameters * getModelParameters ( void  ) const

Accessor for FunctionApproximator::model_parameters_.

Returns
The model parameters of the trained model

Definition at line 91 of file FunctionApproximator.cpp.

92 {
93  return model_parameters_;
94 };
void setParameterVectorModifierPrivate ( std::string  modifier,
bool  new_value 
)
virtual

Turn certain modifiers on or off, see Parameterizable::setParameterVectorModifier().

Parameterizable::setParameterVectorModifierPrivate(), This function may (but must not be) overridden by subclasses of Parameterizable, depending on whether the subclass has modifiers (or not)

Parameters
[in]modifierThe name of the modifier
[in]new_valueWhether to turn the modifier on (true) or off (false)

Reimplemented from Parameterizable.

Definition at line 390 of file FunctionApproximator.cpp.

391 {
392  model_parameters_->setParameterVectorModifier(modifier,new_value);
393 }
void setParameterVectorModifier(std::string modifier, bool new_value)
Turn certain modifiers on or off.
void generateInputsGrid ( const Eigen::VectorXd &  min,
const Eigen::VectorXd &  max,
const Eigen::VectorXi &  n_samples_per_dim,
Eigen::MatrixXd &  inputs_grid 
)
static

Generate a input samples that lie on a grid (much like Matlab's meshgrid) For instance, if min = [2 6], and max = [3 8], and n_samples_per_dim = [3 5] then this function first makes linearly spaces samples along each dimension, e.g.

samples_dim1 = [2 2.5 3] and samples_dim2 = [6 6.5 7 7.5 8] and then combine the combination of samples along all dimensions: inputs_grid = [2 6; 2 6.5; 2 7; 2 7.5 2 8; 2.5 6; 2.5 6.5; 2.5 7; 2.5 7.5 2.5 8; 3 6; 3 6.5; 3 7; 3 7.5 3 8;

Parameters
[in]minMinimum values in the grid along each dimension
[in]maxMaximum values in the grid along each dimension
[in]n_samples_per_dimNumber of samples along each dimension in the grid
[out]inputs_gridA grid of samples

Definition at line 238 of file FunctionApproximator.cpp.

239 {
240  int n_dims = min.size();
241  assert(n_dims==max.size());
242  assert(n_dims==n_samples_per_dim.size());
243 
244  if (n_dims==1)
245  {
246  inputs_grid = VectorXd::LinSpaced(n_samples_per_dim[0], min[0], max[0]);
247  }
248  else if (n_dims==2)
249  {
250  int n_samples = n_samples_per_dim[0]*n_samples_per_dim[1];
251  inputs_grid = MatrixXd::Zero(n_samples, n_dims);
252  VectorXd x1 = VectorXd::LinSpaced(n_samples_per_dim[0], min[0], max[0]);
253  VectorXd x2 = VectorXd::LinSpaced(n_samples_per_dim[1], min[1], max[1]);
254  for (int ii=0; ii<x1.size(); ii++)
255  {
256  for (int jj=0; jj<x2.size(); jj++)
257  {
258  inputs_grid(ii*x2.size()+jj,0) = x1[ii];
259  inputs_grid(ii*x2.size()+jj,1) = x2[jj];
260  }
261  }
262  }
263  else
264  {
265  cerr << __FILE__ << ":" << __LINE__ << ":";
266  cerr << "Can only generate input grids for n_dims<3, but found " << n_dims << endl;
267  }
268 }
bool saveGridData ( const Eigen::VectorXd &  min,
const Eigen::VectorXd &  max,
const Eigen::VectorXi &  n_samples_per_dim,
std::string  directory,
bool  overwrite = false 
) const
virtual

Generate a grid of inputs, and output the response of the basis functions and line segments for these inputs.

This function is not pure virtual, because this might not make sense for every model parameters class.

Parameters
[in]minMinimum values for the grid (one for each dimension)
[in]maxMaximum values for the grid (one for each dimension)
[in]n_samples_per_dimNumber of samples in the grid along each dimension
[in]directoryDirectory to which to save the results to.
[in]overwriteWhether to overwrite existing files. true=do overwrite, false=don't overwrite and give a warning.
Returns
Whether saving the data was successful.

Reimplemented in FunctionApproximatorLWR, FunctionApproximatorRBFN, FunctionApproximatorGPR, and FunctionApproximatorRRRFF.

Definition at line 270 of file FunctionApproximator.cpp.

271 {
272  if (save_directory.empty())
273  return true;
274 
275  //MatrixXd inputs;
276  //FunctionApproximator::generateInputsGrid(min, max, n_samples_per_dim, inputs);
277 
278  if (model_parameters_==NULL)
279  return false;
280  UnifiedModel* mp_unified = model_parameters_->toUnifiedModel();
281  if (mp_unified==NULL)
282  return false;
283 
284  return mp_unified->saveGridData(min,max,n_samples_per_dim,save_directory,overwrite);
285 
286 }
bool saveGridData(const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, std::string directory, bool overwrite=false) const
Generate a grid of inputs, and output the response of the basis functions and line segments for these...
virtual UnifiedModel * toUnifiedModel(void) const =0
Convert these model parameters to unified model parameters.

Here is the call graph for this function:

void setModelParameters ( ModelParameters model_parameters)
protected

Accessor for FunctionApproximator::model_parameters_.

Parameters
[in]model_parametersThe model parameters of a trained model

Definition at line 97 of file FunctionApproximator.cpp.

98 {
99  if (model_parameters_!=NULL)
100  {
101  delete model_parameters_;
102  model_parameters_ = NULL;
103  }
104 
105  model_parameters_ = model_parameters;
106 }

Friends And Related Function Documentation

friend class boost::serialization::access
friend

Give boost serialization access to private members.

Definition at line 331 of file FunctionApproximator.hpp.

std::ostream& operator<< ( std::ostream &  output,
const FunctionApproximator function_approximator 
)
friend

Print to output stream.

Parameters
[in]outputOutput stream to which to write to
[in]function_approximatorFunction approximator to write
Returns
Output stream
Remarks
Calls virtual function FunctionApproximator::toString, which must be implemented by subclasses: http://stackoverflow.com/questions/4571611/virtual-operator

Definition at line 244 of file FunctionApproximator.hpp.

244  {
245  output << function_approximator.toString();
246  return output;
247  }

The documentation for this class was generated from the following files: