invertmotorbigmodel.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by                                            *
00003  *    Georg Martius  <georg dot martius at web dot de>                     *
00004  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00005  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
00006  *                                                                         *
00007  *   ANY COMMERCIAL USE FORBIDDEN!                                         *
00008  *   LICENSE:                                                              *
00009  *   This work is licensed under the Creative Commons                      *
00010  *   Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of   *
00011  *   this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
00012  *   or send a letter to Creative Commons, 543 Howard Street, 5th Floor,   *
00013  *   San Francisco, California, 94105, USA.                                *
00014  *                                                                         *
00015  *   This program is distributed in the hope that it will be useful,       *
00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
00018  *                                                                         *
00019  ***************************************************************************/
00020 #ifndef __INVERTMOTORBIGMODEL_H
00021 #define __INVERTMOTORBIGMODEL_H
00022 
00023 #include "invertmotorcontroller.h"
00024 
00025 #include <assert.h>
00026 #include <cmath>
00027 
00028 #include "matrix.h"
00029 #include "noisegenerator.h"
00030 #include "invertablemodel.h"
00031 
00032 typedef struct InvertMotorBigModelConf {
00033   int buffersize;  ///< buffersize size of the time-buffer for x,y,eta
00034   double cInit;    ///< cInit size of the C matrix to initialised with.
00035   double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones
00036   bool modelInit;  ///< size of the unit-map strenght of the model 
00037   bool useS;    ///< useS decides whether to use the S matrix in addition to the A matrix
00038   bool someInternalParams;  ///< someInternalParams if true only some internal parameters are exported, otherwise all 
00039   
00040   double modelCompliant; ///< learning factor for model (or sensor) compliant learning
00041   
00042   InvertableModel* model;   ///< model used as world model
00043 } InvertMotorBigModelConf;
00044 
00045 /**
00046  * class for robot controller is based on InvertMotorNStep
00047  *  
00048  * - direct inversion 
00049  *
00050  * - motor space
00051  *
00052  * - multilayer,nonlinear model
00053  */
00054 class InvertMotorBigModel : public InvertMotorController {
00055 
00056 public:
00057   InvertMotorBigModel(const InvertMotorBigModelConf& conf = getDefaultConf());
00058   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00059 
00060   virtual ~InvertMotorBigModel();
00061 
00062   /// returns the number of sensors the controller was initialised with or 0 if not initialised
00063   virtual int getSensorNumber() const { return number_sensors; }
00064   /// returns the mumber of motors the controller was initialised with or 0 if not initialised
00065   virtual int getMotorNumber() const  { return number_motors; }
00066 
00067   /// performs one step (includes learning). 
00068   /// Calulates motor commands from sensor inputs.
00069   virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
00070 
00071   /// performs one step without learning. Calulates motor commands from sensor inputs.
00072   virtual void stepNoLearning(const sensor* , int number_sensors, 
00073                               motor* , int number_motors);
00074 
00075 
00076   /**************  STOREABLE **********************************/
00077   /** stores the controller values to a given file. */
00078   virtual bool store(FILE* f) const;
00079   /** loads the controller values from a given file. */
00080   virtual bool restore(FILE* f);  
00081 
00082   /************** INSPECTABLE ********************************/
00083   virtual iparamkeylist getInternalParamNames() const;
00084   virtual iparamvallist getInternalParams() const;  
00085   virtual ilayerlist getStructuralLayers() const;
00086   virtual iconnectionlist getStructuralConnections() const;
00087   
00088   /**** TEACHING ****/
00089   /** The given motor teaching signal is used for this timestep. 
00090       It is used as a feed forward teaching signal for the controller.
00091       Please note, that the teaching signal has to be given each timestep 
00092        for a continuous teaching process.
00093    */
00094   virtual void setMotorTeachingSignal(const motor* teaching, int len);
00095 
00096   /** The given sensor teaching signal (distal learning) is used for this timestep. 
00097       First the belonging motor teachung signal is calculated by the inverse model.
00098       See setMotorTeachingSignal
00099    */
00100   virtual void setSensorTeachingSignal(const sensor* teaching, int len);  
00101 
00102 
00103   static InvertMotorBigModelConf getDefaultConf(){
00104     InvertMotorBigModelConf c;
00105     c.buffersize = 50;
00106     c.cInit = 1.0;
00107     c.cNonDiag = 0;
00108     c.modelInit  = 1.0;
00109     c.someInternalParams = true;
00110     c.useS = false;
00111     c.modelCompliant = 0;
00112     c.model = 0;
00113     return c;
00114   }
00115 
00116   void getLastMotors(motor* motors, int len);
00117 
00118 protected:
00119   unsigned short number_sensors;
00120   unsigned short number_motors;
00121   
00122   matrix::Matrix A; ///< Model Matrix (motors to sensors)
00123   matrix::Matrix S; ///< additional Model Matrix (sensors derivatives to sensors) 
00124   matrix::Matrix C; ///< Controller Matrix
00125   matrix::Matrix H; ///< Controller Bias
00126   NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias
00127   matrix::Matrix R; ///< C*A
00128   matrix::Matrix SmallID; ///< small identity matrix in the dimension of R
00129   matrix::Matrix xsi; ///< current output error
00130   double xsi_norm; ///< norm of matrix
00131   double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns)
00132   double pain;         ///< if the modelling error (xsi) is too high we have a pain signal
00133   matrix::Matrix* x_buffer;
00134   matrix::Matrix* y_buffer;
00135   matrix::Matrix* eta_buffer;
00136   matrix::Matrix zero_eta; // zero initialised eta
00137   matrix::Matrix x_smooth;
00138   //   matrix::Matrix z; ///< membrane potential
00139   matrix::Matrix y_teaching; ///< teaching motor signal
00140   bool useTeaching; ///< flag whether there is an actual teachning signal or not
00141   int t_rand; ///< initial random time to avoid syncronous management of all controllers
00142 
00143 
00144   int managementInterval; ///< interval between subsequent management function calls
00145   paramval inhibition; ///< inhibition strength for sparce kwta strategy (is scaled with epsC)
00146   paramval kwta;       ///< (int) number of synapses that get strengthend
00147   paramval limitRF;    ///< (int) receptive field of motor neurons (number of offcenter sensors) if null then no limitation. Mutual exclusive with inhibition 
00148   paramval dampS;     ///< damping of S matrix
00149 
00150   InvertMotorBigModelConf conf;
00151 
00152   /// puts the sensors in the ringbuffer, generate controller values and put them in the 
00153   //  ringbuffer as well
00154   virtual void fillBuffersAndControl(const sensor* x_, int number_sensors, 
00155                              motor* y_, int number_motors);
00156 
00157   /// calculates the first shift into the motor space useing delayed motor values. 
00158   //  @param delay 0 for no delay and n>0 for n timesteps delay in the time loop
00159   virtual void calcEtaAndBufferIt(int delay);
00160 
00161   /// learn H,C with motors y and corresponding sensors x
00162   virtual void learnController();
00163 
00164   /// calculates the Update for C and H 
00165   // @param y_delay timesteps to delay the y-values.  (usually 0)
00166   //  Please note that the delayed values are NOT used for the error calculation 
00167   //  (this is done in calcXsi())
00168   virtual void calcCandHUpdates(matrix::Matrix& C_update, matrix::Matrix& H_update, int y_delay);
00169 
00170   /// updates the matrix C and H
00171   virtual void updateCandH(const matrix::Matrix& C_update, const matrix::Matrix& H_update, double squashSize);
00172 
00173   /// learn A, (and S) using motors y and corresponding sensors x
00174   //  @param delay 0 for no delay and n>0 for n timesteps delay in the time loop 
00175   virtual void learnModel(int delay);
00176 
00177   /// handles inhibition damping etc.
00178   virtual void management();
00179 
00180   /// returns controller output for given sensor values
00181   virtual matrix::Matrix calculateControllerValues(const matrix::Matrix& x_smooth);   
00182  
00183   /** Calculates first and second derivative and returns both in on matrix (above). 
00184       We use simple discrete approximations:
00185       \f[ f'(x) = (f(x) - f(x-1)) / 2 \f]
00186       \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f]
00187       where we have to go into the past because we do not have f(x+1). The scaling can be neglegted.
00188   */
00189   matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay);
00190 
00191 public:
00192   /** k-winner take all inhibition for synapses. k largest synapses are strengthed and the rest are inhibited.
00193       strong synapes are scaled by 1+(damping/k) and weak synapses are scaled by 1-(damping/(n-k)) where n is the 
00194       number of synapes
00195       @param weightmatrix reference to weight matrix. Synapses for a neuron are in one row. 
00196              The inhibition is done for all rows independently
00197       @param k number of synapes to strengthen
00198       @param damping strength of supression and exitation (typically 0.001)
00199    */
00200   void kwtaInhibition(matrix::Matrix& weightmatrix, unsigned int k, double damping);
00201 
00202   /** sets all connections to zero which are further away then rfSize.
00203       If rfSize == 1 then only main diagonal is left. 
00204       If rfSize = 2: main diagonal and upper and lower side diagonal are kept and so on and so forth.
00205    */
00206   void limitC(matrix::Matrix& weightmatrix, unsigned int rfSize);   
00207  
00208 };
00209 
00210 #endif
Generated on Thu Jun 28 14:45:36 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3