Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/statisticsElements.py
169674 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
3
# This program and the accompanying materials are made available under the
4
# terms of the Eclipse Public License 2.0 which is available at
5
# https://www.eclipse.org/legal/epl-2.0/
6
# This Source Code may also be made available under the following Secondary
7
# Licenses when the conditions for such availability set forth in the Eclipse
8
# Public License 2.0 are satisfied: GNU General Public License, version 2
9
# or later which is available at
10
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13
# @file statisticsElements.py
14
# @author Yun-Pang Floetteroed
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2008-07-10
18
19
"""
20
This script is to define classes, functions, parse-reader and output files/format for
21
- calculating network performances
22
- conducting significance tests (either t-Test or Kruskal-Wallis-Test) and
23
- writing results into files.
24
"""
25
from __future__ import absolute_import
26
27
from xml.sax import handler
28
29
30
# This class is for storing vehicle information, such as departure time,
31
# route and travel time.
32
class Vehicle:
33
34
def __init__(self, label):
35
self.label = label
36
self.method = None
37
self.depart = 0.
38
self.arrival = 0.
39
self.speed = 0.
40
self.route = []
41
self.traveltime = 0.
42
self.travellength = 0.
43
self.departdelay = 0.
44
self.waittime = 0.
45
self.rank = 0.
46
47
def __repr__(self):
48
return "%s_%s_%s_%s_%s_%s<%s>" % (self.label, self.depart, self.arrival,
49
self.speed, self.traveltime, self.travellength,
50
self.route)
51
52
# This class is used in the significance test.
53
54
55
class Assign:
56
57
def __init__(self, method, totalVeh, totalTravelTime, totalTravelLength, totalDepartDelay,
58
totalWaitTime, avgTravelTime, avgTravelLength, avgTravelSpeed, avgDepartDelay,
59
avgWaitTime, SDTravelTime, SDLength, SDSpeed, SDWaitTime):
60
self.label = method
61
self.totalVeh = totalVeh
62
self.totalTravelTime = totalTravelTime
63
self.totalTravelLength = totalTravelLength
64
self.totalDepartDelay = totalDepartDelay
65
self.totalWaitTime = totalWaitTime
66
self.avgTravelTime = avgTravelTime
67
self.avgTravelLength = avgTravelLength
68
self.avgTravelSpeed = avgTravelSpeed
69
self.avgDepartDelay = avgDepartDelay
70
self.avgWaitTime = avgWaitTime
71
self.SDTravelTime = SDTravelTime
72
self.SDLength = SDLength
73
self.SDSpeed = SDSpeed
74
self.SDWaitTime = SDWaitTime
75
self.sumrank = 0.
76
77
def __repr__(self):
78
return "%s_<%s|%s|%s|%s|%s|%s|%s|%s|%s>" % (self.label, self.totalVeh, self.avgTravelTime,
79
self.avgTravelLength, self.avgTravelSpeed,
80
self.avgWaitTime, self.SDTravelTime, self.SDLength,
81
self.SDSpeed, self.SDWaitTime)
82
83
# This cloass is used for the t test in the significance test.
84
85
86
class T_Value:
87
88
def __init__(self, avgtraveltime, avgtravelspeed, avgtravellength, avgwaittime, lowvalue, highvalue):
89
self.avgtraveltime = avgtraveltime
90
self.avgtravelspeed = avgtravelspeed
91
self.avgtravellength = avgtravellength
92
self.avgwaittime = avgwaittime
93
self.lowtvalue = lowvalue
94
self.hightvalue = highvalue
95
96
def __repr__(self):
97
return "<%s|%s|%s|%s>" % (self.avgtraveltime, self.avgtravelspeed, self.avgtravellength, self.avgwaittime)
98
99
# This class is used for the Kruskal-Wallis test in the significance test.
100
101
102
class H_Value:
103
104
def __init__(self, label, lowvalue, highvalue):
105
self.label = label
106
self.traveltime = 0.
107
self.travelspeed = 0.
108
self.travellength = 0.
109
self.waittime = 0.
110
# 95% confidence interval
111
self.lowchivalue = lowvalue
112
# 99% confidence interval
113
self.highchivalue = highvalue
114
115
def __repr__(self):
116
return "<%s|%s|%s|%s|%s|%s>" % (self.traveltime, self.travelspeed, self.travellength, self.waittime,
117
self.lowchivalue, self.highchivalue)
118
119
# The class is for parsing the XML input file (vehicle information). This class is used in the networkStatistics.py for
120
# calculating the gloabal network performances, e.g. avg. travel time and
121
# avg. travel speed.
122
123
124
class VehInformationReader(handler.ContentHandler):
125
126
def __init__(self, vehList):
127
self._vehList = vehList
128
self._Vehicle = None
129
self._routeString = ''
130
131
def startElement(self, name, attrs):
132
if name == 'tripinfo':
133
self._Vehicle = Vehicle(attrs['id'])
134
self._Vehicle.traveltime = float(attrs['duration'])
135
self._Vehicle.travellength = float(attrs['routeLength'])
136
self._Vehicle.departdelay = float(attrs['departDelay'])
137
self._Vehicle.waittime = float(
138
attrs['departDelay']) + float(attrs['waitingTime'])
139
self._vehList.append(self._Vehicle)
140
141
# output the network statistics based on the sumo-simulation results
142
143
144
def getStatisticsOutput(assignments, outputfile):
145
foutveh = open(outputfile, 'w')
146
foutveh.write(
147
'average vehicular travel time(s) = the sum of all vehicular travel times / the number of vehicles\n')
148
foutveh.write(
149
'average vehicular travel length(m) = the sum of all vehicular travel lengths / the number of vehicles\n')
150
foutveh.write(
151
'average vehicular travel speed(m/s) = the sum of all vehicular travel speeds / the number of vehicles\n')
152
for method in assignments.values():
153
foutveh.write('\nAssignment Method:%s\n' % method.label)
154
foutveh.write('- total number of vehicles:%s\n' % method.totalVeh)
155
foutveh.write('- total departure delay(s):%s, ' %
156
method.totalDepartDelay)
157
foutveh.write('- average departure delay(s):%s\n' %
158
method.avgDepartDelay)
159
foutveh.write('- total waiting time(s):%s, ' % method.totalWaitTime)
160
foutveh.write('- average vehicular waiting time(s):%s\n' %
161
method.avgWaitTime)
162
foutveh.write('- total travel time(s):%s, ' % method.totalTravelTime)
163
foutveh.write('- average vehicular travel time(s):%s\n' %
164
method.avgTravelTime)
165
foutveh.write('- total travel length(m):%s, ' %
166
method.totalTravelLength)
167
foutveh.write('- average vehicular travel length(m):%s\n' %
168
method.avgTravelLength)
169
foutveh.write('- average vehicular travel speed(m/s):%s\n' %
170
method.avgTravelSpeed)
171
foutveh.close()
172
173
# output the results of the significance tests according to the
174
# sumo-simulation results
175
176
177
def getSignificanceTestOutput(assignments, tTest, tValueAvg, hValues, outputfile):
178
foutSGtest = open(outputfile, 'w')
179
if tTest:
180
foutSGtest.write(
181
'The significances of the performance averages among the used assignment models ' +
182
'are examined with the t test.\n')
183
assignlist = list(assignments.values())
184
for num, A in enumerate(assignlist):
185
for B in assignlist[num + 1:]:
186
foutSGtest.write('\nmethod:%s' % A.label)
187
foutSGtest.write('\nmethod:%s' % B.label)
188
foutSGtest.write(
189
'\n t-value for the avg. travel time:%s' % tValueAvg[A][B].avgtraveltime)
190
foutSGtest.write(
191
'\n t-value for the avg. travel length:%s' % tValueAvg[A][B].avgtravellength)
192
foutSGtest.write(
193
'\n t-value for the avg.travel speed:%s' % tValueAvg[A][B].avgtravelspeed)
194
foutSGtest.write(
195
'\n t-value for the avg. wait time:%s\n' % tValueAvg[A][B].avgwaittime)
196
foutSGtest.write('\n95 t-value:%s' % tValueAvg[A][B].lowtvalue)
197
foutSGtest.write('\n99 t-value:%s\n' %
198
tValueAvg[A][B].hightvalue)
199
200
foutSGtest.write(
201
'The significance test among the different assignment methods is also done with the Kruskal-Wallis test.\n')
202
for h in hValues:
203
foutSGtest.write('\n\nmethods:%s' % h.label)
204
foutSGtest.write('\nH_traveltime:%s' % h.traveltime)
205
foutSGtest.write('\nH_travelspeed:%s' % h.travelspeed)
206
foutSGtest.write('\nH_travellength:%s' % h.travellength)
207
foutSGtest.write('\nH_waittime:%s\n' % h.waittime)
208
foutSGtest.write('\n95 chi-square value:%s' % h.lowchivalue)
209
foutSGtest.write('\n99 chi-square value:%s\n' % h.highchivalue)
210
foutSGtest.close()
211
212