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
derinf.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 __DERINF_H
20 #define __DERINF_H
21 
22 #include "invertmotorcontroller.h"
23 
24 #include <assert.h>
25 #include <math.h>
26 
27 #include "matrix.h"
28 #include "multilayerffnn.h"
29 #include "noisegenerator.h"
30 #include "invertablemodel.h"
31 
32 typedef struct DerInfConf {
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 } DerInfConf;
40 /**
41  * class for robot controller is based on InvertMotorNStep
42  *
43  * - direct inversion
44  *
45  * - motor space
46  *
47  * - multilayer,nonlinear model
48  */
49 class DerInf : public InvertMotorController {
50 
51 public:
53  virtual void init(int sensornumber, int motornumber, RandGen* randg);
54 
55  virtual ~DerInf();
56 
57  /// returns the number of sensors the controller was initialised with or 0 if not initialised
58  virtual int getSensorNumber() const { return number_sensors; }
59  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
60  virtual int getMotorNumber() const { return number_motors; }
61 
62  /// performs one step (includes learning).
63  /// Calulates motor commands from sensor inputs.
64  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
65 
66  /// performs one step without learning. Calulates motor commands from sensor inputs.
67  virtual void stepNoLearning(const sensor* , int number_sensors,
68  motor* , int number_motors);
69 
70  /************** STOREABLE **********************************/
71  /** stores the controller values to a given file. */
72  virtual bool store(FILE* f) const;
73  /** loads the controller values from a given file. */
74  virtual bool restore(FILE* f);
75 
76  /************** INSPECTABLE ********************************/
77  virtual iparamkeylist getInternalParamNames() const;
78  virtual iparamvallist getInternalParams() const;
79  virtual ilayerlist getStructuralLayers() const;
81 
82 
84  DerInfConf c;
85  c.buffersize = 50;
86  c.cInit = 1.05;
87  c.cNonDiag = 0;
88  c.modelInit = 1.0;
89  c.someInternalParams = true;
90  // c.someInternalParams = false;
91  c.useS = false;
92  return c;
93  }
94 
95  void getLastMotors(motor* motors, int len);
96 
97 protected:
98  unsigned short number_sensors;
99  unsigned short number_motors;
100 
101  matrix::Matrix A; ///< Model Matrix (motors to sensors)
103  matrix::Matrix A_Hat; ///< Model Matrix (motors to sensors) with input shift
104  matrix::Matrix S; ///< additional Model Matrix (sensors to sensors)
105  matrix::Matrix C; ///< Controller Matrix
106  matrix::Matrix GSC; ///< G_Prime times Controller Matrix
107  matrix::Matrix DD; ///< Noise Matrix
108  matrix::Matrix DD1; ///< Noise Matrix
109  matrix::Matrix Dinverse; ///< Inverse Noise Matrix
110  matrix::Matrix H; ///< Controller Bias
111  matrix::Matrix HY; ///< Controller Bias-Y
112  matrix::Matrix B; ///< Model Bias
113  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
114  NoiseGenerator* YNoiseGen; ///< Noisegenerator for noisy motor values
115  matrix::Matrix R; ///< C*A
116  matrix::Matrix P; ///< A*G'*C
117  matrix::Matrix RG; ///< Granger1
118  matrix::Matrix Q; ///<Granger2
119  matrix::Matrix Q1; //<Granger3
120  matrix::Matrix RRT_inv; // (R*R^T)^-1
121  matrix::Matrix ATA_inv; // ((A^T)*A)^-1
122  matrix::Matrix Rm1; ///< R^-1
123  matrix::Matrix ID; ///< identity matrix in the dimension of R
124  matrix::Matrix ID_Sensor; ///< identity matrix in the dimension of sensor space
127  matrix::Matrix xsi; ///< current output error
128  double xsi_norm; ///< norm of matrix
129  double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
130  double pain; ///< if the modelling error (xsi) is too high we have a pain signal
131  double TLE; // TimeLoopError
132  double grang1; //GrangerCausality
133  double grang2; //GrangerCausality
134  double causal; //GrangerCausality
135  double causalfactor; //GrangerCausality
136  double EE;
137  double EE_mean;
138  double EE_sqr;
139  double xsistrength;
140  double sense;
151  matrix::Matrix zero_eta; // zero initialised eta
173 
175 
176 
177  matrix::Matrix y_teaching; ///< teaching motor signal
178  bool useTeaching; ///< flag whether there is an actual teachning signal or not
179 
182 
183  int t_rand; ///< initial random time to avoid syncronous management of all controllers
184  int t_delay;
185  int managementInterval; ///< interval between subsequent management function calls
186  paramval dampS; ///< damping of S matrix
187  paramval dampC; ///< damping of C matrix
188  paramval dampH; ///< damping of H vector
189  paramval weighting; ///< general weighting factor between update concepts
190  paramval epsSat; ///< learning rate for satellite network
191  paramval satelliteTeaching; ///< teaching rate for sat teaching
199 
201 
202  /// puts the sensors in the ringbuffer, generate controller values and put them in the
203  // ringbuffer as well
204  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
205  motor* y_, int number_motors);
206 
207 /** learn values H,C
208  This is the implementation uses a better formula for g^-1 using Mittelwertsatz
209  @param delay 0 for no delay and n>0 for n timesteps delay in the SML (s4delay)
210 */
211  virtual void learnController(int delay);
212 
213  /// handles inhibition damping etc.
214  virtual void management();
215 
216  /// returns controller output for given sensor values
218 
219  /** Calculates first and second derivative and returns both in on matrix (above).
220  We use simple discrete approximations:
221  \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
222  \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
223  where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
224  */
225  matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
226 
227 public:
228 
229  /// calculates the city block distance (abs) norm of the matrix. (abs sum of absolutes / size of matrix)
230  virtual double calcMatrixNorm(const matrix::Matrix& m);
231 
232 
233 };
234 
235 #endif
Matrix type.
Definition: matrix.h:65
matrix::Matrix B
Model Bias.
Definition: derinf.h:112
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
matrix::Matrix y_teaching
teaching motor signal
Definition: derinf.h:177
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: derinf.cpp:210
paramval dampS
damping of S matrix
Definition: derinf.h:186
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: derinf.h:113
matrix::Matrix zz
Definition: derinf.h:172
matrix::Matrix A_updateOld
Definition: derinf.h:166
matrix::Matrix y_sat
Definition: derinf.h:154
matrix::Matrix CCT_inv
Definition: derinf.h:125
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: derinf.cpp:447
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: derinf.h:183
virtual iparamvallist getInternalParams() const
Definition: derinf.cpp:487
matrix::Matrix xx
Definition: derinf.h:170
unsigned short number_motors
Definition: derinf.h:99
matrix::Matrix RRT_inv
Definition: derinf.h:120
bool someInternalParams
someInternalParams if true only some internal parameters are exported, otherwise all ...
Definition: derinf.h:38
matrix::Matrix DD1
Noise Matrix.
Definition: derinf.h:108
double EE
Definition: derinf.h:136
matrix::Matrix * xsi_buffer
Definition: derinf.h:147
virtual void init(int sensornumber, int motornumber, RandGen *randg)
initialisation of the controller with the given sensor/ motornumber Must be called before use...
Definition: derinf.cpp:75
matrix::Matrix v_smooth
Definition: derinf.h:150
matrix::Matrix C
Controller Matrix.
Definition: derinf.h:105
matrix::Matrix * chi_buffer
Definition: derinf.h:144
struct DerInfConf DerInfConf
bool modelInit
size of the unit-map strenght of the model
Definition: derinf.h:36
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: derinf.cpp:184
matrix::Matrix eta
Definition: derinf.h:148
matrix::Matrix GSC
G_Prime times Controller Matrix.
Definition: derinf.h:106
paramval epsSat
learning rate for satellite network
Definition: derinf.h:190
paramval xsifactor
Definition: derinf.h:197
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: derinf.h:35
double grang1
Definition: derinf.h:132
unsigned short number_sensors
Definition: derinf.h:98
matrix::Matrix CST
Definition: derinf.h:126
matrix::Matrix yy
Definition: derinf.h:171
paramval dampC
damping of C matrix
Definition: derinf.h:187
matrix::Matrix * y_buffer
Definition: derinf.h:142
matrix::Matrix vau_avg
Definition: derinf.h:174
double sensor
Definition: types.h:29
matrix::Matrix y_intern
Definition: derinf.h:161
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: derinf.h:33
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
matrix::Matrix zero_eta
Definition: derinf.h:151
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: derinf.cpp:421
paramval intstate
Definition: derinf.h:194
matrix::Matrix y_integration
Definition: derinf.h:159
matrix::Matrix y_smooth
Definition: derinf.h:153
matrix::Matrix Q
Granger2.
Definition: derinf.h:118
matrix::Matrix B_updateOld
Definition: derinf.h:167
paramval creat
Definition: derinf.h:198
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: derinf.cpp:398
double causalfactor
Definition: derinf.h:135
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: derinf.h:129
matrix::Matrix x_smooth_long
Definition: derinf.h:157
matrix::Matrix * ysat_buffer
Definition: derinf.h:143
double sensor
Definition: abstractcontroller.h:48
bool useTeaching
flag whether there is an actual teachning signal or not
Definition: derinf.h:178
matrix::Matrix H_updateOld
Definition: derinf.h:164
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 x_smooth
Definition: derinf.h:152
matrix::Matrix calcDerivatives(const matrix::Matrix *buffer, int delay)
Calculates first and second derivative and returns both in on matrix (above).
Definition: derinf.cpp:408
double xsistrength
Definition: derinf.h:139
matrix::Matrix * x_buffer
Definition: derinf.h:141
matrix::Matrix y_forecast
Definition: derinf.h:158
matrix::Matrix DD
Noise Matrix.
Definition: derinf.h:107
double EE_mean
Definition: derinf.h:137
double causal
Definition: derinf.h:134
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: derinf.h:60
matrix::Matrix ID_Sensor
identity matrix in the dimension of sensor space
Definition: derinf.h:124
matrix::Matrix A0
Definition: derinf.h:102
paramval satelliteTeaching
teaching rate for sat teaching
Definition: derinf.h:191
DerInfConf conf
Definition: derinf.h:200
matrix::Matrix * eta_buffer
Definition: derinf.h:146
matrix::Matrix * rho_buffer
Definition: derinf.h:145
paramval PIDdrv
Definition: derinf.h:193
matrix::Matrix HY_updateOld
Definition: derinf.h:165
paramval weighting
general weighting factor between update concepts
Definition: derinf.h:189
paramval adaptrate
Definition: derinf.h:196
double EE_sqr
Definition: derinf.h:138
int num_iterations
Definition: derinf.h:181
matrix::Matrix L
Definition: derinf.h:168
matrix::Matrix A_Hat
Model Matrix (motors to sensors) with input shift.
Definition: derinf.h:103
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: derinf.h:58
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: derinf.cpp:531
NoiseGenerator * YNoiseGen
Noisegenerator for noisy motor values.
Definition: derinf.h:114
bool useS
useS decides whether to use the S matrix in addition to the A matrix
Definition: derinf.h:37
matrix::Matrix ups
Definition: derinf.h:149
double motor
Definition: types.h:30
double cInit
cInit size of the C matrix to initialised with.
Definition: derinf.h:34
paramval dampH
damping of H vector
Definition: derinf.h:188
int t_delay
Definition: derinf.h:184
matrix::Matrix H
Controller Bias.
Definition: derinf.h:110
matrix::Matrix Dinverse
Inverse Noise Matrix.
Definition: derinf.h:109
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: derinf.h:101
matrix::Matrix P
A*G'*C.
Definition: derinf.h:116
virtual void management()
handles inhibition damping etc.
Definition: derinf.cpp:415
paramval gamma
Definition: derinf.h:195
void getLastMotors(motor *motors, int len)
Definition: derinf.cpp:403
virtual ~DerInf()
Definition: derinf.cpp:63
virtual void learnController(int delay)
learn values H,C This is the implementation uses a better formula for g^-1 using Mittelwertsatz ...
Definition: derinf.cpp:251
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: derinf.cpp:433
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: derinf.cpp:539
matrix::Matrix HY
Controller Bias-Y.
Definition: derinf.h:111
DerInf(const DerInfConf &conf=getDefaultConf())
Definition: derinf.cpp:29
double grang2
Definition: derinf.h:133
double TLE
Definition: derinf.h:131
matrix::Matrix RG
Granger1.
Definition: derinf.h:117
double sense
Definition: derinf.h:140
matrix::Matrix eta_smooth
Definition: derinf.h:156
double motor
Definition: abstractcontroller.h:49
matrix::Matrix y_smooth_long
Definition: derinf.h:155
std::list< ILayer > ilayerlist
Definition: inspectable.h:87
matrix::Matrix Q1
Definition: derinf.h:119
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: derinf.cpp:550
class for robot controller is based on InvertMotorNStep
Definition: derinf.h:49
matrix::Matrix G
Definition: derinf.h:169
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: derinf.h:130
matrix::Matrix xsi
current output error
Definition: derinf.h:127
double xsi_norm
norm of matrix
Definition: derinf.h:128
matrix::Matrix R
C*A.
Definition: derinf.h:115
matrix::Matrix y_error
Definition: derinf.h:160
matrix::Matrix Rm1
R^-1.
Definition: derinf.h:122
Definition: derinf.h:32
matrix::Matrix PID_deriv
Definition: derinf.h:162
int managementInterval
interval between subsequent management function calls
Definition: derinf.h:185
int c
Definition: hexapod.cpp:56
paramval PIDint
Definition: derinf.h:192
static DerInfConf getDefaultConf()
Definition: derinf.h:83
matrix::Matrix C_updateOld
Definition: derinf.h:163
matrix::Matrix S
additional Model Matrix (sensors to sensors)
Definition: derinf.h:104
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: derinf.cpp:202
matrix::Matrix ATA_inv
Definition: derinf.h:121
matrix::Matrix ID
identity matrix in the dimension of R
Definition: derinf.h:123
matrix::Matrix x_intern
Definition: derinf.h:180
Interface and basic class for noise generator.
Definition: noisegenerator.h:37