PandaRoot
PndMvaSOMTrainer.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 /* ****************************************
14  * Self Organizing Map Training functions *
15  * Author: M.Babai@rug.nl *
16  * Version: *
17  * LICENSE: *
18  * ****************************************
19  */
20 //#pragma once
21 #ifndef PND_SOM_TRAINER_H
22 #define PND_SOM_TRAINER_H
23 
24 //________________________
25 #include <vector>
26 
27 //________________________
28 class PndSomNode;
29 
30 //________________________
31 class TRandom3;
32 
36 typedef std::vector<std::pair<std::string, std::vector<float> *>> DataPoints;
37 
41 typedef enum MapNodeInitType {
42  SOM_RAND_FROM_DATA = 0, // Select randomly from data vector.
43  SOM_RANDOM = 1 // Use random numbers
45 
46 /*
47  * Scheme for the shape of the grid.
48  */
49 typedef enum GridInitType {
50  RECTANGULAR = 0, // Init rectangular grid
51  HEXAGONAL = 1 // Init hexagonal grid
52 } GridInitType;
53 
54 // Debug constants, 0 = no DEBUG, 1 = DEBUG info
55 #define PRINT_PND_SOM_TRAIN_DEBUG_INFO 1
56 
58  //----------------------------------------
59  //================== public ==============
60  public:
69  explicit PndMvaSomTrainer(DataPoints const *const InputData, size_t mapWidth, size_t mapHeight, size_t numIter, MapNodeInitType initType = SOM_RAND_FROM_DATA,
70  GridInitType gridInitType = RECTANGULAR);
74  virtual ~PndMvaSomTrainer();
75 
79  virtual void InitMap();
80 
85  virtual void TrainBatch();
86 
91  virtual void TrainOnline();
92 
99  virtual void Calibrate();
100 
101  //____________ Getters and Setters ______
102  //________________________________________
106  inline std::vector<PndSomNode *> const &GetTheMap() const;
107 
112  inline DataPoints const &GetInputDataSet() const;
113 
117  inline size_t GetNumNodes() const;
118 
122  inline void SetSigmaZero(double val);
123  inline double GetSigmaZero() const;
124 
128  inline void SetLambda(double val);
129  inline double GetLambda() const;
130 
135  inline MapNodeInitType GetNodeInitType() const;
136 
140  inline size_t GetMapHeight() const;
141  inline void SetMapHeight(size_t val);
142 
143  inline size_t GetMapWidth() const;
144  inline void SetMapWidth(size_t val);
145 
149  inline size_t GetNumIterations() const;
150  inline void SetNumIterations(size_t val);
151 
152  //--------------------------------------------
153  //================= protected ================
154  protected:
155  //_____________ DEBUG _________________
156 #if (PRINT_PND_SOM_TRAIN_DEBUG_INFO > 0)
157  void printMapGrid() const;
158 #endif
159 
160  //--------------------------------------------
161  //================= private ==================
162 
163  private:
164  //_______________ Functions ____________________
165  // To avoid mistakes, :).
166  PndMvaSomTrainer(PndMvaSomTrainer const &oth); /* Copy */
167  PndMvaSomTrainer &operator=(PndMvaSomTrainer const &oth); /*Assign*/
168 
169  /*
170  * Init a rectangular grid.
171  */
172  void InitGridRectAngular();
173 
174  /*
175  * Init a hexagonal grid.
176  */
177  void InitGridHexagonal();
178 
179  /*
180  * Initialize map nodes using random vectors fetched from the data
181  * set.
182  */
183  void InitMapnodes_RandomFromData();
184 
185  /*
186  * Initialize map nodes using vectors with random numbers.
187  */
188  void InitMapnodes_Random();
189 
190  /*
191  * Returns the index of the BMU map node.
192  *
193  *@param vector vector containing the coordinates of the current
194  * data point.
195  */
196  size_t FindBestMatchingNode(std::vector<float> const &vector);
197 
198  //___________________ Variables ___________________
199  double m_sigmaZero; // the width of the lattice at time t0
200  double m_lambda; // A time constant, determine neighborhood
201  double m_neighbourhoodRadius; // the current width of area of influence
202 
203  size_t m_MapWidth; // Width of the map
204  size_t m_MapHeight; // Height of the map
205  size_t m_NumModelVectors; // Number of map nodes
206  size_t m_NumIterations; // Number of iterations for learning.
207 
208  MapNodeInitType m_InitMode; // Init Scheme
209  GridInitType m_GridType; // Shape of the map grid.
210  std::vector<PndSomNode *> m_TheMap; // The actual map container
211  DataPoints const *m_DataSet; // Data points used to train the map.
212 }; // End of class definition
213 
214 //---------------------- INLINE functions ____________
215 inline std::vector<PndSomNode *> const &PndMvaSomTrainer::GetTheMap() const
216 {
217  return this->m_TheMap;
218 };
219 
221 {
222  return (*(this->m_DataSet));
223 };
224 
225 inline void PndMvaSomTrainer::SetSigmaZero(double val)
226 {
227  this->m_sigmaZero = val;
228 };
229 
230 inline void PndMvaSomTrainer::SetLambda(double val)
231 {
232  this->m_lambda = val;
233 };
234 
236 {
237  this->m_InitMode = val;
238 };
239 
240 inline double PndMvaSomTrainer::GetSigmaZero() const
241 {
242  return this->m_sigmaZero;
243 };
244 
245 inline double PndMvaSomTrainer::GetLambda() const
246 {
247  return this->m_lambda;
248 };
249 
251 {
252  return this->m_InitMode;
253 };
254 
255 inline size_t PndMvaSomTrainer::GetMapHeight() const
256 {
257  return this->m_MapHeight;
258 };
259 
260 inline void PndMvaSomTrainer::SetMapHeight(size_t val)
261 {
262  this->m_MapHeight = val;
263 };
264 
265 inline size_t PndMvaSomTrainer::GetMapWidth() const
266 {
267  return this->m_MapWidth;
268 };
269 
270 inline void PndMvaSomTrainer::SetMapWidth(size_t val)
271 {
272  this->m_MapWidth = val;
273 };
274 
275 inline size_t PndMvaSomTrainer::GetNumNodes() const
276 {
277  return (this->m_MapWidth * this->m_MapHeight);
278 };
279 
281 {
282  return this->m_NumIterations;
283 };
284 
285 inline void PndMvaSomTrainer::SetNumIterations(size_t val)
286 {
287  this->m_NumIterations = val;
288 };
289 #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:36
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