Statistics
| Branch: | Revision:

root / include / cdetect.h @ 47c4b975

History | View | Annotate | Download (11.1 KB)

1
//=========================================================================
2
//  CDETECT.H - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//         File designed and written by the Hollandiaba Team
8
//
9
//   Class declarations:
10
//     cTransientDetection :  virtual base class for transient detection
11
//     cAccuracyDetection  :  virtual base class for result accuracy detection
12
//
13
//     cTDExpandingWindows :  an algorithm for transient detection
14
//     cADByStddev         :  an algorithm for result accuracy detection
15
//
16
//   Bugfixes: Andras Varga, Oct.1996
17
//=========================================================================
18

    
19
/*--------------------------------------------------------------*
20
  Copyright (C) 1992-2008 Andras Varga
21
  Copyright (C) 2006-2008 OpenSim Ltd.
22

23
  This file is distributed WITHOUT ANY WARRANTY. See the file
24
  `license' for details on this and other legal matters.
25
*--------------------------------------------------------------*/
26

    
27
#ifndef __CDETECT_H
28
#define __CDETECT_H
29

    
30
#include "cownedobject.h"
31
#include "cstatistic.h"
32

    
33
NAMESPACE_BEGIN
34

    
35

    
36
class cStatistic;
37

    
38
/**
39
 * Prototype for callback functions used by a cTransientDetection object.
40
 * @ingroup EnumsTypes
41
 */
42
typedef void (*PostTDFunc)(cTransientDetection *, void *);
43

    
44
/**
45
 * Prototype for callback functions used by a cAccuracyDetection object.
46
 * @ingroup EnumsTypes
47
 */
48
typedef void (*PostADFunc)(cAccuracyDetection *, void *);
49

    
50

    
51
/**
52
 * Virtual base class for transient detection classes.
53
 *
54
 * @ingroup Statistics
55
 */
56
class SIM_API cTransientDetection : public cOwnedObject, noncopyable
57
{
58
  protected:
59
    cStatistic *back;    // ptr to cStatistic that uses this object
60
    PostTDFunc pdf;      // function to call after detection
61
    void *pdfdata;       // data for PostDetectFunct
62

    
63
  public:
64
    /** @name Constructors, destructor, assignment. */
65
    //@{
66
    /**
67
     * Constructor.
68
     */
69
    explicit cTransientDetection(const char *name=NULL) : cOwnedObject(name) {}
70

    
71
    /**
72
     * Destructor.
73
     */
74
    virtual ~cTransientDetection()  {}
75

    
76
    /**
77
     * Duplication not supported, this method is redefined to throw an error.
78
     */
79
    virtual cTransientDetection *dup() const {copyNotSupported(); return NULL;}
80
    //@}
81

    
82
    /* Note: no dup() because this is an abstract class. */
83

    
84
    /** @name New methods. */
85
    //@{
86

    
87
    /**
88
     * Should be redefined to update the detection algorithm with a value.
89
     */
90
    virtual void collect(double val) = 0;
91

    
92
    /**
93
     * Should be redefined to return true if end of transient has been detected.
94
     */
95
    virtual bool detected() const = 0;
96

    
97
    /**
98
     * Should be redefined to reset detection algorithm.
99
     */
100
    virtual void reset() = 0;
101

    
102
    /**
103
     * Should be redefined to stop detection (further calls to collect()
104
     * should be ignored.)
105
     */
106
    virtual void stop() = 0;
107

    
108
    /**
109
     * Should be redefined to start detection (further calls to collect()
110
     * should update the detection algorithm.)
111
     */
112
    virtual void start() = 0;
113

    
114
    /**
115
     * Adds a function that will be called when accuracy has reached the
116
     * configured limit.
117
     */
118
    void setPostDetectFunction(PostTDFunc f, void *p) {pdf = f; pdfdata = p;}
119
    //@}
120

    
121
    /** @name Host object. */
122
    //@{
123

    
124
    /**
125
     * Sets the host object. This is internally called by cStatistic's
126
     * addTransientDetection() method.
127
     */
128
    virtual void setHostObject(cStatistic *ptr)  {back = ptr;}
129

    
130
    /**
131
     * Returns a pointer to the host object.
132
     */
133
    virtual cStatistic *getHostObject() const  {return back;}
134
    //@}
135
};
136

    
137

    
138
//----
139

    
140
/**
141
 * Virtual base class for result accuracy detection classes.
142
 *
143
 * @ingroup Statistics
144
 */
