cksplit.h Source File¶

API: cksplit.h Source File
API
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
23namespace omnetpp {
24
25
45{
46 public:
47 // K is the grid size of the algorithm. It must be 2, or a >=3 odd number.
48 static const int K = 2;
49
54 struct Grid
55 {
56 int parent;
58 double total;
59 double mother;
60 double cells[K];
61 };
62
67 typedef int (*CritFunc)(const cKSplit&, cKSplit::Grid&, int, const double *);
68
73 typedef double (*DivFunc)(const cKSplit&, cKSplit::Grid&, double, const double *);
74
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 const double *critData; // data array to pass to crit. function
155
156 DivFunc divFunc; // function to calc. lambda for cell division
157 const 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
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
363int critfunc_const(const cKSplit&, cKSplit::Grid&, int, const double *);
364int critfunc_depth(const cKSplit&, cKSplit::Grid&, int, const double *);
365
366// cell division criteria
367double divfunc_const(const cKSplit&, cKSplit::Grid&, double, const double *);
368double divfunc_babak(const cKSplit&, cKSplit::Grid&, double, const double *);
369
370
371} // namespace omnetpp
372
373
374#endif
375
Buffer for the communications layer of parallel simulation.
Definition ccommbuffer.h:42
Walks along cells of the distribution stored in a cKSplit object.
Definition cksplit.h:79
void init(const cKSplit &ksplit, bool atbeginning=true)
double getCellMin() const
Definition cksplit.h:124
double getCellSize() const
Definition cksplit.h:134
double getCellValue() const
double getCellMax() const
Definition cksplit.h:129
Iterator(const cKSplit &ksplit, bool atbeginning=true)
int getCellNumber() const
Definition cksplit.h:119
bool end() const
Definition cksplit.h:114
virtual void saveToFile(FILE *) const override
void setDivFunc(DivFunc _divfunc, double *_divdata)
void setRangeExtension(bool enabled)
virtual double getBinValue(int k) const override
cKSplit & operator=(const cKSplit &res)
int getTreeDepth() const
virtual void loadFromFile(FILE *) override
void printGrids() const
Grid & getRootGrid() const
Definition cksplit.h:357
virtual void collectWeightedIntoHistogram(double value, double weight) override
virtual cKSplit * dup() const override
Definition cksplit.h:225
virtual void parsimPack(cCommBuffer *buffer) const override
cKSplit(const cKSplit &r)
double getRealCellValue(Grid &grid, int cell) const
virtual int getNumBins() const override
virtual void parsimUnpack(cCommBuffer *buffer) override
virtual void clear() override
Grid & getGrid(int k) const
Definition cksplit.h:352
cKSplit(const char *name=nullptr, bool weighted=false)
virtual double getBinEdge(int k) const override
int getTreeDepth(Grid &grid) const
virtual void collectIntoHistogram(double val) override
virtual void merge(const cStatistic *other) override
void setCritFunc(CritFunc _critfunc, double *_critdata)
virtual ~cKSplit()
virtual void setUpBins() override
cPrecollectionBasedDensityEst(const cPrecollectionBasedDensityEst &other)
Definition cprecolldensityest.h:107
cStatistic is an abstract class for computing statistical properties of a random variable.
Definition cstatistic.h:35
int(* CritFunc)(const cKSplit &, cKSplit::Grid &, int, const double *)
Definition cksplit.h:67
double(* DivFunc)(const cKSplit &, cKSplit::Grid &, double, const double *)
Definition cksplit.h:73
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:59
int parent
index of parent grid
Definition cksplit.h:56
double cells[K]
cell values (sum of weights); if negative: subgrid indices (!!!)
Definition cksplit.h:60
int reldepth
depth = (reldepth - rootgrid's reldepth)
Definition cksplit.h:57
double total
sum of cells & all subgrids (also includes 'mother')
Definition cksplit.h:58