OMNeT++ Simulation Library  5.6.1
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: Babak Fakhamzadeh, TU Delft, Mar-Jun 1996;
17 // Rewritten by Andras Varga
18 
19 #ifndef __OMNETPP_CKSPLIT_H
20 #define __OMNETPP_CKSPLIT_H
21 
22 #include "cprecolldensityest.h"
23 
24 namespace omnetpp {
25 
26 
45 class SIM_API cKSplit : public cPrecollectionBasedDensityEst
46 {
47  public:
48  // K is the grid size of the algorithm. It must be 2, or a >=3 odd number.
49  enum { K = 2 };
50 
55  struct Grid
56  {
57  int parent;
58  int reldepth;
59  double total;
60  double mother;
61  double cells[K];
62  };
63 
68  typedef int (*CritFunc)(const cKSplit&, cKSplit::Grid&, int, double *);
69 
74  typedef double (*DivFunc)(const cKSplit&, cKSplit::Grid&, double, double *);
75 
79  class Iterator
80  {
81  private:
82  cKSplit *ks; // host object
83  int cellnum; // global index of current cell
84  int grid, cell; // root index in gridv[], cell index in grid.cell[]
85  double gridmin; // left edge of current grid
86  double cellsize; // cell width on current grid
87 
88  // internal
89  void dive(int where);
90 
91  public:
95  Iterator(const cKSplit& ksplit, bool atbeginning=true);
96 
100  void init(const cKSplit& ksplit, bool atbeginning=true);
101 
105  void operator++(int);
106 
110  void operator--(int);
111 
115  bool end() const {return grid==0;}
116 
120  int getCellNumber() const {return cellnum;}
121 
125  double getCellMin() const {return gridmin+cell*cellsize;}
126 
130  double getCellMax() const {return gridmin+(cell+1)*cellsize;}
131 
135  double getCellSize() const {return cellsize;}
136 
141  double getCellValue() const;
142  };
143 
144  friend class Iterator;
145 
146  protected:
147  int numCells; // number of cells
148 
149  Grid *gridv; // grid vector
150  int gridvSize; // size of gridv[]+1
151  int rootGridIndex, lastGridIndex; // indices into gridv[]
152  bool rangeExtEnabled; // enable/disable range extension
153 
154  CritFunc critFunc; // function that determines when to split a cell
155  double *critData; // data array to pass to crit. function
156 
157  DivFunc divFunc; // function to calc. lambda for cell division
158  double *divData; // data array to pass to div. function
159 
160  mutable Iterator *iter; // iterator used by getBinEdge(), getBinValue() etc.
161  mutable long iterNumValues; // numValues when iterator was created
162 
163  private:
164  void copy(const cKSplit& other);
165 
166  protected:
167  // internal:
168  void resetGrids(int grid);
169 
170  // internal:
171  void createRootGrid();
172 
173  // internal:
174  void newRootGrids(double value, double weight);
175 
176  // internal:
177  void insertIntoGrids(double value, double weight, int enableSplits);
178 
179  // internal:
180  void splitCell(int grid, int cell);
181 
182  // internal:
183  void distributeMotherObservations(int grid);
184 
185  // internal:
186  void expandGridVector();
187 
188  // internal: helper for getBinEdge(), getBinValue()
189  void iteratorToCell(int cell_nr) const;
190 
191  // abstract method in cPrecollectionBasedDensityEst
192  virtual void doMergeBinValues(const cPrecollectionBasedDensityEst *other) override;
193 
194  public:
197 
201  explicit cKSplit(const char *name=nullptr, bool weighted=false);
202 
206  cKSplit(const cKSplit& r);
207 
211  virtual ~cKSplit();
212 
216  cKSplit& operator=(const cKSplit& res);
218 
221 
226  virtual cKSplit *dup() const override {return new cKSplit (*this);}
227 
233  virtual void parsimPack(cCommBuffer *buffer) const override;
234 
240  virtual void parsimUnpack(cCommBuffer *buffer) override;
242 
243  protected:
248  virtual void collectIntoHistogram(double val) override;
249 
253  virtual void collectWeightedIntoHistogram(double value, double weight) override;
254 
255  public:
258 
262  virtual void setUpBins() override;
263 
267  virtual int getNumBins() const override;
268 
272  virtual double getBinEdge(int k) const override;
273 
277  virtual double getBinValue(int k) const override;
278 
282  virtual void merge(const cStatistic *other) override;
283 
287  virtual void clear() override;
288 
292  virtual void saveToFile(FILE *) const override;
293 
297  virtual void loadFromFile(FILE *) override;
299 
302 
307  void setCritFunc(CritFunc _critfunc, double *_critdata);
308 
313  void setDivFunc(DivFunc _divfunc, double *_divdata);
314 
323  void setRangeExtension(bool enabled);
325 
328 
332  int getTreeDepth() const;
333 
337  int getTreeDepth(Grid& grid) const;
338 
343  double getRealCellValue(Grid& grid, int cell) const;
344 
348  void printGrids() const;
349 
353  Grid& getGrid(int k) const {return gridv[k];}
354 
358  Grid& getRootGrid() const {return gridv[rootGridIndex];}
360 };
361 
362 
363 // cell split criteria
364 int critfunc_const(const cKSplit&, cKSplit::Grid&, int, double *);
365 int critfunc_depth(const cKSplit&, cKSplit::Grid&, int, double *);
366 
367 // cell division criteria
368 double divfunc_const(const cKSplit&, cKSplit::Grid&, double, double *);
369 double divfunc_babak(const cKSplit&, cKSplit::Grid&, double, double *);
370 
371 
372 } // namespace omnetpp
373 
374 
375 #endif
376 
Grid & getRootGrid() const
Definition: cksplit.h:358
Supporting struct for cKSplit. Represents one grid in the k-split data structure. ...
Definition: cksplit.h:55
double mother
observations 'inherited' from mother cell
Definition: cksplit.h:60
double total
sum of cells & all subgrids (also includes 'mother')
Definition: cksplit.h:59
bool end() const
Definition: cksplit.h:115
double getCellMin() const
Definition: cksplit.h:125
int parent
index of parent grid
Definition: cksplit.h:57
virtual cKSplit * dup() const override
Definition: cksplit.h:226
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
int getCellNumber() const
Definition: cksplit.h:120
int reldepth
depth = (reldepth - rootgrid's reldepth)
Definition: cksplit.h:58
double getCellMax() const
Definition: cksplit.h:130
Definition: cabstracthistogram.h:21
Walks along cells of the distribution stored in a cKSplit object.
Definition: cksplit.h:79
Base class for histogram-like density estimation classes.
Definition: cprecolldensityest.h:55
Grid & getGrid(int k) const
Definition: cksplit.h:353
Implements k-split, an adaptive histogram-like density estimation algorithm. During result collection...
Definition: cksplit.h:45
cStatistic is an abstract class for computing statistical properties of a random variable.
Definition: cstatistic.h:34
double getCellSize() const
Definition: cksplit.h:135