OMNeT++ API 6.1
Discrete Event Simulation Library
chistogramstrategy.h
1 //==========================================================================
2 // CHISTOGRAMSTRATEGY.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CHISTOGRAMSTRATEGY_H
17 #define __OMNETPP_CHISTOGRAMSTRATEGY_H
18 
19 #include <omnetpp/chistogram.h>
20 
21 namespace omnetpp {
22 
29 class SIM_API cIHistogramStrategy : public cObject
30 {
31  friend class cHistogram;
32  public:
33  typedef enum cHistogram::Mode Mode;
34 
35  protected:
36  cHistogram *hist = nullptr; // backreference to "owner"
37 
38  public:
43  cIHistogramStrategy& operator=(const cIHistogramStrategy& other) {hist=nullptr; return *this;}
45 
52  void setHistogram(cHistogram *hist);
53 
57  cHistogram *getHistogram() const {return hist;}
59 
67  virtual void collect(double value) = 0;
68 
74  virtual void collectWeighted(double value, double weight) = 0;
75 
82  virtual void setUpBins() = 0;
83 
88  virtual void clear() = 0;
90 };
91 
106 {
107  protected:
108  double lo = NAN;
109  double hi = NAN;
110  int numBins = -1;
111  Mode mode = cHistogram::MODE_REALS; // may not be AUTO
112 
113  private:
114  void copy(const cFixedRangeHistogramStrategy& other);
115 
116  public:
119  cFixedRangeHistogramStrategy() {} // must be configured using setter methods
120  cFixedRangeHistogramStrategy(double lo, double hi, int numBins, Mode mode=cHistogram::MODE_REALS) :
121  lo(lo), hi(hi), numBins(numBins), mode(mode) {}
124  virtual cFixedRangeHistogramStrategy *dup() const override {return new cFixedRangeHistogramStrategy(*this);}
126 
129  void setRange(double lo, double hi) {this->lo = lo; this->hi = hi;}
130  double getUpperLimit() const {return hi;}
131  void setUpperLimit(double hi) {this->hi = hi;}
132  double getLowerLimit() const {return lo;}
133  void setLowerLimit(double lo) {this->lo = lo;}
134  Mode getMode() const {return mode;}
135  void setMode(Mode mode) {this->mode = mode;}
136  double getBinSize() const {return numBins;}
137  void setBinSize(double binSize) {this->numBins = binSize;}
139 
142  virtual void collect(double value) override;
143  virtual void collectWeighted(double value, double weight) override;
144  virtual void setUpBins() override;
145  virtual void clear() override {}
147 };
148 
158 {
159  protected:
160  bool inPrecollection = true;
161  size_t numToPrecollect = 1000;
162  size_t numToCollate = 10;
163  double lastRange = NAN;
164  int rangeUnchangedCounter = 0;
165  int rangeUnchangedThreshold = 50;
166  double finiteMinValue = NAN, finiteMaxValue = NAN; // have to keep track separate from cStdDev, ignoring infs
167  std::vector<double> values;
168  std::vector<double> weights; // same size as values[]
169 
170  protected:
171  virtual void moveValuesIntoHistogram();
172  virtual bool precollect(double value, double weight=1.0); // true: precollection over
173  virtual void createBins() = 0;
174 
175  private:
176  void copy(const cPrecollectionBasedHistogramStrategy& other);
177 
178  public:
185 
188  int getNumToPrecollect() const {return numToPrecollect;}
189  void setNumToPrecollect(int numToPrecollect) {this->numToPrecollect = numToPrecollect;}
190  int getNumToCollate() const {return numToCollate;}
191  void setNumToCollate(int numToCollate) {this->numToCollate = numToCollate;}
192  int getRangeUnchangedThreshold() const {return rangeUnchangedThreshold;}
193  void setRangeUnchangedThreshold(int threshold) {this->rangeUnchangedThreshold = threshold;}
195 
198  virtual void setUpBins() override;
199  virtual void clear() override;
201 };
202 
223 {
224  private:
225  static const int DEFAULT_NUM_BINS = 60; // a number with many divisors
226  double rangeExtensionFactor = 1.5;
227  double binSize = NAN;
228  int numBinsHint = -1;
229  int targetNumBins = DEFAULT_NUM_BINS;
230  Mode mode = cHistogram::MODE_AUTO;
231  bool autoExtend = true;
232  bool binMerging = true;
233  int maxNumBins = 1000;
234 
235  protected:
236  virtual void createBins() override;
237  virtual void extendBinsTo(double value);
238  virtual void reduceNumBinsTo(int numBins);
239  virtual void mergeAllBinsIntoOne(double newApproxBinSize);
240 
241  private:
242  void copy(const cDefaultHistogramStrategy& other);
243 
244  public:
247  explicit cDefaultHistogramStrategy(int numBinsHint=-1, Mode mode=cHistogram::MODE_AUTO) : numBinsHint(numBinsHint), mode(mode) {}
249  cDefaultHistogramStrategy& operator=(const cDefaultHistogramStrategy& other);
250  virtual cDefaultHistogramStrategy *dup() const override {return new cDefaultHistogramStrategy(*this);}
252 
255  int getNumBinsHint() const {return numBinsHint;}
256  void setNumBinsHint(int numBins) {this->numBinsHint = numBins;}
258 
261  virtual void collect(double value) override;
262  virtual void collectWeighted(double value, double weight) override;
265 };
266 
305 {
306  private:
307  static const int DEFAULT_NUM_BINS = 60; // a number with many divisors
308  double lo = NAN; // range lower limit; use NaN for unspecified
309  double hi = NAN; // range upper limit; use NaN for unspecified
310  double rangeExtensionFactor = 1.5;
311  int numBinsHint = -1;
312  int targetNumBins = DEFAULT_NUM_BINS;
313  double requestedBinSize = NAN; // user-given
314  double binSize = NAN; // actual (computed)
315  Mode mode = cHistogram::MODE_AUTO;
316  bool binSizeRounding = true;
317  bool autoExtend = true;
318  bool binMerging = true;
319  int maxNumBins = 1000;
320 
321  protected:
322  virtual void createBins() override;
323  virtual void extendBinsTo(double value);
324  virtual void reduceNumBinsTo(int numBins);
325  virtual void mergeAllBinsIntoOne(double newApproxBinSize);
326 
327  private:
328  void copy(const cAutoRangeHistogramStrategy& other);
329 
330  public:
333  explicit cAutoRangeHistogramStrategy(Mode mode=cHistogram::MODE_AUTO) : mode(mode) {}
334  explicit cAutoRangeHistogramStrategy(int numBins, Mode mode=cHistogram::MODE_AUTO) : numBinsHint(numBins), mode(mode) {}
337  virtual cAutoRangeHistogramStrategy *dup() const override {return new cAutoRangeHistogramStrategy(*this);}
339 
342  void setRangeHint(double lo, double hi) {this->lo = lo; this->hi = hi;}
343  double getUpperLimitHint() const {return hi;}
344  void setUpperLimitHint(double hi) {this->hi = hi;}
345  double getLowerLimitHint() const {return lo;}
346  void setLowerLimitHint(double lo) {this->lo = lo;}
347  double getRangeExtensionFactor() const {return rangeExtensionFactor;}
348  void setRangeExtensionFactor(double rangeExtensionFactor) {this->rangeExtensionFactor = rangeExtensionFactor;}
349  Mode getMode() const {return mode;}
350  void setMode(Mode mode) {this->mode = mode;}
351  double getBinSizeHint() const {return requestedBinSize;}
352  void setBinSizeHint(double binSize) {this->requestedBinSize = binSize;}
353  bool getBinSizeRounding() const {return binSizeRounding;}
354  void setBinSizeRounding(bool binSizeRounding) {this->binSizeRounding = binSizeRounding;}
355  int getNumBinsHint() const {return numBinsHint;}
356  void setNumBinsHint(int numBins) {this->numBinsHint = numBins;}
357  void setAutoExtend(bool enable) {this->autoExtend = enable;}
358  bool getAutoExtend() const {return autoExtend;}
359  void setBinMerging(bool enable) {this->binMerging = enable;}
360  bool getBinMerging() const {return binMerging;}
361  int getMaxNumBins() const {return maxNumBins;}
362  void setMaxNumBins(int numBins) {this->maxNumBins = numBins;}
364 
367  virtual void collect(double value) override;
368  virtual void collectWeighted(double value, double weight) override;
369  virtual void clear() override;
371 };
372 
373 } // namespace omnetpp
374 
375 
376 #endif
377 
378 
379 
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cAutoRangeHistogramStrategy::dup
virtual cAutoRangeHistogramStrategy * dup() const override
Definition: chistogramstrategy.h:337
omnetpp::cPrecollectionBasedHistogramStrategy
Base class for histogram strategies that employ a precollection phase in order to gather input for se...
Definition: chistogramstrategy.h:157
omnetpp::cFixedRangeHistogramStrategy
Histogram strategy that sets up uniform bins over a predetermined interval. The number of bins and th...
Definition: chistogramstrategy.h:105
omnetpp::cDefaultHistogramStrategy::clear
virtual void clear() override
Definition: chistogramstrategy.h:263
omnetpp::cAutoRangeHistogramStrategy
A generic, very configurable histogram strategy that is meant to provide a good quality histogram for...
Definition: chistogramstrategy.h:304
omnetpp::cDefaultHistogramStrategy::dup
virtual cDefaultHistogramStrategy * dup() const override
Definition: chistogramstrategy.h:250
omnetpp::cIHistogramStrategy::getHistogram
cHistogram * getHistogram() const
Definition: chistogramstrategy.h:57
omnetpp::cFixedRangeHistogramStrategy::dup
virtual cFixedRangeHistogramStrategy * dup() const override
Definition: chistogramstrategy.h:124
omnetpp::cHistogram
Generic histogram class, capable of representing both unweighted and weighted distributions....
Definition: chistogram.h:88
omnetpp::cIHistogramStrategy
Interface for histogram strategy classes. Histogram strategies encapsulate the task of setting up and...
Definition: chistogramstrategy.h:29
omnetpp::cFixedRangeHistogramStrategy::clear
virtual void clear() override
Definition: chistogramstrategy.h:145
omnetpp::cDefaultHistogramStrategy
A strategy class used by the default setup of cHistogram. It is meant to provide a good quality unifo...
Definition: chistogramstrategy.h:222
omnetpp::cPrecollectionBasedHistogramStrategy::clear
virtual void clear() override
omnetpp::cHistogram::Mode
Mode
Definition: chistogram.h:95