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
pimax.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2013 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 __PIMAX_H
20 #define __PIMAX_H
21 
22 #include <selforg/abstractcontroller.h>
23 #include <selforg/controller_misc.h>
24 
25 #include <assert.h>
26 #include <cmath>
27 
28 #include <selforg/matrix.h>
29 #include <selforg/teachable.h>
30 #include <selforg/parametrizable.h>
31 
32 
33 /// configuration object for PiMax controller. Use PiMax::getDefaultConf().
34 struct PiMaxConf {
35  double initFeedbackStrength; ///< initial strength of sensor to motor connection
36  bool useExtendedModel; ///< if true, the extended model (S matrix) is used
37  /// if true, the covariance matrix is learned otherwise a unit matrix is used
38  bool useSigma;
39  /// if true the controller can be taught see teachable interface
41  /// # of steps the sensors are averaged (1 means no averaging)
43  /// # of steps the motor values are delayed (1 means no delay)
45  bool someInternalParams; ///< if true only some internal parameters are exported
46  bool onlyMainParameters; ///< if true only some configurable parameters are exported
47 };
48 
49 
50 /**
51  * This controller implements the predictive information maximization
52  described in paper: to be published in PLoS ONE 2013
53  ArXiv preprint: http://arxiv.org/abs/1301.7473
54 
55  Note: the notation is for the model matrices is different than in the paper:
56  A -> V
57  S -> T
58  The code contains more functionality than is described in the paper
59  e.g. the teaching and motor babbling is not used.
60 */
61 class PiMax : public AbstractController, public Teachable, public Parametrizable {
62 
63 public:
64  PiMax(const PiMaxConf& conf = getDefaultConf());
65 
66  virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
67 
68  virtual ~PiMax();
69 
72  conf.initFeedbackStrength = 1.0;
73  conf.useExtendedModel = false;
74  conf.useSigma = true;
75  conf.useTeaching = false;
76  conf.steps4Averaging = 1;
77  conf.steps4Delay = 1;
78  conf.someInternalParams = false;
79  conf.onlyMainParameters = true;
80  return conf;
81  }
82 
83  /// returns the number of sensors the controller was initialised with or 0 if not initialised
84  virtual int getSensorNumber() const { return number_sensors; }
85  /// returns the mumber of motors the controller was initialised with or 0 if not initialised
86  virtual int getMotorNumber() const { return number_motors; }
87 
88  /// performs one step (includes learning).
89  /// Calulates motor commands from sensor inputs.
90  virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
91 
92 
93  /// performs one step without learning. Calulates motor commands from sensor inputs.
94  virtual void stepNoLearning(const sensor* , int number_sensors,
95  motor* , int number_motors);
96 
97  /// called during babbling phase
98  virtual void motorBabblingStep(const sensor* , int number_sensors,
99  const motor* , int number_motors);
100 
101  /***** STOREABLE ****/
102  /** stores the controller values to a given file. */
103  virtual bool store(FILE* f) const;
104  /** loads the controller values from a given file. */
105  virtual bool restore(FILE* f);
106 
107  /* some direct access functions (unsafe!) */
108  virtual matrix::Matrix getA();
109  virtual void setA(const matrix::Matrix& A);
110  virtual matrix::Matrix getC();
111  virtual void setC(const matrix::Matrix& C);
112  virtual matrix::Matrix geth();
113  virtual void seth(const matrix::Matrix& h);
114 
115  /***** TEACHABLE ****/
116  virtual void setMotorTeaching(const matrix::Matrix& teaching);
117  virtual void setSensorTeaching(const matrix::Matrix& teaching);
120 
121  /***** PARAMETRIZABLE ****/
122  virtual std::list<matrix::Matrix> getParameters() const override;
123  virtual int setParameters(const std::list<matrix::Matrix>& params) override;
124 
125 protected:
126  unsigned short number_sensors;
127  unsigned short number_motors;
128  static const unsigned short buffersize = 20;
129 
130  matrix::Matrix A; // Model Matrix
131  matrix::Matrix C; // Controller Matrix
132  matrix::Matrix S; // Model Matrix (sensor branch)
133  matrix::Matrix h; // Controller Bias
134  matrix::Matrix b; // Model Bias
135  matrix::Matrix L; // Jacobi Matrix
136 
137  matrix::Matrix Sigma; // noise covariance matrix
138 
140  matrix::Matrix C_native; // Controller Matrix obtained from motor babbling
141  matrix::Matrix A_native; // Model Matrix obtained from motor babbling
142 
143  matrix::Matrix a_buffer[buffersize]; // buffer needed for delay
144  matrix::Matrix s_buffer[buffersize]; // buffer of sensor values
145  matrix::Matrix xi_buffer[buffersize]; // buffer of pred errors
147  matrix::Matrix L_buffer[buffersize]; // buffer of Jacobians
148 
149  matrix::Matrix s; // current sensor value vector
150  matrix::Matrix s_smooth; // time average of s values
151  PiMaxConf conf; ///< configuration objects
152 
153  int t;
154 
155 
156  bool intern_isTeaching; // teaching signal available?
157  matrix::Matrix a_teaching; // motor teaching signal
158 
159  bool useMetric;
167  paramval gamma; // teaching strength
168 
169  paramint tau; // length of time window
170 
171 
172  /// learn values model and controller (A,b,C,h)
173  virtual void learn();
174 
175  /// neuron transfer function
176  static double g(double z)
177  {
178  return tanh(z);
179  };
180 
181  /// derivative of g
182  static double g_s(double z)
183  {
184  double k=tanh(z);
185  return 1.05 - k*k; // regularized
186  };
187 
188  /// function that clips the second argument to the interval [-first,first]
189  static double clip(double r, double x){
190  return min(max(x,-r),r);
191  }
192  /// calculates the inverse the argument (useful for Matrix::map)
193  static double one_over(double x){
194  return 1/x;
195  }
196 
197 
198 };
199 
200 #endif
201 
202 
virtual int getMotorNumber() const
returns the mumber of motors the controller was initialised with or 0 if not initialised ...
Definition: pimax.h:86
virtual int getSensorNumber() const
returns the number of sensors the controller was initialised with or 0 if not initialised ...
Definition: pimax.h:84
Matrix type.
Definition: matrix.h:65
matrix::Matrix ds0
Definition: pimax.h:139
matrix::Matrix L_buffer[buffersize]
Definition: pimax.h:147
paramval epsSigma
Definition: pimax.h:164
Interface for teachable controller.
Definition: teachable.h:32
bool someInternalParams
if true only some internal parameters are exported
Definition: pimax.h:45
PiMaxConf conf
configuration objects
Definition: pimax.h:151
virtual void motorBabblingStep(const sensor *, int number_sensors, const motor *, int number_motors)
called during babbling phase
Definition: pimax.cpp:197
Abstract class for robot controller (with some basic functionality).
Definition: abstractcontroller.h:46
virtual void setSensorTeaching(const matrix::Matrix &teaching)
The given sensor teaching signal (distal learning) is used for this timestep.
Definition: pimax.cpp:329
virtual void learn()
learn values model and controller (A,b,C,h)
Definition: pimax.cpp:232
matrix::Matrix b
Definition: pimax.h:134
bool useExtendedModel
if true, the extended model (S matrix) is used
Definition: pimax.h:36
matrix::Matrix C_native
Definition: pimax.h:140
double sensor
Definition: types.h:29
matrix::Matrix C
Definition: pimax.h:131
matrix::Matrix a_buffer[buffersize]
Definition: pimax.h:143
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
configuration object for PiMax controller. Use PiMax::getDefaultConf().
Definition: pimax.h:34
bool onlyMainParameters
if true only some configurable parameters are exported
Definition: pimax.h:46
static double g(double z)
neuron transfer function
Definition: pimax.h:176
bool intern_isTeaching
Definition: pimax.h:156
virtual bool restore(FILE *f)
loads the controller values from a given file.
Definition: pimax.cpp:379
static const unsigned short buffersize
Definition: pimax.h:128
virtual void step(const sensor *, int number_sensors, motor *, int number_motors)
performs one step (includes learning).
Definition: pimax.cpp:151
matrix::Matrix S
Definition: pimax.h:132
matrix::Matrix A
Definition: pimax.h:130
static double one_over(double x)
calculates the inverse the argument (useful for Matrix::map)
Definition: pimax.h:193
matrix::Matrix h
Definition: pimax.h:133
double paramval
Definition: configurable.h:88
bool useMetric
Definition: pimax.h:159
paramval damping
Definition: pimax.h:166
virtual void setC(const matrix::Matrix &C)
Definition: pimax.cpp:136
paramval epsA
Definition: pimax.h:163
int steps4Delay
of steps the motor values are delayed (1 means no delay)
Definition: pimax.h:44
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
matrix::Matrix a_teaching
Definition: pimax.h:157
virtual ~PiMax()
Definition: pimax.cpp:72
virtual void stepNoLearning(const sensor *, int number_sensors, motor *, int number_motors)
performs one step without learning. Calulates motor commands from sensor inputs.
Definition: pimax.cpp:167
bool useTeaching
if true the controller can be taught see teachable interface
Definition: pimax.h:40
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: pimax.cpp:76
virtual void setA(const matrix::Matrix &A)
Definition: pimax.cpp:127
matrix::Matrix s_buffer[buffersize]
Definition: pimax.h:144
This controller implements the predictive information maximization described in paper: to be publishe...
Definition: pimax.h:61
virtual void setMotorTeaching(const matrix::Matrix &teaching)
The given motor teaching signal is used for this timestep.
Definition: pimax.cpp:320
matrix::Matrix gs_buffer[buffersize]
Definition: pimax.h:146
virtual matrix::Matrix getC()
Definition: pimax.cpp:132
paramval sense
Definition: pimax.h:161
unsigned short number_motors
Definition: pimax.h:127
static PiMaxConf getDefaultConf()
Definition: pimax.h:70
int paramint
Definition: configurable.h:98
matrix::Matrix xi_buffer[buffersize]
Definition: pimax.h:145
double motor
Definition: types.h:30
matrix::Matrix s_smooth
Definition: pimax.h:150
using ParameterList = std::list<matrix::Matrix>;
Definition: parametrizable.h:39
int t
Definition: pimax.h:153
PiMax(const PiMaxConf &conf=getDefaultConf())
Definition: pimax.cpp:24
virtual void seth(const matrix::Matrix &h)
Definition: pimax.cpp:145
paramval gamma
Definition: pimax.h:167
unsigned short number_sensors
Definition: pimax.h:126
paramval epsC
Definition: pimax.h:162
virtual matrix::Matrix geth()
Definition: pimax.cpp:141
bool useSigma
if true, the covariance matrix is learned otherwise a unit matrix is used
Definition: pimax.h:38
matrix::Matrix L
Definition: pimax.h:135
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
paramval factorH
Definition: pimax.h:165
matrix::Matrix A_native
Definition: pimax.h:141
paramint tau
Definition: pimax.h:169
static double g_s(double z)
derivative of g
Definition: pimax.h:182
virtual bool store(FILE *f) const
stores the controller values to a given file.
Definition: pimax.cpp:367
int steps4Averaging
of steps the sensors are averaged (1 means no averaging)
Definition: pimax.h:42
virtual int setParameters(const std::list< matrix::Matrix > &params) override
sets the parameters.
Definition: pimax.cpp:349
virtual matrix::Matrix getLastSensorValues()
returns the last sensor values (useful for cross sensor coupling)
Definition: pimax.cpp:341
static double clip(double r, double x)
function that clips the second argument to the interval [-first,first]
Definition: pimax.h:189
virtual std::list< matrix::Matrix > getParameters() const override
Returns a list of matrices that parametrize the controller.
Definition: pimax.cpp:345
paramval causeaware
Definition: pimax.h:160
virtual matrix::Matrix getA()
Definition: pimax.cpp:123
matrix::Matrix Sigma
Definition: pimax.h:137
matrix::Matrix s
Definition: pimax.h:149
double initFeedbackStrength
initial strength of sensor to motor connection
Definition: pimax.h:35
virtual matrix::Matrix getLastMotorValues()
returns the last motor values (useful for cross motor coupling)
Definition: pimax.cpp:337