DMP_BBO library
Public Member Functions | Protected Member Functions | Friends | List of all members
DmpContextualOneStep Class Reference

Implementation of Contextual Dynamical Movement Primitives. More...

#include <DmpContextualOneStep.hpp>

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

Public Member Functions

 DmpContextualOneStep (int n_dims_dmp, std::vector< FunctionApproximator * > function_approximators, DmpType dmp_type=KULVICIUS_2012_JOINING)
 Initialization constructor for Contextual DMPs of known dimensionality, but with unknown initial and attractor states. More...
 
void computeFunctionApproximatorOutput (const Eigen::Ref< const Eigen::MatrixXd > &phase_state, Eigen::MatrixXd &fa_output) const
 Compute the outputs of the function approximators. More...
 
void train (const std::vector< Trajectory > &trajectories, const std::vector< Eigen::MatrixXd > &task_parameters, std::string save_directory="", bool overwrite=false)
 Train a contextual Dmp with a set of trajectories (and save results to file) This function is useful for debugging, i.e. More...
 
- Public Member Functions inherited from DmpContextual
 DmpContextual (int n_dims_dmp, std::vector< FunctionApproximator * > function_approximators, DmpType dmp_type)
 Initialization constructor for Contextual DMPs of known dimensionality, but with unknown initial and attractor states. More...
 
void set_task_parameters (const Eigen::MatrixXd &task_parameters)
 Set the current task parameters. More...
 
void set_policy_parameter_function_goal (FunctionApproximator *function_approximator)
 Set a function approximator to predict the goal from the task parameters. More...
 
void set_policy_parameter_function_duration (FunctionApproximator *function_approximator)
 Set a function approximator to predict the duration from the task parameters. More...
 
void train (const std::vector< Trajectory > &trajectories, const std::vector< Eigen::MatrixXd > &task_parameters, std::string save_directory)
 Train a contextual DMP with multiple trajectories. More...
 
void train (const std::vector< Trajectory > &trajectories, const std::vector< Eigen::MatrixXd > &task_parameters)
 Train a contextual DMP with multiple trajectories. More...
 
void train (const std::vector< Trajectory > &trajectories, std::string save_directory, bool overwrite)
 Train a contextual DMP with multiple trajectories. More...
 
void train (const std::vector< Trajectory > &trajectories, std::string save_directory)
 Train a contextual DMP with multiple trajectories. More...
 
void train (const std::vector< Trajectory > &trajectories)
 Train a contextual DMP with multiple trajectories. More...
 
- Public Member Functions inherited from Dmp
 Dmp (double tau, Eigen::VectorXd y_init, Eigen::VectorXd y_attr, std::vector< FunctionApproximator * > function_approximators, double alpha_spring_damper, DynamicalSystem *goal_system, DynamicalSystem *phase_system, DynamicalSystem *gating_system, ForcingTermScaling scaling=NO_SCALING)
 Initialization constructor. More...
 
 Dmp (int n_dims_dmp, std::vector< FunctionApproximator * > function_approximators, double alpha_spring_damper, DynamicalSystem *goal_system, DynamicalSystem *phase_system, DynamicalSystem *gating_system, ForcingTermScaling scaling=NO_SCALING)
 Initialization constructor for Dmps of known dimensionality, but with unknown initial and attractor states. More...
 
 Dmp (double tau, Eigen::VectorXd y_init, Eigen::VectorXd y_attr, std::vector< FunctionApproximator * > function_approximators, DmpType dmp_type=KULVICIUS_2012_JOINING, ForcingTermScaling scaling=NO_SCALING)
 Constructor that initializes the DMP with default dynamical systems. More...
 
 Dmp (int n_dims_dmp, std::vector< FunctionApproximator * > function_approximators, DmpType dmp_type=KULVICIUS_2012_JOINING, ForcingTermScaling scaling=NO_SCALING)
 Initialization constructor for Dmps of known dimensionality, but with unknown initial and attractor states. More...
 
 Dmp (double tau, Eigen::VectorXd y_init, Eigen::VectorXd y_attr, double alpha_spring_damper, DynamicalSystem *goal_system)
 Initialization constructor for Dmps without a forcing term. More...
 
 ~Dmp (void)
 Destructor. More...
 
