1
2
3
4 package org.opentrafficsim.water.statistics;
5
6 import org.apache.poi.ss.usermodel.CellStyle;
7 import org.apache.poi.ss.usermodel.DataFormat;
8 import org.apache.poi.ss.usermodel.Row;
9 import org.apache.poi.ss.usermodel.Sheet;
10
11 import com.thoughtworks.xstream.annotations.XStreamAlias;
12 import com.thoughtworks.xstream.annotations.XStreamOmitField;
13
14 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
15
16
17
18
19
20
21
22
23
24
25
26
27 @XStreamAlias("persistent")
28 public class XPersistent extends XTally
29 {
30
31 @XStreamOmitField
32 private static final long serialVersionUID = 1L;
33
34
35 @XStreamOmitField
36 private DEVSSimulatorInterface.TimeDoubleUnit simulator;
37
38
39 @XStreamOmitField
40 private double startTime = Double.NaN;
41
42
43 private double elapsedTime = Double.NaN;
44
45
46 @XStreamOmitField
47 private double lastValue = Double.NaN;
48
49
50
51
52
53 public XPersistent(String description, DEVSSimulatorInterface.TimeDoubleUnit simulator)
54 {
55 super(description);
56 this.simulator = simulator;
57 }
58
59
60 @Override
61 public double getSampleMean()
62 {
63 if (super.n > 1 && this.elapsedTime > 0)
64 return super.sum / elapsedTime;
65 else
66 return Double.NaN;
67 }
68
69
70 @Override
71 public double getStdDev()
72 {
73 synchronized (this.semaphore)
74 {
75 if (super.n > 1)
76 {
77 return Math.sqrt(super.varianceSum / this.elapsedTime);
78 }
79 return Double.NaN;
80 }
81 }
82
83
84 @Override
85 public double getSampleVariance()
86 {
87 synchronized (this.semaphore)
88 {
89 if (super.n > 1)
90 {
91 return super.varianceSum / this.elapsedTime;
92 }
93 return Double.NaN;
94 }
95 }
96
97
98 @Override
99 public void initialize()
100 {
101 synchronized (this.semaphore)
102 {
103 super.initialize();
104 this.elapsedTime = 0.0;
105 this.lastValue = 0.0;
106 }
107 }
108
109
110
111
112
113
114 public void persist(final double value)
115 {
116 if (!Double.isNaN(value))
117 {
118 synchronized (this.semaphore)
119 {
120 double simTime = this.simulator.getSimulatorTime().si;
121 super.n++;
122 if (value < super.min)
123 super.min = value;
124 if (value > super.max)
125 super.max = value;
126
127 if (super.n == 1)
128 this.startTime = simTime;
129 else
130 {
131 double deltaTime = simTime - (this.elapsedTime + this.startTime);
132 if (deltaTime > 0.0)
133 {
134 super.sum += lastValue * deltaTime;
135 super.varianceSum += lastValue * lastValue * deltaTime;
136 this.elapsedTime = this.elapsedTime + deltaTime;
137 }
138 }
139 this.lastValue = value;
140 }
141 }
142 }
143
144
145
146
147 @Override
148 public int writeToExcel(final Sheet sheet, final int startRow)
149 {
150 DataFormat format = sheet.getWorkbook().createDataFormat();
151 CellStyle style = sheet.getWorkbook().createCellStyle();
152 style.setDataFormat(format.getFormat("0.00"));
153
154 int rownr = startRow;
155 Row row = sheet.createRow(rownr);
156
157 row.createCell(1).setCellValue(description);
158 row.createCell(2).setCellValue("tijdgewogen [n, gem, stdev, min, max]");
159 row.createCell(3).setCellValue(getN());
160 if (getN() > 0)
161 {
162 row.createCell(4).setCellValue(getSampleMean());
163 row.getCell(4).setCellStyle(style);
164 if (getN() > 1 && !Double.isNaN(getStdDev()))
165 {
166 row.createCell(5).setCellValue(getStdDev());
167 row.getCell(5).setCellStyle(style);
168 }
169 row.createCell(6).setCellValue(getMin());
170 row.getCell(6).setCellStyle(style);
171 row.createCell(7).setCellValue(getMax());
172 row.getCell(7).setCellStyle(style);
173 }
174
175 return rownr + 1;
176 }
177
178 }