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> 46 #include <eigen3/Eigen/Core> 50 using namespace Eigen;
54 ModelParametersLWR::ModelParametersLWR(
const Eigen::MatrixXd& centers,
const Eigen::MatrixXd& widths,
const Eigen::MatrixXd& slopes,
const Eigen::MatrixXd& offsets,
bool asymmetric_kernels,
bool lines_pivot_at_max_activation)
60 asymmetric_kernels_(asymmetric_kernels),
61 lines_pivot_at_max_activation_(lines_pivot_at_max_activation),
62 slopes_as_angles_(false),
65 #ifndef NDEBUG // Variables below are only required for asserts; check for NDEBUG to avoid warnings. 66 int n_basis_functions = centers.rows();
67 int n_dims = centers.cols();
69 assert(n_basis_functions==widths_.rows());
70 assert(n_dims ==widths_.cols());
71 assert(n_basis_functions==slopes_.rows());
72 assert(n_dims ==slopes_.cols());
73 assert(n_basis_functions==offsets_.rows());
74 assert(1 ==offsets_.cols());
76 all_values_vector_size_ = 0;
77 all_values_vector_size_ += centers_.rows()*centers_.cols();
78 all_values_vector_size_ += widths_.rows() *widths_.cols();
79 all_values_vector_size_ += offsets_.rows()*offsets_.cols();
80 all_values_vector_size_ += slopes_.rows() *slopes_.cols();
85 return new ModelParametersLWR(centers_,widths_,slopes_,offsets_,asymmetric_kernels_,lines_pivot_at_max_activation_);
90 ENTERING_REAL_TIME_CRITICAL_CODE
91 bool normalized_basis_functions=
false;
92 BasisFunction::Gaussian::activations(centers_,widths_,inputs,kernel_activations,
93 normalized_basis_functions,asymmetric_kernels_);
94 EXITING_REAL_TIME_CRITICAL_CODE
103 if ( inputs.rows()==inputs_cached_.rows() && inputs.cols()==inputs_cached_.cols() )
106 if ( (inputs.array()==inputs_cached_.array()).all() )
109 kernel_activations = kernel_activations_cached_;
115 ENTERING_REAL_TIME_CRITICAL_CODE
118 bool normalize_activations =
true;
119 BasisFunction::Gaussian::activations(centers_,widths_,inputs,kernel_activations,normalize_activations,asymmetric_kernels_);
121 EXITING_REAL_TIME_CRITICAL_CODE
126 inputs_cached_ = inputs;
127 kernel_activations_cached_ = kernel_activations;
135 if (lines_pivot_at_max_activation_ == lines_pivot_at_max_activation)
149 int n_lines = centers_.rows();
150 VectorXd ac(n_lines);
151 for (
int i_line=0; i_line<n_lines; i_line++)
153 ac[i_line] = slopes_.row(i_line) * centers_.row(i_line).transpose();
156 if (lines_pivot_at_max_activation)
160 offsets_ = offsets_ + ac;
166 offsets_ = offsets_ - ac;
173 lines_pivot_at_max_activation_ = lines_pivot_at_max_activation;
178 slopes_as_angles_ = slopes_as_angles;
179 cerr << __FILE__ <<
":" << __LINE__ <<
":";
180 cerr <<
"Not implemented yet!!!" << endl;
181 slopes_as_angles_ =
false;
218 ENTERING_REAL_TIME_CRITICAL_CODE
227 int n_time_steps = inputs.rows();
228 int n_lines = centers_.rows();
229 lines.resize(n_time_steps,n_lines);
235 for (
int i_line=0; i_line<n_lines; i_line++)
237 lines.col(i_line).noalias() = inputs*slopes_.row(i_line).transpose();
238 lines.col(i_line).array() += offsets_(i_line);
240 if (lines_pivot_at_max_activation_)
244 double ac = slopes_.row(i_line).dot(centers_.row(i_line));
245 lines.col(i_line).array() -= ac;
249 EXITING_REAL_TIME_CRITICAL_CODE
291 template<
class Archive>
292 void ModelParametersLWR::serialize(Archive & ar,
const unsigned int version)
297 ar & BOOST_SERIALIZATION_NVP(centers_);
298 ar & BOOST_SERIALIZATION_NVP(widths_);
299 ar & BOOST_SERIALIZATION_NVP(slopes_);
300 ar & BOOST_SERIALIZATION_NVP(offsets_);
301 ar & BOOST_SERIALIZATION_NVP(asymmetric_kernels_);
302 ar & BOOST_SERIALIZATION_NVP(lines_pivot_at_max_activation_);
303 ar & BOOST_SERIALIZATION_NVP(slopes_as_angles_);
304 ar & BOOST_SERIALIZATION_NVP(all_values_vector_size_);
305 ar & BOOST_SERIALIZATION_NVP(caching_);
315 selected_values_labels = set<string>();
316 selected_values_labels.insert(
"centers");
317 selected_values_labels.insert(
"widths");
318 selected_values_labels.insert(
"offsets");
319 selected_values_labels.insert(
"slopes");
327 selected_mask.fill(0);
333 size = centers_.rows()*centers_.cols();
334 if (selected_values_labels.find(
"centers")!=selected_values_labels.end())
335 selected_mask.segment(offset,size).fill(1);
339 size = widths_.rows()*widths_.cols();
340 if (selected_values_labels.find(
"widths")!=selected_values_labels.end())
341 selected_mask.segment(offset,size).fill(2);
345 size = offsets_.rows()*offsets_.cols();
346 if (selected_values_labels.find(
"offsets")!=selected_values_labels.end())
347 selected_mask.segment(offset,size).fill(3);
351 size = slopes_.rows()*slopes_.cols();
352 if (selected_values_labels.find(
"slopes")!=selected_values_labels.end())
353 selected_mask.segment(offset,size).fill(4);
364 for (
int i_dim=0; i_dim<centers_.cols(); i_dim++)
366 values.segment(offset,centers_.rows()) = centers_.col(i_dim);
367 offset += centers_.rows();
370 for (
int i_dim=0; i_dim<widths_.cols(); i_dim++)
372 values.segment(offset,widths_.rows()) = widths_.col(i_dim);
373 offset += widths_.rows();
376 values.segment(offset,offsets_.rows()) = offsets_;
377 offset += offsets_.rows();
380 for (
int i_dim=0; i_dim<slopes_.cols(); i_dim++)
382 cur_slopes = slopes_.col(i_dim);
383 if (slopes_as_angles_)
387 for (
int ii=0; ii<cur_slopes.size(); ii++)
388 cur_slopes[ii] = atan2(cur_slopes[ii],1.0);
391 values.segment(offset,slopes_.rows()) = cur_slopes;
392 offset += slopes_.rows();
400 if (all_values_vector_size_ != values.size())
402 cerr << __FILE__ <<
":" << __LINE__ <<
": values is of wrong size." << endl;
407 int size = centers_.rows();
408 int n_dims = centers_.cols();
409 for (
int i_dim=0; i_dim<n_dims; i_dim++)
413 if ( !(centers_.col(i_dim).array() == values.segment(offset,size).array()).all() )
416 centers_.col(i_dim) = values.segment(offset,size);
419 for (
int i_dim=0; i_dim<n_dims; i_dim++)
423 if ( !(widths_.col(i_dim).array() == values.segment(offset,size).array()).all() )
426 widths_.col(i_dim) = values.segment(offset,size);
430 offsets_ = values.segment(offset,size);
434 MatrixXd old_slopes = slopes_;
435 for (
int i_dim=0; i_dim<n_dims; i_dim++)
437 slopes_.col(i_dim) = values.segment(offset,size);
447 if (modifier.compare(
"lines_pivot_at_max_activation")==0)
450 if (modifier.compare(
"slopes_as_angles")==0)
459 bool normalized_basis_functions =
true;
460 return new UnifiedModel(centers_, widths_, slopes_, offsets_, normalized_basis_functions);
UnifiedModel class header file.
void kernelActivations(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &kernel_activations) const
Get the normalized kernel activations for given inputs.
ModelParameters * clone(void) const
Return a pointer to a deep copy of the ModelParameters object.
FunctionApproximator class header file.
UnifiedModel * toUnifiedModel(void) const
Convert these model parameters to unified model parameters.
Model parameters for the Locally Weighted Regression (LWR) function approximator. ...
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.
#define RETURN_STRING_FROM_BOOST_SERIALIZATION_XML(name)
Macro to convert the boost XML serialization of an object into a string.
void getSelectableParameters(std::set< std::string > &selected_values_labels) const
Return all the names of the parameter types that can be selected.
The unified model, which can be used to represent the model of all other function approximators...
void getLines(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &lines) const
Get the output of each linear model (unweighted) for the given inputs.
void setParameterVectorModifierPrivate(std::string modifier, bool new_value)
Turn certain modifiers on or off, see Parameterizable::setParameterVectorModifier().
Base class for all model parameters of function approximators.
ModelParametersLWR class header file.
BOOST_CLASS_EXPORT_IMPLEMENT(DmpBbo::ModelParametersLWR)
For boost::serialization.
void getParameterVectorMask(const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const
Get a mask for selecting parameters.
Header file to generate strings from boost serialized files.
void set_slopes_as_angles(bool slopes_as_angles)
Whether to return slopes as angles or slopes in ModelParametersLWR::getParameterVectorAll() ...
void setParameterVectorAll(const Eigen::VectorXd &values)
Set all available parameter values with one vector.
void set_lines_pivot_at_max_activation(bool lines_pivot_at_max_activation)
Set whether the offsets should be adapted so that the line segments pivot around the mode of the basi...
void unnormalizedKernelActivations(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &kernel_activations) const
Get the unnormalized kernel activations for given inputs.
void getParameterVectorAll(Eigen::VectorXd &all_values) const
Return a vector that returns all available parameter values.
Header file for serialization of Eigen matrices.