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
mutualinformationcontroller.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 __MUTUALINFORMATIONCONTROLLER_H
25 #define __MUTUALINFORMATIONCONTROLLER_H
26 
27 #include "abstractcontroller.h"
28 #include "matrix.h"
29 
30 /**
31  * This is a controller who is passive at the moment, that means, he will not
32  * generate any motor values. This controller calculates the mutual information
33  * from one to the next time step. Note that many steps are necessary for a good
34  * prediction of the mutual information.
35  */
37 {
38 
39 public:
40 
41  static const int numberSensorsPreInitialized = 10;
42  static const int numberMotorsPreInitialized = 2;
43 
44  /**
45  * Constructs the mutual information controller. At this time the
46  * controller is not yet initialized (this does the Agent).
47  * @param sensorIntervalCount is the number of the intervals used. This
48  * is important for generating the internal matrices.
49  * @param minSensorValue is the minimum value the sensors can become
50  * @param maxSensorValue is the maximum value the sensors can become
51  *
52  */
53  MutualInformationController(int sensorIntervalCount, double minSensorValue=-1, double maxSensorValue=1, bool showF =false, bool showP =false, bool showXsiF = false);
55 
56  virtual double& getMI(int index) {
57  if (!initialized)
59  assert(index<sensorNumber);
60  return MI[index];
61  }
62 
63  virtual double& getH_x(int index) {
64  if (!initialized)
66  assert(index<sensorNumber);
67  return H_x[index];
68  }
69 
70  virtual double& getH_yx(int index) {
71  if (!initialized)
73  assert(index<sensorNumber);
74  return H_yx[index];
75  }
76 
77  virtual double& getH_Xsi(int index) {
78  if (!initialized)
80  assert(index<sensorNumber);
81  return H_Xsi[index];
82  }
83 
84  /**
85  * we like to calculate the entropy of the xsi, therefore we need for the (self calculated) update rule
86  * x_t+1=-a * tanh(c * x_t) the a and c, which should be set in the main.cpp.
87  */
88  virtual void setAandCandCalcH_xsi(double ainit, double cinit);
89 
90 
91  /****************************************************************************/
92  /* BEGIN methods of AbstractController */
93  /****************************************************************************/
94 
95  /** initialisation of the controller with the given sensor/ motornumber
96  * Must NORMALLY be called before use. For all ControllerAdapters
97  * call first AbstractControllerAdapter::init(sensornumber,motornumber)
98  * if you overwrite this method
99  */
100  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
101 
102  /** @return Number of sensors the controller
103  was initialised with or 0 if not initialised */
104  virtual int getSensorNumber() const { return sensorNumber; }
105 
106  /** @return Number of motors the controller
107  was initialised with or 0 if not initialised */
108  virtual int getMotorNumber() const { return motorNumber; }
109 
110  /** performs one step (includes learning).
111  Calculates motor commands from sensor inputs.
112  @param sensors sensors inputs scaled to [-1,1]
113  @param sensornumber length of the sensor array
114  @param motors motors outputs. MUST have enough space for motor values!
115  @param motornumber length of the provided motor array
116  */
117  virtual void step(const sensor* sensors, int sensornumber,
118  motor* motors, int motornumber);
119 
120  /** performs one step without learning.
121  @see step
122  */
123  virtual void stepNoLearning(const sensor* sensors , int sensornumber,
124  motor* motors, int motornumber);
125 
126  /****************************************************************************/
127  /* END methods of AbstractController */
128  /****************************************************************************/
129 
130 
131  /****************************************************************************/
132  /* BEGIN methods of Inspectable */
133  /****************************************************************************/
134 
135  /** The list of the names of all internal parameters given by getInternalParams().
136  The naming convention is "v[i]" for vectors
137  and "A[i][j]" for matrices, where i, j start at 0.
138  @return: list of keys
139  */
140 // virtual iparamkeylist getInternalParamNames() const;
141 
142  /** @return: list of values
143  */
144 // virtual iparamvallist getInternalParams() const;
145 
146  /****************************************************************************/
147  /* END methods of Inspectable */
148  /****************************************************************************/
149 
150  /****************************************************************************/
151  /* BEGIN methods of Storeable */
152  /****************************************************************************/
153 
154  /** stores the object to the given file stream (binary).
155  */
156  virtual bool store(FILE* f) const { return true; }
157 
158  /** loads the object from the given file stream (binary).
159  */
160  virtual bool restore(FILE* f) { return true; }
161 
162  /****************************************************************************/
163  /* END methods of Storeable */
164  /****************************************************************************/
165 
166 
167  /****************************************************************************/
168  /* BEGIN methods of Configurable */
169  /****************************************************************************/
170 
171 // Configurable::paramval getParam(const paramkey& key) const;
172 
173 // bool setParam(const paramkey& key, paramval val);
174 
175 // Configurable::paramlist getParamList() const;
176 
177  /****************************************************************************/
178  /* END methods of Configurable */
179  /****************************************************************************/
180 
181 
182 protected:
186  bool initialized; // tells wether the controller is already initialized by the agent
188 
194  std::list<matrix::Matrix*> freqMatrixList; // stores the number of occurances of t-1 to t (frequency)
195  std::list<matrix::Matrix*> probMatrixList; // stores the probability of state to state occurances of t-1 to t
196  std::list<matrix::Matrix*> xsiFreqMatrixList; // stores the number of occurances of xsi(x) (frequency)
197 
198  double* oldSensorStates; // stores the sensor states for previous step (t-1)
199  int t; // indicates the step (time)
200 
201  double* MI; // mutual information = MI(X_{t-1},X_t)
202  double* H_x; // H(x) = H(X_{t-1})
203  double* H_yx; // H(X_t|X_{t-1})
204  double* H_Xsi; // H(Xsi)
205 
206  // the next two variables are only used if Xsi_x is calculated
207  double ainit;
208  double cinit;
209 
210  /**
211  * Calculates the mutual information
212  * This is made by normal formula, which
213  * needs O(n^2) costs.
214  */
215  virtual void calculateMIs(double* MI);
216 
217  /**
218  * Calculates the entropy of x
219  * This is made by normal formula, which
220  * needs O(n) costs.
221  */
222  virtual void calculateH_x(double* H);
223 
224 
225  /**
226  * Calculates the conditional entropy of y|x
227  * This is made by normal formula, which
228  * needs O(n²) costs.
229  */
230  virtual void calculateH_yx(double* H_yx);
231 
232 
233  /**
234  * Updates the xsi frequency matrix list
235  */
236  virtual void updateXsiFreqMatrixList(const sensor* sensors);
237 
238  /**
239  * Calculates the entropy of H(Xsi)
240  * This is made by normal formula, which
241  * needs O(n) costs.
242  */
243  virtual void calculateH_Xsi(double* H_Xsi);
244 
245 
246 
247  /**
248  * Updates the mutual information
249  * This is made by a difference term, which
250  * is added to the old MI(t-1).
251  * This function needs the OLD MI(t-1) AND
252  * the OLD F matrix, so update the MI with this
253  * function first before updating the F matrix!
254  * calculation costs: O(1)
255  */
256  virtual void updateMIs(const sensor* sensors);
257 
258 
259 
260  /**
261  * Returns the appropiate state which belongs to the given sensorValue.
262  */
263  virtual int getState(double sensorValue);
264 
265 
266  /**
267  * Does the pre-initialization functionality.
268  * @param sensornumber
269  * @param motornumber
270  * @param randGen
271  */
272  virtual void internalInit(int sensornumber, int motornumber, RandGen* randGen = 0);
273 
274 
275 };
276 
277 #endif
std::list< matrix::Matrix * > xsiFreqMatrixList
Definition: mutualinformationcontroller.h:196
virtual void stepNoLearning(const sensor *sensors, int sensornumber, motor *motors, int motornumber)
performs one step without learning.
Definition: mutualinformationcontroller.cpp:207
std::list< matrix::Matrix * > probMatrixList
Definition: mutualinformationcontroller.h:195
bool parambool
Definition: configurable.h:93
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
int sensorNumber
Definition: mutualinformationcontroller.h:192
double ainit
Definition: mutualinformationcontroller.h:207
int motorNumber
Definition: mutualinformationcontroller.h:193
virtual void init(int sensornumber, int motornumber, RandGen *randGen=0)
initialisation of the controller with the given sensor/ motornumber Must NORMALLY be called before us...
Definition: mutualinformationcontroller.cpp:55
virtual void updateXsiFreqMatrixList(const sensor *sensors)
Updates the xsi frequency matrix list.
Definition: mutualinformationcontroller.cpp:272
static const int numberSensorsPreInitialized
Definition: mutualinformationcontroller.h:41
double * H_yx
Definition: mutualinformationcontroller.h:203
virtual void calculateH_x(double *H)
Calculates the entropy of x This is made by normal formula, which needs O(n) costs.
Definition: mutualinformationcontroller.cpp:309
virtual double & getH_yx(int index)
Definition: mutualinformationcontroller.h:70
double sensor
Definition: types.h:29
static const int numberMotorsPreInitialized
Definition: mutualinformationcontroller.h:42
double maxSensorValue
Definition: mutualinformationcontroller.h:190
This is a controller who is passive at the moment, that means, he will not generate any motor values...
Definition: mutualinformationcontroller.h:36
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual void calculateH_Xsi(double *H_Xsi)
Calculates the entropy of H(Xsi) This is made by normal formula, which needs O(n) costs...
Definition: mutualinformationcontroller.cpp:295
parambool showF
Definition: mutualinformationcontroller.h:183
virtual double & getMI(int index)
Definition: mutualinformationcontroller.h:56
int t
Definition: mutualinformationcontroller.h:199
virtual double & getH_Xsi(int index)
Definition: mutualinformationcontroller.h:77
virtual void calculateMIs(double *MI)
Calculates the mutual information This is made by normal formula, which needs O(n^2) costs...
Definition: mutualinformationcontroller.cpp:341
double cinit
Definition: mutualinformationcontroller.h:208
double sensor
Definition: abstractcontroller.h:48
virtual bool store(FILE *f) const
The list of the names of all internal parameters given by getInternalParams().
Definition: mutualinformationcontroller.h:156
double * MI
Definition: mutualinformationcontroller.h:201
MutualInformationController(int sensorIntervalCount, double minSensorValue=-1, double maxSensorValue=1, bool showF=false, bool showP=false, bool showXsiF=false)
Constructs the mutual information controller.
Definition: mutualinformationcontroller.cpp:33
virtual int getState(double sensorValue)
Returns the appropiate state which belongs to the given sensorValue.
Definition: mutualinformationcontroller.cpp:212
std::list< matrix::Matrix * > freqMatrixList
Definition: mutualinformationcontroller.h:194
double * H_x
Definition: mutualinformationcontroller.h:202
virtual bool restore(FILE *f)
loads the object from the given file stream (binary).
Definition: mutualinformationcontroller.h:160
int sensorIntervalCount
Definition: mutualinformationcontroller.h:191
double * oldSensorStates
Definition: mutualinformationcontroller.h:198
double motor
Definition: types.h:30
virtual void internalInit(int sensornumber, int motornumber, RandGen *randGen=0)
Does the pre-initialization functionality.
Definition: mutualinformationcontroller.cpp:85
virtual int getSensorNumber() const
Definition: mutualinformationcontroller.h:104
parambool showXsiF
Definition: mutualinformationcontroller.h:185
double * H_Xsi
Definition: mutualinformationcontroller.h:204
virtual void calculateH_yx(double *H_yx)
Calculates the conditional entropy of y|x This is made by normal formula, which needs O(n²) costs...
Definition: mutualinformationcontroller.cpp:324
virtual void step(const sensor *sensors, int sensornumber, motor *motors, int motornumber)
performs one step (includes learning).
Definition: mutualinformationcontroller.cpp:152
virtual int getMotorNumber() const
Definition: mutualinformationcontroller.h:108
virtual ~MutualInformationController()
Definition: mutualinformationcontroller.h:54
bool initialized
Definition: mutualinformationcontroller.h:186
virtual double & getH_x(int index)
Definition: mutualinformationcontroller.h:63
bool useXsiCalculation
Definition: mutualinformationcontroller.h:187
double minSensorValue
Definition: mutualinformationcontroller.h:189
parambool showP
Definition: mutualinformationcontroller.h:184
virtual void setAandCandCalcH_xsi(double ainit, double cinit)
we like to calculate the entropy of the xsi, therefore we need for the (self calculated) update rule ...
Definition: mutualinformationcontroller.cpp:138
virtual void updateMIs(const sensor *sensors)
Updates the mutual information This is made by a difference term, which is added to the old MI(t-1)...
Definition: mutualinformationcontroller.cpp:222