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
derpseudosensor.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 __DERPSEUDOSENSOR_H
20 #define __DERPSEUDOSENSOR_H
21 
22 #include "invertmotorcontroller.h"
23 
24 #include <assert.h>
25 #include <cmath>
26 
27 #include "matrix.h"
28 #include "multilayerffnn.h"
29 #include "noisegenerator.h"
30 #include "invertablemodel.h"
31 
32 #include "position.h"
33 
34 typedef struct DerPseudoSensorConf {
35  int buffersize; ///< buffersize size of the time-buffer for x,y,eta
36  double cInit; ///< cInit size of the C matrix to initialised with.
37  double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones
38  bool modelInit; ///< size of the unit-map strenght of the model
39  bool useS; ///< useS decides whether to use the S matrix in addition to the A matrix
40  bool someInternalParams; ///< someInternalParams if true only some internal parameters are exported, otherwise all
41 
42  double modelCompliant; ///< learning factor for model (or sensor) compliant learning
43  bool useFantasy; ///< if true fantasising is enabled
44 
45  InvertableModel* model; ///< model used as world model
46  InvertableModel* sat; ///< satellite network, that learns and teaches (can be 0)
48 
49 /**
50  * class for robot controller is based on InvertMotorNStep
51  *
52  * - direct inversion
53  *
54  * - motor space
55  *
56  * - multilayer,nonlinear model
57  */
59 
60 public:
62  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
63 
64  virtual ~DerPseudoSensor();
65 
66  /// returns the number of sensors the controller was initialised with or 0 if not initialised
67  virtual int getSensorNumber() const { return number_sensors; }
68  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
69  virtual int getMotorNumber() const { return number_motors; }
70 
71  /// performs one step (includes learning).
72  /// Calulates motor commands from sensor inputs.
73  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
74 
75  /// performs one step without learning. Calulates motor commands from sensor inputs.
76  virtual void stepNoLearning(const sensor* , int number_sensors,
77  motor* , int number_motors);
78 
79 
80  /************** STOREABLE **********************************/
81  /** stores the controller values to a given file. */
82  virtual bool store(FILE* f) const;
83  /** loads the controller values from a given file. */
84  virtual bool restore(FILE* f);
85 
86  /************** INSPECTABLE ********************************/
87  virtual iparamkeylist getInternalParamNames() const;
88  virtual iparamvallist getInternalParams() const;
89  virtual ilayerlist getStructuralLayers() const;
91 
92  /************** CONFIGURABLE ********************************/
93  virtual void notifyOnChange(const paramkey& key);
94 
95  /**** TEACHING ****/
96  /** The given motor teaching signal is used for this timestep.
97  It is used as a feed forward teaching signal for the controller.
98  Please note, that the teaching signal has to be given each timestep
99  for a continuous teaching process.
100  */
101  virtual void setMotorTeachingSignal(const motor* teaching, int len);
102 
103  /** The given sensor teaching signal (distal learning) is used for this timestep.
104  First the belonging motor teachung signal is calculated by the inverse model.
105  See setMotorTeachingSignal
106  */
107  virtual void setSensorTeachingSignal(const sensor* teaching, int len);
108 
109 
112  c.buffersize = 50;
113  c.cInit = 1.05;
114  c.cNonDiag = 0;
115  c.modelInit = 1.0;
116  c.someInternalParams = true;
117  // c.someInternalParams = false;
118  c.useS = false;
119  c.modelCompliant = 0;
120  c.model = 0;
121  c.useFantasy = false;
122  c.model = 0;
123  c.sat = 0;
124  return c;
125  }
126 
127  void getLastMotors(motor* motors, int len);
128 
129 protected:
130  unsigned short number_sensors;
131  unsigned short number_motors;
132 
133  matrix::Matrix A; ///< Model Matrix (motors to sensors)
134  matrix::Matrix A_Hat; ///< Model Matrix (motors to sensors) with input shift
135  matrix::Matrix S; ///< additional Model Matrix (sensors to sensors)
136  matrix::Matrix C; ///< Controller Matrix
137  matrix::Matrix GSC; ///< G_Prime times Controller Matrix
138  matrix::Matrix DD; ///< Noise Matrix
139  matrix::Matrix Dinverse; ///< Inverse Noise Matrix
140  matrix::Matrix H; ///< Controller Bias
141  matrix::Matrix B; ///< Model Bias
142  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
143  NoiseGenerator* YNoiseGen; ///< Noisegenerator for noisy motor values
144  matrix::Matrix R; ///< C*A
145  matrix::Matrix RG; ///< Granger1
146  matrix::Matrix Q; ///<Granger2
147  matrix::Matrix Q1; //<Granger3
148  matrix::Matrix RRT_inv; // (R*R^T)^-1
149  matrix::Matrix ATA_inv; // ((A^T)*A)^-1
150  matrix::Matrix Rm1; ///< R^-1
151  matrix::Matrix ID; ///< identity matrix in the dimension of R
152  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
172  matrix::Matrix zero_eta; // zero initialised eta
177 
178  MultiLayerFFNN* sat; ///< satilite network, that learns and teaches
179 
180  matrix::Matrix y_teaching; ///< teaching motor signal
181  bool useTeaching; ///< flag whether there is an actual teachning signal or not
182 
183  matrix::Matrix x_intern; ///< fantasy sensor values
184  int fantControl; ///< interval length for fantasising
185  int fantControlLen; ///< length of fantasy control
186  int fantReset; ///< number of fantasy control events before reseting internal state
187 
188  int t_rand; ///< initial random time to avoid syncronous management of all controllers
189  int managementInterval; ///< interval between subsequent management function calls
190  paramval dampS; ///< damping of S matrix
191  paramval dampC; ///< damping of C matrix
192  paramval dampH; ///< damping of H vector
193  paramval weighting; ///< general weighting factor between update concepts
194  paramval epsSat; ///< learning rate for satellite network
195  paramval satelliteTeaching; ///< teaching rate for sat teaching
196 
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  /// learn conf.model, (and S) using motors y and corresponding sensors x
214  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
215  virtual void learnModel(int delay);
216 
217  /// handles inhibition damping etc.
218  virtual void management();
219 
220  /// returns controller output for given sensor values
222 
223  /** Calculates first and second derivative and returns both in on matrix (above).
224  We use simple discrete approximations:
225  \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
226  \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
227  where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
228  */
229  matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
230 
231 public:
232 
233  /// calculates the city block distance (abs) norm of the matrix. (abs sum of absolutes / size of matrix)
234  virtual double calcMatrixNorm(const matrix::Matrix& m);
235 
236  virtual void setHeadPosition(Position pos) { headPosition=pos; }
237  virtual void setTrunkPosition(Position pos) { trunkPosition=pos; }
238 
239 
240 };
241 
242 #endif
matrix::Matrix eta_smooth
Definition: derpseudosensor.h:175
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: derpseudosensor.cpp:153
Matrix type.
Definition: matrix.h:65
abstract class (interface) for invertable models.
Definition: invertablemodel.h:33
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: derpseudosensor.cpp:67
std::list< IConnection > iconnectionlist
Definition: inspectable.h:88
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: derpseudosensor.h:67
NoiseGenerator * YNoiseGen
Noisegenerator for noisy motor values.
Definition: derpseudosensor.h:143
virtual ilayerlist getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: derpseudosensor.cpp:617
DerPseudoSensor(const DerPseudoSensorConf &conf=getDefaultConf())
Definition: derpseudosensor.cpp:29
matrix::Matrix Q
Granger2.
Definition: derpseudosensor.h:146
matrix::Matrix GSC
G_Prime times Controller Matrix.
Definition: derpseudosensor.h:137
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: derpseudosensor.h:133
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: derpseudosensor.cpp:507
matrix::Matrix x_smooth_long
Definition: derpseudosensor.h:176
matrix::Matrix x_smooth
Definition: derpseudosensor.h:173
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: derpseudosensor.h:69
matrix::Matrix x_intern
fantasy sensor values
Definition: derpseudosensor.h:183
matrix::Matrix ID_Sensor
identity matrix in the dimension of sensor space
Definition: derpseudosensor.h:152
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: derpseudosensor.h:142
paramval satelliteTeaching
teaching rate for sat teaching
Definition: derpseudosensor.h:195
matrix::Matrix H
Controller Bias.
Definition: derpseudosensor.h:140
matrix::Matrix * y_buffer
Definition: derpseudosensor.h:165
matrix::Matrix S
additional Model Matrix (sensors to sensors)
Definition: derpseudosensor.h:135
virtual iconnectionlist getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: derpseudosensor.cpp:625
charArray paramkey
Definition: avrtypes.h:36
bool modelInit
size of the unit-map strenght of the model
Definition: derpseudosensor.h:38
virtual void setMotorTeachingSignal(const motor *teaching, int len)
The given motor teaching signal is used for this timestep.
Definition: derpseudosensor.cpp:636
matrix::Matrix eta
Definition: derpseudosensor.h:170
matrix::Matrix * x_buffer
Definition: derpseudosensor.h:164
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: derpseudosensor.h:158
matrix::Matrix A_Hat
Model Matrix (motors to sensors) with input shift.
Definition: derpseudosensor.h:134
Position trunkPosition
Definition: derpseudosensor.h:198
double sensor
Definition: types.h:29
int fantControl
interval length for fantasising
Definition: derpseudosensor.h:184
matrix::Matrix ATA_inv
Definition: derpseudosensor.h:149
matrix::Matrix B
Model Bias.
Definition: derpseudosensor.h:141
virtual void setHeadPosition(Position pos)
Definition: derpseudosensor.h:236
virtual void learnModel(int delay)
learn conf.model, (and S) using motors y and corresponding sensors x
Definition: derpseudosensor.cpp:432
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: derpseudosensor.h:188
MultiLayerFFNN * sat
satilite network, that learns and teaches
Definition: derpseudosensor.h:178
matrix::Matrix * eta_buffer
Definition: derpseudosensor.h:169
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: derpseudosensor.cpp:465
matrix::Matrix * ysat_buffer
Definition: derpseudosensor.h:166
double causal
Definition: derpseudosensor.h:162
matrix::Matrix Q1
Definition: derpseudosensor.h:147
matrix::Matrix * rho_buffer
Definition: derpseudosensor.h:168
unsigned short number_motors
Definition: derpseudosensor.h:131
void getLastMotors(motor *motors, int len)
Definition: derpseudosensor.cpp:479
matrix::Matrix y_smooth
Definition: derpseudosensor.h:174
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: derpseudosensor.h:37
unsigned short number_sensors
Definition: derpseudosensor.h:130
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: derpseudosensor.cpp:652
bool useS
useS decides whether to use the S matrix in addition to the A matrix
Definition: derpseudosensor.h:39
Definition: position.h:30
double sensor
Definition: abstractcontroller.h:48
virtual void learnController(int delay)
learn values H,C This is the implementation uses a better formula for g^-1 using Mittelwertsatz ...
Definition: derpseudosensor.cpp:215
matrix::Matrix y_teaching
teaching motor signal
Definition: derpseudosensor.h:180
int fantReset
number of fantasy control events before reseting internal state
Definition: derpseudosensor.h:186
virtual void setSensorTeachingSignal(const sensor *teaching, int len)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: derpseudosensor.cpp:643
double paramval
Definition: configurable.h:88
Extended HomeokinBase class (still abstract) for robot controller work in motorspace and use possibly...
Definition: invertmotorcontroller.h:36
InvertableModel * model
model used as world model
Definition: derpseudosensor.h:45
virtual void setTrunkPosition(Position pos)
Definition: derpseudosensor.h:237
paramval epsSat
learning rate for satellite network
Definition: derpseudosensor.h:194
static DerPseudoSensorConf getDefaultConf()
Definition: derpseudosensor.h:110
double causalfactor
Definition: derpseudosensor.h:163
matrix::Matrix Dinverse
Inverse Noise Matrix.
Definition: derpseudosensor.h:139
struct DerPseudoSensorConf DerPseudoSensorConf
bool someInternalParams
someInternalParams if true only some internal parameters are exported, otherwise all ...
Definition: derpseudosensor.h:40
bool useFantasy
if true fantasising is enabled
Definition: derpseudosensor.h:43
paramval dampH
damping of H vector
Definition: derpseudosensor.h:192
matrix::Matrix xsi
current output error
Definition: derpseudosensor.h:155
matrix::Matrix calcDerivatives(const matrix::Matrix *buffer, int delay)
Calculates first and second derivative and returns both in on matrix (above).
Definition: derpseudosensor.cpp:484
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: derpseudosensor.cpp:171
double grang1
Definition: derpseudosensor.h:160
matrix::Matrix v_smooth
Definition: derpseudosensor.h:171
DerPseudoSensorConf conf
Definition: derpseudosensor.h:200
double xsi_norm
norm of matrix
Definition: derpseudosensor.h:156
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: derpseudosensor.h:35
matrix::Matrix * chi_buffer
Definition: derpseudosensor.h:167
matrix::Matrix CCT_inv
Definition: derpseudosensor.h:153
matrix::Matrix RRT_inv
Definition: derpseudosensor.h:148
virtual iparamvallist getInternalParams() const
Definition: derpseudosensor.cpp:577
double cInit
cInit size of the C matrix to initialised with.
Definition: derpseudosensor.h:36
double motor
Definition: types.h:30
int fantControlLen
length of fantasy control
Definition: derpseudosensor.h:185
int managementInterval
interval between subsequent management function calls
Definition: derpseudosensor.h:189
virtual void management()
handles inhibition damping etc.
Definition: derpseudosensor.cpp:491
matrix::Matrix ID
identity matrix in the dimension of R
Definition: derpseudosensor.h:151
matrix::Matrix zero_eta
Definition: derpseudosensor.h:172
Position headPosition
Definition: derpseudosensor.h:197
paramval dampS
damping of S matrix
Definition: derpseudosensor.h:190
Definition: derpseudosensor.h:34
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: derpseudosensor.cpp:518
matrix::Matrix RG
Granger1.
Definition: derpseudosensor.h:145
class for robot controller is based on InvertMotorNStep
Definition: derpseudosensor.h:58
matrix::Matrix R
C*A.
Definition: derpseudosensor.h:144
InvertableModel * sat
satellite network, that learns and teaches (can be 0)
Definition: derpseudosensor.h:46
matrix::Matrix C
Controller Matrix.
Definition: derpseudosensor.h:136
multi layer neural network with configurable activation functions
Definition: multilayerffnn.h:35
virtual ~DerPseudoSensor()
Definition: derpseudosensor.cpp:55
paramval dampC
damping of C matrix
Definition: derpseudosensor.h:191
double grang2
Definition: derpseudosensor.h:161
double motor
Definition: abstractcontroller.h:49
std::list< ILayer > ilayerlist
Definition: inspectable.h:87
matrix::Matrix DD
Noise Matrix.
Definition: derpseudosensor.h:138
double TLE
Definition: derpseudosensor.h:159
matrix::Matrix CST
Definition: derpseudosensor.h:154
std::list< iparamkey > iparamkeylist
Definition: inspectable.h:59
std::list< iparamval > iparamvallist
Definition: inspectable.h:61
virtual iparamkeylist getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: derpseudosensor.cpp:538
bool useTeaching
flag whether there is an actual teachning signal or not
Definition: derpseudosensor.h:181
virtual void notifyOnChange(const paramkey &key)
Is called when a parameter was changes via setParam().
Definition: derpseudosensor.cpp:530
int c
Definition: hexapod.cpp:56
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: derpseudosensor.h:157
double modelCompliant
learning factor for model (or sensor) compliant learning
Definition: derpseudosensor.h:42
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: derpseudosensor.cpp:179
matrix::Matrix Rm1
R^-1.
Definition: derpseudosensor.h:150
paramval weighting
general weighting factor between update concepts
Definition: derpseudosensor.h:193
Interface and basic class for noise generator.
Definition: noisegenerator.h:37