Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

cksplit.h

00001 //==========================================================================
00002 //   CKSPLIT.H - header for
00003 //                             OMNeT++
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-2001 Andras Varga
00016   Technical University of Budapest, Dept. of Telecommunications,
00017   Stoczek u.2, H-1111 Budapest, Hungary.
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 <iostream.h>
00027 #include "cdensity.h"
00028 
00029 // classes declared
00030 class cKSplit;
00031 struct sGrid;
00032 class cKSplitIterator;
00033 
00034 // K: the grid size of the algorithm
00035 #define K 2
00036 
00037 
00042 typedef int (*KSplitCritFunc)(const cKSplit&, sGrid&, int, double *);
00043 
00048 typedef double (*KSplitDivFunc)(const cKSplit&, sGrid&, double, double *);
00049 
00050 // cell split criteria
00051 int critfunc_const(const cKSplit&, sGrid&, int, double *);
00052 int critfunc_depth(const cKSplit&, sGrid&, int, double *);
00053 
00054 // cell division criteria
00055 double divfunc_const(const cKSplit&, sGrid&, double, double *);
00056 double divfunc_babak(const cKSplit&, sGrid&, double, double *);
00057 
00058 //==========================================================================
00059 
00064 struct sGrid   // FIXME: to be renamed to sKSplitGrid! make internal class to cKSplit?
00065 {
00066   int parent;      
00067   int reldepth;    
00068   long total;      
00069   int mother;      
00070   int cells[K];    
00071 };
00072 
00073 //==========================================================================
00074 
00082 class SIM_API cKSplit : public cDensityEstBase
00083 {
00084     friend class cKSplitIterator;
00085 
00086   protected:
00087     int num_cells;            // number of cells
00088 
00089     sGrid *gridv;             // grid vector
00090     int gridv_size;           // size of gridv[]+1
00091     int rootgrid, lastgrid;   // indices into gridv[]
00092     bool rangeext_enabled;    // enable/disable range extension
00093 
00094     KSplitCritFunc critfunc;  // function that determines when to split a cell
00095     double *critdata;         // data array to pass to crit. function
00096 
00097     KSplitDivFunc divfunc;    // function to calc. lambda for cell division
00098     double *divdata;          // data array to pass to div. function
00099 
00100     mutable cKSplitIterator *iter; // iterator used by basepoint(), cell() etc.
00101     mutable long iter_num_samples; // num_samples when iterator was created
00102 
00103   protected:
00104     // internal:
00105     void resetGrids(int grid);
00106 
00107     // internal:
00108     void createRootGrid();
00109 
00110     // internal:
00111     void newRootGrids(double x);
00112 
00113     // internal:
00114     void insertIntoGrids(double x, int enable_splits);
00115 
00116     // internal:
00117     void splitCell(int grid, int cell);
00118 
00119     // internal:
00120     void distributeMotherObservations(int grid);
00121 
00122     // internal:
00123     void expandGridVector();
00124 
00125     // internal: helper for basepoint(), cell()
00126     void iteratorToCell(int cell_nr) const;
00127 
00128   public:
00131 
00135     cKSplit(const cKSplit& r);
00136 
00140     explicit cKSplit(const char *name=NULL);
00141 
00145     virtual ~cKSplit();
00146 
00150     cKSplit& operator=(const cKSplit& res);
00152 
00155 
00159     virtual const char *className() const {return "cKSplit";}
00160 
00165     virtual cObject *dup() const   {return new cKSplit (*this);}
00166 
00171     virtual void writeContents(ostream& os);
00172 
00178     virtual int netPack();
00179 
00185     virtual int netUnpack();
00187 
00188   protected:
00193     virtual void collectTransformed(double val);
00194 
00195   public:
00198 
00202     virtual void transform();
00203 
00207     virtual int cells() const;
00208 
00212     virtual double basepoint(int k) const;
00213 
00217     virtual double cell(int k) const;
00218 
00222     virtual double pdf(double x) const;
00223 
00227     virtual double cdf(double x) const;
00228 
00232     virtual double random() const;
00233 
00237     virtual void saveToFile(FILE *) const;
00238 
00242     virtual void loadFromFile(FILE *);
00244 
00247 
00252     void setCritFunc(KSplitCritFunc _critfunc, double *_critdata);
00253 
00258     void setDivFunc(KSplitDivFunc _divfunc, double *_divdata);
00259 
00268     void rangeExtension( bool enabled );
00270 
00273 
00277     int treeDepth() const;
00278 
00282     int treeDepth(sGrid& grid) const;
00283 
00288     double realCellValue(sGrid& grid, int cell) const;
00289 
00293     void printGrids() const;
00294 
00298     sGrid& grid(int k) const {return gridv[k];}
00299 
00303     sGrid& rootGrid() const {return gridv[rootgrid];}
00305 };
00306 
00307 
00311 class SIM_API cKSplitIterator
00312 {
00313   private:
00314     cKSplit *ks;             // host object
00315     int cellnum;             // global index of current cell
00316     int grid, cell;          // root index in gridv[], cell index in grid.cell[]
00317     double gridmin;          // left edge of current grid
00318     double cellsize;         // cell width on current grid
00319 
00320     // internal
00321     void dive(int where);
00322 
00323   public:
00327     cKSplitIterator(const cKSplit& _ks, int _beg=1);
00328 
00332     void init(const cKSplit& _ks, int _beg=1);
00333 
00337     void operator++(int);
00338 
00342     void operator--(int);
00343 
00347     bool end() const           {return grid==0;}
00348 
00352     int cellNumber() const     {return cellnum;}
00353 
00357     double cellMin() const     {return gridmin+cell*cellsize;}
00358 
00362     double cellMax() const     {return gridmin+(cell+1)*cellsize;}
00363 
00367     double cellSize() const    {return cellsize;}
00368 
00373     double cellValue() const;
00374 };
00375 
00376 #endif
00377 

Generated at Sat May 4 15:45:48 2002 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001