abstractiafcontroller.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by                                            *
00003  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
00004  *    Georg Martius  <georg dot martius at web dot de>                     *
00005  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00006  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
00007  *                                                                         *
00008  *   ANY COMMERCIAL USE FORBIDDEN!                                         *
00009  *   LICENSE:                                                              *
00010  *   This work is licensed under the Creative Commons                      *
00011  *   Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of   *
00012  *   this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
00013  *   or send a letter to Creative Commons, 543 Howard Street, 5th Floor,   *
00014  *   San Francisco, California, 94105, USA.                                *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
00019  *                                                                         *
00020  ***************************************************************************/
00021 #ifndef __ABSTRACTIAFCONTROLLER_H
00022 #define __ABSTRACTIAFCONTROLLER_H
00023 
00024 #include "abstractcontroller.h"
00025 
00026 #include <selforg/matrix.h>
00027 #include "controller_misc.h"
00028 #include <selforg/configurable.h>
00029 
00030 typedef struct AbstractIAFControllerConf {
00031   AbstractIAFControllerConf() {
00032     thresholdI=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00033     thresholdO=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00034     leakI=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00035     leakO=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00036     restingPotential=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00037     wIInitScale=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00038     wOInitScale=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00039     numberIAFNeuronsPerInput=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00040     numberIAFNeuronsPerOutput=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
00041   }
00042   ~AbstractIAFControllerConf() {
00043     /*  if(thresholdI) free(thresholdI);
00044     if(thresholdO) free(thresholdO);
00045     if(leakI) free(leakI);
00046     if(leakO) free(leakO);*/
00047   }
00048   Configurable::paramval* numberIAFNeuronsPerInput;  ///< simulate a population if >1
00049   Configurable::paramval* numberIAFNeuronsPerOutput; ///< simulate a population if >1
00050   Configurable::paramval* wIInitScale;            ///< scaling factor of weights, initialized random
00051   Configurable::paramval* wOInitScale;            ///< between [-1*wInitScale,1*wInitScale]
00052   Configurable::paramval* thresholdI;
00053   Configurable::paramval* thresholdO;
00054   Configurable::paramval* leakI;
00055   Configurable::paramval* leakO;
00056   Configurable::paramval* restingPotential;
00057   //Configurable::paramval* test;
00058 } AbstractIAFControllerConf;
00059 
00060 
00061 /**
00062  * Abstract class (interface) for robot controller that uses an integrate
00063  * and fire neuron model
00064  *
00065  * Implements (assumes) that the sensor values are only 0 or 1.
00066  * Implements standard configureable interface for some useful parameters
00067  * like leakI and leakO (input and output layer)
00068  */
00069 class AbstractIAFController : public AbstractController {
00070 
00071 public:
00072   AbstractIAFController(const AbstractIAFControllerConf& conf = getDefaultConf());
00073 
00074   virtual ~AbstractIAFController() {}
00075 
00076 
00077   static AbstractIAFControllerConf getDefaultConf(){
00078     AbstractIAFControllerConf c;
00079     *c.numberIAFNeuronsPerInput  = 10;
00080     *c.numberIAFNeuronsPerOutput = 10;
00081     *c.wIInitScale= 0.5;
00082     *c.wOInitScale= 0.5;
00083     *c.thresholdI=0.5;
00084     *c.thresholdO=0.5;
00085     *c.leakI=0.01;
00086     *c.leakO=0.01;
00087     *c.restingPotential=0.0;
00088 
00089     return c;
00090   }
00091 
00092 
00093   /// ABSTRACTCONTROLLER INTERFACE
00094 
00095   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00096 
00097   virtual int getSensorNumber() const { return sensorNumber; }
00098 
00099   virtual int getMotorNumber() const { return motorNumber; }
00100 
00101   virtual void step(const sensor* sensors, int sensornumber, motor* motors, int motornumber);
00102 
00103   virtual void stepNoLearning(const sensor* sensors, int sensornumber, motor* motors, int motornumber);
00104 
00105   /// STORABLE INTERFACE
00106 
00107   virtual bool store(FILE* f) const { return true; }
00108 
00109   virtual bool restore(FILE* f) { return true; }
00110 
00111   /// CONFIGURABLE INTERFACE
00112   virtual void notifyOnChange(const paramkey& key);
00113 
00114 protected:
00115   AbstractIAFControllerConf conf;
00116   RandGen* randG;
00117   bool initialised;
00118   int sensorNumber;
00119   int motorNumber;
00120   double range;
00121   matrix::Matrix xI; // matrix with input for the input layer neurons
00122   matrix::Matrix xO; // matrix with input for the output layer neurons
00123   matrix::Matrix wI; // matrix with weights of the input layer neurons, incl.leak
00124   matrix::Matrix wO; // matrix with weights of the output layer neurons, incl.leak
00125   matrix::Matrix sumI; // matrix with current sums of the input layer neurons
00126   matrix::Matrix sumO; // matrix with current sums of the output layer neurons
00127   matrix::Matrix tI; // matrix with threshold value of the input layer neurons
00128   matrix::Matrix tO; // matrix with threshold value of the output layer neurons
00129 
00130   /**
00131    * makes a forward step (without any learning)
00132    */
00133   virtual void forwardStep(const sensor* sensors, int number_sensors, motor* motors, int number_motors);
00134 
00135   /**
00136    * inits the internal used matrices
00137    * If you change something on dimensions, you should call
00138    * this function.
00139    * @note that all learned weights are lost.
00140    */
00141   void initMatrices();
00142 
00143 
00144 
00145 
00146   /// returns -1 if probability is to low, otherwise 1 for mapP
00147   static double toTristateWithProbability(void* r,double x) {
00148     RandGen* g = (RandGen*) r;
00149     if (!g) return 0.;
00150     double rand = g->rand();
00151     return x < -rand ? -1. : (x < rand ? 0. : 1.);
00152   }
00153 
00154   /// returns -1 if below -threshold, 0 if above -threshold
00155   /// and threshold, otherwise 1, for map2
00156   static double toTristateWithThreshold(double x, double threshold){
00157     return x < -threshold ? -1. : (x < threshold ? 0. : 1.);
00158   }
00159 
00160   /// damps the value, if <0, damp value is added
00161   /// if >0, damp value is subtracted
00162   /// and threshold, otherwise 1, for map2
00163   static double dampToZero(void* r, double x){
00164     double damp = *(double*)r;
00165     return x < -damp ? x+damp : (x > damp ? x-damp : 0.);
00166   }
00167 
00168   // returns 0 if fired==1 (-1 or 1), otherwise x
00169   static double toZeroIfFired(double x, double fired) {
00170     return (fired==1 || fired==-1) ? 0 : x ;
00171   }
00172 
00173     // returns value if fired==1 (-1 or 1), otherwise x
00174   static double toValueIfFired(void* r,double x, double fired) {
00175     double value = *(double*)r;
00176     return (fired==1 || fired==-1) ? value : x ;
00177   }
00178 
00179 
00180   /// returns 0 if probability is to low, otherwise 1 for mapP
00181   static double toDualStateWithProbability(void* r,double x) {
00182     RandGen* g = (RandGen*) r;
00183     if (!g) return 0.;
00184     double rand = g->rand();
00185     return x < rand ? 0. : 1.;
00186   }
00187 
00188     /// returns 0 if below threshold, otherwise 1, for map2
00189   static double toDualStateWithThreshold(double x, double threshold){
00190     return x < threshold ? 0. : 1.;
00191   }
00192 
00193 
00194 
00195 
00196 };
00197 
00198 
00199 #endif
Generated on Thu Jun 28 14:45:35 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3