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
derlininvert.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 __DERLININVERT_H
20 #define __DERLININVERT_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 DerLinInvertConf {
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  bool useFantasy; ///< if true fantasising is enabled
42 
43  InvertableModel* model; ///< model used as world model
44  InvertableModel* sat; ///< satellite network, that learns and teaches (can be 0)
46 /**
47  * class for robot controller is based on InvertMotorNStep
48  *
49  * - direct inversion
50  *
51  * - motor space
52  *
53  * - multilayer,nonlinear model
54  */
56 
57 public:
59  virtual void init(int sensornumber, int motornumber, RandGen* randg);
60 
61  virtual ~DerLinInvert();
62 
63  /// returns the number of sensors the controller was initialised with or 0 if not initialised
64  virtual int getSensorNumber() const { return number_sensors; }
65  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
66  virtual int getMotorNumber() const { return number_motors; }
67 
68  /// performs one step (includes learning).
69  /// Calulates motor commands from sensor inputs.
70  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
71 
72  /// performs one step without learning. Calulates motor commands from sensor inputs.
73  virtual void stepNoLearning(const sensor* , int number_sensors,
74  motor* , int number_motors);
75 
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 iparamkeylist getInternalParamNames() const;
85  virtual iparamvallist getInternalParams() const;
86  virtual ilayerlist getStructuralLayers() const;
88 
89  /**** TEACHING ****/
90  /** The given motor teaching signal is used for this timestep.
91  It is used as a feed forward teaching signal for the controller.
92  Please note, that the teaching signal has to be given each timestep
93  for a continuous teaching process.
94  */
95  virtual void setMotorTeachingSignal(const motor* teaching, int len);
96 
97  /** The given sensor teaching signal (distal learning) is used for this timestep.
98  First the belonging motor teachung signal is calculated by the inverse model.
99  See setMotorTeachingSignal
100  */
101  virtual void setSensorTeachingSignal(const sensor* teaching, int len);
102 
103 
106  c.buffersize = 50;
107  c.cInit = 1.05;
108  c.cNonDiag = 0;
109  c.modelInit = 1.0;
110  c.someInternalParams = true;
111  // c.someInternalParams = false;
112  c.useS = true;
113  c.modelCompliant = 0.0;
114  c.model = 0;
115  c.useFantasy = false;
116  c.model = 0;
117  c.sat = 0;
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)
129  matrix::Matrix A_Hat; ///< Model Matrix (motors to sensors) with input shift
130  matrix::Matrix S; ///< additional Model Matrix (sensors to sensors)
131  matrix::Matrix C; ///< Controller Matrix
132  matrix::Matrix GSC; ///< G_Prime times Controller Matrix
133  matrix::Matrix DD; ///< Noise Matrix
134  matrix::Matrix Dinverse; ///< Inverse Noise Matrix
135  matrix::Matrix H; ///< Controller Bias
136  matrix::Matrix HY; ///< Controller Bias-Y
137  matrix::Matrix B; ///< Model Bias
138  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
139  NoiseGenerator* YNoiseGen; ///< Noisegenerator for noisy motor values
140  matrix::Matrix R; ///< C*A
141  matrix::Matrix P; ///< A*G'*C
142  matrix::Matrix RG; ///< Granger1
143  matrix::Matrix Q; ///<Granger2
144  matrix::Matrix Q1; //<Granger3
145  matrix::Matrix RRT_inv; // (R*R^T)^-1
146  matrix::Matrix ATA_inv; // ((A^T)*A)^-1
147  matrix::Matrix Rm1; ///< R^-1
148  matrix::Matrix ID; ///< identity matrix in the dimension of R
149  matrix::Matrix ID_Sensor; ///< identity matrix in the dimension of sensor space
155  matrix::Matrix xsi; ///< current output error
156  double xsi_norm; ///< norm of matrix
157  double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
158  double pain; ///< if the modelling error (xsi) is too high we have a pain signal
159  double TLE; // TimeLoopError
160  double grang1; //GrangerCausality
161  double grang2; //GrangerCausality
162  double causal; //GrangerCausality
163  double causalfactor; //GrangerCausality
164  double EE_mean;
165  double EE_sqr;
174  matrix::Matrix zero_eta; // zero initialised eta
186 
187  MultiLayerFFNN* sat; ///< satilite network, that learns and teaches
188 
189  matrix::Matrix y_teaching; ///< teaching motor signal
190  bool useTeaching; ///< flag whether there is an actual teachning signal or not
191 
192  matrix::Matrix x_intern; ///< fantasy sensor values
193  int fantControl; ///< interval length for fantasising
194  int fantControlLen; ///< length of fantasy control
195  int fantReset; ///< number of fantasy control events before reseting internal state
196 
197  int t_rand; ///< initial random time to avoid syncronous management of all controllers
198  int t_delay;
199  int managementInterval; ///< interval between subsequent management function calls
200  paramval dampS; ///< damping of S matrix
201  paramval dampC; ///< damping of C matrix
202  paramval dampH; ///< damping of H vector
203  paramval weighting; ///< general weighting factor between update concepts
204  paramval epsSat; ///< learning rate for satellite network
205  paramval satelliteTeaching; ///< teaching rate for sat teaching
210  // paramval test;
211 
213 
214  /// puts the sensors in the ringbuffer, generate controller values and put them in the
215  // ringbuffer as well
216  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
217  motor* y_, int number_motors);
218 
219 /** learn values H,C
220  This is the implementation uses a better formula for g^-1 using Mittelwertsatz
221  @param delay 0 for no delay and n>0 for n timesteps delay in the SML (s4delay)
222 */
223  virtual void learnController(int delay);
224 
225  /// learn conf.model, (and S) using motors y and corresponding sensors x
226  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
227  virtual void learnModel(int delay);
228 
229  /// handles inhibition damping etc.
230  virtual void management();
231 
232  /// returns controller output for given sensor values
234 
235  /** Calculates first and second derivative and returns both in on matrix (above).
236  We use simple discrete approximations:
237  \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
238  \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
239  where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
240  */
241  matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
242 
243 public:
244 
245  /// calculates the city block distance (abs) norm of the matrix. (abs sum of absolutes / size of matrix)
246  virtual double calcMatrixNorm(const matrix::Matrix& m);
247 
248 
249 };
250 
251 #endif
Matrix type.
Definition: matrix.h:65
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: derlininvert.cpp:613
abstract class (interface) for invertable models.
Definition: invertablemodel.h:33
matrix::Matrix A_Hat
Model Matrix (motors to sensors) with input shift.
Definition: derlininvert.h:129
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: derlininvert.h:35
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: derlininvert.h:33
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: derlininvert.cpp:177
double grang2
Definition: derlininvert.h:161
int managementInterval
interval between subsequent management function calls
Definition: derlininvert.h:199
NoiseGenerator * YNoiseGen
Noisegenerator for noisy motor values.
Definition: derlininvert.h:139
matrix::Matrix x_smooth
Definition: derlininvert.h:175
paramval dampS
damping of S matrix
Definition: derlininvert.h:200
matrix::Matrix mue1
Definition: derlininvert.h:185
matrix::Matrix * rho_buffer
Definition: derlininvert.h:170
paramval intstate
Definition: derlininvert.h:208
InvertableModel * model
model used as world model
Definition: derlininvert.h:43
virtual void setMotorTeachingSignal(const motor *teaching, int len)
The given motor teaching signal is used for this timestep.
Definition: derlininvert.cpp:730
int fantReset
number of fantasy control events before reseting internal state
Definition: derlininvert.h:195
matrix::Matrix DeltaH_Old
Definition: derlininvert.h:153
matrix::Matrix ID_Sensor
identity matrix in the dimension of sensor space
Definition: derlininvert.h:149
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: derlininvert.cpp:627
matrix::Matrix P
A*G'*C.
Definition: derlininvert.h:141
MultiLayerFFNN * sat
satilite network, that learns and teaches
Definition: derlininvert.h:187
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: derlininvert.cpp:195
matrix::Matrix y_sat
Definition: derlininvert.h:177
unsigned short number_sensors
Definition: derlininvert.h:124
matrix::Matrix Rm1
R^-1.
Definition: derlininvert.h:147
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: derlininvert.cpp:203
matrix::Matrix Dinverse
Inverse Noise Matrix.
Definition: derlininvert.h:134
matrix::Matrix y_teaching
teaching motor signal
Definition: derlininvert.h:189
paramval epsSat
learning rate for satellite network
Definition: derlininvert.h:204
paramval satelliteTeaching
teaching rate for sat teaching
Definition: derlininvert.h:205
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: derlininvert.cpp:719
double sensor
Definition: types.h:29
int t_delay
Definition: derlininvert.h:198
matrix::Matrix DeltaC_Old
Definition: derlininvert.h:152
unsigned short number_motors
Definition: derlininvert.h:125
matrix::Matrix H
Controller Bias.
Definition: derlininvert.h:135
matrix::Matrix * eta_buffer
Definition: derlininvert.h:171
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
matrix::Matrix ID
identity matrix in the dimension of R
Definition: derlininvert.h:148
matrix::Matrix * y_buffer
Definition: derlininvert.h:167
int fantControl
interval length for fantasising
Definition: derlininvert.h:193
virtual void setSensorTeachingSignal(const sensor *teaching, int len)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: derlininvert.cpp:737
double EE_sqr
Definition: derlininvert.h:165
matrix::Matrix CST
Definition: derlininvert.h:151
matrix::Matrix x_smooth_long
Definition: derlininvert.h:180
matrix::Matrix Q1
Definition: derlininvert.h:144
matrix::Matrix y_integration
Definition: derlininvert.h:182
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: derlininvert.h:66
double TLE
Definition: derlininvert.h:159
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: derlininvert.h:138
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: derlininvert.h:64
static DerLinInvertConf getDefaultConf()
Definition: derlininvert.h:104
bool someInternalParams
someInternalParams if true only some internal parameters are exported, otherwise all ...
Definition: derlininvert.h:38
Definition: derlininvert.h:32
bool useS
useS decides whether to use the S matrix in addition to the A matrix
Definition: derlininvert.h:37
matrix::Matrix * ysat_buffer
Definition: derlininvert.h:168
matrix::Matrix * x_buffer
Definition: derlininvert.h:166
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: derlininvert.cpp:600
double sensor
Definition: abstractcontroller.h:48
DerLinInvert(const DerLinInvertConf &conf=getDefaultConf())
Definition: derlininvert.cpp:29
double xsi_norm
norm of matrix
Definition: derlininvert.h:156
double paramval
Definition: configurable.h:88
Extended HomeokinBase class (still abstract) for robot controller work in motorspace and use possibly...
Definition: invertmotorcontroller.h:36
double cInit
cInit size of the C matrix to initialised with.
Definition: derlininvert.h:34
matrix::Matrix zero_eta
Definition: derlininvert.h:174
matrix::Matrix PID_deriv
Definition: derlininvert.h:183
matrix::Matrix B
Model Bias.
Definition: derlininvert.h:137
matrix::Matrix C
Controller Matrix.
Definition: derlininvert.h:131
paramval PIDint
Definition: derlininvert.h:206
matrix::Matrix vau1
Definition: derlininvert.h:184
void getLastMotors(motor *motors, int len)
Definition: derlininvert.cpp:571
bool modelInit
size of the unit-map strenght of the model
Definition: derlininvert.h:36
matrix::Matrix x_intern
fantasy sensor values
Definition: derlininvert.h:192
matrix::Matrix DD
Noise Matrix.
Definition: derlininvert.h:133
struct DerLinInvertConf DerLinInvertConf
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: derlininvert.h:157
paramval weighting
general weighting factor between update concepts
Definition: derlininvert.h:203
InvertableModel * sat
satellite network, that learns and teaches (can be 0)
Definition: derlininvert.h:44
double modelCompliant
learning factor for model (or sensor) compliant learning
Definition: derlininvert.h:40
virtual void management()
handles inhibition damping etc.
Definition: derlininvert.cpp:583
matrix::Matrix xsi
current output error
Definition: derlininvert.h:155
double causal
Definition: derlininvert.h:162
matrix::Matrix Q
Granger2.
Definition: derlininvert.h:143
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: derlininvert.h:127
matrix::Matrix v_smooth
Definition: derlininvert.h:173
paramval dampC
damping of C matrix
Definition: derlininvert.h:201
double motor
Definition: types.h:30
matrix::Matrix CCT_inv
Definition: derlininvert.h:150
matrix::Matrix y_forecast
Definition: derlininvert.h:181
virtual ~DerLinInvert()
Definition: derlininvert.cpp:61
matrix::Matrix * chi_buffer
Definition: derlininvert.h:169
matrix::Matrix DeltaHY_Old
Definition: derlininvert.h:154
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: derlininvert.cpp:711
matrix::Matrix calcDerivatives(const matrix::Matrix *buffer, int delay)
Calculates first and second derivative and returns both in on matrix (above).
Definition: derlininvert.cpp:576
matrix::Matrix S
additional Model Matrix (sensors to sensors)
Definition: derlininvert.h:130
matrix::Matrix y_smooth_long
Definition: derlininvert.h:178
matrix::Matrix y_smooth
Definition: derlininvert.h:176
bool useTeaching
flag whether there is an actual teachning signal or not
Definition: derlininvert.h:190
bool useFantasy
if true fantasising is enabled
Definition: derlininvert.h:41
matrix::Matrix eta
Definition: derlininvert.h:172
double grang1
Definition: derlininvert.h:160
multi layer neural network with configurable activation functions
Definition: multilayerffnn.h:35
matrix::Matrix GSC
G_Prime times Controller Matrix.
Definition: derlininvert.h:132
matrix::Matrix eta_smooth
Definition: derlininvert.h:179
virtual void learnModel(int delay)
learn conf.model, (and S) using motors y and corresponding sensors x
Definition: derlininvert.cpp:520
double motor
Definition: abstractcontroller.h:49
matrix::Matrix HY
Controller Bias-Y.
Definition: derlininvert.h:136
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: derlininvert.h:197
matrix::Matrix A0
Definition: derlininvert.h:128
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: derlininvert.cpp:554
matrix::Matrix ATA_inv
Definition: derlininvert.h:146
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: derlininvert.cpp:746
double causalfactor
Definition: derlininvert.h:163
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
virtual void init(int sensornumber, int motornumber, RandGen *randg)
initialisation of the controller with the given sensor/ motornumber Must be called before use...
Definition: derlininvert.cpp:73
matrix::Matrix RRT_inv
Definition: derlininvert.h:145
DerLinInvertConf conf
Definition: derlininvert.h:212
double EE_mean
Definition: derlininvert.h:164
int fantControlLen
length of fantasy control
Definition: derlininvert.h:194
virtual void learnController(int delay)
learn values H,C This is the implementation uses a better formula for g^-1 using Mittelwertsatz ...
Definition: derlininvert.cpp:270
paramval gamma
Definition: derlininvert.h:209
int c
Definition: hexapod.cpp:56
paramval dampH
damping of H vector
Definition: derlininvert.h:202
virtual iparamvallist getInternalParams() const
Definition: derlininvert.cpp:667
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: derlininvert.h:158
paramval PIDdrv
Definition: derlininvert.h:207
class for robot controller is based on InvertMotorNStep
Definition: derlininvert.h:55
matrix::Matrix RG
Granger1.
Definition: derlininvert.h:142
matrix::Matrix R
C*A.
Definition: derlininvert.h:140
Interface and basic class for noise generator.
Definition: noisegenerator.h:37