Last time I got the system to handle multiple types of entities and today I updated the reporting capability so it generates results for the system as a whole and for each type of entity that we might want to report on. Entities can be differentiated by many properties singly or in combination. I therefore found it necessary to structure the solution so the hand-coding required to ensure all the correct statistical accumulators get updated is concentrated into as few, easily identifiable places as possible.
The existing code that managed the various accumulators was easily modified to update an accumulator array of choice instead of just the single global one. The recordGroupStats
function shown below used to write directly to the statsArray
accumulator directly, but here we’ve modified it to write to an accumulator of choice by specifying target accumulator array in the whichStatsArray
parameter. We then created a “wrapper” function that called the original function so it’s applied to each desired accumulator. The invocations of the original function were easy to find in the code and update to their wrapped version with a slight change of parameters.
Note that the wrapper function must refer to the relevant entity so its type and classification can be determined. This determination can be done using any combination of an entity’s properties, although this example shows a very simple division based on a single property. It’s also possible to collect the statistics in numerous different ways. In this case we broke things down by an entity’s residency but in the future we’ll also break it down by processing speed, when we segregate those entities within the simulation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
function recordGroupStats(whichStatsArray,groupName,beginTime) { var elapsedTime = globalSimClock - beginTime; var index = getIndexOfGroupStatsItem(groupName); if (index >= 0) { whichStatsArray[index][statsTimeIndex][0]++; //number of entries whichStatsArray[index][statsTimeIndex][1] += elapsedTime; //elapsed time accumulator if (elapsedTime > whichStatsArray[index][statsTimeIndex][2]) { whichStatsArray[index][statsTimeIndex][2] = elapsedTime; //highest elapsed time } if (elapsedTime < whichStatsArray[index][statsTimeIndex][3]) { whichStatsArray[index][statsTimeIndex][3] = elapsedTime; //lowest elapsed time } whichStatsArray[index][statsTimeIndex][4]--; // number of entities in system at time of recording if (whichStatsArray[index][statsTimeIndex][4] > whichStatsArray[index][statsTimeIndex][5]) { whichStatsArray[index][statsTimeIndex][5] = whichStatsArray[index][statsTimeIndex][4]; //highest count in system } if (whichStatsArray[index][statsTimeIndex][4] < whichStatsArray[index][statsTimeIndex][6]) { whichStatsArray[index][statsTimeIndex][6] = whichStatsArray[index][statsTimeIndex][4]; //lowest count in system } } else { alert("Tried to add stats to nonexistent name: "+groupName); } } //recordGroupStats function recordGroupStatsWrapper(groupName,beginTime,entity) { recordGroupStats(statsArrayGlobal,groupName,beginTime); if (entity.getPropertyValue("Residency") == "Citizen") { targetStatsArray = statsArrayCitizen; } else if (entity.getPropertyValue("Residency") == "LPR") { targetStatsArray = statsArrayLPR; } else if (entity.getPropertyValue("Residency") == "Visitor") { targetStatsArray = statsArrayVisitor; } recordGroupStats(targetStatsArray,groupName,beginTime); } |
Initialization of the statistical accumulator arrays was rolled up into a function so the reset mechanism could easily make a single call instead of having to duplicate the code in two places. We can define accumulators for as many entity subtypes as we’d like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var groupStatsNamesList = []; var statsArrayGlobal; //accumulator for all entities var statsArrayCitizen; //accumulator for one subtype var statsArrayLPR; //accumulator for one subtype var statsArrayVisitor; //accumulator for one subtype var statsTimeIndex = 0; var statsUpdateInterval = 30; function initStatsArrays() { statsArrayGlobal = []; //this line was directly invoked here and in the reset function statsArrayCitizen = []; //...but resetting just calls the function now statsArrayLPR = []; statsArrayVisitor = []; } initStatsArrays(); |
There were a few places where I had to something slightly more complex, like ensure a counter referred to by all the accumulators was only updated once, but the main work was straightforward.
The output of the report is simply repeated once for all entities and again for each entity type or subtype the programmer wanted. I added a text title for each set of accumulated data to identify its entity subtype and corrected a bug I noticed where the Max and Min values in each row weren’t being calculated correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
All Entities Tabulated Results Primary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 1 1 0 90 0 0.0 0.0 0.0 3 3 1 120 3 28.9 33.1 23.5 6 6 1 150 4 29.4 33.1 23.5 6 7 5 180 5 34.4 40.4 26.5 9 10 6 210 5 35.4 37.8 29.9 13 13 7 240 6 51.2 67.2 40.2 12 13 11 270 6 56.4 64.8 43.7 17 18 12 300 6 63.1 74.2 49.6 18 18 15 330 6 69.3 77.0 60.8 14 18 12 360 6 79.2 90.6 74.1 11 15 11 390 7 81.1 93.1 59.1 6 11 6 420 5 70.1 78.2 55.3 1 6 1 450 1 39.8 39.8 39.8 0 1 0 480 0 0.0 0.0 0.0 0 0 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 60 57.1 93.1 0.0 18 18 0 Secondary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 0 0 0 180 0 0.0 0.0 0.0 0 0 0 210 0 0.0 0.0 0.0 1 1 0 240 0 0.0 0.0 0.0 4 4 1 270 2 46.3 46.4 46.2 2 4 2 300 1 78.5 78.5 78.5 3 3 2 330 1 66.3 66.3 66.3 3 4 3 360 2 50.3 52.0 48.5 3 3 2 390 1 83.1 83.1 83.1 4 4 3 420 1 46.2 46.2 46.2 3 4 3 450 2 62.2 69.5 54.9 2 3 2 480 2 66.0 85.8 46.2 0 2 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 12 60.3 85.8 0.0 4 4 0 System Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 1 1 0 90 0 0.0 0.0 0.0 3 3 1 120 3 43.5 47.9 37.4 6 7 3 150 3 43.8 47.9 39.2 10 11 6 180 5 49.7 57.1 40.4 12 14 10 210 5 49.5 52.6 43.8 15 15 11 240 2 62.4 70.8 54.0 22 22 15 270 5 73.5 107.2 56.9 24 24 21 300 6 84.6 116.6 71.6 24 24 20 330 7 101.9 153.5 77.8 21 24 18 360 6 105.7 138.4 88.9 17 21 17 390 5 106.2 156.2 86.2 13 17 13 420 7 99.7 147.8 74.0 6 13 6 450 4 124.2 171.7 70.1 2 6 2 480 1 200.7 200.7 200.7 1 2 1 510 1 110.6 110.6 110.6 0 1 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 60 85.7 200.7 0.0 24 24 0 Citizens Tabulated Results Primary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 1 1 0 90 0 0.0 0.0 0.0 3 3 1 120 3 28.9 33.1 23.5 6 6 1 150 4 29.4 33.1 23.5 6 7 5 180 5 34.4 40.4 26.5 4 7 4 210 3 34.5 37.8 29.9 9 9 2 240 3 45.3 50.2 40.2 10 10 8 270 5 55.7 64.8 43.7 11 13 10 300 5 64.1 74.2 49.6 10 12 9 330 6 69.3 77.0 60.8 5 10 4 360 1 78.6 78.6 78.6 7 8 5 390 4 79.4 93.1 59.1 3 7 3 420 3 72.6 78.2 67.7 0 3 0 450 0 0.0 0.0 0.0 0 0 0 480 0 0.0 0.0 0.0 0 0 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 42 53.5 93.1 0.0 11 13 0 Secondary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 0 0 0 180 0 0.0 0.0 0.0 0 0 0 210 0 0.0 0.0 0.0 0 0 0 240 0 0.0 0.0 0.0 1 1 0 270 0 0.0 0.0 0.0 1 1 1 300 1 78.5 78.5 78.5 2 2 1 330 0 0.0 0.0 0.0 3 3 2 360 2 50.3 52.0 48.5 1 3 1 390 1 83.1 83.1 83.1 1 1 0 420 0 0.0 0.0 0.0 1 1 1 450 0 0.0 0.0 0.0 1 1 1 480 1 85.8 85.8 85.8 0 1 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 5 69.6 85.8 0.0 3 3 0 System Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 1 1 0 90 0 0.0 0.0 0.0 3 3 1 120 3 43.5 47.9 37.4 6 7 3 150 3 43.8 47.9 39.2 8 10 6 180 5 49.7 57.1 40.4 7 9 5 210 4 48.9 52.6 43.8 10 10 6 240 1 54.0 54.0 54.0 17 17 10 270 4 65.1 70.9 56.9 15 18 14 300 3 81.4 88.2 77.1 15 15 13 330 6 93.3 143.7 77.8 12 15 10 360 4 113.5 138.4 90.1 9 12 9 390 3 113.4 156.2 90.4 6 9 6 420 4 87.6 107.6 74.0 2 6 2 450 1 93.1 93.1 93.1 1 2 1 480 1 200.7 200.7 200.7 0 1 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 42 77.7 200.7 0.0 17 18 0 LPRs Tabulated Results Primary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 0 0 0 180 0 0.0 0.0 0.0 1 1 0 210 1 36.0 36.0 36.0 0 1 0 240 0 0.0 0.0 0.0 1 1 0 270 0 0.0 0.0 0.0 5 5 1 300 1 57.6 57.6 57.6 5 5 4 330 0 0.0 0.0 0.0 6 6 5 360 4 76.5 81.4 74.1 2 6 2 390 1 70.5 70.5 70.5 2 2 1 420 2 66.2 77.1 55.3 0 2 0 450 0 0.0 0.0 0.0 0 0 0 480 0 0.0 0.0 0.0 0 0 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 9 67.0 81.4 0.0 6 6 0 Secondary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 0 0 0 180 0 0.0 0.0 0.0 0 0 0 210 0 0.0 0.0 0.0 0 0 0 240 0 0.0 0.0 0.0 0 0 0 270 0 0.0 0.0 0.0 0 0 0 300 0 0.0 0.0 0.0 0 0 0 330 0 0.0 0.0 0.0 0 0 0 360 0 0.0 0.0 0.0 2 2 0 390 0 0.0 0.0 0.0 2 2 2 420 1 46.2 46.2 46.2 1 2 1 450 1 69.5 69.5 69.5 0 1 0 480 0 0.0 0.0 0.0 0 0 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 2 57.9 69.5 0.0 2 2 0 System Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 1 1 0 180 0 0.0 0.0 0.0 1 1 1 210 1 51.9 51.9 51.9 0 1 0 240 0 0.0 0.0 0.0 1 1 0 270 0 0.0 0.0 0.0 5 5 1 300 1 71.6 71.6 71.6 5 6 5 330 0 0.0 0.0 0.0 6 6 5 360 2 90.0 91.0 88.9 5 6 4 390 1 86.2 86.2 86.2 4 5 4 420 2 119.9 147.8 92.0 2 4 2 450 2 120.9 171.7 70.1 0 2 0 480 0 0.0 0.0 0.0 0 0 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 9 96.8 171.7 0.0 6 6 0 Visitors Tabulated Results Primary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 0 0 0 180 0 0.0 0.0 0.0 4 4 0 210 1 37.4 37.4 37.4 4 4 3 240 3 57.2 67.2 49.9 1 4 1 270 1 60.2 60.2 60.2 1 1 0 300 0 0.0 0.0 0.0 3 3 1 330 0 0.0 0.0 0.0 3 3 3 360 1 90.6 90.6 90.6 2 3 2 390 2 89.8 92.5 87.0 1 2 1 420 0 0.0 0.0 0.0 1 1 1 450 1 39.8 39.8 39.8 0 1 0 480 0 0.0 0.0 0.0 0 0 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 9 64.3 92.5 0.0 4 4 0 Secondary Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 0 0 0 180 0 0.0 0.0 0.0 0 0 0 210 0 0.0 0.0 0.0 1 1 0 240 0 0.0 0.0 0.0 3 3 1 270 2 46.3 46.4 46.2 1 3 1 300 0 0.0 0.0 0.0 1 1 1 330 1 66.3 66.3 66.3 0 1 0 360 0 0.0 0.0 0.0 0 0 0 390 0 0.0 0.0 0.0 1 1 0 420 0 0.0 0.0 0.0 1 1 1 450 1 54.9 54.9 54.9 1 2 1 480 1 46.2 46.2 46.2 0 1 0 510 0 0.0 0.0 0.0 0 0 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 5 52.0 66.3 0.0 3 3 0 System Time Exits AvgTm MaxTm MinTm Size Max Min 30 0 0.0 0.0 0.0 0 0 0 60 0 0.0 0.0 0.0 0 0 0 90 0 0.0 0.0 0.0 0 0 0 120 0 0.0 0.0 0.0 0 0 0 150 0 0.0 0.0 0.0 1 1 0 180 0 0.0 0.0 0.0 4 4 1 210 0 0.0 0.0 0.0 5 5 4 240 1 70.8 70.8 70.8 4 5 4 270 1 107.2 107.2 107.2 4 4 3 300 2 96.0 116.6 75.3 4 4 2 330 1 153.5 153.5 153.5 3 4 3 360 0 0.0 0.0 0.0 3 3 3 390 1 104.8 104.8 104.8 3 3 2 420 1 107.4 107.4 107.4 2 3 2 450 1 161.9 161.9 161.9 1 2 1 480 0 0.0 0.0 0.0 1 1 1 510 1 110.6 110.6 110.6 0 1 0 540 0 0.0 0.0 0.0 0 0 0 570 0 0.0 0.0 0.0 0 0 0 600 0 0.0 0.0 0.0 0 0 0 630 0 0.0 0.0 0.0 0 0 0 Total 9 112.0 161.9 0.0 5 5 0 |
I’ve tried to build the reporting mechanism in the most general and modular way possible but there’s only so far this can be taken. The instrumentation needed to collect different kinds of data will necessarily vary based on what we might want to know. In the end all we can do is keep the code as clean as possible and understand it well.