145
class SIM_API cAccuracyDetection : public cOwnedObject, noncopyable
146
{
147
  protected:
148
    cStatistic *back;           // ptr to cStatistic that uses this object
149
    PostADFunc pdf;             // function to call after detection
150
    void *pdfdata;              // data for PostDetectFunc
151

    
152
  public:
153
    /** @name Constructors, destructor, assignment. */
154
    //@{
155
    /**
156
     * Constructor.
157
     */
158
    explicit cAccuracyDetection(const char *name=NULL) : cOwnedObject(name)  {}
159

    
160
    /**
161
     * Destructor.
162
     */
163
    virtual ~cAccuracyDetection()  {}
164

    
165
    /**
166
     * Duplication not supported, this method is redefined to throw an error.
167
     */
168
    virtual cAccuracyDetection *dup() const {copyNotSupported(); return NULL;}
169
    //@}
170

    
171
    /** @name Redefined cObject member functions. */
172
    //@{
173

    
174
    /* No dup() because this is an abstract class. */
175
    //@}
176

    
177
    /** @name New methods. */
178
    //@{
179

    
180
    /**
181
     * Should be redefined to update the detection algorithm with a value.
182
     */
183
    virtual void collect(double val) = 0;
184

    
185
    /**
186
     * Should be redefined to return true if required accuracy has been reached.
187
     */
188
    virtual bool detected() const = 0;
189

    
190
    /**
191
     * Should be redefined to reset detection algorithm.
192
     */
193
    virtual void reset() = 0;
194

    
195
    /**
196
     * Should be redefined to stop detection (further calls to collect()
197
     * should be ignored).
198
     */
199
    virtual void stop() = 0;
200

    
201
    /**
202
     * Should be redefined to start detection (further calls to collect()
203
     * will update the detection algorithm).
204
     */
205
    virtual void start() = 0;
206

    
207
    /**
208
     * Adds a function that will be called when accuracy has reached the
209
     * configured limit.
210
     */
211
    void setPostDetectFunction(PostADFunc f, void *p) {pdf=f; pdfdata=p;}
212
    //@}
213

    
214
    /** @name Host object. */
215
    //@{
216

    
217
    /**
218
     * Sets the host object. This is internally called by cStatistic's
219
     * addTransientDetection() method.
220
     */
221
    virtual void setHostObject(cStatistic *ptr)  {back = ptr;}
222

    
223
    /**
224
     * Returns a pointer to the host object.
225
     */
226
    virtual cStatistic *getHostObject() const  {return back;}
227
    //@}
228
};
229

    
230
//----
231

    
232
/**
233
 * A transient detection algorithm. Uses sliding window approach
234
 * with two windows, and checks the difference of the two averages
235
 * to see if the transient period is over.
236
 *
237
 * @ingroup Statistics
238
 */
239
class SIM_API cTDExpandingWindows : public cTransientDetection
240
{
241
  private:
242
    bool go;                  // collect & detect
243
    bool transval;            // value of the last detection
244
    double accuracy;          // accuracy for detection
245
    int minwinds;             // minimum windows size
246
    double windexp;           // window expansion factor
247
    int repeats;              // repetitions necessary for detection
248
    int detreps;              // number of detections in a row
249
    int size;                 // number of collected values
250
    struct xy {double x; double y; xy *next;};
251
    xy *func;                 // structure of collected values
252

    
253
  private:
254
    // internal: computes new value of transval
255
    void detectTransient();
256

    
257
  public:
258
    /** @name Constructors, destructor, assignment. */
259
    //@{
260

    
261
    /**
262
     * Copy constructor.
263
     */
264
    cTDExpandingWindows(const cTDExpandingWindows& r);
265

    
266
    /**
267
     * Constructor.
268
     */
269
    explicit cTDExpandingWindows(const char *name=NULL,
270
                        int reps=3, int minw=4, double wind=1.3, double acc=0.3,
271
                        PostTDFunc f=NULL,void *p=NULL);
272

    
273
    /**
274
     * Destructor.
275
     */
276
    virtual ~cTDExpandingWindows();
277

    
278
    /**
279
     * Assignment operator. The name member is not copied; see cOwnedObject's
280
     * operator=() for more details.
281
     */
282
    cTDExpandingWindows& operator=(const cTDExpandingWindows& res);
283
    //@}
284

    
285
    /** @name Redefined cObject member functions. */
286
    //@{
287

    
288
    /**
289
     * Dupping is not implemented for this class. This function
290
     * gives an error (throws cRuntimeError) when called.
291
     */
292
    virtual cTDExpandingWindows *dup() const  {return new cTDExpandingWindows(*this);}
293
    //@}
294

    
295
    /** @name Redefined cTransientDetection member functions. */
296
    //@{
297

    
298
    /**
299
     * Updates the detection algorithm with a value.
300
     */
301
    virtual void collect(double val);
302

    
303
    /**
304
     * Returns true if end of transient has been detected.
305
     */
306
    virtual bool detected() const {return transval;}
307

    
308
    /**
309
     * Resets detection algorithm.
310
     */
311
    virtual void reset();
312

    
313
    /**
314
     * Stop detection; further calls to collect() will be ignored.
315
     */
316
    virtual void stop()      {go = false;}
317

    
318
    /**
319
     * Start detection; further calls to collect() will update the
320
     * detection algorithm.
321
     */
322
    virtual void start()     {go = true;}
323
    //@}
324

    
325
    /** @name Setting up the detection object. */
326
    //@{
327
    /**
328
     * Sets the parameters of the detection algorithm.
329
     */
330
    void setParameters(int reps=3, int minw=4,
331
                       double wind=1.3, double acc=0.3);
332
    //@}
333
};
334

    
335

    
336
//----
337

    
338
/**
339
 * An algorithm for result accuracy detection. The actual algorithm:
340
 * divide the standard deviation by the square of the number of values
341
 * and check if this is small enough.
342
 *
343
 * @ingroup Statistics
344
 */
