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
semox.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 __SEMOX_H
20 #define __SEMOX_H
21 
22 #include <selforg/homeokinbase.h>
23 #include <selforg/matrix.h>
24 #include <selforg/teachable.h>
25 #include <selforg/noisegenerator.h>
26 #include <selforg/randomgenerator.h>
27 #include <selforg/parametrizable.h>
28 
29 
30 typedef struct SeMoXConf {
31  int buffersize; ///< buffersize size of the time-buffer for x,y,eta
32  matrix::Matrix initialC; ///< initialC initial controller matrix (if null matrix then automatic, see cInit)
33  double cInit; ///< cInit initial size of the diagonals of the C matrix (if C is not given)
34  double cNonDiag; ///< cNonDiag initial size of the non-diagonal elements of the C matrix (if C is not given)
35  double aInit; ///< aInit initial size of the diagonals of the A matrix
36  double sInit; ///< sInit initial size of the diagonals of the S matrix
37  bool modelExt; ///< modelExt if true then additional matrix S is used in forward model (sees sensors)
38  /** number of context sensors (considered at the end of the sensor
39  vector, which are only feed to the model extended model */
41  bool someInternalParams; ///< someInternalParams if true only some internal parameters are exported
42 } SeMoXConf;
43 
44 /**
45  * This controller follows the prinziple of homeokinesis and
46  * implements the extensions described in the thesis of Georg Martius
47  * 2009, University Goettingen:
48  * Goal-Oriented Control of Self-organizing Behavior in Autonomous Robots
49  *
50  * This class also implements part of the guided self-organization
51  *
52  * Name: SElf-organizing MOtor space eXtended
53  *
54  * Main characteristics: Motor Space, Extended World model, Continuity, Teaching interface
55  *
56  */
57 class SeMoX : public HomeokinBase, public Teachable, public Parametrizable {
58  friend class ThisSim;
59 public:
60  SeMoX(const SeMoXConf& conf = getDefaultConf());
61 
62  /// returns the default configuration
64  SeMoXConf c;
65  c.buffersize = 50;
66  // c.initialC // remains 0x0
67  c.cInit = 1.0;
68  c.cNonDiag = 0;
69  c.aInit = 1.0;
70  c.sInit = 0.0;
71  c.modelExt = true;
72  c.someInternalParams = true;
73  c.numContext = 0;
74  return c;
75  }
76 
77  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
78 
79  virtual ~SeMoX();
80 
81  /// returns the number of sensors the controller was initialised with or 0 if not initialised
82  virtual int getSensorNumber() const { return number_sensors; }
83  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
84  virtual int getMotorNumber() const { return number_motors; }
85 
86  /// performs one step (includes learning).
87  /// Calulates motor commands from sensor inputs.
88  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
89 
90  /// performs one step without learning. Calulates motor commands from sensor inputs.
91  virtual void stepNoLearning(const sensor* , int number_sensors,
92  motor* , int number_motors);
93 
94  /**** STOREABLE ****/
95  /** stores the controller values to a given file. */
96  virtual bool store(FILE* f) const;
97  /** loads the controller values from a given file. */
98  virtual bool restore(FILE* f);
99 
100  /**** INSPECTABLE ****/
101  virtual std::list<ILayer> getStructuralLayers() const;
102  virtual std::list<IConnection> getStructuralConnections() const;
103 
104  /**** TEACHABLE ****/
105  /** The given motor teaching signal is used for this timestep.
106  It is used as a feed forward teaching signal for the controller.
107  Please note, that the teaching signal has to be given each timestep
108  for a continuous teaching process.
109  @param teaching: matrix with dimensions (motornumber,1)
110  */
111  virtual void setMotorTeaching(const matrix::Matrix& teaching);
112 
113  /** The given sensor teaching signal (distal learning) is used for this timestep.
114  The belonging motor teachung signal is calculated by the inverse model.
115  See setMotorTeaching
116  @param teaching: matrix with dimensions (motorsensors,1)
117  */
118  virtual void setSensorTeaching(const matrix::Matrix& teaching);
119  /// returns the last motor values (useful for cross motor coupling)
121  /// returns the last sensor values (useful for cross sensor coupling)
123 
124  /***** PARAMETRIZABLE ****/
125  virtual std::list<matrix::Matrix> getParameters() const override;
126  virtual int setParameters(const std::list<matrix::Matrix>& params) override;
127 
128 protected:
129  unsigned short number_sensors;
130  unsigned short number_motors;
131 
132  matrix::Matrix A; ///< Model Matrix (motors to sensors)
133  matrix::Matrix S; ///< additional Model Matrix (sensors derivatives to sensors)
134  matrix::Matrix C; ///< Controller Matrix
135  matrix::Matrix H; ///< Controller Bias
136  matrix::Matrix B; ///< Model Bias
137  matrix::Matrix R; ///< C*A
138  matrix::Matrix SmallID; ///< small identity matrix in the dimension of R
139  matrix::Matrix v; ///< shift
140  matrix::Matrix xsi; ///< current output error
141 
142  NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
143  paramval modelNoise; ///< strength of noisy bias
144 
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
148 
150  matrix::Matrix* x_c_buffer; ///< buffer for sensors with context sensors
152 
153  matrix::Matrix y_teaching; ///< motor teaching signal
154 
155  paramval gamma_cont; ///< parameter to include contiuity in motor values (avoid high frequencies)
156  paramval gamma_teach; ///< strength of teaching
157  paramval discountS; ///< discount strength for hierachical model
158 
159  paramval dampModel; ///< damping of A and S matrices
160  paramval dampController; ///< damping of C matrix
161 
163 
164  // internal
165  bool intern_useTeaching; ///< flag whether there is an actual teachning signal or not
166  int t_rand; ///< initial random time to avoid syncronous management of all controllers
167  int managementInterval; ///< interval between subsequent management function calls
168  parambool _modelExt_copy; ///< copy of modelExtension variable (to achieve readonly)
169 
170  /// puts the sensors in the ringbuffer, generate controller values and put them in the
171  // ringbuffer as well
172  virtual void fillBuffersAndControl(const sensor* x_, int number_sensors,
173  motor* y_, int number_motors);
174 
175  /// calculates xsi for the current time step using the delayed y values
176  // and x delayed by one
177  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
178  virtual void calcXsi(int delay);
179 
180  /// learn H,C with motors y and corresponding sensors x
181  virtual void learnController();
182 
183  /// learn A, (and S) using motors y and corresponding sensors x
184  // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
185  virtual void learnModel(int delay);
186 
187  /// calculates the predicted sensor values
188  virtual matrix::Matrix model(const matrix::Matrix* x_buffer, int delay, const matrix::Matrix& y);
189 
190  /// handles inhibition damping etc.
191  virtual void management();
192 
193  /// returns controller output for given sensor values
195 
196 protected:
197  static double regularizedInverse(double v);
198 
199 
200 };
201 
202 #endif
Matrix type.
Definition: matrix.h:65
matrix::Matrix initialC
initialC initial controller matrix (if null matrix then automatic, see cInit)
Definition: semox.h:32
bool modelExt
modelExt if true then additional matrix S is used in forward model (sees sensors) ...
Definition: semox.h:37
paramval dampController
damping of C matrix
Definition: semox.h:160
bool parambool
Definition: configurable.h:93
Interface for teachable controller.
Definition: teachable.h:32
unsigned short number_motors
Definition: semox.h:130
matrix::Matrix B
Model Bias.
Definition: semox.h:136
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: semox.cpp:143
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: semox.cpp:126
double sInit
sInit initial size of the diagonals of the S matrix
Definition: semox.h:36
double xsi_norm
norm of matrix
Definition: semox.h:145
int managementInterval
interval between subsequent management function calls
Definition: semox.h:167
Abstract class (interface) for robot controller that use are based on the homeokinetic prinziple...
Definition: homeokinbase.h:36
double sensor
Definition: types.h:29
parambool _modelExt_copy
copy of modelExtension variable (to achieve readonly)
Definition: semox.h:168
double aInit
aInit initial size of the diagonals of the A matrix
Definition: semox.h:35
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
SeMoX(const SeMoXConf &conf=getDefaultConf())
Definition: semox.cpp:25
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: semox.h:82
virtual void calcXsi(int delay)
calculates xsi for the current time step using the delayed y values
Definition: semox.cpp:181
virtual int setParameters(const std::list< matrix::Matrix > &params) override
sets the parameters.
Definition: semox.cpp:333
virtual std::list< matrix::Matrix > getParameters() const override
Returns a list of matrices that parametrize the controller.
Definition: semox.cpp:329
double cNonDiag
cNonDiag initial size of the non-diagonal elements of the C matrix (if C is not given) ...
Definition: semox.h:34
SeMoXConf conf
Definition: semox.h:162
int buffersize
buffersize size of the time-buffer for x,y,eta
Definition: semox.h:31
matrix::Matrix H
Controller Bias.
Definition: semox.h:135
static SeMoXConf getDefaultConf()
returns the default configuration
Definition: semox.h:63
paramval modelNoise
strength of noisy bias
Definition: semox.h:143
int t_rand
initial random time to avoid syncronous management of all controllers
Definition: semox.h:166
double cInit
cInit initial size of the diagonals of the C matrix (if C is not given)
Definition: semox.h:33
This controller follows the prinziple of homeokinesis and implements the extensions described in the ...
Definition: semox.h:57
matrix::Matrix * x_buffer
Definition: semox.h:149
matrix::Matrix S
additional Model Matrix (sensors derivatives to sensors)
Definition: semox.h:133
matrix::Matrix xsi
current output error
Definition: semox.h:140
virtual matrix::Matrix getLastMotorValues()
returns the last motor values (useful for cross motor coupling)
Definition: semox.cpp:408
paramval gamma_cont
parameter to include contiuity in motor values (avoid high frequencies)
Definition: semox.h:155
double sensor
Definition: abstractcontroller.h:48
matrix::Matrix y_teaching
motor teaching signal
Definition: semox.h:153
double paramval
Definition: configurable.h:88
bool intern_useTeaching
flag whether there is an actual teachning signal or not
Definition: semox.h:165
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: semox.cpp:350
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: semox.h:84
virtual void setMotorTeaching(const matrix::Matrix &teaching)
The given motor teaching signal is used for this timestep.
Definition: semox.cpp:392
NoiseGenerator * BNoiseGen
Noisegenerator for noisy bias.
Definition: semox.h:142
matrix::Matrix SmallID
small identity matrix in the dimension of R
Definition: semox.h:138
virtual void setSensorTeaching(const matrix::Matrix &teaching)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: semox.cpp:401
matrix::Matrix * y_buffer
Definition: semox.h:151
virtual matrix::Matrix calculateControllerValues(const matrix::Matrix &x_smooth)
returns controller output for given sensor values
Definition: semox.cpp:313
paramval dampModel
damping of A and S matrices
Definition: semox.h:159
virtual matrix::Matrix getLastSensorValues()
returns the last sensor values (useful for cross sensor coupling)
Definition: semox.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: semox.cpp:151
matrix::Matrix v
shift
Definition: semox.h:139
matrix::Matrix A
Model Matrix (motors to sensors)
Definition: semox.h:132
paramval discountS
discount strength for hierachical model
Definition: semox.h:157
virtual void management()
handles inhibition damping etc.
Definition: semox.cpp:318
double motor
Definition: types.h:30
paramval gamma_teach
strength of teaching
Definition: semox.h:156
matrix::Matrix C
Controller Matrix.
Definition: semox.h:134
matrix::Matrix * x_c_buffer
buffer for sensors with context sensors
Definition: semox.h:150
matrix::Matrix R
C*A.
Definition: semox.h:137
virtual ~SeMoX()
Definition: semox.cpp:60
using ParameterList = std::list<matrix::Matrix>;
Definition: parametrizable.h:39
int numContext
number of context sensors (considered at the end of the sensor vector, which are only feed to the mod...
Definition: semox.h:40
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: semox.cpp:361
Just create your own simulation, it's up to you.
Definition: ode_robots/examples/basic/main.cpp:46
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: semox.cpp:69
virtual std::list< ILayer > getStructuralLayers() const
Specifies which parameter vector forms a structural layer (in terms of a neural network) The ordering...
Definition: semox.cpp:373
bool someInternalParams
someInternalParams if true only some internal parameters are exported
Definition: semox.h:41
virtual void learnController()
learn H,C with motors y and corresponding sensors x
Definition: semox.cpp:203
double motor
Definition: abstractcontroller.h:49
Definition: semox.h:30
virtual std::list< IConnection > getStructuralConnections() const
Specifies which parameter matrix forms a connection between layers (in terms of a neural network) The...
Definition: semox.cpp:381
static double regularizedInverse(double v)
virtual void learnModel(int delay)
learn A, (and S) using motors y and corresponding sensors x
Definition: semox.cpp:279
int c
Definition: hexapod.cpp:56
virtual matrix::Matrix model(const matrix::Matrix *x_buffer, int delay, const matrix::Matrix &y)
calculates the predicted sensor values
Definition: semox.cpp:193
double pain
if the modelling error (xsi) is too high we have a pain signal
Definition: semox.h:147
struct SeMoXConf SeMoXConf
unsigned short number_sensors
Definition: semox.h:129
Interface and basic class for noise generator.
Definition: noisegenerator.h:37
double xsi_norm_avg
average norm of xsi (used to define whether Modell learns)
Definition: semox.h:146