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
dercontroller.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __DERCONTROLLER_H
25 #define __DERCONTROLLER_H
26 
27 #include "invertmotorcontroller.h"
28 
29 #include <assert.h>
30 #include <cmath>
31 
32 #include "matrix.h"
33 #include "noisegenerator.h"
34 
35 typedef struct DerControllerConf {
36  int buffersize; ///< buffersize size of the time-buffer for x,y,eta
37  double cInit; ///< cInit size of the C matrix to initialised with.
38  double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones
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, all otherwise
41  bool useTeaching; ///< if true, the controller honors the teaching signal
42  bool useFantasy; ///< if true fantasising is enabled
44 
45 /**
46  * class for robot controller that uses the georg's matrixlib for
47  * direct matrix inversion for n channels
48  * (simple one layer networks)
49  *
50  * Implements standart parameters: eps, rho, mu, stepnumber4avg, stepnumber4delay
51  */
53 
54 public:
55 
57  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
58 
59  virtual ~DerController();
60 
61  /// returns the number of sensors the controller was initialised with or 0 if not initialised
62  virtual int getSensorNumber() const { return number_sensors; }
63  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
64  virtual int getMotorNumber() const { return number_motors; }
65 
66  /// performs one step (includes learning).
67  /// Calulates motor commands from sensor inputs.
68  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
69 
70  /// performs one step without learning. Calulates motor commands from sensor inputs.
71  virtual void stepNoLearning(const sensor* , int number_sensors,
72  motor* , int number_motors);
73 
74 
75  /**** STOREABLE ****/
76  virtual bool store(FILE* f) const;
77  virtual bool restore(FILE* f);
78 
79  /**** CONFIGURABLE ****/
80  virtual std::list<iparamkey> getInternalParamNames() const;
81  virtual std::list<iparamval> getInternalParams() const;
82  virtual std::list<ILayer> getStructuralLayers() const;
83  virtual std::list<IConnection> getStructuralConnections() const;
84 
85  /**** TEACHING ****/
86  virtual void setTeachingMode(bool onOff);
87  virtual bool getTeachingMode();
88  virtual void setMotorTeachingSignal(const motor* teaching, int len);
89  //void calcCandHUpdatesTeaching(Matrix& C_update, Matrix& H_update, int y_delay);
90  //void calcCandHUpdates(Matrix& C_update, Matrix& H_update,Matrix& A_update, int y_delay);//Test A
91 
94  c.buffersize = 50;
95  c.cInit = 1.2;
96  c.cNonDiag = 0;
97  c.useS = false;
98  //c.someInternalParams = true;//This is for gnuplout, only the first few nodiagonal elements
99  c.someInternalParams = false;//This is for gnuplout,to plot all matrix elements
100  c.useTeaching = false;
101  c.useFantasy = false;
102  return c;
103  }
104 
105  void getLastMotors(motor* motors, int len);
106 
107 protected:
108  unsigned short number_sensors;
109  unsigned short number_motors;
110 
111  matrix::Matrix A; ///< Model Matrix (motors to sensors)
112  matrix::Matrix S; ///< additional Model Matrix (sensors to sensors)
113  matrix::Matrix C; ///< Controller Matrix
114  matrix::Matrix DD; ///< Noise Matrix
115  matrix::Matrix Dinverse; ///< Inverse Noise Matrix
116  matrix::Matrix H; ///< Controller Bias
117  matrix::Matrix B; ///< Model Bias
118  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
119  NoiseGenerator* YNoiseGen; ///< Noisegenerator for noisy motor values
120  matrix::Matrix R; ///< C*A
121  matrix::Matrix RRT; // R*R^T
122  matrix::Matrix AAT; // (A^T)*A
123  // matrix::Matrix Rm1; ///< R^-1
124  matrix::Matrix SmallID; ///< small identity matrix in the dimension of R
125  matrix::Matrix xsi; ///< current output error
126  double xsi_norm; ///< norm of matrix
127  double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
128  double pain; ///< if the modelling error (xsi) is too high we have a pain signal
132  matrix::Matrix zero_eta; // zero initialised eta
134  // matrix::Matrix v_smooth;
136 
137 
138  matrix::Matrix y_teaching; ///< teaching motor signal
139 
140  matrix::Matrix x_intern; ///< fantasy sensor values
141  int fantControl; ///< interval length for fantasising
142  int fantControlLen; ///< length of fantasy control
143  int fantReset; ///< number of fantasy control events before reseting internal state
144 
146 
147  /// puts the sensors in the ringbuffer, generate controller values and put them in the
148  // ringbuffer as well
149  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
150  motor* y_, int number_motors);
151 
152  /// calculates the first shift into the motor space useing delayed motor values.
153  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
154  virtual void calcEtaAndBufferIt(int delay);
155  /// calculates xsi for the current time step using the delayed y values
156  // and x delayed by one
157  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
158  virtual void calcXsi(int delay);
159 
160  /// learn H,C with motors y and corresponding sensors x
161  virtual void learnController();
162 
163  /// calculates the predicted sensor values
164  virtual matrix::Matrix model(const matrix::Matrix& x, const matrix::Matrix& y);
165 
166  /// calculates the Update for C, H and A
167  // @param y_delay timesteps to delay the y-values. (usually 0)
168  // Please note that the delayed values are NOT used for the error calculation
169  // (this is done in calcXsi())
170  virtual void calcCandHandAUpdates(matrix::Matrix& C_update, matrix::Matrix& H_update,
171  matrix::Matrix& A_update, int y_delay);//Test A
172  /// updates the matrices C, H and A
173  virtual void updateCandHandA(const matrix::Matrix& C_update, const matrix::Matrix& H_update,
174  const matrix::Matrix& A_update, double squashSize);//Test A
175 
177 
178  /// calculates the city block distance (abs) norm of the matrix. (abs sum of absolutes / size of matrix)
179  virtual double calcMatrixNorm(const matrix::Matrix& m);
180  /// calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error
181  virtual double calcErrorFactor(const matrix::Matrix& e, bool loga, bool root);
182 
183 };
184 
185 #endif
paramval squashSize
size of the box, where the parameter updates are clipped to
Definition: homeokinbase.h:65
void getLastMotors(motor *motors, int len)
Definition: dercontroller.cpp:416
Matrix type.
Definition: matrix.h:65
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: dercontroller.h:62
matrix::Matrix AAT
Definition: dercontroller.h:122
double cInit
cInit size of the C matrix to initialised with.
Definition: dercontroller.h:37
matrix::Matrix B
Model Bias.
Definition: dercontroller.h:117
bool useFantasy
if true fantasising is enabled
Definition: dercontroller.h:42
virtual void updateCandHandA(const matrix::Matrix &C_update, const matrix::Matrix &H_update, const matrix::Matrix &A_update, double squashSize)
updates the matrices C, H and A
Definition: dercontroller.cpp:379
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: dercontroller.cpp:59
bool someInternalParams
someInternalParams if true only some internal parameters are exported, all otherwise ...
Definition: dercontroller.h:40
matrix::Matrix S
additional Model Matrix (sensors to sensors)
Definition: dercontroller.h:112
matrix::Matrix x_intern
fantasy sensor values
Definition: dercontroller.h:140
bool useS
useS decides whether to use the S matrix in addition to the A matrix
Definition: dercontroller.h:39
virtual void setMotorTeachingSignal(const motor *teaching, int len)
Definition: dercontroller.cpp:519
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: dercontroller.h:118
int fantReset
number of fantasy control events before reseting internal state
Definition: dercontroller.h:143
matrix::Matrix R
C*A.
Definition: dercontroller.h:120
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: dercontroller.h:128
double cNonDiag
cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones ...
Definition: dercontroller.h:38
matrix::Matrix eta_smooth
Definition: dercontroller.h:135
matrix::Matrix SmallID
small identity matrix in the dimension of R
Definition: dercontroller.h:124
unsigned short number_motors
Definition: dercontroller.h:109
Definition: dercontroller.h:35
double sensor
Definition: types.h:29
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: dercontroller.h:127
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual double calcErrorFactor(const matrix::Matrix &e, bool loga, bool root)
calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error ...
Definition: dercontroller.cpp:366
virtual ~DerController()
Definition: dercontroller.cpp:48
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: dercontroller.cpp:135
virtual bool getTeachingMode()
Definition: dercontroller.cpp:515
int fantControlLen
length of fantasy control
Definition: dercontroller.h:142
matrix::Matrix * y_buffer
Definition: dercontroller.h:130
struct DerControllerConf DerControllerConf
double sensor
Definition: abstractcontroller.h:48
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: dercontroller.h:111
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: dercontroller.cpp:117
Extended HomeokinBase class (still abstract) for robot controller work in motorspace and use possibly...
Definition: invertmotorcontroller.h:36
virtual std::list< ILayer > getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: dercontroller.cpp:495
matrix::Matrix H
Controller Bias.
Definition: dercontroller.h:116
class for robot controller that uses the georg's matrixlib for direct matrix inversion for n channels...
Definition: dercontroller.h:52
unsigned short number_sensors
Definition: dercontroller.h:108
virtual void calcCandHandAUpdates(matrix::Matrix &C_update, matrix::Matrix &H_update, matrix::Matrix &A_update, int y_delay)
calculates the Update for C, H and A
Definition: dercontroller.cpp:270
virtual bool store(FILE *f) const
stores the object to the given file stream (ASCII preferred).
Definition: dercontroller.cpp:422
DerController(const DerControllerConf &conf=getDefaultConf())
Definition: dercontroller.cpp:32
virtual void learnController()
learn H,C with motors y and corresponding sensors x
Definition: dercontroller.cpp:228
bool useTeaching
if true, the controller honors the teaching signal
Definition: dercontroller.h:41
int fantControl
interval length for fantasising
Definition: dercontroller.h:141
matrix::Matrix xsi
current output error
Definition: dercontroller.h:125
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: dercontroller.cpp:524
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
calculate controller outputs
Definition: dercontroller.cpp:412
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: dercontroller.cpp:142
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: dercontroller.h:64
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: dercontroller.h:36
double motor
Definition: types.h:30
matrix::Matrix * x_buffer
Definition: dercontroller.h:129
matrix::Matrix Dinverse
Inverse Noise Matrix.
Definition: dercontroller.h:115
matrix::Matrix DD
Noise Matrix.
Definition: dercontroller.h:114
matrix::Matrix x_smooth
Definition: dercontroller.h:133
NoiseGenerator * YNoiseGen
Noisegenerator for noisy motor values.
Definition: dercontroller.h:119
matrix::Matrix zero_eta
Definition: dercontroller.h:132
matrix::Matrix * eta_buffer
Definition: dercontroller.h:131
matrix::Matrix RRT
Definition: dercontroller.h:121
double motor
Definition: abstractcontroller.h:49
virtual std::list< iparamval > getInternalParams() const
Definition: dercontroller.cpp:469
virtual std::list< iparamkey > getInternalParamNames() const
The list of the names of all internal parameters given by getInternalParams().
Definition: dercontroller.cpp:443
virtual void calcEtaAndBufferIt(int delay)
calculates the first shift into the motor space useing delayed motor values.
Definition: dercontroller.cpp:190
virtual void calcXsi(int delay)
calculates xsi for the current time step using the delayed y values
Definition: dercontroller.cpp:210
matrix::Matrix y_teaching
teaching motor signal
Definition: dercontroller.h:138
matrix::Matrix C
Controller Matrix.
Definition: dercontroller.h:113
virtual std::list< IConnection > getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: dercontroller.cpp:503
DerControllerConf conf
Definition: dercontroller.h:145
static DerControllerConf getDefaultConf()
Definition: dercontroller.h:92
virtual bool restore(FILE *f)
loads the object from the given file stream (ASCII preferred).
Definition: dercontroller.cpp:432
int c
Definition: hexapod.cpp:56
virtual void setTeachingMode(bool onOff)
Definition: dercontroller.cpp:511
virtual matrix::Matrix model(const matrix::Matrix &x, const matrix::Matrix &y)
calculates the predicted sensor values
Definition: dercontroller.cpp:218
double xsi_norm
norm of matrix
Definition: dercontroller.h:126
Interface and basic class for noise generator.
Definition: noisegenerator.h:37