Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/city_mobil/data/createNet.py
169689 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file createNet.py
16
# @author Michael Behrisch
17
# @author Daniel Krajzewicz
18
# @date 2008-07-04
19
20
"""
21
Create the XML input files for the generation of the SUMO network
22
of the CityMobil parking lot.
23
"""
24
from __future__ import absolute_import
25
from __future__ import print_function
26
from __future__ import division
27
import random
28
import subprocess
29
import os
30
import sys
31
from constants import PREFIX, DOUBLE_ROWS, ROW_DIST, STOP_POS, SLOTS_PER_ROW, SLOT_WIDTH
32
from constants import SLOT_LENGTH, SLOT_FOOT_LENGTH, CAR_CAPACITY, CYBER_CAPACITY, BUS_CAPACITY, TOTAL_CAPACITY
33
from constants import CYBER_SPEED, CYBER_LENGTH, OCCUPATION_PROBABILITY
34
35
if 'SUMO_HOME' in os.environ:
36
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
37
sys.path.append(tools)
38
else:
39
sys.exit("please declare environment variable 'SUMO_HOME'")
40
41
import sumolib # noqa
42
43
occupied = 0
44
nodes = open("%s.nod.xml" % PREFIX, "w")
45
print("<nodes>", file=nodes)
46
edges = open("%s.edg.xml" % PREFIX, "w")
47
print("<edges>", file=edges)
48
connections = open("%s.con.xml" % PREFIX, "w")
49
print("<connections>", file=connections)
50
routes = open("%s.rou.xml" % PREFIX, "w")
51
print(("""<routes>
52
<vType id="car" length="3" minGap=".5" guiShape="passenger" maxSpeed="50" color="0.7,0.7,0.7"/>
53
<vType id="person" length=".25" minGap="0" guiShape="pedestrian" width=".25" maxSpeed="5" color="1,0.2,0.2"/>
54
<vType id="cybercar" length="%s" minGap="1" guiShape="evehicle" maxSpeed="%s" color="0,1,0" \
55
emissionClass="HBEFA2/P_7_7"/>""") % (CYBER_LENGTH, CYBER_SPEED), file=routes)
56
# streets
57
nodeID = "main-0"
58
print('<node id="in" x="-100" y="0"/>', file=nodes)
59
print('<node id="%s" x="0" y="0"/>' % nodeID, file=nodes)
60
print('<edge id="mainin" from="in" to="%s" numLanes="2" spreadType="center"/>' %
61
nodeID, file=edges)
62
for row in range(DOUBLE_ROWS):
63
nextNodeID = "main%s-%s" % (row, row + 1)
64
if row + 1 == DOUBLE_ROWS:
65
nextNodeID = "main%s-" % row
66
x = (row + 1) * ROW_DIST
67
print('<node id="%s" x="%s" y="0"/>' % (nextNodeID, x), file=nodes)
68
print('<edge id="main%s" from="%s" to="%s" numLanes="2" spreadType="center"/>' % (
69
row, nodeID, nextNodeID), file=edges)
70
nodeID = nextNodeID
71
print('<node id="out" x="%s" y="0"/>' % (x + 100), file=nodes)
72
print('<edge id="mainout" from="%s" to="out" numLanes="2" spreadType="center"/>' %
73
nodeID, file=edges)
74
roads = ["road-0"]
75
for row in range(DOUBLE_ROWS - 1):
76
roads.append("road%s-%s" % (row, row + 1))
77
roads.append("road%s-" % (row + 1))
78
for idx, road in enumerate(roads):
79
nodeID = road.replace("road", "main")
80
for slot in range(SLOTS_PER_ROW):
81
partID = "%s-%s" % (road, slot)
82
print('<node id="%st" x="%s" y="%s"/>' % (
83
partID, idx * ROW_DIST, (slot + 1) * SLOT_WIDTH), file=nodes)
84
print('<edge id="%s" from="%s" to="%st" numLanes="2" spreadType="center"/>' % (
85
partID, nodeID, partID), file=edges)
86
print('<edge id="-%s" from="%st" to="%s" numLanes="2" spreadType="center"/>' % (
87
partID, partID, nodeID), file=edges)
88
nodeID = "%st" % partID
89
for row in range(DOUBLE_ROWS):
90
for slot in range(SLOTS_PER_ROW):
91
slotID = "slot%s-%sl" % (row, slot)
92
source = "%s-%st" % (roads[row], slot)
93
print('<node id="%st" x="%s" y="%s"/>' % (
94
slotID, row * ROW_DIST + SLOT_LENGTH, (slot + 1) * SLOT_WIDTH), file=nodes)
95
print('<edge id="%s" from="%s" to="%st" spreadType="center"/>' % (
96
slotID, source, slotID), file=edges)
97
print('<edge id="-%s" from="%st" to="%s" spreadType="center"/>' % (
98
slotID, slotID, source), file=edges)
99
slotID = "slot%s-%sr" % (row, slot)
100
source = "%s-%st" % (roads[row + 1], slot)
101
print('<node id="%st" x="%s" y="%s"/>' % (
102
slotID, (row + 1) * ROW_DIST - SLOT_LENGTH, (slot + 1) * SLOT_WIDTH), file=nodes)
103
print('<edge id="%s" from="%s" to="%st" spreadType="center"/>' % (
104
slotID, source, slotID), file=edges)
105
print('<edge id="-%s" from="%st" to="%s" spreadType="center"/>' % (
106
slotID, slotID, source), file=edges)
107
# footpaths
108
y = (SLOTS_PER_ROW + 1) * SLOT_WIDTH
109
print('<node id="foot" x="-100" y="%s"/>' % y, file=nodes)
110
print(
111
'<edge id="footmainin" from="foot" to="foot0" speed="5" spreadType="center"/>', file=edges)
112
for row in range(DOUBLE_ROWS):
113
nodeID = "foot%s" % row
114
x = row * ROW_DIST + ROW_DIST / 2
115
print('<node id="%s" x="%s" y="%s"/>' % (nodeID, x, y), file=nodes)
116
if row > 0:
117
edgeID = "footmain%sto%s" % (row - 1, row)
118
print('<edge id="%s" from="foot%s" to="foot%s" speed="5" spreadType="center"/>' % (
119
edgeID, row - 1, row), file=edges)
120
for slot in reversed(range(SLOTS_PER_ROW)):
121
slotID = "foot%s-%s" % (row, slot)
122
print('<node id="%s" x="%s" y="%s"/>' % (
123
slotID, x, (slot + 1) * SLOT_WIDTH), file=nodes)
124
print('<edge id="%sto%s" from="%s" to="%s" speed="5" spreadType="center"/>' % (
125
nodeID, slot, nodeID, slotID), file=edges)
126
print('<edge id="-%sto%s" from="%s" to="%s" speed="5" spreadType="center"/>' % (
127
nodeID, slot, slotID, nodeID), file=edges)
128
print('<node id="%srt" x="%s" y="%s"/>' % (
129
slotID, x + SLOT_FOOT_LENGTH, (slot + 1) * SLOT_WIDTH), file=nodes)
130
print('<edge id="%sr" from="%s" to="%srt" spreadType="center"/>' % (
131
slotID, slotID, slotID), file=edges)
132
print('<edge id="-%sr" from="%srt" to="%s" spreadType="center"/>' % (
133
slotID, slotID, slotID), file=edges)
134
print('<node id="%slt" x="%s" y="%s"/>' % (
135
slotID, x - SLOT_FOOT_LENGTH, (slot + 1) * SLOT_WIDTH), file=nodes)
136
print('<edge id="%sl" from="%s" to="%slt" spreadType="center"/>' % (
137
slotID, slotID, slotID), file=edges)
138
print('<edge id="-%sl" from="%slt" to="%s" spreadType="center"/>' % (
139
slotID, slotID, slotID), file=edges)
140
nodeID = slotID
141
vSlot = slotID.replace("foot", "slot")
142
if random.uniform(0, 1) < OCCUPATION_PROBABILITY:
143
occupied += 1
144
print("""\
145
<vehicle id="v%sr" type="car" depart="0" departPos="0">
146
<route edges="%sr -%sr"/>
147
</vehicle>""" % (vSlot, vSlot, vSlot), file=routes)
148
else:
149
print("""\
150
<flow id="p%sr" type="person" begin="0" period="1" number="%s">
151
<route edges="%sr -%sr"/>
152
</flow>""" % (slotID, CAR_CAPACITY, slotID, slotID), file=routes)
153
if random.uniform(0, 1) < OCCUPATION_PROBABILITY:
154
occupied += 1
155
print("""\
156
<vehicle id="v%sl" type="car" depart="0" departPos="0">
157
<route edges="%sl -%sl"/>
158
</vehicle>""" % (vSlot, vSlot, vSlot), file=routes)
159
else:
160
print("""\
161
<flow id="p%sl" type="person" begin="0" period="1" number="%s">
162
<route edges="%sl -%sl"/>
163
</flow>""" % (slotID, CAR_CAPACITY, slotID, slotID), file=routes)
164
x = DOUBLE_ROWS * ROW_DIST + ROW_DIST / 2
165
print('<node id="foot%s" x="%s" y="%s"/>' % (DOUBLE_ROWS, x, y), file=nodes)
166
edgeID = "footmain%sto%s" % (DOUBLE_ROWS - 1, DOUBLE_ROWS)
167
print('<edge id="%s" from="foot%s" to="foot%s" speed="5" spreadType="center"/>' % (
168
edgeID, DOUBLE_ROWS - 1, DOUBLE_ROWS), file=edges)
169
print('<node id="footend" x="%s" y="%s"/>' % (x + 100, y), file=nodes)
170
print('<edge id="footmainout" from="foot%s" to="footend" speed="5" spreadType="center"/>' %
171
DOUBLE_ROWS, file=edges)
172
print('<node id="fair" x="%s" y="%s"/>' % (x + 100, y - 10), file=nodes)
173
print('<edge id="footfairin" from="fair" to="foot%s" speed="5" spreadType="center"/>' %
174
DOUBLE_ROWS, file=edges)
175
176
# cybercar (automated bus)
177
y = (SLOTS_PER_ROW + 3) * SLOT_WIDTH
178
print('<node id="cyber" x="-100" y="%s"/>' % y, file=nodes)
179
print(
180
'<edge id="cyberin" from="cyber" to="cyber0" numLanes="2" spreadType="center"/>', file=edges)
181
print(
182
'<edge id="-cyberin" from="cyber0" to="cyber" numLanes="2" spreadType="center"/>', file=edges)
183
for row in range(DOUBLE_ROWS + 1):
184
nodeID = "cyber%s" % row
185
x = row * ROW_DIST + ROW_DIST / 2
186
print('<node id="%s" x="%s" y="%s"/>' % (nodeID, x, y), file=nodes)
187
if row > 0:
188
edgeID = "cyber%sto%s" % (row - 1, row)
189
print('<edge id="%s" from="cyber%s" to="cyber%s" numLanes="2" spreadType="center"/>' % (
190
edgeID, row - 1, row), file=edges)
191
print('<edge id="-%s" from="cyber%s" to="cyber%s" numLanes="2" spreadType="center"/>' % (
192
edgeID, row, row - 1), file=edges)
193
if row < DOUBLE_ROWS:
194
print('<connection from="%s" to="cyber%sto%s"/>' % (
195
edgeID, row, row + 1), file=connections)
196
print('<connection from="-cyber%sto%s" to="-%s"/>' % (
197
row, row + 1, edgeID), file=connections)
198
else:
199
print('<connection from="%s" to="cyberout"/>' %
200
edgeID, file=connections)
201
print('<connection from="-cyberout" to="-%s"/>' %
202
edgeID, file=connections)
203
print('<node id="cyberend" x="%s" y="%s"/>' % (x + 100, y), file=nodes)
204
print('<edge id="cyberout" from="cyber%s" to="cyberend" numLanes="2" spreadType="center"/>' %
205
row, file=edges)
206
print('<edge id="-cyberout" from="cyberend" to="cyber%s" numLanes="2" spreadType="center"/>' %
207
row, file=edges)
208
209
210
print("</nodes>", file=nodes)
211
nodes.close()
212
print("</edges>", file=edges)
213
edges.close()
214
print("</connections>", file=connections)
215
connections.close()
216
217
NETCONVERT = os.environ.get(
218
"NETCONVERT_BINARY", os.path.join(os.environ["SUMO_HOME"], 'bin', 'netconvert'))
219
subprocess.call([NETCONVERT,
220
'--no-internal-links',
221
'-n', '%s.nod.xml' % PREFIX,
222
'-e', '%s.edg.xml' % PREFIX,
223
'-x', '%s.con.xml' % PREFIX,
224
'-o', '%s.net.xml' % PREFIX])
225
226
numBusses = TOTAL_CAPACITY // BUS_CAPACITY
227
print(""" <flow id="b" type="cybercar" begin="0" period="100" number="%s">
228
<route edges="cyberin"/>
229
</flow>
230
</routes>""" % numBusses, file=routes)
231
routes.close()
232
233
routes = open("%s_cyber.rou.xml" % PREFIX, "w")
234
print("""<routes>
235
<flow id="c" type="cybercar" begin="50" period="100" number="%s">
236
<route edges="cyberin"/>
237
</flow>
238
</routes>""" % (TOTAL_CAPACITY // CYBER_CAPACITY - numBusses), file=routes)
239
routes.close()
240
241
stops = open("%s.add.xml" % PREFIX, "w")
242
print("<additional>", file=stops)
243
for row in range(DOUBLE_ROWS):
244
edgeID = "cyber%sto%s" % (row, row + 1)
245
print(' <busStop id="%sstop" lane="%s_0"' %
246
(edgeID, edgeID), end=' ', file=stops)
247
print('startPos="%s" endPos="%s"/>' % (
248
STOP_POS - 2 * CYBER_LENGTH - 1, STOP_POS), file=stops)
249
for edge in ["cyberin", "cyberout"]:
250
print(' <busStop id="%sstop" lane="%s_0"' %
251
(edge, edge), end=' ', file=stops)
252
print('startPos="%s" endPos="%s"/>' % (
253
90 - 2 * CYBER_LENGTH - 1, 90), file=stops)
254
print(
255
' <edgeData id="dump" freq="3600" file="aggregated.xml" excludeEmpty="true" type="emissions"/>', file=stops)
256
print("</additional>", file=stops)
257
stops.close()
258
259
totalSlots = 2 * DOUBLE_ROWS * SLOTS_PER_ROW
260
bat = open("%s.bat" % PREFIX, "w")
261
breakbat = open("%s_break.bat" % PREFIX, "w")
262
263
for period in range(5, 50, 5):
264
routes = open("%s_demand%02i.rou.xml" % (PREFIX, period), "w")
265
print("<routes>", file=routes)
266
if occupied < totalSlots:
267
print(""" <flow id="v" type="car" begin="10" period="%s" number="%s">
268
<route edges="mainin"/>
269
</flow>""" % (period, totalSlots - occupied), file=routes)
270
if occupied > 0:
271
print(""" <flow id="p" type="person" begin="10" period="%s" number="%s">
272
<route edges="footfairin"/>
273
</flow>""" % (period, occupied * CAR_CAPACITY), file=routes)
274
print("</routes>", file=routes)
275
routes.close()
276
277
config = open("%s%02i.sumocfg" % (PREFIX, period), "w")
278
print("""<configuration>
279
<input>
280
<net-file value="%s.net.xml"/>
281
<route-files value="%s.rou.xml,%s_demand%02i.rou.xml"/>
282
<additional-files value="%s.add.xml"/>
283
<no-step-log value="True"/>
284
<time-to-teleport value="0"/>
285
</input>
286
</configuration>""" % (PREFIX, PREFIX, PREFIX, period, PREFIX), file=config)
287
config.close()
288
print("simpleManager.py -d %s" % period, file=bat)
289
print("simpleManager.py -b 120 -d %s" % period, file=breakbat)
290
291
config = open("%s%02i_cyber.sumocfg" % (PREFIX, period), "w")
292
print("""<configuration>
293
<input>
294
<net-file value="%s.net.xml"/>
295
<route-files value="%s.rou.xml,%s_cyber.rou.xml,%s_demand%02i.rou.xml"/>
296
<additional-files value="%s.add.xml"/>
297
<no-step-log value="True"/>
298
<time-to-teleport value="0"/>
299
</input>
300
</configuration>""" % (PREFIX, PREFIX, PREFIX, PREFIX, period, PREFIX), file=config)
301
config.close()
302
print("agentManager.py -c -d %s" % period, file=bat)
303
print("agentManager.py -c -b 120 -d %s" % period, file=breakbat)
304
bat.close()
305
breakbat.close()
306
307