Dmpclone (void) const
 Return a deep copy of this object. More...
 
virtual void integrateStart (Eigen::Ref< Eigen::VectorXd > x, Eigen::Ref< Eigen::VectorXd > xd) const
 Start integrating the system. More...
 
void differentialEquation (const Eigen::Ref< const Eigen::VectorXd > &x, Eigen::Ref< Eigen::VectorXd > xd) const
 The differential equation which defines the system. More...
 
void analyticalSolution (const Eigen::VectorXd &ts, Eigen::MatrixXd &xs, Eigen::MatrixXd &xds, Eigen::MatrixXd &forcing_terms, Eigen::MatrixXd &fa_output) const
 Return analytical solution of the system at certain times (and return forcing terms) More...
 
void analyticalSolution (const Eigen::VectorXd &ts, Eigen::MatrixXd &xs, Eigen::MatrixXd &xds, Eigen::MatrixXd &forcing_terms) const
 Return analytical solution of the system at certain times (and return forcing terms) More...
 
void analyticalSolution (const Eigen::VectorXd &ts, Eigen::MatrixXd &xs, Eigen::MatrixXd &xds) const
 Return analytical solution of the system at certain times. More...
 
virtual void analyticalSolution (const Eigen::VectorXd &ts, Trajectory &trajectory) const
 Return analytical solution of the system at certain times. More...
 
void analyticalSolution (const Eigen::VectorXd &ts, Trajectory &trajectory, Eigen::MatrixXd &forcing_terms) const
 Return analytical solution of the system at certain times. More...
 
virtual void statesAsTrajectory (const Eigen::MatrixXd &x_in, const Eigen::MatrixXd &xd_in, Eigen::MatrixXd &y_out, Eigen::MatrixXd &yd_out, Eigen::MatrixXd &ydd_out) const
 Get the output of a DMP dynamical system as a trajectory. More...
 
virtual void statesAsTrajectory (const Eigen::VectorXd &ts, const Eigen::MatrixXd &x_in, const Eigen::MatrixXd &xd_in, Trajectory &trajectory) const
 Get the output of a DMP dynamical system as a trajectory. More...
 
virtual void train (const Trajectory &trajectory)
 Train a DMP with a trajectory. More...
 
virtual void train (const Trajectory &trajectory, std::string save_directory, bool overwrite=false)
 Train a DMP with a trajectory, and write results to file. More...
 
virtual void set_tau (double tau)
 Accessor function for the time constant. More...
 
virtual void set_initial_state (const Eigen::VectorXd &y_init)
 Accessor function for the initial state of the system. More...
 
virtual void set_attractor_state (const Eigen::VectorXd &y_attr)
 Accessor function for the attractor state of the system. More...
 
void set_damping_coefficient (double damping_coefficient)
 Accessor function for damping coefficient of spring-damper system. More...
 
void set_spring_constant (double spring_constant)
 Accessor function for spring constant of spring-damper system. More...
 
std::string toString (void) const
 Returns a string representation of the object. More...
 
