Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/city_mobil/data/statistics.py
169708 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file statistics.py
15
# @author Michael Behrisch
16
# @author Daniel Krajzewicz
17
# @date 2008-10-17
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
import sys
22
persons = {}
23
personsRunning = 0
24
25
26
class Person:
27
28
def __init__(self, id, source, target, step):
29
self.id = id
30
self.source = source
31
self.target = target
32
self.waitStart = step
33
self.depart = None
34
self.arrive = None
35
36
37
def personArrived(personID, edge, target, step):
38
global personsRunning
39
persons[personID] = Person(personID, edge, target, step)
40
personsRunning += 1
41
42
43
def personLoaded(personID, step):
44
persons[personID].depart = step
45
46
47
def personUnloaded(personID, step):
48
global personsRunning
49
persons[personID].arrive = step
50
personsRunning -= 1
51
52
53
def evaluate(forTest=False):
54
try:
55
import numpy
56
import math
57
except ImportError:
58
print("No numpy available, skipping statistics")
59
return
60
waitTimes = []
61
routeTimes = {}
62
for person in persons.values():
63
waitTimes.append(person.depart - person.waitStart)
64
route = (person.source, person.target)
65
if route not in routeTimes:
66
routeTimes[route] = []
67
routeTimes[route].append(person.arrive - person.depart)
68
waitArray = numpy.array(waitTimes)
69
if forTest:
70
print("waiting time (max, mean, dev):", waitArray.max() < 1000,
71
waitArray.mean() < 1000, math.sqrt(waitArray.var()) < 200)
72
else:
73
print("waiting time (max, mean, dev):", waitArray.max(),
74
waitArray.mean(), math.sqrt(waitArray.var()))
75
76
for route, times in sorted(routeTimes.items()):
77
timeArray = numpy.array(times)
78
if forTest:
79
print(route, timeArray.max() < 1000, timeArray.mean() < 1000, math.sqrt(timeArray.var()) < 200)
80
else:
81
print(route, timeArray.max(), timeArray.mean(), math.sqrt(timeArray.var()))
82
83
co2 = 0.
84
for line in open("aggregated.xml"):
85
if "cyber" in line:
86
pos = line.find('CO2_abs="') + 9
87
if pos >= 9:
88
endpos = line.find('"', pos)
89
co2 += float(line[pos:endpos])
90
91
if forTest:
92
print("CO2:", co2 < 10000000)
93
else:
94
print("CO2:", co2)
95
96
97
if __name__ == "__main__":
98
from pylab import figure, errorbar, legend, savefig, show, title, xlabel, xlim, ylabel, ylim
99
stats = open(sys.argv[1])
100
demand = []
101
simpleWaitMean = []
102
agentWaitMean = []
103
simpleWaitDev = []
104
agentWaitDev = []
105
simpleRouteMean = []
106
agentRouteMean = []
107
simpleRouteDev = []
108
agentRouteDev = []
109
for line in stats:
110
if "simple" in line:
111
mean = simpleWaitMean
112
dev = simpleWaitDev
113
rmean = simpleRouteMean
114
rdev = simpleRouteDev
115
demand.append(int(line.split()[-1]))
116
if "agent" in line:
117
mean = agentWaitMean
118
dev = agentWaitDev
119
rmean = agentRouteMean
120
rdev = agentRouteDev
121
if "waiting" in line:
122
mean.append(float(line.split()[-2]))
123
dev.append(float(line.split()[-1]))
124
if line.startswith("('footmain0to1'"):
125
rmean.append(float(line.split()[-2]))
126
rdev.append(float(line.split()[-1]))
127
stats.close()
128
figure()
129
errorbar(demand, simpleWaitMean, simpleWaitDev, lw=2,
130
ms=10, fmt='o', label='standard bus scenario')
131
errorbar(demand, agentWaitMean, agentWaitDev, lw=2, ms=10,
132
color="red", fmt='o', label='agent controlled cyber cars')
133
xlim(0, 50)
134
ylim(0, 3300)
135
xlabel('Repeater interval (s)')
136
ylabel('Waiting time (s)')
137
title('Mean and standard deviation of waiting time')
138
legend(numpoints=1)
139
savefig("waitingtime.png")
140
figure()
141
errorbar(demand, simpleRouteMean, simpleRouteDev, lw=2,
142
ms=10, fmt='o', label='standard bus scenario')
143
errorbar(demand, agentRouteMean, agentRouteDev, lw=2, ms=10,
144
color="red", fmt='o', label='agent controlled cyber cars')
145
xlim(0, 50)
146
ylim(0, 300)
147
xlabel('Repeater interval (s)')
148
ylabel('Travel time (s)')
149
title('Mean and standard deviation of travel time on the longest route')
150
legend(numpoints=1)
151
savefig("traveltime.png")
152
show()
153
154