Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/san_pablo_dam/runner.py
169685 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 runner.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2007-10-25
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
22
import os
23
import subprocess
24
import sys
25
import shutil
26
from scipy.optimize import fmin_cobyla
27
28
if 'SUMO_HOME' in os.environ:
29
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
30
sys.path.append(tools)
31
else:
32
sys.exit("please declare environment variable 'SUMO_HOME'")
33
from sumolib import checkBinary # noqa
34
import validate # noqa
35
36
37
def buildVSS(obs7file, obs8file, vss):
38
t7Times = validate.readTimes(obs7file)
39
t8Times = validate.readTimes(obs8file)
40
print('data read: ', len(t7Times), len(t8Times))
41
42
fp = open(vss, 'w')
43
lObs8 = 337.5
44
print('<vss>', file=fp)
45
for i, t7 in enumerate(t7Times):
46
v = lObs8 / (t8Times[i] - t7)
47
if i != len(t7Times) - 1 and t7 != t7Times[i + 1]:
48
print(' <step time ="%s" speed="%s"/>' % (t7, v), file=fp)
49
print('</vss>', file=fp)
50
fp.close()
51
52
53
def genDemand(inputFile, outputFile):
54
t1Times = validate.readTimes(inputFile)
55
fRou = open(outputFile, 'w')
56
fRou.write('<routes>\n')
57
fRou.write(' <route id="route01" edges="1to7 7to8"/>\n')
58
for vehID, t in enumerate(t1Times):
59
print(' <vehicle depart="%s" arrivalPos="-1" id="%s" route="route01" type="pass" departSpeed="max" />' % (
60
t, vehID), file=fRou)
61
print('</routes>', file=fRou)
62
fRou.close()
63
64
# definition of gof() function to be given to fmin_cobyla() or fmin()
65
66
67
def gof(p):
68
para = [('vMax', p[0]),
69
('aMax', p[1]),
70
('bMax', p[2]),
71
('lCar', p[3]),
72
('sigA', max(0, min(1, p[4]))),
73
('tTau', p[5])]
74
print('# simulation with:', *["%s:%.3f" % i for i in para])
75
fType = open('data/input_types.add.xml', 'w')
76
fType.write(('<routes>\n <vType accel="%(aMax)s" decel="%(bMax)s" id="pass"' +
77
' length="%(lCar)s" minGap="2.5" maxSpeed="%(vMax)s"' +
78
' sigma="%(sigA)s" tau="%(tTau)s" />\n</routes>') % dict(para))
79
fType.close()
80
result = validate.validate(checkBinary('sumo'))
81
print('#### yields rmse: %.4f' % result)
82
print("%s %s" % (" ".join(["%.3f" % pe for pe in p]), result), file=fpLog)
83
fpLog.flush()
84
return result
85
86
# defining all the constraints
87
88
89
def conVmax(params): # vMax < 25
90
return 25.0 - params[0]
91
92
93
def conTtau(params): # tTau > 1.1
94
return params[5] - 1.1
95
96
97
def conSigA(params): # sigA > 0.1
98
return params[4] - 0.1
99
100
101
def conSigA2(params): # sigA < 1.0
102
return 1.0 - params[4]
103
104
105
def conAmax(params): # aMax > 0.1
106
return params[1] - 0.1
107
108
109
netconvertBinary = checkBinary('netconvert')
110
# build/check network
111
retcode = subprocess.call([netconvertBinary, "-n", "data/spd-road.nod.xml", "-e",
112
"data/spd-road.edg.xml", "-o", "data/spd-road.net.xml", "-v"],
113
stdout=sys.stdout, stderr=sys.stderr)
114
try:
115
shutil.copy("data/spd-road.net.xml", "net.net.xml")
116
except IOError:
117
print("Missing 'spd-road.net.xml'")
118
print(">> Netbuilding closed with status %s" % retcode)
119
sys.stdout.flush()
120
# build/check vss
121
buildVSS('data/obstimes_1_7.txt',
122
'data/obstimes_1_8.txt', 'data/spd-road.vss.xml')
123
shutil.copy("data/spd-road.vss.xml", "vss.xml")
124
sys.stdout.flush()
125
genDemand('data/obstimes_1_1.txt', 'data/spd-road.rou.xml')
126
validate.parseObsTimes()
127
# perform calibration
128
fpLog = open('results.csv', 'w')
129
params = [22.0, 2.0, 2.0, 5.0, 0.5, 1.5]
130
# call to (unconstrained) Nelder Mead; does not work correctly, because
131
# method very often stumples over unrealistic input parameters (like tTau<1),
132
# which causes SUMO to behave strangely.
133
# fmin(gof, params)
134
fmin_cobyla(
135
gof, params, [conVmax, conAmax, conTtau, conSigA, conSigA2], rhoend=1.0e-4)
136
fpLog.close()
137
138