Main Page Modules Class Hierarchy Compound List File List Compound Members

# cstat.h

    00001 //==========================================================================
    00002 //   CSTAT.H - header for
    00003 //                             OMNeT++
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 //
    00007 //  Declaration of the following classes:
    00008 //    cStatistic : base for statistics
    00009 //    cStdDev    : collects min, max, mean, standard deviation
    00010 //
    00011 //==========================================================================
    00012
    00013 /*--------------------------------------------------------------*
    00014   Copyright (C) 1992-2003 Andras Varga
    00015
    00016   This file is distributed WITHOUT ANY WARRANTY. See the file
    00017   `license' for details on this and other legal matters.
    00018 *--------------------------------------------------------------*/
    00019
    00020 #ifndef __CSTAT_H
    00021 #define __CSTAT_H
    00022
    00023 #include "defs.h"
    00024
    00025 #include <stdio.h>
    00026 #include "cobject.h"
    00027
    00028 class cTransientDetection;
    00029 class cAccuracyDetection;
    00030
    00031 //==========================================================================
    00032
    00043 class SIM_API cStatistic : public cObject
    00044 {
    00045   public:
    00046     cTransientDetection *td;    // ptr to associated object
    00047     cAccuracyDetection *ra;     // ptr to associated object
    00048     int genk;                   // index of random number generator to use
    00049
    00050   protected:
    00051
    00052     // internal: utility function for implementing loadFromFile() functions
    00053     void freadvarsf (FILE *f,  const char *fmt, ...);
    00054
    00055   public:
    00058
    00062     cStatistic(const cStatistic& r);
    00063
    00067     explicit cStatistic(const char *name=NULL);
    00068
    00072     virtual ~cStatistic()  {}
    00073
    00078     cStatistic& operator=(const cStatistic& res);
    00080
    00083
    00084     /* Note: No dup() because this is an abstract class! */
    00085
    00091     virtual int netPack();
    00092
    00098     virtual int netUnpack();
    00100
    00103
    00108     virtual void collect(double val) = 0;
    00109
    00113     virtual void collect2(double val, double weight);
    00114
    00118     void operator+= (double val) {collect(val);}
    00119
    00125     virtual void clearResult() = 0;
    00127
    00130
    00135     virtual long samples() const = 0;
    00136
    00141     virtual double weights() const = 0;
    00142
    00147     virtual double sum() const = 0;
    00148
    00153     virtual double sqrSum() const = 0;
    00154
    00159     virtual double min() const = 0;
    00160
    00165     virtual double max() const = 0;
    00166
    00171     virtual double mean() const = 0;
    00172
    00177     virtual double stddev() const = 0;
    00178
    00183     virtual double variance() const = 0;
    00185
    00188
    00193     void addTransientDetection(cTransientDetection *object);
    00194
    00199     void addAccuracyDetection(cAccuracyDetection *object);
    00200
    00204     cTransientDetection *transientDetectionObject() const  {return td;}
    00205
    00209     cAccuracyDetection  *accuracyDetectionObject() const   {return ra;}
    00211
    00214
    00220     void setGenK(int gen_nr)   {genk=gen_nr;}
    00221
    00226     virtual double random() const = 0;
    00228
    00231
    00236     virtual void saveToFile(FILE *) const = 0;
    00237
    00242     virtual void loadFromFile(FILE *) = 0;
    00244 };
    00245
    00246 //==========================================================================
    00247
    00253 class SIM_API cStdDev : public cStatistic
    00254 {
    00255   protected:
    00256     long num_samples;
    00257     double min_samples,max_samples;
    00258     double sum_samples,sqrsum_samples;
    00259
    00260   public:
    00263
    00267     cStdDev(const cStdDev& r) : cStatistic(r) {setName(r.name());operator=(r);}
    00268
    00272     explicit cStdDev(const char *name=NULL);
    00273
    00277     virtual ~cStdDev() {}
    00278
    00282     cStdDev& operator=(const cStdDev& res);
    00284
    00287
    00292     virtual cObject *dup() const {return new cStdDev(*this);}
    00293
    00298     virtual void info(char *buf);
    00299
    00304     virtual void writeContents(ostream& os);
    00305
    00311     virtual int netPack();
    00312
    00318     virtual int netUnpack();
    00320
    00323
    00327     virtual void collect(double val);
    00328
    00332     virtual long samples() const   {return num_samples;}
    00333
    00337     virtual double weights() const {return num_samples;}
    00338
    00342     virtual double sum() const     {return sum_samples;}
    00343
    00347     virtual double sqrSum() const  {return sqrsum_samples;}
    00348
    00352     virtual double min() const     {return min_samples;}
    00353
    00357     virtual double max() const     {return max_samples;}
    00358
    00362     virtual double mean() const    {return num_samples ? sum_samples/num_samples : 0.0;}
    00363
    00367     virtual double stddev() const;
    00368
    00372     virtual double variance() const;
    00373
    00378     virtual double random() const;
    00379
    00383     virtual void clearResult();
    00384
    00388     virtual void saveToFile(FILE *) const;
    00389
    00394     virtual void loadFromFile(FILE *);
    00396 };
    00397
    00398 //==========================================================================
    00399
    00406 class SIM_API cWeightedStdDev : public cStdDev
    00407 {
    00408   protected:
    00409     double sum_weights;
    00410
    00411   public:
    00414
    00418     cWeightedStdDev(const cWeightedStdDev& r) : cStdDev(r) {setName(r.name());operator=(r);}
    00419
    00423     explicit cWeightedStdDev(const char *name=NULL) : cStdDev(name)  {sum_weights=0;}
    00424
    00428     virtual ~cWeightedStdDev() {}
    00429
    00433     cWeightedStdDev& operator=(const cWeightedStdDev& res);
    00435
    00438
    00443     virtual cObject *dup() const {return new cWeightedStdDev(*this);}
    00444
    00450     virtual int netPack();
    00451
    00457     virtual int netUnpack();
    00459
    00462
    00466     virtual void collect(double val)  {collect2(val,1.0);}
    00467
    00471     virtual void collect2(double val, double weight);
    00472
    00476     virtual void clearResult();
    00477
    00481     virtual double weights() const {return sum_weights;}
    00482
    00486     virtual double mean() const    {return sum_weights!=0 ? sum_samples/sum_weights : 0.0;}
    00487
    00491     virtual double variance() const;
    00492
    00496     virtual void saveToFile(FILE *) const;
    00497
    00501     virtual void loadFromFile(FILE *);
    00503 };
    00504
    00505 #endif
    00506