- Main Page
- Modules
- Classes
- Files
- Directories
- Related Pages

# cksplit.h

    00001 //==========================================================================
    00002 //  CKSPLIT.H - part of
    00003 //                     OMNeT++/OMNEST
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 //
    00007 //  Declaration of the following classes:
    00008 //    cKSplit : implements the k-split algorithm in 1 dimension
    00009 //
    00010 //  Written by Babak Fakhamzadeh, TU Delft, Mar-Jun 1996
    00011 //  Rewritten by Andras Varga
    00012 //
    00013 //==========================================================================
    00014 /*--------------------------------------------------------------*
    00015   Copyright (C) 1992-2005 Andras Varga
    00016
    00017   This file is distributed WITHOUT ANY WARRANTY. See the file
    00018   `license' for details on this and other legal matters.
    00019 *--------------------------------------------------------------*/
    00020
    00021 #ifndef __CKSPLIT_H
    00022 #define __CKSPLIT_H
    00023
    00024 #include "cdensity.h"
    00025
    00026
    00027 // K: the grid size of the algorithm
    00028 #define K 2
    00029
    00030
    00038 class SIM_API cKSplit : public cDensityEstBase
    00039 {
    00040    public:
    00045     struct Grid
    00046     {
    00047       int parent;
    00048       int reldepth;
    00049       long total;
    00050       int mother;
    00051       int cells[K];
    00052     };
    00053
    00058     typedef int (*CritFunc)(const cKSplit&, cKSplit::Grid&, int, double *);
    00059
    00064     typedef double (*DivFunc)(const cKSplit&, cKSplit::Grid&, double, double *);
    00065
    00069     class Iterator
    00070     {
    00071       private:
    00072         cKSplit *ks;             // host object
    00073         int cellnum;             // global index of current cell
    00074         int grid, cell;          // root index in gridv[], cell index in grid.cell[]
    00075         double gridmin;          // left edge of current grid
    00076         double cellsize;         // cell width on current grid
    00077
    00078         // internal
    00079         void dive(int where);
    00080
    00081       public:
    00085         Iterator(const cKSplit& ksplit, bool atbeginning=true);
    00086
    00090         void init(const cKSplit& ksplit, bool atbeginning=true);
    00091
    00095         void operator++(int);
    00096
    00100         void operator--(int);
    00101
    00105         bool end() const           {return grid==0;}
    00106
    00110         int cellNumber() const     {return cellnum;}
    00111
    00115         double cellMin() const     {return gridmin+cell*cellsize;}
    00116
    00120         double cellMax() const     {return gridmin+(cell+1)*cellsize;}
    00121
    00125         double cellSize() const    {return cellsize;}
    00126
    00131         double cellValue() const;
    00132     };
    00133
    00134     friend class Iterator;
    00135
    00136   protected:
    00137     int num_cells;            // number of cells
    00138
    00139     Grid *gridv;              // grid vector
    00140     int gridv_size;           // size of gridv[]+1
    00141     int rootgrid, lastgrid;   // indices into gridv[]
    00142     bool rangeext_enabled;    // enable/disable range extension
    00143
    00144     CritFunc critfunc;        // function that determines when to split a cell
    00145     double *critdata;         // data array to pass to crit. function
    00146
    00147     DivFunc divfunc;          // function to calc. lambda for cell division
    00148     double *divdata;          // data array to pass to div. function
    00149
    00150     mutable Iterator *iter;   // iterator used by basepoint(), cell() etc.
    00151     mutable long iter_num_samples; // num_samples when iterator was created
    00152
    00153   protected:
    00154     // internal:
    00155     void resetGrids(int grid);
    00156
    00157     // internal:
    00158     void createRootGrid();
    00159
    00160     // internal:
    00161     void newRootGrids(double x);
    00162
    00163     // internal:
    00164     void insertIntoGrids(double x, int enable_splits);
    00165
    00166     // internal:
    00167     void splitCell(int grid, int cell);
    00168
    00169     // internal:
    00170     void distributeMotherObservations(int grid);
    00171
    00172     // internal:
    00173     void expandGridVector();
    00174
    00175     // internal: helper for basepoint(), cell()
    00176     void iteratorToCell(int cell_nr) const;
    00177
    00178   public:
    00181
    00185     cKSplit(const cKSplit& r);
    00186
    00190     explicit cKSplit(const char *name=NULL);
    00191
    00195     virtual ~cKSplit();
    00196
    00200     cKSplit& operator=(const cKSplit& res);
    00202
    00205
    00210     virtual cPolymorphic *dup() const   {return new cKSplit (*this);}
    00211
    00216     virtual void writeContents(std::ostream& os);
    00217
    00223     virtual void netPack(cCommBuffer *buffer);
    00224
    00230     virtual void netUnpack(cCommBuffer *buffer);
    00232
    00233   protected:
    00238     virtual void collectTransformed(double val);
    00239
    00240   public:
    00243
    00247     virtual void transform();
    00248
    00252     virtual int cells() const;
    00253
    00257     virtual double basepoint(int k) const;
    00258
    00262     virtual double cell(int k) const;
    00263
    00267     virtual double pdf(double x) const;
    00268
    00272     virtual double cdf(double x) const;
    00273
    00277     virtual double random() const;
    00278
    00282     virtual void saveToFile(FILE *) const;
    00283
    00287     virtual void loadFromFile(FILE *);
    00289
    00292
    00297     void setCritFunc(CritFunc _critfunc, double *_critdata);
    00298
    00303     void setDivFunc(DivFunc _divfunc, double *_divdata);
    00304
    00313     void rangeExtension( bool enabled );
    00315
    00318
    00322     int treeDepth() const;
    00323
    00327     int treeDepth(Grid& grid) const;
    00328
    00333     double realCellValue(Grid& grid, int cell) const;
    00334
    00338     void printGrids() const;
    00339
    00343     Grid& grid(int k) const {return gridv[k];}
    00344
    00348     Grid& rootGrid() const {return gridv[rootgrid];}
    00350 };
    00351
    00352
    00353 // cell split criteria
    00354 int critfunc_const(const cKSplit&, cKSplit::Grid&, int, double *);
    00355 int critfunc_depth(const cKSplit&, cKSplit::Grid&, int, double *);
    00356
    00357 // cell division criteria
    00358 double divfunc_const(const cKSplit&, cKSplit::Grid&, double, double *);
    00359 double divfunc_babak(const cKSplit&, cKSplit::Grid&, double, double *);
    00360
    00361
    00362 #endif
    00363