soml.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  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
00005  *                                                                         *
00006  *   ANY COMMERCIAL USE FORBIDDEN!                                         *
00007  *   LICENSE:                                                              *
00008  *   This work is licensed under the Creative Commons                      *
00009  *   Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of   *
00010  *   this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
00011  *   or send a letter to Creative Commons, 543 Howard Street, 5th Floor,   *
00012  *   San Francisco, California, 94105, USA.                                *
00013  *                                                                         *
00014  *   This program is distributed in the hope that it will be useful,       *
00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
00017  *                                                                         *
00018  ***************************************************************************/
00019 #ifndef __SOML_H
00020 #define __SOML_H
00021 
00022 #include <selforg/abstractcontroller.h>
00023 #include <selforg/controller_misc.h>
00024 #include <selforg/controllernet.h>
00025 
00026 #include <assert.h>
00027 #include <cmath>
00028 
00029 #include <selforg/matrix.h>
00030 
00031 /// Configuration object for SoML controller
00032 struct SoMLConf {
00033   bool useHiddenContr; ///< use a hidden layer in the controller network?
00034   /// ratio of motor units and hidden units in the controller (2 -> double amount of hidden unit)
00035   double hiddenContrUnitsRatio; 
00036   bool useHiddenModel; ///< use a hiddenlayer in the model network?
00037   /// ratio of motor units and hidden units in the model (2 -> double amount of hidden unit) 
00038   double hiddenModelUnitsRatio;
00039   bool useS;         ///< direct connection from x_t to xp_t+1
00040   bool initUnitMatrix; /// if true then the network is initialized with unit matrices
00041 
00042   bool someInternalParams; ///< only export some internal parameters
00043 };
00044 
00045 /**
00046  * This controller implements the homeokinetic learning algorihm
00047  * in sensor space with extended controller network
00048  */
00049 class SoML : public AbstractController {
00050 
00051 public:
00052   SoML(const SoMLConf& conf = getDefaultConf());
00053 
00054   static SoMLConf getDefaultConf(){
00055     SoMLConf c;
00056     c.useHiddenContr = true;
00057     c.useHiddenModel = true;
00058     c.hiddenContrUnitsRatio =  1.0;
00059     c.hiddenModelUnitsRatio = 1.0;
00060     c.someInternalParams=false;
00061     c.useS = false;
00062     c.initUnitMatrix = true;
00063     return c;
00064   }
00065   
00066   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00067 
00068   virtual ~SoML();
00069 
00070   /// returns the number of sensors the controller was initialised with or 0 if not initialised
00071   virtual int getSensorNumber() const { return number_sensors; }
00072   /// returns the mumber of motors the controller was initialised with or 0 if not initialised
00073   virtual int getMotorNumber() const  { return number_motors; }
00074 
00075   /// performs one step (includes learning). 
00076   /// Calulates motor commands from sensor inputs.
00077   virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
00078 
00079   /// performs one step without learning. Calulates motor commands from sensor inputs.
00080   virtual void stepNoLearning(const sensor* , int number_sensors, 
00081                               motor* , int number_motors);
00082   
00083   // motor babbling: learn the basic relations from observed sensors/motors 
00084   virtual void motorBabblingStep(const sensor* , int number_sensors,
00085                                  const motor* , int number_motors);
00086 
00087 
00088   /***** STOREABLE ****/
00089   /** stores the controller values to a given file. */
00090   virtual bool store(FILE* f) const;
00091   /** loads the controller values from a given file. */
00092   virtual bool restore(FILE* f);  
00093 
00094   /// returns controller network (to be added to inspectables of agent)
00095   virtual ControllerNet* getCNet();
00096 
00097 protected:
00098   /// performs control step (activates network and stores results in buffer and y_)
00099   void control(const matrix::Matrix& x, motor* y_, int number_motors);
00100 
00101   /** learn values model and controller network
00102       using the current sensors x, the commands y (from last step (or earlier in case of delay))
00103    */
00104   virtual void learn(const matrix::Matrix& x, const matrix::Matrix& y);
00105 
00106   /* learns the model using backprop. It uses the current activation, 
00107      the current x and x_tm1 from the buffer */
00108   virtual void learnModelBP(double factor);
00109 
00110 protected:
00111   unsigned short number_sensors;
00112   unsigned short number_motors;
00113   static const unsigned short buffersize = 10;
00114 
00115   matrix::Matrix y_buffer[buffersize]; ///< buffer needed for delay
00116   matrix::Matrix x_buffer[buffersize]; ///< buffer needed for delay
00117   ControllerNet* cNet; ///< Controller network
00118   unsigned int numControllerLayer; ///< number of controller layer
00119 
00120   SoMLConf conf; ///< configuration object
00121 
00122   matrix::Matrix x;        // current sensor value vector
00123   matrix::Matrix x_smooth; // time average of x values
00124   matrix::Matrix eta_avg;    // time average of shift (in motor space)
00125   int t;
00126   double E;
00127 
00128   paramval creativity;
00129   paramval epsC;
00130   paramval epsA;
00131   paramval harmony;        ///< harmony
00132   paramval dampA;
00133   paramval discountS;      ///< discount for S part of the model
00134   paramint s4avg;          ///< # of steps the sensors are averaged (1 means no averaging)
00135   paramint s4delay;        ///< # of steps the motor values are delayed (1 means no delay)
00136   paramval biasnoise;        
00137   parambool loga;          ///< # use logarithmic error
00138 
00139 
00140   
00141 };
00142 
00143 #endif
00144 
00145 
Generated on Thu Jun 28 14:45:37 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3