OMNeT++ Simulation Library  6.0.3
cksplit.h
1 //==========================================================================
2 // CKSPLIT.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 // Author: Andras Varga, based on code of Babak Fakhamzadeh, TU Delft, Mar-Jun 1996;
17 
18 #ifndef __OMNETPP_CKSPLIT_H
19 #define __OMNETPP_CKSPLIT_H
20 
21 #include "cprecolldensityest.h"
22 
23 namespace omnetpp {
24 
25 
44 class SIM_API cKSplit : public cPrecollectionBasedDensityEst
45 {
46  public:
47  // K is the grid size of the algorithm. It must be 2, or a >=3 odd number.
48  enum { K = 2 };
49 
54  struct Grid
55  {
56  int parent;
57  int reldepth;
58  double total;
59  double mother;
60  double cells[K];
61  };
62 
67  typedef int (*CritFunc)(const cKSplit&, cKSplit::Grid&, int, double *);
68 
73  typedef double (*DivFunc)(const cKSplit&, cKSplit::Grid&, double, double *);
74 
78  class Iterator
79  {
80  private:
81  cKSplit *ks; // host object
82  int cellnum; // global index of current cell
83  int grid, cell; // root index in gridv[], cell index in grid.cell[]
84  double gridmin; // left edge of current grid
85  double cellsize; // cell width on current grid
86 
87  // internal
88  void dive(int where);
89 
90  public:
94  Iterator(const cKSplit& ksplit, bool atbeginning=true);
95 
99  void init(const cKSplit& ksplit, bool atbeginning=true);
100 
104  void operator++(int);
105 
109  void operator--(int);
110 
114  bool end() const {return grid==0;}
115 
119  int getCellNumber() const {return cellnum;}
120 
124  double getCellMin() const {return gridmin+cell*cellsize;}
125 
129  double getCellMax() const {return gridmin+(cell+1)*cellsize;}
130 
134  double getCellSize() const {return cellsize;}
135 
140  double getCellValue() const;
141  };
142 
143  friend class Iterator;
144 
145  protected:
146  int numCells; // number of cells
147 
148  Grid *gridv; // grid vector
149  int gridvSize; // size of gridv[]+1
150  int rootGridIndex, lastGridIndex; // indices into gridv[]
151  bool rangeExtEnabled; // enable/disable range extension
152 
153  CritFunc critFunc; // function that determines when to split a cell
154  double *critData; // data array to pass to crit. function
155 
156  DivFunc divFunc; // function to calc. lambda for cell division
157  double *divData; // data array to pass to div. function
158 
159  mutable Iterator *iter; // iterator used by getBinEdge(), getBinValue() etc.
160  mutable long iterNumValues; // numValues when iterator was created
161 
162  private:
163  void copy(const cKSplit& other);
164 
165  protected:
166  // internal:
167  void resetGrids(int grid);
168 
169  // internal:
170  void createRootGrid();
171 
172  // internal:
173  void newRootGrids(double value, double weight);
174 
175  // internal:
176  void insertIntoGrids(double value, double weight, int enableSplits);
177 
178  // internal:
179  void splitCell(int grid, int cell);
180 
181  // internal:
182  void distributeMotherObservations(int grid);
183 
184  // internal:
185  void expandGridVector();
186 
187  // internal: helper for getBinEdge(), getBinValue()
188  void iteratorToCell(int cell_nr) const;
189 
190  // abstract method in cPrecollectionBasedDensityEst
191  virtual void doMergeBinValues(const cPrecollectionBasedDensityEst *other) override;
192 
193  public:
196 
200  explicit cKSplit(const char *name=nullptr, bool weighted=false);
201 
205  cKSplit(const cKSplit& r);
206 
210  virtual ~cKSplit();
211 
215  cKSplit& operator=(const cKSplit& res);
217 
220 
225  virtual cKSplit *dup() const override {return new cKSplit (*this);}
226 
232  virtual void parsimPack(cCommBuffer *buffer) const override;
233 
239  virtual void parsimUnpack(cCommBuffer *buffer) override;
241 
242  protected:
247  virtual void collectIntoHistogram(double val) override;
248 
252  virtual void collectWeightedIntoHistogram(double value, double weight) override;
253 
254  public:
257 
261  virtual void setUpBins() override;
262 
266  virtual int getNumBins() const override;
267 
271  virtual double getBinEdge(int k) const override;
272 
276  virtual double getBinValue(int k) const override;
277 
281  virtual void merge(const cStatistic *other) override;
282 
286  virtual void clear() override;
287 
291  virtual void saveToFile(FILE *) const override;
292 
296  virtual void loadFromFile(FILE *) override;
298 
301 
306  void setCritFunc(CritFunc _critfunc, double *_critdata);
307 
312  void setDivFunc(DivFunc _divfunc, double *_divdata);
313 
322  void setRangeExtension(bool enabled);
324 
327 
331  int getTreeDepth() const;
332 
336  int getTreeDepth(Grid& grid) const;
337 
342  double getRealCellValue(Grid& grid, int cell) const;
343 
347  void printGrids() const;
348 
352  Grid& getGrid(int k) const {return gridv[k];}
353 
357  Grid& getRootGrid() const {return gridv[rootGridIndex];}
359 };
360 
361 
362 // cell split criteria
363 int critfunc_const(const cKSplit&, cKSplit::Grid&, int, double *);
364 int critfunc_depth(const cKSplit&, cKSplit::Grid&, int, double *);
365 
366 // cell division criteria
367 double divfunc_const(const cKSplit&, cKSplit::Grid&, double, double *);
368 double divfunc_babak(const cKSplit&, cKSplit::Grid&, double, double *);
369 
370 
371 } // namespace omnetpp
372 
373 
374 #endif
375 
omnetpp::cKSplit::Iterator::getCellNumber
int getCellNumber() const
Definition: cksplit.h:119
omnetpp::cKSplit::dup
virtual cKSplit * dup() const override
Definition: cksplit.h:225
omnetpp::cKSplit::getGrid
Grid & getGrid(int k) const
Definition: cksplit.h:352
omnetpp::cKSplit::Iterator
Walks along cells of the distribution stored in a cKSplit object.
Definition: cksplit.h:78
omnetpp::cKSplit::Grid
Supporting struct for cKSplit. Represents one grid in the k-split data structure.
Definition: cksplit.h:54
omnetpp::cKSplit::Iterator::getCellMax
double getCellMax() const
Definition: cksplit.h:129
omnetpp::cKSplit::Grid::parent
int parent
index of parent grid
Definition: cksplit.h:56
omnetpp::cKSplit::Grid::mother
double mother
observations 'inherited' from mother cell
Definition: cksplit.h:59
omnetpp::cKSplit::Grid::total
double total
sum of cells & all subgrids (also includes 'mother')
Definition: cksplit.h:58
omnetpp::cKSplit
Implements k-split, an adaptive histogram-like density estimation algorithm. During result collection...
Definition: cksplit.h:44
omnetpp::cKSplit::Grid::reldepth
int reldepth
depth = (reldepth - rootgrid's reldepth)
Definition: cksplit.h:57
omnetpp::cPrecollectionBasedDensityEst
Base class for histogram-like density estimation classes.
Definition: cprecolldensityest.h:55
omnetpp::cStatistic
cStatistic is an abstract class for computing statistical properties of a random variable.
Definition: cstatistic.h:34
omnetpp::cKSplit::getRootGrid
Grid & getRootGrid() const
Definition: cksplit.h:357
omnetpp::cKSplit::Iterator::getCellMin
double getCellMin() const
Definition: cksplit.h:124
omnetpp::cKSplit::Iterator::getCellSize
double getCellSize() const
Definition: cksplit.h:134
omnetpp::cKSplit::Iterator::end
bool end() const
Definition: cksplit.h:114
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41