PandaRoot
PndMvaSOMTrainer.h
Go to the documentation of this file.
1 /* ****************************************
2  * Self Organizing Map Training functions *
3  * Author: M.Babai@rug.nl *
4  * Version: *
5  * LICENSE: *
6  * ****************************************
7  */
8 //#pragma once
9 #ifndef PND_SOM_TRAINER_H
10 #define PND_SOM_TRAINER_H
11 
12 //________________________
13 #include <vector>
14 
15 //________________________
16 class PndSomNode;
17 
18 //________________________
19 class TRandom3;
20 
24 typedef std::vector<std::pair<std::string, std::vector<float> *>> DataPoints;
25 
29 typedef enum MapNodeInitType {
30  SOM_RAND_FROM_DATA = 0, // Select randomly from data vector.
31  SOM_RANDOM = 1 // Use random numbers
33 
34 /*
35  * Scheme for the shape of the grid.
36  */
37 typedef enum GridInitType {
38  RECTANGULAR = 0, // Init rectangular grid
39  HEXAGONAL = 1 // Init hexagonal grid
40 } GridInitType;
41 
42 // Debug constants, 0 = no DEBUG, 1 = DEBUG info
43 #define PRINT_PND_SOM_TRAIN_DEBUG_INFO 1
44 
46  //----------------------------------------
47  //================== public ==============
48  public:
57  explicit PndMvaSomTrainer(DataPoints const *const InputData, size_t mapWidth, size_t mapHeight, size_t numIter, MapNodeInitType initType = SOM_RAND_FROM_DATA,
58  GridInitType gridInitType = RECTANGULAR);
62  virtual ~PndMvaSomTrainer();
63 
67  virtual void InitMap();
68 
73  virtual void TrainBatch();
74 
79  virtual void TrainOnline();
80 
87  virtual void Calibrate();
88 
89  //____________ Getters and Setters ______
90  //________________________________________
94  inline std::vector<PndSomNode *> const &GetTheMap() const;
95 
100  inline DataPoints const &GetInputDataSet() const;
101 
105  inline size_t GetNumNodes() const;
106 
110  inline void SetSigmaZero(double val);
111  inline double GetSigmaZero() const;
112 
116  inline void SetLambda(double val);
117  inline double GetLambda() const;
118 
123  inline MapNodeInitType GetNodeInitType() const;
124 
128  inline size_t GetMapHeight() const;
129  inline void SetMapHeight(size_t val);
130 
131  inline size_t GetMapWidth() const;
132  inline void SetMapWidth(size_t val);
133 
137  inline size_t GetNumIterations() const;
138  inline void SetNumIterations(size_t val);
139 
140  //--------------------------------------------
141  //================= protected ================
142  protected:
143  //_____________ DEBUG _________________
144 #if (PRINT_PND_SOM_TRAIN_DEBUG_INFO > 0)
145  void printMapGrid() const;
146 #endif
147 
148  //--------------------------------------------
149  //================= private ==================
150 
151  private:
152  //_______________ Functions ____________________
153  // To avoid mistakes, :).
154  PndMvaSomTrainer(PndMvaSomTrainer const &oth); /* Copy */
155  PndMvaSomTrainer &operator=(PndMvaSomTrainer const &oth); /*Assign*/
156 
157  /*
158  * Init a rectangular grid.
159  */
160  void InitGridRectAngular();
161 
162  /*
163  * Init a hexagonal grid.
164  */
165  void InitGridHexagonal();
166 
167  /*
168  * Initialize map nodes using random vectors fetched from the data
169  * set.
170  */
171  void InitMapnodes_RandomFromData();
172 
173  /*
174  * Initialize map nodes using vectors with random numbers.
175  */
176  void InitMapnodes_Random();
177 
178  /*
179  * Returns the index of the BMU map node.
180  *
181  *@param vector vector containing the coordinates of the current
182  * data point.
183  */
184  size_t FindBestMatchingNode(std::vector<float> const &vector);
185 
186  //___________________ Variables ___________________
187  double m_sigmaZero; // the width of the lattice at time t0
188  double m_lambda; // A time constant, determine neighborhood
189  double m_neighbourhoodRadius; // the current width of area of influence
190 
191  size_t m_MapWidth; // Width of the map
192  size_t m_MapHeight; // Height of the map
193  size_t m_NumModelVectors; // Number of map nodes
194  size_t m_NumIterations; // Number of iterations for learning.
195 
196  MapNodeInitType m_InitMode; // Init Scheme
197  GridInitType m_GridType; // Shape of the map grid.
198  std::vector<PndSomNode *> m_TheMap; // The actual map container
199  DataPoints const *m_DataSet; // Data points used to train the map.
200 }; // End of class definition
201 
202 //---------------------- INLINE functions ____________
203 inline std::vector<PndSomNode *> const &PndMvaSomTrainer::GetTheMap() const
204 {
205  return this->m_TheMap;
206 };
207 
209 {
210  return (*(this->m_DataSet));
211 };
212 
213 inline void PndMvaSomTrainer::SetSigmaZero(double val)
214 {
215  this->m_sigmaZero = val;
216 };
217 
218 inline void PndMvaSomTrainer::SetLambda(double val)
219 {
220  this->m_lambda = val;
221 };
222 
224 {
225  this->m_InitMode = val;
226 };
227 
228 inline double PndMvaSomTrainer::GetSigmaZero() const
229 {
230  return this->m_sigmaZero;
231 };
232 
233 inline double PndMvaSomTrainer::GetLambda() const
234 {
235  return this->m_lambda;
236 };
237 
239 {
240  return this->m_InitMode;
241 };
242 
243 inline size_t PndMvaSomTrainer::GetMapHeight() const
244 {
245  return this->m_MapHeight;
246 };
247 
248 inline void PndMvaSomTrainer::SetMapHeight(size_t val)
249 {
250  this->m_MapHeight = val;
251 };
252 
253 inline size_t PndMvaSomTrainer::GetMapWidth() const
254 {
255  return this->m_MapWidth;
256 };
257 
258 inline void PndMvaSomTrainer::SetMapWidth(size_t val)
259 {
260  this->m_MapWidth = val;
261 };
262 
263 inline size_t PndMvaSomTrainer::GetNumNodes() const
264 {
265  return (this->m_MapWidth * this->m_MapHeight);
266 };
267 
269 {
270  return this->m_NumIterations;
271 };
272 
273 inline void PndMvaSomTrainer::SetNumIterations(size_t val)
274 {
275  this->m_NumIterations = val;
276 };
277 #endif // End of interface
double GetLambda() const
DataPoints const & GetInputDataSet() const
void SetSigmaZero(double val)
virtual ~PndMvaSomTrainer()
virtual void InitMap()
size_t GetNumIterations() const
void printMapGrid() const
size_t GetMapWidth() const
MapNodeInitType
virtual void Calibrate()
void SetMapHeight(size_t val)
std::vector< std::pair< std::string, std::vector< float > * > > DataPoints
Data structure of the space points and the cluster centers.
Definition: PndMvaCluster.h:24
virtual void TrainOnline()
MapNodeInitType GetNodeInitType() const
void SetMapWidth(size_t val)
void SetNumIterations(size_t val)
PndMvaSomTrainer(DataPoints const *const InputData, size_t mapWidth, size_t mapHeight, size_t numIter, MapNodeInitType initType=SOM_RAND_FROM_DATA, GridInitType gridInitType=RECTANGULAR)
double GetSigmaZero() const
void SetNodeInitType(MapNodeInitType val=SOM_RAND_FROM_DATA)
void SetLambda(double val)
GridInitType
virtual void TrainBatch()
std::vector< PndSomNode * > const & GetTheMap() const
size_t GetNumNodes() const
std::vector< std::pair< std::string, std::vector< float > * > > DataPoints
size_t GetMapHeight() const