Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PausableBasicAlgorithm/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.0.7/SimSharp-3.0.7/Analysis/ContinuousStatistics.cs @ 14207

Last change on this file since 14207 was 12657, checked in by abeham, 9 years ago

#2420: Added Sim# as an ExtLib plugin

File size: 2.4 KB
Line 
1#region License Information
2/* SimSharp - A .NET port of SimPy, discrete event simulation framework
3Copyright (C) 2014  Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
17#endregion
18
19using System;
20
21namespace SimSharp {
22  public sealed class ContinuousStatistics {
23    private readonly Environment env;
24
25    public int Count { get; private set; }
26    public double TotalTimeD { get; private set; }
27    public TimeSpan TotalTime { get { return env.ToTimeSpan(TotalTimeD); } }
28
29    public double Min { get; private set; }
30    public double Max { get; private set; }
31    public double Area { get; private set; }
32    public double Mean { get; private set; }
33    public double StdDev { get { return Math.Sqrt(Variance); } }
34    public double Variance { get { return (TotalTimeD > 0) ? variance / TotalTimeD : 0.0; } }
35
36    private double lastUpdateTime;
37    private double lastValue;
38    private double variance;
39
40    private bool firstSample;
41
42
43    public ContinuousStatistics(Environment env) {
44      this.env = env;
45      lastUpdateTime = env.NowD;
46    }
47
48    public void Update(double value) {
49      Count++;
50
51      if (!firstSample) {
52        Min = Max = Mean = value;
53        firstSample = true;
54      } else {
55        if (value < Min) Min = value;
56        if (value > Max) Max = value;
57
58        var duration = env.NowD - lastUpdateTime;
59        if (duration > 0) {
60          Area += (lastValue * duration);
61          var oldMean = Mean;
62          Mean = oldMean + (lastValue - oldMean) * duration / (duration + TotalTimeD);
63          variance = variance + (lastValue - oldMean) * (lastValue - Mean) * duration;
64          TotalTimeD += duration;
65        }
66      }
67
68      lastUpdateTime = env.NowD;
69      lastValue = value;
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.