Robotsystem of the Robot Group Leipzig: abstractiafcontroller.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages
- Examples

- Search for |

- File List
- File Members

# abstractiafcontroller.h

Go to the documentation of this file.

    00001 /***************************************************************************
    00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
    00003  *    martius@informatik.uni-leipzig.de                                    *
    00004  *    fhesse@informatik.uni-leipzig.de                                     *
    00005  *    der@informatik.uni-leipzig.de                                        *
    00006  *    guettler@informatik.uni-leipzig.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  *   $Log: abstractiafcontroller.h,v $
    00021  *   Revision 1.2  2008/05/05 13:45:32  guettler
    00022  *   -tristateiafcontroller moved to ode_robots/simulations/iafsim
    00023  *   -this iafcontroller implements a new, more realistic iaf model
    00024  *   still first approaches
    00025  *
    00026  *   Revision 1.1  2008/04/30 14:56:27  guettler
    00027  *   neuron population support, other improvements
    00028  *
    00029  *   Revision 1.7  2008/04/29 15:31:57  guettler
    00030  *   tristate try
    00031  *
    00032  *   Revision 1.6  2008/04/29 11:06:48  guettler
    00033  *   adjusted to changes of matrix.h
    00034  *
    00035  *   Revision 1.5  2008/04/29 07:31:41  guettler
    00036  *   improvements
    00037  *
    00038  *   Revision 1.4  2008/04/28 10:31:11  guettler
    00039  *   first working version, still incomplete
    00040  *
    00041  *   Revision 1.3  2008/04/28 06:55:20  guettler
    00042  *   some advancements
    00043  *
    00044  *   Revision 1.2  2008/04/25 10:36:48  guettler
    00045  *   bugfix
    00046  *
    00047  *   Revision 1.1  2008/04/25 10:31:50  guettler
    00048  *   first version
    00049  *
    00050  *                                                                         *
    00051  ***************************************************************************/
    00052 #ifndef __ABSTRACTIAFCONTROLLER_H
    00053 #define __ABSTRACTIAFCONTROLLER_H
    00054
    00055 #include "abstractcontroller.h"
    00056
    00057 #include <selforg/matrix.h>
    00058 #include "controller_misc.h"
    00059 #include <selforg/configurable.h>
    00060
    00061 typedef struct AbstractIAFControllerConf {
    00062   AbstractIAFControllerConf() {
    00063     thresholdI=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00064     thresholdO=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00065     leakI=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00066     leakO=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00067     restingPotential=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00068     wIInitScale=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00069     wOInitScale=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00070     numberIAFNeuronsPerInput=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00071     numberIAFNeuronsPerOutput=(Configurable::paramval*) malloc(sizeof(Configurable::paramval));
    00072   }
    00073   ~AbstractIAFControllerConf() {
    00074     /*  if(thresholdI) free(thresholdI);
    00075     if(thresholdO) free(thresholdO);
    00076     if(leakI) free(leakI);
    00077     if(leakO) free(leakO);*/
    00078   }
    00079   Configurable::paramval* numberIAFNeuronsPerInput;  ///< simulate a population if >1
    00080   Configurable::paramval* numberIAFNeuronsPerOutput; ///< simulate a population if >1
    00081   Configurable::paramval* wIInitScale;            ///< scaling factor of weights, initialized random
    00082   Configurable::paramval* wOInitScale;            ///< between [-1*wInitScale,1*wInitScale]
    00083   Configurable::paramval* thresholdI;
    00084   Configurable::paramval* thresholdO;
    00085   Configurable::paramval* leakI;
    00086   Configurable::paramval* leakO;
    00087   Configurable::paramval* restingPotential;
    00088 } AbstractIAFControllerConf;
    00089
    00090
    00091 /**
    00092  * Abstract class (interface) for robot controller that uses an integrate
    00093  * and fire neuron model
    00094  *
    00095  * Implements (assumes) that the sensor values are only 0 or 1.
    00096  * Implements standard configureable interface for some useful parameters
    00097  * like leakI and leakO (input and output layer)
    00098  */
    00099 class AbstractIAFController : public AbstractController {
    00100
    00101 public:
    00102   AbstractIAFController(const AbstractIAFControllerConf& conf = getDefaultConf());
    00103
    00104   virtual ~AbstractIAFController() {}
    00105
    00106
    00107   static AbstractIAFControllerConf getDefaultConf(){
    00108     AbstractIAFControllerConf c;
    00109     *c.numberIAFNeuronsPerInput  = 10;
    00110     *c.numberIAFNeuronsPerOutput = 10;
    00111     *c.wIInitScale= 0.5;
    00112     *c.wOInitScale= 0.5;
    00113     *c.thresholdI=0.5;
    00114     *c.thresholdO=0.5;
    00115     *c.leakI=0.01;
    00116     *c.leakO=0.01;
    00117     *c.restingPotential=0.0;
    00118
    00119     return c;
    00120   }
    00121
    00122
    00123   /// ABSTRACTCONTROLLER INTERFACE
    00124
    00125   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
    00126
    00127   virtual int getSensorNumber() const { return sensorNumber; }
    00128
    00129   virtual int getMotorNumber() const { return motorNumber; }
    00130
    00131   virtual void step(const sensor* sensors, int sensornumber, motor* motors, int motornumber);
    00132
    00133   virtual void stepNoLearning(const sensor* sensors, int sensornumber, motor* motors, int motornumber);
    00134
    00135   /// STORABLE INTERFACE
    00136
    00137   virtual bool store(FILE* f) const { return true; }
    00138
    00139   virtual bool restore(FILE* f) { return true; }
    00140
    00141   /// CONFIGURABLE INTERFACE
    00142   virtual bool setParam(const paramkey& key, paramval val);
    00143
    00144 protected:
    00145   AbstractIAFControllerConf conf;
    00146   RandGen* randG;
    00147   bool initialised;
    00148   int sensorNumber;
    00149   int motorNumber;
    00150   double range;
    00151   matrix::Matrix xI; // matrix with input for the input layer neurons
    00152   matrix::Matrix xO; // matrix with input for the output layer neurons
    00153   matrix::Matrix wI; // matrix with weights of the input layer neurons, incl.leak
    00154   matrix::Matrix wO; // matrix with weights of the output layer neurons, incl.leak
    00155   matrix::Matrix sumI; // matrix with current sums of the input layer neurons
    00156   matrix::Matrix sumO; // matrix with current sums of the output layer neurons
    00157   matrix::Matrix tI; // matrix with threshold value of the input layer neurons
    00158   matrix::Matrix tO; // matrix with threshold value of the output layer neurons
    00159
    00160   /**
    00161    * makes a forward step (without any learning)
    00162    */
    00163   virtual void forwardStep(const sensor* sensors, int number_sensors, motor* motors, int number_motors);
    00164
    00165   /**
    00166    * inits the internal used matrices
    00167    * If you change something on dimensions, you should call
    00168    * this function.
    00169    * @note that all learned weights are lost.
    00170    */
    00171   void initMatrices();
    00172
    00173
    00174
    00175
    00176   /// returns -1 if probability is to low, otherwise 1 for mapP
    00177   static double toTristateWithProbability(void* r,double x) {
    00178     RandGen* g = (RandGen*) r;
    00179     if (!g) return 0.;
    00180     double rand = g->rand();
    00181     return x < -rand ? -1. : (x < rand ? 0. : 1.);
    00182   }
    00183
    00184   /// returns -1 if below -threshold, 0 if above -threshold
    00185   /// and threshold, otherwise 1, for map2
    00186   static double toTristateWithThreshold(double x, double threshold){
    00187     return x < -threshold ? -1. : (x < threshold ? 0. : 1.);
    00188   }
    00189
    00190   /// damps the value, if <0, damp value is added
    00191   /// if >0, damp value is subtracted
    00192   /// and threshold, otherwise 1, for map2
    00193   static double dampToZero(void* r, double x){
    00194     double damp = *(double*)r;
    00195     return x < -damp ? x+damp : (x > damp ? x-damp : 0.);
    00196   }
    00197
    00198   // returns 0 if fired==1 (-1 or 1), otherwise x
    00199   static double toZeroIfFired(double x, double fired) {
    00200     return (fired==1 || fired==-1) ? 0 : x ;
    00201   }
    00202
    00203     // returns value if fired==1 (-1 or 1), otherwise x
    00204   static double toValueIfFired(void* r,double x, double fired) {
    00205     double value = *(double*)r;
    00206     return (fired==1 || fired==-1) ? value : x ;
    00207   }
    00208
    00209
    00210   /// returns 0 if probability is to low, otherwise 1 for mapP
    00211   static double toDualStateWithProbability(void* r,double x) {
    00212     RandGen* g = (RandGen*) r;
    00213     if (!g) return 0.;
    00214     double rand = g->rand();
    00215     return x < rand ? 0. : 1.;
    00216   }
    00217
    00218     /// returns 0 if below threshold, otherwise 1, for map2
    00219   static double toDualStateWithThreshold(double x, double threshold){
    00220     return x < threshold ? 0. : 1.;
    00221   }
    00222
    00223
    00224
    00225
    00226 };
    00227
    00228
    00229 #endif