Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/san_pablo_dam/validate.py
169685 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 validate.py
14
# @author Michael Behrisch
15
# @date 2012-01-21
16
17
from __future__ import absolute_import
18
from __future__ import print_function
19
20
import sys
21
import math
22
import subprocess
23
24
dDay = 1
25
obsTimes = {}
26
verbose = False
27
28
29
def readTimes(obsfile):
30
times = []
31
with open(obsfile) as ifile:
32
for line in ifile:
33
ll = line.split(':')
34
if ll:
35
times.append(
36
3600 * int(ll[0]) + 60 * int(ll[1]) + int(float(ll[2])))
37
return times
38
39
40
def parseObsTimes():
41
for i in range(0, 9):
42
obsTimes[i] = []
43
for i in range(1, 8):
44
if dDay == 1 and i == 5:
45
continue
46
if dDay == 2 and i == 6:
47
continue
48
obsTimes[i] = readTimes('data/obstimes_%s_%s.txt' % (dDay, i))
49
50
# convert obsTimes[][] into travel-times:
51
for i in range(1, 8):
52
ni = len(obsTimes[i])
53
if ni == len(obsTimes[i + 1]) and ni > 100:
54
for j in range(ni):
55
obsTimes[i][j] = obsTimes[i + 1][j] - obsTimes[i][j]
56
57
58
def validate(sumoBinary):
59
subprocess.call(
60
[sumoBinary, "-c", "data/spd-road.sumocfg"], stdout=sys.stdout, stderr=sys.stderr)
61
sys.stdout.flush()
62
sys.stderr.flush()
63
64
# analyzing the results...
65
# read the empirical times
66
simTimes = {}
67
for i in range(0, 9):
68
simTimes[i] = []
69
70
# read the simulated times
71
obs2Nr = {'obs1': 1, 'obs2': 2, 'obs3': 3,
72
'obs4': 4, 'obs5': 5, 'obs6': 6, 'obs7': 7}
73
74
with open('data/detector.xml') as ifile:
75
for line in ifile:
76
if line.find('<interval') != -1:
77
ll = line.split('"')
78
iObs = obs2Nr[ll[5]]
79
if int(ll[7]) > 0:
80
simTimes[iObs].append(float(ll[1]))
81
82
# convert simTimes[][] into travel-times:
83
for i in range(1, 8):
84
ni = len(simTimes[i])
85
if ni == len(simTimes[i + 1]) and ni > 100:
86
for j in range(ni):
87
simTimes[i][j] = simTimes[i + 1][j] - simTimes[i][j]
88
89
# compute final statistics
90
err = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
91
errAll = 0.0
92
cntAll = 0
93
if verbose:
94
f = open('data/sumo-obs-error.txt', 'w')
95
for i in range(1, 7):
96
if len(obsTimes[i]) <= 100 or len(obsTimes[i + 1]) <= 100:
97
continue
98
if len(obsTimes[i]) == len(simTimes[i]):
99
tmp = 0.0
100
for o, s in zip(obsTimes[i], simTimes[i]):
101
d = o - s
102
tmp += d * d
103
err[i] = math.sqrt(tmp / len(obsTimes[i]))
104
if verbose:
105
print("%s %s" % (i, err[i]), file=f)
106
errAll += err[i]
107
cntAll += 1
108
if verbose:
109
f.close()
110
111
# finally, write the individual travel times into a csv-file
112
# this is not really needed when validate is called from calibrate as an intermediate
113
# step, but it makes analyzing of the result more simple.
114
# first the header
115
if verbose:
116
c = open('data/compare-tt.csv', 'w')
117
c.write('# indx;')
118
for i in range(1, 7):
119
if len(obsTimes[i]) > 100 and len(obsTimes[i + 1]) > 100:
120
c.write('obs%s;sim%s;' % (i, i))
121
c.write('\n')
122
123
# then the data, of course on the ones which are useable
124
for line in range(len(simTimes[1])):
125
c.write(repr(line) + ';')
126
for i in range(1, 7):
127
if len(obsTimes[i]) > 100 and len(obsTimes[i + 1]) > 100:
128
ttObs = int(obsTimes[i][line])
129
ttSim = int(simTimes[i][line])
130
c.write(repr(ttObs) + ';' + repr(ttSim) + ';')
131
c.write('\n')
132
c.close()
133
return errAll / cntAll
134
135