void getSelectableParameters (std::set< std::string > &selectable_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...
 
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...
 
void getParameterVectorMask (const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const
 Get a mask for selecting parameters. More...
 
void computeFunctionApproximatorInputsAndTargets (const Trajectory &trajectory, Eigen::VectorXd &fa_inputs_phase, Eigen::MatrixXd &fa_targets) const
 Given a trajectory, compute the inputs and targets for the function approximators. More...
 
void set_perturbation_analytical_solution (double perturbation_standard_deviation)
 Add a perturbation to the forcing term when computing the analytical solution. More...
 
double get_perturbation_analytical_solution () const
 Get the perturbation to the forcing term when computing the analytical solution. More...
 
- Public Member Functions inherited from DynamicalSystem
 DynamicalSystem (int order, double tau, Eigen::VectorXd initial_state, Eigen::VectorXd attractor_state, std::string name)
 Initialization constructor. More...
 
virtual ~DynamicalSystem (void)
 Destructor.
 
void integrateStart (const Eigen::VectorXd &x_init, Eigen::Ref< Eigen::VectorXd > x, Eigen::Ref< Eigen::VectorXd > xd)
 Start integrating the system with a new initial state. More...
 
virtual void integrateStep (double dt, const Eigen::Ref< const Eigen::VectorXd > x, Eigen::Ref< Eigen::VectorXd > x_updated, Eigen::Ref< Eigen::VectorXd > xd_updated) const
 Integrate the system one time step. More...
 
void set_integration_method (IntegrationMethod integration_method)
 Choose the integration method. More...
 
int dim (void) const
 Get the dimensionality of the dynamical system, i.e. More...
 
int dim_orig (void) const
 Get the dimensionality of the dynamical system, i.e. More...
 
double tau (void) const
 Accessor function for the time constant. More...
 
Eigen::VectorXd initial_state (void) const
 Accessor function for the initial state of the dynamical system. More...
 
void initial_state (Eigen::VectorXd &initial_state) const
 Accessor function for the initial state of the dynamical system. More...
 
Eigen::VectorXd attractor_state (void) const
 Accessor function for the attractor state of the dynamical system. More...
 
void attractor_state (Eigen::VectorXd &attractor_state) const
 Accessor function for the attractor state of the dynamical system. More...
 
virtual void set_attractor_state (const Eigen::Ref< const Eigen::VectorXd > &attractor_state)
 Mutator function for the attractor state of the dynamical system. More...
 
std::string name (void) const
 Accessor function for the name of the dynamical system. More...
 
virtual void set_name (std::string name)
 Mutator function for the name of the dynamical system. More...
 
- Public Member Functions inherited from Parameterizable
virtual ~Parameterizable (void)
 Destructor.
 
virtual int getParameterVectorSelectedSize (void) const
 Get the size of the vector of selected parameters, as returned by getParameterVectorSelected(. More...
 
virtual void getParameterVectorSelected (Eigen::VectorXd &values, bool normalized=false) const
 Get the values of the selected parameters in one vector. More...
 
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 setParameterVectorSelected (const Eigen::VectorXd &values, bool normalized=false)
 Set all the values of the selected parameters with one vector. 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...
 

Protected Member Functions

 DmpContextualOneStep (void)
 Default constructor. More...
 
- Protected Member Functions inherited from DmpContextual
void trainLocal (const std::vector< Trajectory > &trajectories, const std::vector< Eigen::MatrixXd > &task_parameters, std::string save_directory, bool overwrite)
 Train a contextual DMP. More...
 
void checkTrainTrajectories (const std::vector< Trajectory > &trajectories)
 Check if several trajectories have the same duration and initial/final states. More...
 
- Protected Member Functions inherited from Dmp
FunctionApproximatorfunction_approximator (int i_dim) const
 Get a pointer to the function approximator for a certain dimension. More...
 
- Protected Member Functions inherited from DynamicalSystem
 DynamicalSystem (void)
 Default constructor. More...
 
void set_dim (int dim)
 Set the dimensionality of the dynamical system, i.e. More...
 

Friends

class boost::serialization::access
 Give boost serialization access to private members. More...
 

Additional Inherited Members

- Public Types inherited from Dmp
enum  DmpType { IJSPEERT_2002_MOVEMENT, KULVICIUS_2012_JOINING, COUNTDOWN_2013 }
 Different types of DMPs that can be initialized. More...
 
enum  ForcingTermScaling { NO_SCALING, G_MINUS_Y0_SCALING, AMPLITUDE_SCALING }
 Different ways to scale the forcing term. More...
 
- Public Types inherited from DynamicalSystem
enum  IntegrationMethod { EULER, RUNGE_KUTTA }
 The possible integration methods that can be used. More...
 
- Protected Attributes inherited from DmpContextual
Eigen::MatrixXd task_parameters_
 The current task parameters.
 
std::vector< FunctionApproximator * > policy_parameter_function_goal_
 FunctionApproximators that relate task parameters the goal of the DMP.
 
FunctionApproximatorpolicy_parameter_function_duration_
 FunctionApproximator that relates task parameters the duration of the DMP.
 

Detailed Description

Implementation of Contextual Dynamical Movement Primitives.

Definition at line 39 of file DmpContextualOneStep.hpp.

Constructor & Destructor Documentation

DmpContextualOneStep ( int  n_dims_dmp,
std::vector< FunctionApproximator * >  function_approximators,
DmpType  dmp_type = KULVICIUS_2012_JOINING 
)

Initialization constructor for Contextual DMPs of known dimensionality, but with unknown initial and attractor states.

Initializes the DMP with default dynamical systems.

Parameters
n_dims_dmpDimensionality of the DMP
function_approximatorsFunction approximators for the forcing term
dmp_typeThe type of DMP, see Dmp::DmpType

Definition at line 38 of file DmpContextualOneStep.cpp.

40 : DmpContextual(n_dims_dmp, function_approximators, dmp_type)
41 {
42 }
DmpContextual(int n_dims_dmp, std::vector< FunctionApproximator * > function_approximators, DmpType dmp_type)
Initialization constructor for Contextual DMPs of known dimensionality, but with unknown initial and ...
DmpContextualOneStep ( void  )
inlineprotected

Default constructor.

Remarks
This default constuctor is required for boost::serialization to work. See Boost serialization issues

Definition at line 65 of file DmpContextualOneStep.hpp.

65 {};

Here is the call graph for this function:

Member Function Documentation

void computeFunctionApproximatorOutput ( const Eigen::Ref< const Eigen::MatrixXd > &  phase_state,
Eigen::MatrixXd &  fa_output 
) const
virtual

Compute the outputs of the function approximators.

Parameters
[in]phase_stateThe phase states for which the outputs are computed.
[out]fa_outputThe outputs of the function approximators.

Implements DmpContextual.

Definition at line 45 of file DmpContextualOneStep.cpp.

47 {
48  int n_time_steps = phase_state.rows();
49  fa_output.resize(n_time_steps,dim_orig());
50  fa_output.fill(0.0);
51 
52  MatrixXd task_parameters = task_parameters_;
53  if (task_parameters.rows()==1)
54  {
55  task_parameters = task_parameters.row(0).replicate(n_time_steps,1).eval();
56  }
57  else if (task_parameters.cols()==1)
58  {
59  task_parameters = task_parameters.col(0).transpose().replicate(n_time_steps,1).eval();
60  }
61 
62 
63 
64  assert(n_time_steps==task_parameters.rows());
65 
66  int n_task_parameters = task_parameters.cols();
67  MatrixXd fa_input(n_time_steps,n_task_parameters+1);
68  fa_input << phase_state, task_parameters;
69 
70 
71  MatrixXd output(n_time_steps,1);
72  for (int dd=0; dd<dim_orig(); dd++)
73  {
74  if (function_approximator(dd)!=NULL)
75  {
76  if (function_approximator(dd)->isTrained())
77  {
78  function_approximator(dd)->predict(fa_input,output);
79  if (output.size()>0)
80  {
81  fa_output.col(dd) = output;
82  }
83  }
84  }
85  }
86 }
int dim_orig(void) const
Get the dimensionality of the dynamical system, i.e.
bool isTrained(void) const
Determine whether the function approximator has already been trained with data or not...
virtual void predict(const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs)=0
Query the function approximator to make a prediction.
FunctionApproximator * function_approximator(int i_dim) const
Get a pointer to the function approximator for a certain dimension.
Definition: Dmp.hpp:345
Eigen::MatrixXd task_parameters_
The current task parameters.

Here is the call graph for this function:

void train ( const std::vector< Trajectory > &  trajectories,
const std::vector< Eigen::MatrixXd > &  task_parameters,
std::string  save_directory = "",
bool  overwrite = false 
)
virtual

Train a contextual Dmp with a set of trajectories (and save results to file) This function is useful for debugging, i.e.

if you want to save intermediate results to a directory

Parameters
[in]trajectoriesThe set of trajectories
[in]task_parametersThe task parameters for each of the trajectories.
[in]save_directoryDirectory to which to save intermediate results.
[in]overwriteOverwrite existing files in the directory above Overloads Dmp::train

Implements DmpContextual.

Definition at line 88 of file DmpContextualOneStep.cpp.

89 {
90  // Check if inputs are of the right size.
91  unsigned int n_demonstrations = trajectories.size();
92  assert(n_demonstrations==task_parameters.size());
93 
94  // Then check if the trajectories have the same duration and initial/final state
95  // Later on, if they are not the same, they should be learned also.
96  checkTrainTrajectories(trajectories);
97 
98  // Set tau, initial_state and attractor_state from the trajectories
99  set_tau(trajectories[0].duration());
100  set_initial_state(trajectories[0].initial_y());
101  set_attractor_state(trajectories[0].final_y());
102 
103  MatrixXd all_fa_inputs(0,0);
104  MatrixXd all_fa_targets(0,0);
105  int n_task_parameters = -1;
106  int n_time_steps_total = 0;
107 
108  VectorXd cur_fa_input_phase;
109  MatrixXd cur_fa_target;
110  MatrixXd cur_task_parameters;
111  for (unsigned int i_demo=0; i_demo<n_demonstrations; i_demo++)
112  {
113  cur_task_parameters = task_parameters[i_demo];
114  if (i_demo==0)
115  {
116  n_task_parameters = cur_task_parameters.cols();
117 
118  // This is the first time task_parameters_ is set, because this is the first time we know
119  // n_task_parameters.
120  // We set it so that set_task_parameters can check if task_parameters_.cols()==n_task_parameters
121  task_parameters_ = MatrixXd::Zero(1,n_task_parameters);
122  }
123  else
124  {
125  assert(n_task_parameters==cur_task_parameters.cols());
126  }
127 
128  int n_time_steps = trajectories[i_demo].length();
129  n_time_steps_total += n_time_steps;
130 
131  // Make sure cur_task_parameters has n_time_steps. Copy if it doesn't.
132  if (cur_task_parameters.rows()==1 && n_time_steps>1) {
133  MatrixXd cur_task_parameters_tmp = cur_task_parameters.replicate(n_time_steps,1);
134  cur_task_parameters = cur_task_parameters_tmp;
135  }
136  assert(n_time_steps==cur_task_parameters.rows());
137 
138  computeFunctionApproximatorInputsAndTargets(trajectories[i_demo], cur_fa_input_phase, cur_fa_target);
139 
140  all_fa_inputs.conservativeResize(n_time_steps_total,1+n_task_parameters);
141 
142  all_fa_inputs.bottomLeftCorner(n_time_steps,1) = cur_fa_input_phase;
143  all_fa_inputs.bottomRightCorner(n_time_steps,n_task_parameters) = cur_task_parameters;
144 
145  all_fa_targets.conservativeResize(n_time_steps_total,dim_orig());
146  all_fa_targets.bottomRows(n_time_steps) = cur_fa_target;
147 
148  }
149 
150  // We have all inputs and targets. Now train the function approximator for each dimension.
151 
152  for (int dd=0; dd<dim_orig(); dd++)
153  {
154  // This is just boring stuff to figure out if and where to store the results of training
155  string save_directory_dim;
156  if (!save_directory.empty())
157  {
158  if (dim_orig()==1)
159  save_directory_dim = save_directory;
160  else
161  save_directory_dim = save_directory + "/dim" + to_string(dd);
162  }
163 
164  VectorXd fa_target = all_fa_targets.col(dd);
165  function_approximator(dd)->train(all_fa_inputs,fa_target,save_directory_dim,overwrite);
166  }
167 
168 }
virtual void set_attractor_state(const Eigen::VectorXd &y_attr)
Accessor function for the attractor state of the system.
Definition: Dmp.cpp:873
int dim_orig(void) const
Get the dimensionality of the dynamical system, i.e.
virtual void set_tau(double tau)
Accessor function for the time constant.
Definition: Dmp.cpp:853
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 computeFunctionApproximatorInputsAndTargets(const Trajectory &trajectory, Eigen::VectorXd &fa_inputs_phase, Eigen::MatrixXd &fa_targets) const
Given a trajectory, compute the inputs and targets for the function approximators.
Definition: Dmp.cpp:583
FunctionApproximator * function_approximator(int i_dim) const
Get a pointer to the function approximator for a certain dimension.
Definition: Dmp.hpp:345
Eigen::MatrixXd task_parameters_
The current task parameters.
virtual void set_initial_state(const Eigen::VectorXd &y_init)
Accessor function for the initial state of the system.
Definition: Dmp.cpp:864
void checkTrainTrajectories(const std::vector< Trajectory > &trajectories)
Check if several trajectories have the same duration and initial/final states.

Here is the call graph for this function:

Friends And Related Function Documentation

friend class boost::serialization::access
friend

Give boost serialization access to private members.

Definition at line 65 of file DmpContextualOneStep.hpp.


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