som.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 LpzRobots development team                    *
00003  *    Georg Martius  <georg dot martius at web dot de>                     *
00004  *    Frank Guettler <guettler at informatik dot uni-leipzig 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  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  *                                                                         *
00023  ***************************************************************************/
00024 #ifndef __SOM_H
00025 #define __SOM_H
00026 
00027 #include "abstractmodel.h"
00028 #include "controller_misc.h"
00029 
00030 #include <vector>
00031 
00032 /** self-organised map class. Implementation follows normal vector-quantiser
00033  scheme. 
00034 The output of the network is  \f$exp(- |x-w_i|^2/rdfsize)\f$ for each neuron.
00035 */
00036 class SOM : public AbstractModel {
00037 public: 
00038   typedef std::list< std::pair<int,double> > Neighbours;
00039   typedef std::vector<std::pair<matrix::Matrix,double> > Neighbourhood;
00040   
00041   SOM(const std::string& name="SOM",
00042       const std::string& revision = "$Id$");
00043 
00044   /** create a som
00045       @param dimensions number of dimensions of the neuron lattice
00046    */
00047   SOM(unsigned int dimensions, double sigma, double eps, double rbfsize,
00048       const std::string& name="SOM", 
00049       const std::string& revision = "$Id$");
00050   virtual ~SOM(){};
00051 
00052   /** initialised som
00053       @param inputDim dimension of input vector
00054       @param outputDim number of outputneurons (must be a multiple of "dimensions" given at constructor)
00055       @param unit_map if zero then weights are randomly choosed, otherwise
00056              uniformly distributed in the inputspace of size (unit_map x unit_map x ...)
00057    */
00058   virtual void init(unsigned int inputDim, unsigned  int outputDim, 
00059                     double unit_map = 0.0, RandGen* randGen = 0);
00060 
00061   virtual const matrix::Matrix process (const matrix::Matrix& input);
00062 
00063   /*  performs training. Nominal output is ignored.
00064       A zero-Matrix is returned.
00065       learnRateFactor can be given to modify eps for this learning step
00066       (process should be called before)
00067   */
00068   virtual const matrix::Matrix learn (const matrix::Matrix& input, 
00069                                       const matrix::Matrix& nom_output, 
00070                                       double learnRateFactor = 1);
00071 
00072   virtual void damp(double damping) { return;}
00073 
00074   virtual unsigned int getInputDim() const { return weights[0].getM();}
00075   virtual unsigned int getOutputDim() const  { return weights.size();}
00076 
00077     
00078   virtual bool store(FILE* f) const;
00079   virtual bool restore(FILE* f);  
00080 
00081   virtual void printWeights(FILE* f) const;
00082 
00083   const Neighbourhood& getNeighbourhood(){return neighbourhood;}
00084 
00085 protected:
00086  
00087   /// activation function (rbf)
00088   static double activationfunction(void* rdfsize, double d);
00089   
00090   /// checks whether the given coordinate is within the lattice
00091   static bool validCoord(const matrix::Matrix& m, int size);
00092   /// converts index  to coordinates (size is the size of the space)
00093   static int coordToIndex(const matrix::Matrix& m, int size);
00094   /// converts coordinates to index (size is the size of the space)
00095   static matrix::Matrix indexToCoord(int index, int size, int dimensions);
00096 
00097 
00098   /// initialised neighbourhood
00099   void initNeighbourhood(double sigma);
00100 
00101   /** returns neighbourhood as a list of indices with weights      
00102   */
00103   Neighbours getNeighbours(int winner);
00104 
00105 
00106 public: 
00107   double eps; ///< learning rate for weight update
00108 private:
00109 
00110   std::vector<matrix::Matrix> weights;
00111   std::vector<matrix::Matrix> diffvectors; ///< temporary difference vectors
00112   matrix::Matrix distances; ///< vector of distances
00113   int dimensions; ///< number of dimensions of lattice
00114   double sigma; ///< neighbourhood size
00115   double rbfsize; ///< size of rbf function
00116   int size; ///< size of the lattice in each dimension
00117 
00118 
00119   /// list of vectors defining relative neighbourhood coordinates and weights
00120   Neighbourhood neighbourhood; 
00121   
00122   
00123   bool initialised;
00124 };
00125 
00126 
00127 #endif
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