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
invertmotornstep.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  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
5  * Ralf Der <ralfder at mis dot mpg dot de> *
6  * *
7  * ANY COMMERCIAL USE FORBIDDEN! *
8  * LICENSE: *
9  * This work is licensed under the Creative Commons *
10  * Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of *
11  * this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
12  * or send a letter to Creative Commons, 543 Howard Street, 5th Floor, *
13  * San Francisco, California, 94105, USA. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
18  * *
19  ***************************************************************************/
20 #ifndef __INVERTMOTORNSTEP_H
21 #define __INVERTMOTORNSTEP_H
22 
23 #include "invertmotorcontroller.h"
24 #include "teachable.h"
25 
26 #include <assert.h>
27 #include <cmath>
28 
29 #include <selforg/matrix.h>
30 #include <selforg/noisegenerator.h>
31 
32 typedef struct InvertMotorNStepConf {
33  int buffersize; ///< buffersize size of the time-buffer for x,y,eta
34  matrix::Matrix initialC; ///< initial controller matrix C (if not given then randomly chosen, see cInit, cNonDiag...)
35  double cInit; ///< cInit size of the C matrix to initialised with.
36  double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones
37  double cNonDiagAbs; ///< cNonDiag is the value of the nondiagonal elements
38  bool useS; ///< useS decides whether to use the S matrix in addition to the A matrix (sees sensors)
39  /** useSD decides whether to use the SD matrix in addition to the A
40  matrix (sees first and second derivatives) */
41  bool useSD;
42  /** number of context sensors(considered at the end of the sensor
43  vector. If not 0, then S will only get them as input */
45  bool someInternalParams; ///< someInternalParams if true only some internal parameters are exported, all otherwise
47 
48 /**
49  * class for robot controller that uses the georg's matrixlib for
50  * direct matrix inversion for n channels
51  * (simple one layer networks)
52  *
53  * Implements standart parameters: eps, rho, mu, stepnumber4avg, stepnumber4delay
54  */
56 
57 public:
59 
60  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
61 
62  virtual ~InvertMotorNStep();
63 
64  /// returns the number of sensors the controller was initialised with or 0 if not initialised
65  virtual int getSensorNumber() const { return number_sensors; }
66  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
67  virtual int getMotorNumber() const { return number_motors; }
68 
69  /// performs one step (includes learning).
70  /// Calulates motor commands from sensor inputs.
71  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
72 
73  /// performs one step without learning. Calulates motor commands from sensor inputs.
74  virtual void stepNoLearning(const sensor* , int number_sensors,
75  motor* , int number_motors);
76 
77  /**** STOREABLE ****/
78  /** stores the controller values to a given file. */
79  virtual bool store(FILE* f) const;
80  /** loads the controller values from a given file. */
81  virtual bool restore(FILE* f);
82 
83  /**** INSPECTABLE ****/
84  virtual std::list<ILayer> getStructuralLayers() const;
85  virtual std::list<IConnection> getStructuralConnections() const;
86 
87  /**** TEACHING ****/
88  /** The given motor teaching signal is used for this timestep.
89  It is used as a feed forward teaching signal for the controller.
90  Please note, that the teaching signal has to be given each timestep
91  for a continuous teaching process.
92  */
93  virtual void setMotorTeachingSignal(const motor* teaching, int len);
94 
95  /** The given sensor teaching signal (distal learning) is used for this timestep.
96  First the belonging motor teachung signal is calculated by the inverse model.
97  See setMotorTeachingSignal
98  */
99  virtual void setSensorTeachingSignal(const sensor* teaching, int len);
100  void getLastMotors(motor* motors, int len);
101  void getLastSensors(sensor* sensors, int len);
102 
103 
104  /**** New TEACHING interface ****/
105  /** The given motor teaching signal is used for this timestep.
106  It is used as a feed forward teaching signal for the controller.
107  Please note, that the teaching signal has to be given each timestep
108  for a continuous teaching process.
109  @param teaching: matrix with dimensions (motornumber,1)
110  */
111  virtual void setMotorTeaching(const matrix::Matrix& teaching);
112 
113  /** The given sensor teaching signal (distal learning) is used for this timestep.
114  The belonging motor teachung signal is calculated by the inverse model.
115  See setMotorTeaching
116  @param teaching: matrix with dimensions (motorsensors,1)
117  */
118  virtual void setSensorTeaching(const matrix::Matrix& teaching);
119  /// returns the last motor values (useful for cross motor coupling)
121  /// returns the last sensor values (useful for cross sensor coupling)
123 
124 
125 
126  // UNUSED! OLD IMPLEMENTATION which hat some consistency arguments
127  void calcCandHUpdatesTeaching(matrix::Matrix& C_update, matrix::Matrix& H_update, int y_delay);
128 
129  /**** REINFORCEMENT ****/
130  /** set the reinforcement signal for this timestep.
131  It is used to calculate a factor for the update.
132  Factor = 1-0.95*reinforcement.
133  @param reinforcement value between -1 and 1 (-1 bad, 0 neutral, 1 good)
134  */
135  virtual void setReinforcement(double reinforcement);
136 
137 
138 
141  c.buffersize = 50;
142  c.cInit = 1.0;
143  c.cNonDiag = 0;
144  c.useS = false;
145  c.useSD = false;
146  c.numberContext = 0;
147  c.someInternalParams = true;
148  c.cNonDiagAbs=0.0;
149  return c;
150  }
151 
152  /// sets the sensor channel weights (matrix should be (getSensorNumber() x 1)
153  void setSensorWeights(const matrix::Matrix& weights);
155  /// reference to C-matrix
156  matrix::Matrix& getC(){return C;};
157 
158  double getE() const {return v.multTM().val(0,0);}
159 
160 protected:
161  unsigned short number_sensors;
162  unsigned short number_motors;
163 
164  public:
165  matrix::Matrix A; ///< Model Matrix (motors to sensors)
166  matrix::Matrix S; ///< additional Model Matrix (sensors to sensors)
167  matrix::Matrix SD; ///< additional Model Matrix (sensors derivatives to sensors)
168  matrix::Matrix C; ///< Controller Matrix
169  matrix::Matrix H; ///< Controller Bias
170  matrix::Matrix B; ///< Model Bias
171 protected:
172  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
173  NoiseGenerator* YNoiseGen; ///< Noisegenerator for noisy motor output
174  matrix::Matrix R; ///< C*A
175  matrix::Matrix SmallID; ///< small identity matrix in the dimension of R
176  matrix::Matrix xsi; ///< current output error
177  matrix::Matrix v; ///< current reconstructed error
178  double E_val; ///< value of Error function
179  double xsi_norm; ///< norm of matrix
180  double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
181  double pain; ///< if the modelling error (xsi) is too high we have a pain signal
185  matrix::Matrix zero_eta; // zero initialised eta
187  // matrix::Matrix z; ///< membrane potential
188  matrix::Matrix y_teaching; ///< teaching motor signal
189  bool useTeaching; ///< flag whether there is an actual teachning signal or not
190  double reinforcement; ///< reinforcement value (set via setReinforcement())
191  double reinforcefactor; ///< reinforcement factor (set to 1 every step after learning)
192  int t_rand; ///< initial random time to avoid syncronous management of all controllers
193 
194  matrix::Matrix sensorweights; ///< sensor channel weight (each channel gets a certain importance)
195 
196  /** factor to teach for continuity: subsequent motor commands
197  should not differ too much */
198  double continuity;
199  double modelCompliant; ///< learning factor for model (or sensor) compliant learning
200  int managementInterval; ///< interval between subsequent management function calls
201  paramval inhibition; ///< inhibition strength for sparce kwta strategy (is scaled with epsC)
202  paramval kwta; ///< (int) number of synapses that get strengthend
203  paramval limitRF; ///< (int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation. Mutual exclusive with inhibition
204  paramval dampS; ///< damping of S matrix
205  paramval dampC; ///< damping of C matrix
206  paramval activeExplore; ///< decides whether and how strong the backpropagated error is used as a control signal
210 
211 
212  paramval noiseY; ///< noise strength for y
213 
215 
216  /// puts the sensors in the ringbuffer, generate controller values and put them in the
217  // ringbuffer as well
218  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
219  motor* y_, int number_motors);
220 
221  /// calculates the first shift into the motor space useing delayed motor values.
222  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
223  virtual void calcEtaAndBufferIt(int delay);
224  /// calculates xsi for the current time step using the delayed y values
225  // and x delayed by one
226  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
227  virtual void calcXsi(int delay);
228 
229  /// learn H,C with motors y and corresponding sensors x
230  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
231  virtual void learnController(int delay);
232 
233  /// calculates the Update for C and H
234  // @param delay timesteps to delay the y-values. (usually 0)
235  // Please note that the delayed values are NOT used for the error calculation
236  // (this is done in calcXsi())
237  virtual void calcCandHUpdates(matrix::Matrix& C_update, matrix::Matrix& H_update, int delay);
238 
239  /// updates the matrix C and H
240  virtual void updateCandH(const matrix::Matrix& C_update, const matrix::Matrix& H_update, double squashSize);
241 
242  /// learn A, (and S) using motors y and corresponding sensors x
243  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
244  virtual void learnModel(int delay);
245 
246  /// calculates the predicted sensor values
247  virtual matrix::Matrix model(const matrix::Matrix* x_buffer, int delay, const matrix::Matrix& y);
248 
249  /// handles inhibition damping etc.
250  virtual void management();
251 
252  /// returns controller output for given sensor values
254 
255  /** Calculates first and second derivative and returns both in on matrix (above).
256  We use simple discrete approximations:
257  \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
258  \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
259  where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
260  */
261  matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
262 
263 public:
264  /** k-winner take all inhibition for synapses. k largest synapses are strengthed and the rest are inhibited.
265  strong synapes are scaled by 1+(damping/k) and weak synapses are scaled by 1-(damping/(n-k)) where n is the
266  number of synapes
267  @param weightmatrix reference to weight matrix. Synapses for a neuron are in one row.
268  The inhibition is done for all rows independently
269  @param k number of synapes to strengthen
270  @param damping strength of supression and exitation (typically 0.001)
271  */
272  void kwtaInhibition(matrix::Matrix& weightmatrix, unsigned int k, double damping);
273 
274  /** sets all connections to zero which are further away then rfSize
275  from the diagonal.
276  If rfSize == 1 then only main diagonal is left.
277  If rfSize = 2: main diagonal and upper and lower side diagonal are kept and so on and so forth.
278  */
279  void limitC(matrix::Matrix& weightmatrix, unsigned int rfSize);
280 
281 
282  static double clip095(double x);
283  static double regularizedInverse(double v);
284 
285 };
286 
287 #endif
paramval squashSize
size of the box, where the parameter updates are clipped to
Definition: homeokinbase.h:65
Matrix type.
Definition: matrix.h:65
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: invertmotornstep.h:67
InvertMotorNStep(const InvertMotorNStepConf &conf=getDefaultConf())
Definition: invertmotornstep.cpp:26
matrix::Matrix initialC
initial controller matrix C (if not given then randomly chosen, see cInit, cNonDiag...)
Definition: invertmotornstep.h:34
double getE() const
Definition: invertmotornstep.h:158
void kwtaInhibition(matrix::Matrix &weightmatrix, unsigned int k, double damping)
k-winner take all inhibition for synapses.
Definition: invertmotornstep.cpp:605
Interface for teachable controller.
Definition: teachable.h:32
virtual void setMotorTeachingSignal(const motor *teaching, int len)
The given motor teaching signal is used for this timestep.
Definition: invertmotornstep.cpp:692
double reinforcement
reinforcement value (set via setReinforcement())
Definition: invertmotornstep.h:190
bool someInternalParams
someInternalParams if true only some internal parameters are exported, all otherwise ...
Definition: invertmotornstep.h:45
matrix::Matrix S
additional Model Matrix (sensors to sensors)
Definition: invertmotornstep.h:166
double E_val
value of Error function
Definition: invertmotornstep.h:178
virtual matrix::Matrix getLastMotorValues()
returns the last motor values (useful for cross motor coupling)
Definition: invertmotornstep.cpp:730
InvertMotorNStepConf conf
Definition: invertmotornstep.h:214
virtual matrix::Matrix getLastSensorValues()
returns the last sensor values (useful for cross sensor coupling)
Definition: invertmotornstep.cpp:734
matrix::Matrix getSensorWeights() const
Definition: invertmotornstep.h:154
matrix::Matrix R
C*A.
Definition: invertmotornstep.h:174
virtual void setSensorTeaching(const matrix::Matrix &teaching)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: invertmotornstep.cpp:723
bool useSD
useSD decides whether to use the SD matrix in addition to the A matrix (sees first and second derivat...
Definition: invertmotornstep.h:41
virtual ~InvertMotorNStep()
Definition: invertmotornstep.cpp:81
matrix::Matrix * y_buffer
Definition: invertmotornstep.h:183
D val(I i, I j) const
Definition: matrix.h:96
double sensor
Definition: types.h:29
virtual void setReinforcement(double reinforcement)
set the reinforcement signal for this timestep.
Definition: invertmotornstep.cpp:739
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: invertmotornstep.h:33
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: invertmotornstep.cpp:545
int numberContext
number of context sensors(considered at the end of the sensor vector.
Definition: invertmotornstep.h:44
paramval inhibition
inhibition strength for sparce kwta strategy (is scaled with epsC)
Definition: invertmotornstep.h:201
matrix::Matrix * eta_buffer
Definition: invertmotornstep.h:184
virtual void calcEtaAndBufferIt(int delay)
calculates the first shift into the motor space useing delayed motor values.
Definition: invertmotornstep.cpp:255
virtual std::list< ILayer > getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: invertmotornstep.cpp:669
virtual void learnModel(int delay)
learn A, (and S) using motors y and corresponding sensors x
Definition: invertmotornstep.cpp:490
virtual void management()
handles inhibition damping etc.
Definition: invertmotornstep.cpp:583
matrix::Matrix x_smooth
Definition: invertmotornstep.h:186
matrix::Matrix SmallID
small identity matrix in the dimension of R
Definition: invertmotornstep.h:175
Definition: invertmotornstep.h:32
void calcCandHUpdatesTeaching(matrix::Matrix &C_update, matrix::Matrix &H_update, int y_delay)
calculates the Update for C and H using the teaching signal
Definition: invertmotornstep.cpp:455
matrix::Matrix zero_eta
Definition: invertmotornstep.h:185
double sensor
Definition: abstractcontroller.h:48
virtual void learnController(int delay)
learn H,C with motors y and corresponding sensors x
Definition: invertmotornstep.cpp:318
matrix::Matrix sensorweights
sensor channel weight (each channel gets a certain importance)
Definition: invertmotornstep.h:194
unsigned short number_motors
Definition: invertmotornstep.h:162
double paramval
Definition: configurable.h:88
Extended HomeokinBase class (still abstract) for robot controller work in motorspace and use possibly...
Definition: invertmotorcontroller.h:36
void getLastMotors(motor *motors, int len)
Definition: invertmotornstep.cpp:550
static double regularizedInverse(double v)
Definition: invertmotornstep.cpp:248
double xsi_norm
norm of matrix
Definition: invertmotornstep.h:179
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: invertmotornstep.cpp:163
double cNonDiagAbs
cNonDiag is the value of the nondiagonal elements
Definition: invertmotornstep.h:37
virtual void calcXsi(int delay)
calculates xsi for the current time step using the delayed y values
Definition: invertmotornstep.cpp:287
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: invertmotornstep.h:181
paramval kwta
(int) number of synapses that get strengthend
Definition: invertmotornstep.h:202
double cInit
cInit size of the C matrix to initialised with.
Definition: invertmotornstep.h:35
virtual void fillBuffersAndControl(const sensor *x_, int number_sensors, motor *y_, int number_motors)
puts the sensors in the ringbuffer, generate controller values and put them in the ...
Definition: invertmotornstep.cpp:208
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: invertmotornstep.cpp:200
static double clip095(double x)
Definition: invertmotornstep.cpp:687
virtual matrix::Matrix model(const matrix::Matrix *x_buffer, int delay, const matrix::Matrix &y)
calculates the predicted sensor values
Definition: invertmotornstep.cpp:299
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: invertmotornstep.h:172
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: invertmotornstep.cpp:655
matrix::Matrix SD
additional Model Matrix (sensors derivatives to sensors)
Definition: invertmotornstep.h:167
paramval cfactor
Definition: invertmotornstep.h:207
struct InvertMotorNStepConf InvertMotorNStepConf
Matrix multTM() const
optimised multiplication of transpsoed of Matrix with itself: M^T * M
Definition: matrix.cpp:654
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: invertmotornstep.h:36
void limitC(matrix::Matrix &weightmatrix, unsigned int rfSize)
sets all connections to zero which are further away then rfSize from the diagonal.
Definition: invertmotornstep.cpp:628
void setSensorWeights(const matrix::Matrix &weights)
sets the sensor channel weights (matrix should be (getSensorNumber() x 1)
Definition: invertmotornstep.cpp:563
static InvertMotorNStepConf getDefaultConf()
Definition: invertmotornstep.h:139
matrix::Matrix calcDerivatives(const matrix::Matrix *buffer, int delay)
Calculates first and second derivative and returns both in on matrix (above).
Definition: invertmotornstep.cpp:575
int managementInterval
interval between subsequent management function calls
Definition: invertmotornstep.h:200
double reinforcefactor
reinforcement factor (set to 1 every step after learning)
Definition: invertmotornstep.h:191
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: invertmotornstep.h:180
bool useTeaching
flag whether there is an actual teachning signal or not
Definition: invertmotornstep.h:189
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: invertmotornstep.h:165
paramval dampS
damping of S matrix
Definition: invertmotornstep.h:204
matrix::Matrix v
current reconstructed error
Definition: invertmotornstep.h:177
double motor
Definition: types.h:30
class for robot controller that uses the georg's matrixlib for direct matrix inversion for n channels...
Definition: invertmotornstep.h:55
paramval cnondiagabs
Definition: invertmotornstep.h:208
paramval activeExplore
decides whether and how strong the backpropagated error is used as a control signal ...
Definition: invertmotornstep.h:206
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)
initialisation of the controller with the given sensor/ motornumber Must be called before use...
Definition: invertmotornstep.cpp:94
matrix::Matrix B
Model Bias.
Definition: invertmotornstep.h:170
virtual void setMotorTeaching(const matrix::Matrix &teaching)
The given motor teaching signal is used for this timestep.
Definition: invertmotornstep.cpp:714
bool useS
useS decides whether to use the S matrix in addition to the A matrix (sees sensors) ...
Definition: invertmotornstep.h:38
virtual void setSensorTeachingSignal(const sensor *teaching, int len)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: invertmotornstep.cpp:700
double modelCompliant
learning factor for model (or sensor) compliant learning
Definition: invertmotornstep.h:199
matrix::Matrix H
Controller Bias.
Definition: invertmotornstep.h:169
paramval dampC
damping of C matrix
Definition: invertmotornstep.h:205
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: invertmotornstep.cpp:642
matrix::Matrix * x_buffer
Definition: invertmotornstep.h:182
unsigned short number_sensors
Definition: invertmotornstep.h:161
paramval limitRF
(int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation...
Definition: invertmotornstep.h:203
matrix::Matrix C
Controller Matrix.
Definition: invertmotornstep.h:168
virtual void calcCandHUpdates(matrix::Matrix &C_update, matrix::Matrix &H_update, int delay)
calculates the Update for C and H
Definition: invertmotornstep.cpp:341
double motor
Definition: abstractcontroller.h:49
virtual std::list< IConnection > getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: invertmotornstep.cpp:678
matrix::Matrix xsi
current output error
Definition: invertmotornstep.h:176
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: invertmotornstep.h:65
paramval cdiagabs
Definition: invertmotornstep.h:209
virtual void updateCandH(const matrix::Matrix &C_update, const matrix::Matrix &H_update, double squashSize)
updates the matrix C and H
Definition: invertmotornstep.cpp:481
double continuity
factor to teach for continuity: subsequent motor commands should not differ too much ...
Definition: invertmotornstep.h:198
paramval noiseY
noise strength for y
Definition: invertmotornstep.h:212
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: invertmotornstep.h:192
int c
Definition: hexapod.cpp:56
void getLastSensors(sensor *sensors, int len)
Definition: invertmotornstep.cpp:556
matrix::Matrix y_teaching
teaching motor signal
Definition: invertmotornstep.h:188
Interface and basic class for noise generator.
Definition: noisegenerator.h:37
matrix::Matrix & getC()
reference to C-matrix
Definition: invertmotornstep.h:156
NoiseGenerator * YNoiseGen
Noisegenerator for noisy motor output.
Definition: invertmotornstep.h:173