Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
som.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __SOM_H
25 #define __SOM_H
26 
27 #include "abstractmodel.h"
28 #include "controller_misc.h"
29 
30 #include <vector>
31 
32 /** self-organised map class. Implementation follows normal vector-quantiser
33  scheme.
34 The output of the network is \f$exp(- |x-w_i|^2/rdfsize)\f$ for each neuron.
35 */
36 class SOM : public AbstractModel {
37 public:
38  typedef std::list< std::pair<int,double> > Neighbours;
39  typedef std::vector<std::pair<matrix::Matrix,double> > Neighbourhood;
40 
41  SOM(const std::string& name="SOM",
42  const std::string& revision = "$Id$");
43 
44  /** create a som
45  @param dimensions number of dimensions of the neuron lattice
46  */
47  SOM(unsigned int dimensions, double sigma, double eps, double rbfsize,
48  const std::string& name="SOM",
49  const std::string& revision = "$Id$");
50  virtual ~SOM(){};
51 
52  /** initialised som
53  @param inputDim dimension of input vector
54  @param outputDim number of outputneurons (must be a multiple of "dimensions" given at constructor)
55  @param unit_map if zero then weights are randomly choosed, otherwise
56  uniformly distributed in the inputspace of size (unit_map x unit_map x ...)
57  */
58  virtual void init(unsigned int inputDim, unsigned int outputDim,
59  double unit_map = 0.0, RandGen* randGen = 0);
60 
61  virtual const matrix::Matrix process (const matrix::Matrix& input);
62 
63  /* performs training. Nominal output is ignored.
64  A zero-Matrix is returned.
65  learnRateFactor can be given to modify eps for this learning step
66  (process should be called before)
67  */
68  virtual const matrix::Matrix learn (const matrix::Matrix& input,
69  const matrix::Matrix& nom_output,
70  double learnRateFactor = 1);
71 
72  virtual void damp(double damping) { return;}
73 
74  virtual unsigned int getInputDim() const { return weights[0].getM();}
75  virtual unsigned int getOutputDim() const { return weights.size();}
76 
77 
78  virtual bool store(FILE* f) const;
79  virtual bool restore(FILE* f);
80 
81  virtual void printWeights(FILE* f) const;
82 
83  const Neighbourhood& getNeighbourhood(){return neighbourhood;}
84 
85 protected:
86 
87  /// activation function (rbf)
88  static double activationfunction(void* rdfsize, double d);
89 
90  /// checks whether the given coordinate is within the lattice
91  static bool validCoord(const matrix::Matrix& m, int size);
92  /// converts index to coordinates (size is the size of the space)
93  static int coordToIndex(const matrix::Matrix& m, int size);
94  /// converts coordinates to index (size is the size of the space)
95  static matrix::Matrix indexToCoord(int index, int size, int dimensions);
96 
97 
98  /// initialised neighbourhood
99  void initNeighbourhood(double sigma);
100 
101  /** returns neighbourhood as a list of indices with weights
102  */
103  Neighbours getNeighbours(int winner);
104 
105 
106 public:
107  double eps; ///< learning rate for weight update
108 private:
109 
110  std::vector<matrix::Matrix> weights;
111  std::vector<matrix::Matrix> diffvectors; ///< temporary difference vectors
112  matrix::Matrix distances; ///< vector of distances
113  int dimensions; ///< number of dimensions of lattice
114  double sigma; ///< neighbourhood size
115  double rbfsize; ///< size of rbf function
116  int size; ///< size of the lattice in each dimension
117 
118 
119  /// list of vectors defining relative neighbourhood coordinates and weights
120  Neighbourhood neighbourhood;
121 
122 
123  bool initialised;
124 };
125 
126 
127 #endif
Matrix type.
Definition: matrix.h:65
static matrix::Matrix indexToCoord(int index, int size, int dimensions)
converts coordinates to index (size is the size of the space)
Definition: som.cpp:92
virtual ~SOM()
Definition: som.h:50
static double activationfunction(void *rdfsize, double d)
activation function (rbf)
Definition: som.cpp:76
double eps
learning rate for weight update
Definition: som.h:107
SOM(const std::string &name="SOM", const std::string &revision="$Id$")
Definition: som.cpp:31
static bool validCoord(const matrix::Matrix &m, int size)
checks whether the given coordinate is within the lattice
Definition: som.cpp:103
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
virtual bool store(FILE *f) const
stores the object to the given file stream (ASCII preferred).
Definition: som.cpp:188
virtual void damp(double damping)
damps the weights and the biases by multiplying (1-damping)
Definition: som.h:72
virtual const matrix::Matrix process(const matrix::Matrix &input)
passive processing of the input (this function is not constant since a recurrent network for example ...
Definition: som.cpp:159
virtual void init(unsigned int inputDim, unsigned int outputDim, double unit_map=0.0, RandGen *randGen=0)
initialised som
Definition: som.cpp:45
std::list< std::pair< int, double > > Neighbours
Definition: som.h:38
self-organised map class.
Definition: som.h:36
virtual bool restore(FILE *f)
loads the object from the given file stream (ASCII preferred).
Definition: som.cpp:203
void initNeighbourhood(double sigma)
initialised neighbourhood
Definition: som.cpp:113
const Neighbourhood & getNeighbourhood()
Definition: som.h:83
virtual void printWeights(FILE *f) const
Definition: som.cpp:152
std::vector< std::pair< matrix::Matrix, double > > Neighbourhood
Definition: som.h:39
static int coordToIndex(const matrix::Matrix &m, int size)
converts index to coordinates (size is the size of the space)
Definition: som.cpp:82
abstract class (interface) for a model that can be used by a controller
Definition: abstractmodel.h:34
virtual const matrix::Matrix learn(const matrix::Matrix &input, const matrix::Matrix &nom_output, double learnRateFactor=1)
Definition: som.cpp:170
virtual unsigned int getInputDim() const
returns the number of input neurons
Definition: som.h:74
virtual unsigned int getOutputDim() const
returns the number of output neurons
Definition: som.h:75
Neighbours getNeighbours(int winner)
returns neighbourhood as a list of indices with weights
Definition: som.cpp:130