345
class SIM_API cADByStddev : public cAccuracyDetection
346
{
347
  private:
348
    bool go;                    // start collecting if true
349
    bool resaccval;             // value of the last detection
350
    double accuracy;            // minimum needed for detection
351
    long int sctr;              // counter
352
    double ssum,sqrsum;         // sum, square sum;
353
    int repeats, detreps;       // repetitions necessary for detection
354

    
355
  private:
356
    // internal: compute new value of transval
357
    void detectAccuracy();
358

    
359
    // internal: compute the standard deviation
360
    double getStddev() const;
361

    
362
  public:
363
    /** @name Constructors, destructor, assignment. */
364
    //@{
365

    
366
    /**
367
     * Copy constructor.
368
     */
369
    cADByStddev(const cADByStddev& r);
370

    
371
    /**
372
     * Constructor.
373
     */
374
    explicit cADByStddev(const char *name=NULL,
375
                         double acc=0.01, int reps=3,
376
                         PostADFunc f=NULL, void *p=NULL);
377

    
378
    /**
379
     * Destructor.
380
     */
381
    virtual ~cADByStddev()  {}
382

    
383
    /**
384
     * Assignment operator. The name member is not copied;
385
     * see cOwnedObject's operator=() for more details.
386
     */
387
    cADByStddev& operator=(const cADByStddev& res);
388
    //@}
389

    
390
    /** @name Redefined cObject member functions. */
391
    //@{
392

    
393
    /**
394
     * Dupping is not implemented for this class. This function
395
     * gives an error (throws cRuntimeError) when called.
396
     */
397
    virtual cADByStddev *dup() const  {return new cADByStddev(*this);}
398
    //@}
399

    
400
    /** @name Redefined cAccuracyDetection functions. */
401
    //@{
402

    
403
    /**
404
     * Updates the detection algorithm with a value.
405
     */
406
    virtual void collect(double val);
407

    
408
    /**
409
     * Returns true if required accuracy has been reached.
410
     */
411
    virtual bool detected() const {return resaccval;}
412

    
413
    /**
414
     * Resets detection algorithm.
415
     */
416
    virtual void reset();
417

    
418
    /**
419
     * Stop detection; further calls to collect() will be ignored.
420
     */
421
    virtual void stop()   {go=false;}
422

    
423
    /**
424
     * Start detection; further calls to collect() will update the
425
     * detection algorithm.
426
     */
427
    virtual void start()  {go=true;}
428
    //@}
429

    
430
    /** @name Setting up the detection object. */
431
    //@{
432

    
433
    /**
434
     * Sets the parameters of the detection algorithm.
435
     */
436
    void setParameters(double acc=0.1, int reps=3)
437
        {accuracy=acc; repeats=detreps=reps;}
438
    //@}
439
};
440

    
441
NAMESPACE_END
442

    
443

    
444
#endif
445