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
invertmotorbigmodel.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 __INVERTMOTORBIGMODEL_H
21 #define __INVERTMOTORBIGMODEL_H
22 
23 #include "invertmotorcontroller.h"
24 
25 #include <assert.h>
26 #include <cmath>
27 
28 #include "matrix.h"
29 #include "noisegenerator.h"
30 #include "invertablemodel.h"
31 
32 typedef struct InvertMotorBigModelConf {
33  int buffersize; ///< buffersize size of the time-buffer for x,y,eta
34  double cInit; ///< cInit size of the C matrix to initialised with.
35  double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones
36  bool modelInit; ///< size of the unit-map strenght of the model
37  bool useS; ///< useS decides whether to use the S matrix in addition to the A matrix
38  bool someInternalParams; ///< someInternalParams if true only some internal parameters are exported, otherwise all
39 
40  double modelCompliant; ///< learning factor for model (or sensor) compliant learning
41 
42  InvertableModel* model; ///< model used as world model
44 
45 /**
46  * class for robot controller is based on InvertMotorNStep
47  *
48  * - direct inversion
49  *
50  * - motor space
51  *
52  * - multilayer,nonlinear model
53  */
55 
56 public:
58  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
59 
60  virtual ~InvertMotorBigModel();
61 
62  /// returns the number of sensors the controller was initialised with or 0 if not initialised
63  virtual int getSensorNumber() const { return number_sensors; }
64  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
65  virtual int getMotorNumber() const { return number_motors; }
66 
67  /// performs one step (includes learning).
68  /// Calulates motor commands from sensor inputs.
69  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
70 
71  /// performs one step without learning. Calulates motor commands from sensor inputs.
72  virtual void stepNoLearning(const sensor* , int number_sensors,
73  motor* , int number_motors);
74 
75 
76  /************** STOREABLE **********************************/
77  /** stores the controller values to a given file. */
78  virtual bool store(FILE* f) const;
79  /** loads the controller values from a given file. */
80  virtual bool restore(FILE* f);
81 
82  /************** INSPECTABLE ********************************/
83  virtual iparamkeylist getInternalParamNames() const;
84  virtual iparamvallist getInternalParams() const;
85  virtual ilayerlist getStructuralLayers() const;
87 
88  /**** TEACHING ****/
89  /** The given motor teaching signal is used for this timestep.
90  It is used as a feed forward teaching signal for the controller.
91  Please note, that the teaching signal has to be given each timestep
92  for a continuous teaching process.
93  */
94  virtual void setMotorTeachingSignal(const motor* teaching, int len);
95 
96  /** The given sensor teaching signal (distal learning) is used for this timestep.
97  First the belonging motor teachung signal is calculated by the inverse model.
98  See setMotorTeachingSignal
99  */
100  virtual void setSensorTeachingSignal(const sensor* teaching, int len);
101 
102 
105  c.buffersize = 50;
106  c.cInit = 1.0;
107  c.cNonDiag = 0;
108  c.modelInit = 1.0;
109  c.someInternalParams = true;
110  c.useS = false;
111  c.modelCompliant = 0;
112  c.model = 0;
113  return c;
114  }
115 
116  void getLastMotors(motor* motors, int len);
117 
118 protected:
119  unsigned short number_sensors;
120  unsigned short number_motors;
121 
122  matrix::Matrix A; ///< Model Matrix (motors to sensors)
123  matrix::Matrix S; ///< additional Model Matrix (sensors derivatives to sensors)
124  matrix::Matrix C; ///< Controller Matrix
125  matrix::Matrix H; ///< Controller Bias
126  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
127  matrix::Matrix R; ///< C*A
128  matrix::Matrix SmallID; ///< small identity matrix in the dimension of R
129  matrix::Matrix xsi; ///< current output error
130  double xsi_norm; ///< norm of matrix
131  double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
132  double pain; ///< if the modelling error (xsi) is too high we have a pain signal
136  matrix::Matrix zero_eta; // zero initialised eta
138  // matrix::Matrix z; ///< membrane potential
139  matrix::Matrix y_teaching; ///< teaching motor signal
140  bool useTeaching; ///< flag whether there is an actual teachning signal or not
141  int t_rand; ///< initial random time to avoid syncronous management of all controllers
142 
143 
144  int managementInterval; ///< interval between subsequent management function calls
145  paramval inhibition; ///< inhibition strength for sparce kwta strategy (is scaled with epsC)
146  paramval kwta; ///< (int) number of synapses that get strengthend
147  paramval limitRF; ///< (int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation. Mutual exclusive with inhibition
148  paramval dampS; ///< damping of S matrix
149 
151 
152  /// puts the sensors in the ringbuffer, generate controller values and put them in the
153  // ringbuffer as well
154  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
155  motor* y_, int number_motors);
156 
157  /// calculates the first shift into the motor space useing delayed motor values.
158  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
159  virtual void calcEtaAndBufferIt(int delay);
160 
161  /// learn H,C with motors y and corresponding sensors x
162  virtual void learnController();
163 
164  /// calculates the Update for C and H
165  // @param y_delay timesteps to delay the y-values. (usually 0)
166  // Please note that the delayed values are NOT used for the error calculation
167  // (this is done in calcXsi())
168  virtual void calcCandHUpdates(matrix::Matrix& C_update, matrix::Matrix& H_update, int y_delay);
169 
170  /// updates the matrix C and H
171  virtual void updateCandH(const matrix::Matrix& C_update, const matrix::Matrix& H_update, double squashSize);
172 
173  /// learn A, (and S) using motors y and corresponding sensors x
174  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
175  virtual void learnModel(int delay);
176 
177  /// handles inhibition damping etc.
178  virtual void management();
179 
180  /// returns controller output for given sensor values
182 
183  /** Calculates first and second derivative and returns both in on matrix (above).
184  We use simple discrete approximations:
185  \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
186  \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
187  where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
188  */
189  matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
190 
191 public:
192  /** k-winner take all inhibition for synapses. k largest synapses are strengthed and the rest are inhibited.
193  strong synapes are scaled by 1+(damping/k) and weak synapses are scaled by 1-(damping/(n-k)) where n is the
194  number of synapes
195  @param weightmatrix reference to weight matrix. Synapses for a neuron are in one row.
196  The inhibition is done for all rows independently
197  @param k number of synapes to strengthen
198  @param damping strength of supression and exitation (typically 0.001)
199  */
200  void kwtaInhibition(matrix::Matrix& weightmatrix, unsigned int k, double damping);
201 
202  /** sets all connections to zero which are further away then rfSize.
203  If rfSize == 1 then only main diagonal is left.
204  If rfSize = 2: main diagonal and upper and lower side diagonal are kept and so on and so forth.
205  */
206  void limitC(matrix::Matrix& weightmatrix, unsigned int rfSize);
207 
208 };
209 
210 #endif
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: invertmotorbigmodel.h:65
paramval squashSize
size of the box, where the parameter updates are clipped to
Definition: homeokinbase.h:65
Matrix type.
Definition: matrix.h:65
matrix::Matrix x_smooth
Definition: invertmotorbigmodel.h:137
abstract class (interface) for invertable models.
Definition: invertablemodel.h:33
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
matrix::Matrix * eta_buffer
Definition: invertmotorbigmodel.h:135
class for robot controller is based on InvertMotorNStep
Definition: invertmotorbigmodel.h:54
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: invertmotorbigmodel.h:132
void limitC(matrix::Matrix &weightmatrix, unsigned int rfSize)
sets all connections to zero which are further away then rfSize.
Definition: invertmotorbigmodel.cpp:347
matrix::Matrix SmallID
small identity matrix in the dimension of R
Definition: invertmotorbigmodel.h:128
paramval inhibition
inhibition strength for sparce kwta strategy (is scaled with epsC)
Definition: invertmotorbigmodel.h:145
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: invertmotorbigmodel.cpp:132
InvertableModel * model
model used as world model
Definition: invertmotorbigmodel.h:42
matrix::Matrix * x_buffer
Definition: invertmotorbigmodel.h:133
unsigned short number_sensors
Definition: invertmotorbigmodel.h:119
paramval dampS
damping of S matrix
Definition: invertmotorbigmodel.h:148
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: invertmotorbigmodel.cpp:370
double sensor
Definition: types.h:29
virtual void setSensorTeachingSignal(const sensor *teaching, int len)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: invertmotorbigmodel.cpp:457
double modelCompliant
learning factor for model (or sensor) compliant learning
Definition: invertmotorbigmodel.h:40
InvertMotorBigModel(const InvertMotorBigModelConf &conf=getDefaultConf())
Definition: invertmotorbigmodel.cpp:27
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: invertmotorbigmodel.h:141
void getLastMotors(motor *motors, int len)
Definition: invertmotorbigmodel.cpp:302
double xsi_norm
norm of matrix
Definition: invertmotorbigmodel.h:130
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: invertmotorbigmodel.h:126
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: invertmotorbigmodel.h:33
void kwtaInhibition(matrix::Matrix &weightmatrix, unsigned int k, double damping)
k-winner take all inhibition for synapses.
Definition: invertmotorbigmodel.cpp:328
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: invertmotorbigmodel.cpp:107
Definition: invertmotorbigmodel.h:32
double sensor
Definition: abstractcontroller.h:48
bool useTeaching
flag whether there is an actual teachning signal or not
Definition: invertmotorbigmodel.h:140
double paramval
Definition: configurable.h:88
Extended HomeokinBase class (still abstract) for robot controller work in motorspace and use possibly...
Definition: invertmotorcontroller.h:36
matrix::Matrix * y_buffer
Definition: invertmotorbigmodel.h:134
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: invertmotorbigmodel.cpp:431
matrix::Matrix H
Controller Bias.
Definition: invertmotorbigmodel.h:125
matrix::Matrix C
Controller Matrix.
Definition: invertmotorbigmodel.h:124
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: invertmotorbigmodel.h:131
virtual iparamvallist getInternalParams() const
Definition: invertmotorbigmodel.cpp:407
paramval kwta
(int) number of synapses that get strengthend
Definition: invertmotorbigmodel.h:146
matrix::Matrix S
additional Model Matrix (sensors derivatives to sensors)
Definition: invertmotorbigmodel.h:123
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: invertmotorbigmodel.cpp:125
virtual void calcEtaAndBufferIt(int delay)
calculates the first shift into the motor space useing delayed motor values.
Definition: invertmotorbigmodel.cpp:165
bool modelInit
size of the unit-map strenght of the model
Definition: invertmotorbigmodel.h:36
virtual void updateCandH(const matrix::Matrix &C_update, const matrix::Matrix &H_update, double squashSize)
updates the matrix C and H
Definition: invertmotorbigmodel.cpp:265
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: invertmotorbigmodel.cpp:297
static InvertMotorBigModelConf getDefaultConf()
Definition: invertmotorbigmodel.h:103
InvertMotorBigModelConf conf
Definition: invertmotorbigmodel.h:150
virtual void setMotorTeachingSignal(const motor *teaching, int len)
The given motor teaching signal is used for this timestep.
Definition: invertmotorbigmodel.cpp:450
double motor
Definition: types.h:30
matrix::Matrix R
C*A.
Definition: invertmotorbigmodel.h:127
virtual void management()
handles inhibition damping etc.
Definition: invertmotorbigmodel.cpp:314
struct InvertMotorBigModelConf InvertMotorBigModelConf
matrix::Matrix xsi
current output error
Definition: invertmotorbigmodel.h:129
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: invertmotorbigmodel.cpp:359
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: invertmotorbigmodel.cpp:383
virtual void learnController()
learn H,C with motors y and corresponding sensors x
Definition: invertmotorbigmodel.cpp:180
int managementInterval
interval between subsequent management function calls
Definition: invertmotorbigmodel.h:144
virtual void learnModel(int delay)
learn A, (and S) using motors y and corresponding sensors x
Definition: invertmotorbigmodel.cpp:273
matrix::Matrix y_teaching
teaching motor signal
Definition: invertmotorbigmodel.h:139
virtual ~InvertMotorBigModel()
Definition: invertmotorbigmodel.cpp:44
double motor
Definition: abstractcontroller.h:49
virtual void calcCandHUpdates(matrix::Matrix &C_update, matrix::Matrix &H_update, int y_delay)
calculates the Update for C and H
Definition: invertmotorbigmodel.cpp:200
matrix::Matrix zero_eta
Definition: invertmotorbigmodel.h:136
std::list< ILayer > ilayerlist
Definition: inspectable.h:87
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: invertmotorbigmodel.h:63
double cInit
cInit size of the C matrix to initialised with.
Definition: invertmotorbigmodel.h:34
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
bool someInternalParams
someInternalParams if true only some internal parameters are exported, otherwise all ...
Definition: invertmotorbigmodel.h:38
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: invertmotorbigmodel.cpp:439
bool useS
useS decides whether to use the S matrix in addition to the A matrix
Definition: invertmotorbigmodel.h:37
matrix::Matrix calcDerivatives(const matrix::Matrix *buffer, int delay)
Calculates first and second derivative and returns both in on matrix (above).
Definition: invertmotorbigmodel.cpp:307
unsigned short number_motors
Definition: invertmotorbigmodel.h:120
int c
Definition: hexapod.cpp:56
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: invertmotorbigmodel.cpp:56
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: invertmotorbigmodel.h:35
paramval limitRF
(int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation...
Definition: invertmotorbigmodel.h:147
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: invertmotorbigmodel.h:122
Interface and basic class for noise generator.
Definition: noisegenerator.h:37