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 //  Author: Babak Fakhamzadeh, TU Delft, Mar-Jun 1996;
00011 //  Rewritten by Andras Varga
00012 //
00013 //==========================================================================
00014 
00015 /*--------------------------------------------------------------*
00016   Copyright (C) 1992-2008 Andras Varga
00017   Copyright (C) 2006-2008 OpenSim Ltd.
00018 
00019   This file is distributed WITHOUT ANY WARRANTY. See the file
00020   `license' for details on this and other legal matters.
00021 *--------------------------------------------------------------*/
00022 
00023 #ifndef __CKSPLIT_H
00024 #define __CKSPLIT_H
00025 
00026 #include "cdensityestbase.h"
00027 
00028 NAMESPACE_BEGIN
00029 
00030 
00038 class SIM_API cKSplit : public cDensityEstBase
00039 {
00040   public:
00044     enum { K = 2 };
00045 
00050     struct Grid
00051     {
00052       int parent;      
00053       int reldepth;    
00054       long total;      
00055       int mother;      
00056       int cells[K];    
00057     };
00058 
00063     typedef int (*CritFunc)(const cKSplit&, cKSplit::Grid&, int, double *);
00064 
00069     typedef double (*DivFunc)(const cKSplit&, cKSplit::Grid&, double, double *);
00070 
00074     class Iterator
00075     {
00076       private:
00077         cKSplit *ks;             // host object
00078         int cellnum;             // global index of current cell
00079         int grid, cell;          // root index in gridv[], cell index in grid.cell[]
00080         double gridmin;          // left edge of current grid
00081         double cellsize;         // cell width on current grid
00082 
00083         // internal
00084         void dive(int where);
00085 
00086       public:
00090         Iterator(const cKSplit& ksplit, bool atbeginning=true);
00091 
00095         void init(const cKSplit& ksplit, bool atbeginning=true);
00096 
00100         void operator++(int);
00101 
00105         void operator--(int);
00106 
00110         bool end() const  {return grid==0;}
00111 
00115         int getCellNumber() const  {return cellnum;}
00116 
00120         double getCellMin() const  {return gridmin+cell*cellsize;}
00121 
00125         double getCellMax() const  {return gridmin+(cell+1)*cellsize;}
00126 
00130         double getCellSize() const  {return cellsize;}
00131 
00136         double getCellValue() const;
00137     };
00138 
00139     friend class Iterator;
00140 
00141   protected:
00142     int num_cells;            // number of cells
00143 
00144     Grid *gridv;              // grid vector
00145     int gridv_size;           // size of gridv[]+1
00146     int rootgrid, lastgrid;   // indices into gridv[]
00147     bool rangeext_enabled;    // enable/disable range extension
00148 
00149     CritFunc critfunc;        // function that determines when to split a cell
00150     double *critdata;         // data array to pass to crit. function
00151 
00152     DivFunc divfunc;          // function to calc. lambda for cell division
00153     double *divdata;          // data array to pass to div. function
00154 
00155     mutable Iterator *iter;   // iterator used by getBasepoint(), getCellValue() etc.
00156     mutable long iter_num_vals; // num_vals when iterator was created
00157 
00158   private:
00159     void copy(const cKSplit& other);
00160 
00161   protected:
00162     // internal:
00163     void resetGrids(int grid);
00164 
00165     // internal:
00166     void createRootGrid();
00167 
00168     // internal:
00169     void newRootGrids(double x);
00170 
00171     // internal:
00172     void insertIntoGrids(double x, int enable_splits);
00173 
00174     // internal:
00175     void splitCell(int grid, int cell);
00176 
00177     // internal:
00178     void distributeMotherObservations(int grid);
00179 
00180     // internal:
00181     void expandGridVector();
00182 
00183     // internal: helper for getBasepoint(), getCellValue()
00184     void iteratorToCell(int cell_nr) const;
00185 
00186     // abstract method in cDensityEstBase
00187     virtual void doMergeCellValues(const cDensityEstBase *other);
00188 
00189   public:
00192 
00196     cKSplit(const cKSplit& r);
00197 
00201     explicit cKSplit(const char *name=NULL);
00202 
00206     virtual ~cKSplit();
00207 
00211     cKSplit& operator=(const cKSplit& res);
00213 
00216 
00221     virtual cKSplit *dup() const  {return new cKSplit (*this);}
00222 
00227     virtual std::string detailedInfo() const;
00228 
00234     virtual void parsimPack(cCommBuffer *buffer);
00235 
00241     virtual void parsimUnpack(cCommBuffer *buffer);
00243 
00244   protected:
00249     virtual void collectTransformed(double val);
00250 
00251   public:
00254 
00258     virtual void transform();
00259 
00263     virtual int getNumCells() const;
00264 
00268     virtual double getBasepoint(int k) const;
00269 
00273     virtual double getCellValue(int k) const;
00274 
00278     virtual double getPDF(double x) const;
00279 
00283     virtual double getCDF(double x) const;
00284 
00288     virtual void merge(const cStatistic *other);
00289 
00293     virtual double random() const;
00294 
00298     virtual void saveToFile(FILE *) const;
00299 
00303     virtual void loadFromFile(FILE *);
00305 
00308 
00313     void setCritFunc(CritFunc _critfunc, double *_critdata);
00314 
00319     void setDivFunc(DivFunc _divfunc, double *_divdata);
00320 
00329     void rangeExtension(bool enabled);
00331 
00334 
00338     int getTreeDepth() const;
00339 
00343     int getTreeDepth(Grid& grid) const;
00344 
00349     double getRealCellValue(Grid& grid, int cell) const;
00350 
00354     void printGrids() const;
00355 
00359     Grid& getGrid(int k) const {return gridv[k];}
00360 
00364     Grid& getRootGrid() const {return gridv[rootgrid];}
00366 };
00367 
00368 
00369 // cell split criteria
00370 int critfunc_const(const cKSplit&, cKSplit::Grid&, int, double *);
00371 int critfunc_depth(const cKSplit&, cKSplit::Grid&, int, double *);
00372 
00373 // cell division criteria
00374 double divfunc_const(const cKSplit&, cKSplit::Grid&, double, double *);
00375 double divfunc_babak(const cKSplit&, cKSplit::Grid&, double, double *);
00376 
00377 
00378 NAMESPACE_END
00379 
00380 
00381 #endif
00382 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3