sos.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 __SOS_H
00020 #define __SOS_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  * This controller implements the standard algorihm described the Chapter 5 (Homeokinesis)
00032  *  of book "The Playful Machine"
00033  */
00034 class Sos : public AbstractController {
00035 
00036 public:
00037   Sos(double init_feedback_strength = 1.0);
00038   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00039 
00040   virtual ~Sos();
00041 
00042   /// returns the number of sensors the controller was initialised with or 0 if not initialised
00043   virtual int getSensorNumber() const { return number_sensors; }
00044   /// returns the mumber of motors the controller was initialised with or 0 if not initialised
00045   virtual int getMotorNumber() const  { return number_motors; }
00046 
00047   /// performs one step (includes learning). 
00048   /// Calulates motor commands from sensor inputs.
00049   virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
00050 
00051 
00052   /// performs one step without learning. Calulates motor commands from sensor inputs.
00053   virtual void stepNoLearning(const sensor* , int number_sensors, 
00054                               motor* , int number_motors);
00055 
00056 
00057   /***** STOREABLE ****/
00058   /** stores the controller values to a given file. */
00059   virtual bool store(FILE* f) const;
00060   /** loads the controller values from a given file. */
00061   virtual bool restore(FILE* f);  
00062 
00063   /* some direct access functions (unsafe!) */
00064   virtual matrix::Matrix getA();
00065   virtual void setA(const matrix::Matrix& A);
00066   virtual matrix::Matrix getC();
00067   virtual void setC(const matrix::Matrix& C);
00068   virtual matrix::Matrix geth();
00069   virtual void seth(const matrix::Matrix& h);
00070   
00071 protected:
00072   unsigned short number_sensors;
00073   unsigned short number_motors;
00074   static const unsigned short buffersize = 10;
00075 
00076   matrix::Matrix A; // Model Matrix
00077   matrix::Matrix C; // Controller Matrix
00078   matrix::Matrix h; // Controller Bias
00079   matrix::Matrix b; // Model Bias
00080   matrix::Matrix L; // Jacobi Matrix
00081   matrix::Matrix y_buffer[buffersize]; // buffer needed for delay
00082   matrix::Matrix x_buffer[buffersize]; // buffer of sensor values 
00083   matrix::Matrix v_avg;
00084   matrix::Matrix x;        // current sensor value vector
00085   matrix::Matrix x_smooth; // time average of x values
00086   int t;
00087   bool TLE;
00088   bool loga;
00089 
00090   double init_feedback_strength;
00091 
00092   paramval creativity;
00093   paramval epsC;
00094   paramval epsA;
00095   paramint s4avg;          // # of steps the sensors are averaged (1 means no averaging)
00096   paramint s4delay;        // # of steps the motor values are delayed (1 means no delay)
00097 
00098   
00099   /// learn values model and controller (A,b,C,h)
00100   virtual void learn();
00101 
00102   /// neuron transfer function
00103   static double g(double z)
00104   {
00105     return tanh(z);
00106   };
00107 
00108   /// derivative of g
00109   static double g_s(double z)
00110   {
00111     double k=tanh(z);
00112     return 1.0 - k*k;
00113   };
00114 
00115   /// function that clips the second argument to the interval [-first,first]
00116   static double clip(double r, double x){  
00117     return min(max(x,-r),r);
00118   }
00119   /// calculates the inverse the argument (useful for Matrix::map)
00120   static double one_over(double x){
00121     return 1/x;
00122   }
00123 
00124 
00125 };
00126 
00127 #endif
00128 
00129 
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