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
derbigcontroller.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 #ifndef __DERBIGCONTROLLER_H
20 #define __DERBIGCONTROLLER_H
21 
22 #include "invertmotorcontroller.h"
23 
24 #include <assert.h>
25 #include <cmath>
26 
27 #include "matrix.h"
28 #include "noisegenerator.h"
29 #include "invertablemodel.h"
30 
31 typedef struct DerBigControllerConf {
32  int buffersize; ///< buffersize size of the time-buffer for x,y,eta
33  double cInit; ///< cInit size of the C matrix to initialised with.
34  double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones
35  bool modelInit; ///< size of the unit-map strenght of the model
36  bool useS; ///< useS decides whether to use the S matrix in addition to the A matrix
37  bool someInternalParams; ///< someInternalParams if true only some internal parameters are exported, otherwise all
38 
39  double modelCompliant; ///< learning factor for model (or sensor) compliant learning
40  bool useFantasy; ///< if true fantasising is enabled
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 ~DerBigController();
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  /************** CONFIGURABLE ********************************/
89  virtual void notifyOnChange(const paramkey& key);
90 
91  /**** TEACHING ****/
92  /** The given motor teaching signal is used for this timestep.
93  It is used as a feed forward teaching signal for the controller.
94  Please note, that the teaching signal has to be given each timestep
95  for a continuous teaching process.
96  */
97  virtual void setMotorTeachingSignal(const motor* teaching, int len);
98 
99  /** The given sensor teaching signal (distal learning) is used for this timestep.
100  First the belonging motor teachung signal is calculated by the inverse model.
101  See setMotorTeachingSignal
102  */
103  virtual void setSensorTeachingSignal(const sensor* teaching, int len);
104 
105 
108  c.buffersize = 50;
109  c.cInit = 1.05;
110  c.cNonDiag = 0;
111  c.modelInit = 1.0;
112  c.someInternalParams = true;
113  // c.someInternalParams = false;
114  c.useS = false;
115  c.modelCompliant = 0;
116  c.model = 0;
117  c.useFantasy = false;
118  return c;
119  }
120 
121  void getLastMotors(motor* motors, int len);
122 
123 protected:
124  unsigned short number_sensors;
125  unsigned short number_motors;
126 
127  matrix::Matrix A; ///< Model Matrix (motors to sensors)
128  matrix::Matrix A_Hat; ///< Model Matrix (motors to sensors) with input shift
129  matrix::Matrix S; ///< additional Model Matrix (sensors to sensors)
130  matrix::Matrix C; ///< Controller Matrix
131  matrix::Matrix GSC; ///< G_Prime times Controller Matrix
132  matrix::Matrix DD; ///< Noise Matrix
133  matrix::Matrix Dinverse; ///< Inverse Noise Matrix
134  matrix::Matrix H; ///< Controller Bias
135  matrix::Matrix B; ///< Model Bias
136  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
137  NoiseGenerator* YNoiseGen; ///< Noisegenerator for noisy motor values
138  matrix::Matrix R; ///< C*A
139  matrix::Matrix RRT_inv; // (R*R^T)^-1
140  matrix::Matrix ATA_inv; // ((A^T)*A)^-1
141  matrix::Matrix Rm1; ///< R^-1
142  matrix::Matrix ID; ///< identity matrix in the dimension of R
143  matrix::Matrix ID_Sensor; ///< identity matrix in the dimension of sensor space
144  matrix::Matrix xsi; ///< current output error
145  double xsi_norm; ///< norm of matrix
146  double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
147  double pain; ///< if the modelling error (xsi) is too high we have a pain signal
153  matrix::Matrix zero_eta; // zero initialised eta
158 
159  matrix::Matrix y_teaching; ///< teaching motor signal
160  bool useTeaching; ///< flag whether there is an actual teachning signal or not
161 
162  matrix::Matrix x_intern; ///< fantasy sensor values
163  int fantControl; ///< interval length for fantasising
164  int fantControlLen; ///< length of fantasy control
165  int fantReset; ///< number of fantasy control events before reseting internal state
166 
167  int t_rand; ///< initial random time to avoid syncronous management of all controllers
168  int managementInterval; ///< interval between subsequent management function calls
169  paramval inhibition; ///< inhibition strength for sparce kwta strategy (is scaled with epsC)
170  paramval kwta; ///< (int) number of synapses that get strengthend
171  paramval limitRF; ///< (int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation. Mutual exclusive with inhibition
172  paramval dampS; ///< damping of S matrix
173  paramval dampC; ///< damping of C matrix
174  paramval dampH; ///< damping of H vector
175  paramval weighting; ///< general weighting fator between update concepts
176 
178 
179  /// puts the sensors in the ringbuffer, generate controller values and put them in the
180  // ringbuffer as well
181  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
182  motor* y_, int number_motors);
183 
184 /** learn values H,C
185  This is the implementation uses a better formula for g^-1 using Mittelwertsatz
186  @param delay 0 for no delay and n>0 for n timesteps delay in the SML (s4delay)
187 */
188  virtual void learnController(int delay);
189 
190  /// learn conf.model, (and S) using motors y and corresponding sensors x
191  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
192  virtual void learnModel(int delay);
193 
194  /// handles inhibition damping etc.
195  virtual void management();
196 
197  /// returns controller output for given sensor values
199 
200  /** Calculates first and second derivative and returns both in on matrix (above).
201  We use simple discrete approximations:
202  \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
203  \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
204  where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
205  */
206  matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
207 
208 public:
209  /** k-winner take all inhibition for synapses. k largest synapses are strengthed and the rest are inhibited.
210  strong synapes are scaled by 1+(damping/k) and weak synapses are scaled by 1-(damping/(n-k)) where n is the
211  number of synapes
212  @param weightmatrix reference to weight matrix. Synapses for a neuron are in one row.
213  The inhibition is done for all rows independently
214  @param k number of synapes to strengthen
215  @param damping strength of supression and exitation (typically 0.001)
216  */
217  void kwtaInhibition(matrix::Matrix& weightmatrix, unsigned int k, double damping);
218 
219  /** sets all connections to zero which are further away then rfSize.
220  If rfSize == 1 then only main diagonal is left.
221  If rfSize = 2: main diagonal and upper and lower side diagonal are kept and so on and so forth.
222  */
223  void limitC(matrix::Matrix& weightmatrix, unsigned int rfSize);
224 
225 
226  /// calculates the city block distance (abs) norm of the matrix. (abs sum of absolutes / size of matrix)
227  virtual double calcMatrixNorm(const matrix::Matrix& m);
228 
229 };
230 
231 #endif
int fantControl
interval length for fantasising
Definition: derbigcontroller.h:163
Matrix type.
Definition: matrix.h:65
int managementInterval
interval between subsequent management function calls
Definition: derbigcontroller.h:168
abstract class (interface) for invertable models.
Definition: invertablemodel.h:33
matrix::Matrix B
Model Bias.
Definition: derbigcontroller.h:135
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: derbigcontroller.cpp:140
bool useTeaching
flag whether there is an actual teachning signal or not
Definition: derbigcontroller.h:160
paramval limitRF
(int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation...
Definition: derbigcontroller.h:171
matrix::Matrix RRT_inv
Definition: derbigcontroller.h:139
static DerBigControllerConf getDefaultConf()
Definition: derbigcontroller.h:106
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: derbigcontroller.cpp:525
matrix::Matrix * eta_buffer
Definition: derbigcontroller.h:150
virtual void notifyOnChange(const paramkey &key)
Is called when a parameter was changes via setParam().
Definition: derbigcontroller.cpp:517
matrix::Matrix x_smooth
Definition: derbigcontroller.h:154
matrix::Matrix v_smooth
Definition: derbigcontroller.h:152
virtual ~DerBigController()
Definition: derbigcontroller.cpp:56
virtual void setMotorTeachingSignal(const motor *teaching, int len)
The given motor teaching signal is used for this timestep.
Definition: derbigcontroller.cpp:600
matrix::Matrix Dinverse
Inverse Noise Matrix.
Definition: derbigcontroller.h:133
int fantReset
number of fantasy control events before reseting internal state
Definition: derbigcontroller.h:165
charArray paramkey
Definition: avrtypes.h:36
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: derbigcontroller.cpp:68
matrix::Matrix H
Controller Bias.
Definition: derbigcontroller.h:134
void getLastMotors(motor *motors, int len)
Definition: derbigcontroller.cpp:433
matrix::Matrix Rm1
R^-1.
Definition: derbigcontroller.h:141
matrix::Matrix x_smooth_long
Definition: derbigcontroller.h:157
unsigned short number_sensors
Definition: derbigcontroller.h:124
paramval dampS
damping of S matrix
Definition: derbigcontroller.h:172
double sensor
Definition: types.h:29
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: derbigcontroller.h:32
double xsi_norm
norm of matrix
Definition: derbigcontroller.h:145
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
matrix::Matrix xsi
current output error
Definition: derbigcontroller.h:144
bool useFantasy
if true fantasising is enabled
Definition: derbigcontroller.h:40
virtual void learnController(int delay)
learn values H,C This is the implementation uses a better formula for g^-1 using Mittelwertsatz ...
Definition: derbigcontroller.cpp:201
matrix::Matrix calcDerivatives(const matrix::Matrix *buffer, int delay)
Calculates first and second derivative and returns both in on matrix (above).
Definition: derbigcontroller.cpp:438
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: derbigcontroller.h:127
matrix::Matrix GSC
G_Prime times Controller Matrix.
Definition: derbigcontroller.h:131
double modelCompliant
learning factor for model (or sensor) compliant learning
Definition: derbigcontroller.h:39
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: derbigcontroller.cpp:494
DerBigController(const DerBigControllerConf &conf=getDefaultConf())
Definition: derbigcontroller.cpp:29
bool modelInit
size of the unit-map strenght of the model
Definition: derbigcontroller.h:35
NoiseGenerator * YNoiseGen
Noisegenerator for noisy motor values.
Definition: derbigcontroller.h:137
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: derbigcontroller.cpp:160
matrix::Matrix R
C*A.
Definition: derbigcontroller.h:138
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: derbigcontroller.h:34
double sensor
Definition: abstractcontroller.h:48
InvertableModel * model
model used as world model
Definition: derbigcontroller.h:42
virtual void learnModel(int delay)
learn conf.model, (and S) using motors y and corresponding sensors x
Definition: derbigcontroller.cpp:392
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: derbigcontroller.h:167
double paramval
Definition: configurable.h:88
Extended HomeokinBase class (still abstract) for robot controller work in motorspace and use possibly...
Definition: invertmotorcontroller.h:36
paramval dampH
damping of H vector
Definition: derbigcontroller.h:174
matrix::Matrix * x_buffer
Definition: derbigcontroller.h:148
bool someInternalParams
someInternalParams if true only some internal parameters are exported, otherwise all ...
Definition: derbigcontroller.h:37
virtual void setSensorTeachingSignal(const sensor *teaching, int len)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: derbigcontroller.cpp:607
paramval inhibition
inhibition strength for sparce kwta strategy (is scaled with epsC)
Definition: derbigcontroller.h:169
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: derbigcontroller.cpp:505
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: derbigcontroller.h:146
bool useS
useS decides whether to use the S matrix in addition to the A matrix
Definition: derbigcontroller.h:36
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: derbigcontroller.cpp:167
matrix::Matrix x_intern
fantasy sensor values
Definition: derbigcontroller.h:162
DerBigControllerConf conf
Definition: derbigcontroller.h:177
paramval weighting
general weighting fator between update concepts
Definition: derbigcontroller.h:175
paramval kwta
(int) number of synapses that get strengthend
Definition: derbigcontroller.h:170
int fantControlLen
length of fantasy control
Definition: derbigcontroller.h:164
matrix::Matrix C
Controller Matrix.
Definition: derbigcontroller.h:130
matrix::Matrix DD
Noise Matrix.
Definition: derbigcontroller.h:132
double cInit
cInit size of the C matrix to initialised with.
Definition: derbigcontroller.h:33
virtual void management()
handles inhibition damping etc.
Definition: derbigcontroller.cpp:445
double motor
Definition: types.h:30
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: derbigcontroller.h:136
unsigned short number_motors
Definition: derbigcontroller.h:125
matrix::Matrix eta_smooth
Definition: derbigcontroller.h:156
struct DerBigControllerConf DerBigControllerConf
matrix::Matrix ID_Sensor
identity matrix in the dimension of sensor space
Definition: derbigcontroller.h:143
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: derbigcontroller.cpp:589
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: derbigcontroller.h:63
matrix::Matrix y_smooth
Definition: derbigcontroller.h:155
matrix::Matrix * y_buffer
Definition: derbigcontroller.h:149
void kwtaInhibition(matrix::Matrix &weightmatrix, unsigned int k, double damping)
k-winner take all inhibition for synapses.
Definition: derbigcontroller.cpp:463
matrix::Matrix ATA_inv
Definition: derbigcontroller.h:140
class for robot controller is based on InvertMotorNStep
Definition: derbigcontroller.h:54
matrix::Matrix ID
identity matrix in the dimension of R
Definition: derbigcontroller.h:142
virtual iparamvallist getInternalParams() const
Definition: derbigcontroller.cpp:553
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: derbigcontroller.h:147
double motor
Definition: abstractcontroller.h:49
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: derbigcontroller.h:65
std::list< ILayer > ilayerlist
Definition: inspectable.h:87
virtual double calcMatrixNorm(const matrix::Matrix &m)
calculates the city block distance (abs) norm of the matrix. (abs sum of absolutes / size of matrix) ...
Definition: derbigcontroller.cpp:616
Definition: derbigcontroller.h:31
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
paramval dampC
damping of C matrix
Definition: derbigcontroller.h:173
matrix::Matrix S
additional Model Matrix (sensors to sensors)
Definition: derbigcontroller.h:129
matrix::Matrix zero_eta
Definition: derbigcontroller.h:153
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: derbigcontroller.cpp:426
void limitC(matrix::Matrix &weightmatrix, unsigned int rfSize)
sets all connections to zero which are further away then rfSize.
Definition: derbigcontroller.cpp:482
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: derbigcontroller.cpp:581
matrix::Matrix y_teaching
teaching motor signal
Definition: derbigcontroller.h:159
matrix::Matrix eta
Definition: derbigcontroller.h:151
matrix::Matrix A_Hat
Model Matrix (motors to sensors) with input shift.
Definition: derbigcontroller.h:128
int c
Definition: hexapod.cpp:56
Interface and basic class for noise generator.
Definition: noisegenerator.h:37