Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
homeokinbase.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Ralf Der <ralfder at mis dot mpg dot de> *
5  * *
6  * ANY COMMERCIAL USE FORBIDDEN! *
7  * LICENSE: *
8  * This work is licensed under the Creative Commons *
9  * Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of *
10  * this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
11  * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, *
12  * San Francisco, California, 94105, USA. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17  * *
18  ***************************************************************************/
19 
20 #ifndef __HOMEOKINBASE_H
21 #define __HOMEOKINBASE_H
22 
23 #include "abstractcontroller.h"
24 #include "controller_misc.h"
25 #include <stdlib.h>
26 #include <string.h>
27 
28 
29 /**
30  * Abstract class (interface) for robot controller that use are based on the homeokinetic
31  * prinziple
32  *
33  * Implements standard buffers and
34  * configureable interface for some useful parameters like epsC, epsA, s4avg ...
35  */
37 public:
38  HomeokinBase( unsigned short buffersize ,
39  const std::string& name, const std::string& revision)
40  : AbstractController(name, revision){
41  this->buffersize = buffersize;
42  addParameterDef("epsC", &epsC, 0.1);
43  addParameterDef("epsA",&epsA, 0.1);
44  addParameterDef("s4delay",&s4delay,1);
45  addParameterDef("s4avg",&s4avg,1);
46  addParameterDef("factorB",&factorB,0.2);
47  addParameterDef("squashsize",&squashSize,0.01);
48  addParameterDef("rootE",&rootE,0);
49  addParameterDef("logaE",&logaE,0);
50  t=0;
51  initialised = false;
52  }
53 
54 
55 protected:
56  paramval epsC; ///< learning rate factor for controller learning
57  paramval epsA; ///< learning rate factor for model learning
58  paramval factorB; ///< additional learning rate factor for model bias
59  paramint s4delay; ///< number of timesteps of delay in the SML
60  paramint s4avg; ///< number of timesteps used for smoothing the controller output values
61  paramint logaE; ///< logarithmic error is used for learning 1: controller 2: model 3: both
62  paramint rootE; ///< root error is used for learning 1: controller 2: model 3: both
63 
64 
65  paramval squashSize; ///< size of the box, where the parameter updates are clipped to
66 
67  int t;
68  unsigned short buffersize;
70 
71 protected:
72  /// put new value in ring buffer
73  void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay=0){
74  buffer[(t-delay)%buffersize] = vec;
75  }
76 
77  /// calculate delayed values
79  int number_steps_of_delay_){
80  // number_steps_of_delay must not be smaller than buffersize
81  assert ((unsigned)number_steps_of_delay_ < buffersize);
82  return buffer[(t - number_steps_of_delay_) % buffersize];
83  };
84 
85  /// calculate time-smoothed values
87  int number_steps_for_averaging_){
88  // number_steps_for_averaging_ must not be larger than buffersize
89  assert ((int)number_steps_for_averaging_ <= buffersize);
90 
91  matrix::Matrix result(buffer[t % buffersize]);
92  for (int k = 1; k < number_steps_for_averaging_; k++) {
93  result += buffer[(t - k + buffersize) % buffersize];
94  }
95  result *= 1/((double) (number_steps_for_averaging_)); // scalar multiplication
96  return result;
97  };
98 
99  /// calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error
100  virtual double calcErrorFactor(const matrix::Matrix& e, bool loga, bool root) {
101  double error_factor = 1;
102  if (loga){ // using logarithmic error E=ln(v^T*v)
103  error_factor= 1/(e.multTM().val(0,0)+0.000001)*0.01; // factor 1/100 for normalising (empirically)
104  }
105  if (root){ // using root error E=(v^T*v)^(1/2)
106  error_factor= 1/sqrt(e.multTM().val(0,0)+0.000001)*0.1; // factor 1/10 for normalising (empirically)
107  }
108  return error_factor;
109  }
110 
111 
112  /// neuron transfer function
113  static double g(double z)
114  {
115  return tanh(z);
116  };
117 
118 
119 
120 };
121 
122 #endif
paramval squashSize
size of the box, where the parameter updates are clipped to
Definition: homeokinbase.h:65
Matrix type.
Definition: matrix.h:65
paramint s4avg
number of timesteps used for smoothing the controller output values
Definition: homeokinbase.h:60
paramint logaE
logarithmic error is used for learning 1: controller 2: model 3: both
Definition: homeokinbase.h:61
paramint s4delay
number of timesteps of delay in the SML
Definition: homeokinbase.h:59
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
virtual matrix::Matrix calculateSmoothValues(const matrix::Matrix *buffer, int number_steps_for_averaging_)
calculate time-smoothed values
Definition: homeokinbase.h:86
paramval epsA
learning rate factor for model learning
Definition: homeokinbase.h:57
D val(I i, I j) const
Definition: matrix.h:96
Abstract class (interface) for robot controller that use are based on the homeokinetic prinziple...
Definition: homeokinbase.h:36
static double g(double z)
neuron transfer function
Definition: homeokinbase.h:113
virtual matrix::Matrix calculateDelayedValues(const matrix::Matrix *buffer, int number_steps_of_delay_)
calculate delayed values
Definition: homeokinbase.h:78
unsigned short buffersize
Definition: homeokinbase.h:68
paramint rootE
root error is used for learning 1: controller 2: model 3: both
Definition: homeokinbase.h:62
void putInBuffer(matrix::Matrix *buffer, const matrix::Matrix &vec, int delay=0)
put new value in ring buffer
Definition: homeokinbase.h:73
bool initialised
Definition: homeokinbase.h:69
double paramval
Definition: configurable.h:88
Matrix multTM() const
optimised multiplication of transpsoed of Matrix with itself: M^T * M
Definition: matrix.cpp:654
virtual double calcErrorFactor(const matrix::Matrix &e, bool loga, bool root)
calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error ...
Definition: homeokinbase.h:100
paramval epsC
learning rate factor for controller learning
Definition: homeokinbase.h:56
int paramint
Definition: configurable.h:98
virtual void addParameterDef(const paramkey &key, paramval *val, paramval def, paramval minBound, paramval maxBound, const paramdescr &descr=paramdescr())
This function is only provided for convenience.
Definition: configurable.h:220
paramval factorB
additional learning rate factor for model bias
Definition: homeokinbase.h:58
HomeokinBase(unsigned short buffersize, const std::string &name, const std::string &revision)
Definition: homeokinbase.h:38
int t
Definition: homeokinbase.h:67