# cstat.h

    00001 //==========================================================================
    00002 //  CSTAT.H - part of
    00003 //                     OMNeT++/OMNEST
    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-2005 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 void netPack(cCommBuffer *buffer);
    00092
    00098     virtual void netUnpack(cCommBuffer *buffer);
    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;
    00243
    00253     virtual void recordScalar(const char *name=NULL);
    00255 };
    00256
    00257 //==========================================================================
    00258
    00264 class SIM_API cStdDev : public cStatistic
    00265 {
    00266   protected:
    00267     long num_samples;
    00268     double min_samples,max_samples;
    00269     double sum_samples,sqrsum_samples;
    00270
    00271   public:
    00274
    00278     cStdDev(const cStdDev& r) : cStatistic() {setName(r.name());operator=(r);}
    00279
    00283     explicit cStdDev(const char *name=NULL);
    00284
    00288     virtual ~cStdDev() {}
    00289
    00293     cStdDev& operator=(const cStdDev& res);
    00295
    00298
    00303     virtual cPolymorphic *dup() const {return new cStdDev(*this);}
    00304
    00309     virtual std::string info() const;
    00310
    00315     virtual void writeContents(std::ostream& os);
    00316
    00322     virtual void netPack(cCommBuffer *buffer);
    00323
    00329     virtual void netUnpack(cCommBuffer *buffer);
    00331
    00334
    00338     virtual void collect(double val);
    00339
    00343     virtual long samples() const   {return num_samples;}
    00344
    00348     virtual double weights() const {return num_samples;}
    00349
    00353     virtual double sum() const     {return sum_samples;}
    00354
    00358     virtual double sqrSum() const  {return sqrsum_samples;}
    00359
    00363     virtual double min() const     {return min_samples;}
    00364
    00368     virtual double max() const     {return max_samples;}
    00369
    00373     virtual double mean() const    {return num_samples ? sum_samples/num_samples : 0.0;}
    00374
    00378     virtual double stddev() const;
    00379
    00383     virtual double variance() const;
    00384
    00389     virtual double random() const;
    00390
    00394     virtual void clearResult();
    00395
    00399     virtual void saveToFile(FILE *) const;
    00400
    00405     virtual void loadFromFile(FILE *);
    00407 };
    00408
    00409 //==========================================================================
    00410
    00417 class SIM_API cWeightedStdDev : public cStdDev
    00418 {
    00419   protected:
    00420     double sum_weights;
    00421
    00422   public:
    00425
    00429     cWeightedStdDev(const cWeightedStdDev& r) : cStdDev() {setName(r.name());operator=(r);}
    00430
    00434     explicit cWeightedStdDev(const char *name=NULL) : cStdDev(name)  {sum_weights=0;}
    00435
    00439     virtual ~cWeightedStdDev() {}
    00440
    00444     cWeightedStdDev& operator=(const cWeightedStdDev& res);
    00446
    00449
    00454     virtual cPolymorphic *dup() const {return new cWeightedStdDev(*this);}
    00455
    00461     virtual void netPack(cCommBuffer *buffer);
    00462
    00468     virtual void netUnpack(cCommBuffer *buffer);
    00470
    00473
    00477     virtual void collect(double val)  {collect2(val,1.0);}
    00478
    00482     virtual void collect2(double val, double weight);
    00483
    00487     virtual void clearResult();
    00488
    00492     virtual double weights() const {return sum_weights;}
    00493
    00497     virtual double mean() const    {return sum_weights!=0 ? sum_samples/sum_weights : 0.0;}
    00498
    00502     virtual double variance() const;
    00503
    00507     virtual void saveToFile(FILE *) const;
    00508
    00512     virtual void loadFromFile(FILE *);
    00514 };
    00515
    00516 #endif
    00517