soxexpand.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 __SOXEXPAND_H
00020 #define __SOXEXPAND_H
00021 
00022 #include <selforg/abstractcontroller.h>
00023 #include <selforg/controller_misc.h>
00024 
00025 #include <assert.h>
00026 #include <cmath>
00027 
00028 #include <selforg/matrix.h>
00029 
00030 /**
00031    Configuration object for SoxExpand.
00032    use SoxExpand::getDefaultConf() and then modify as needed before passing it to 
00033    the contructor.
00034  */
00035 struct SoxExpandConf {
00036   double initFeedbackStrength; ///< initial value of diagonals of C
00037   unsigned int numberContextSensors; ///< number of sensors considered as context sensors
00038   matrix::Matrix contextCoupling; ///< coupling of context senors to bias
00039 };
00040 
00041 
00042 /**
00043  * This controller implements the standard algorihm described the Chapter 3 (Homeokinesis)
00044  * with body expansion via context sensors
00045  */
00046 class SoxExpand : public AbstractController {
00047 
00048 public:
00049   SoxExpand(const SoxExpandConf& conf = getDefaultConf());
00050   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00051 
00052   virtual ~SoxExpand();
00053 
00054   static SoxExpandConf getDefaultConf(){
00055     SoxExpandConf c;
00056     c.initFeedbackStrength=1.0;
00057     c.numberContextSensors=0;
00058     return c;
00059   }
00060 
00061   /// returns the number of sensors the controller was initialised with or 0 if not initialised
00062   virtual int getSensorNumber() const { return number_sensors; }
00063   /// returns the mumber of motors the controller was initialised with or 0 if not initialised
00064   virtual int getMotorNumber() const  { return number_motors; }
00065 
00066   /// performs one step (includes learning). 
00067   /// Calulates motor commands from sensor inputs.
00068   virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
00069 
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   /* some direct access functions (unsafe!) */
00083   virtual matrix::Matrix getA();
00084   virtual void setA(const matrix::Matrix& A);
00085   virtual matrix::Matrix getC();
00086   virtual void setC(const matrix::Matrix& C);
00087   virtual matrix::Matrix geth();
00088   virtual void seth(const matrix::Matrix& h);
00089   virtual matrix::Matrix getContextC();
00090   virtual void setContextC(const matrix::Matrix& CC);
00091 
00092   
00093 protected:
00094   unsigned short number_sensors;
00095   unsigned short number_motors;
00096   static const unsigned short buffersize = 10;
00097 
00098   matrix::Matrix A; // Model Matrix
00099   matrix::Matrix C; // Controller Matrix
00100   matrix::Matrix S; // Model Matrix (sensor branch)
00101   matrix::Matrix h; // Controller Bias
00102   matrix::Matrix b; // Model Bias
00103   matrix::Matrix L; // Jacobi Matrix
00104   matrix::Matrix AC; // 
00105   matrix::Matrix R; // 
00106   
00107   SoxExpandConf conf;
00108   
00109   matrix::Matrix y_buffer[buffersize]; // buffer needed for delay
00110   matrix::Matrix x_buffer[buffersize]; // buffer of sensor values 
00111   matrix::Matrix v_avg;
00112   matrix::Matrix x;        // current sensor value vector
00113   matrix::Matrix x_c;      // current context sensor value vector
00114   matrix::Matrix x_smooth; // time average of x values
00115   int t;
00116   bool TLE;
00117   bool loga;
00118 
00119   paramval creativity;
00120   paramval sense;
00121   paramval harmony;
00122   paramval causeaware;
00123   paramval epsC;
00124   paramval epsA;
00125   paramint s4avg;          // # of steps the sensors are averaged (1 means no averaging)
00126   paramint s4delay;        // # of steps the motor values are delayed (1 means no delay)
00127 
00128   
00129   /// learn values model and controller (A,b,C,h)
00130   virtual void learn();
00131 
00132   /// neuron transfer function
00133   static double g(double z)
00134   {
00135     return tanh(z);
00136   };
00137 
00138   /// derivative of g
00139   static double g_s(double z)
00140   {
00141     double k=tanh(z);
00142     return 1.0 - k*k;
00143   };
00144 
00145   /// function that clips the second argument to the interval [-first,first]
00146   static double clip(double r, double x){  
00147     return min(max(x,-r),r);
00148   }
00149   /// calculates the inverse the argument (useful for Matrix::map)
00150   static double one_over(double x){
00151     return 1/x;
00152   }
00153 
00154 
00155 };
00156 
00157 #endif
00158 
00159 
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