Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/city_mobil/data/createNetTaxi.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 createNetTaxi.py
16
# @author Michael Behrisch
17
# @author Daniel Krajzewicz
18
# @author Melanie Weber
19
# @date 2008-07-04
20
21
"""
22
Create the XML input files for the generation of the SUMO network
23
of the CityMobil parking lot.
24
"""
25
from __future__ import absolute_import
26
from __future__ import print_function
27
from __future__ import division
28
import random
29
import subprocess
30
import os
31
import sys
32
33
from constants import PREFIX, DOUBLE_ROWS, ROW_DIST, SLOTS_PER_ROW, SLOT_WIDTH
34
from constants import CAR_CAPACITY, CYBER_CAPACITY, BUS_CAPACITY, TOTAL_CAPACITY
35
from constants import CYBER_SPEED, CYBER_LENGTH
36
37
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
38
import sumolib # noqa
39
40
random.seed(42)
41
# network building
42
nodes = open("%s.nod.xml" % PREFIX, "w")
43
sumolib.xml.writeHeader(nodes, root="nodes")
44
edges = open("%s.edg.xml" % PREFIX, "w")
45
sumolib.xml.writeHeader(edges, root="edges")
46
47
# main street (passenger car inflow)
48
nodeID = "main0"
49
print('<node id="in" x="-100" y="0"/>', file=nodes)
50
print('<edge id="mainin" from="in" to="%s" numLanes="2"/>' % nodeID, file=edges)
51
for row in range(DOUBLE_ROWS):
52
nextNodeID = "main%s" % row
53
x = row * ROW_DIST
54
print('<node id="%s" x="%s" y="0"/>' % (nextNodeID, x), file=nodes)
55
if row > 0:
56
print('<edge id="main%sto%s" from="%s" to="%s" numLanes="2"/>' %
57
(row - 1, row, nodeID, nextNodeID), file=edges)
58
nodeID = nextNodeID
59
print('<node id="out" x="%s" y="0"/>' % (x + 100), file=nodes)
60
print('<edge id="mainout" from="%s" to="out" numLanes="2"/>' % nodeID, file=edges)
61
62
# cybercar infrastructure (automated bus)
63
y = (SLOTS_PER_ROW + 3) * SLOT_WIDTH
64
print('<node id="cyber" x="-100" y="%s"/>' % y, file=nodes)
65
print("""<edge id="cyberin" from="cyber" to="cyber0" numLanes="3" spreadType="center">
66
<lane index="0" allow="pedestrian" width="2.00"/>
67
<lane index="1" allow="taxi bus"/>
68
<lane index="2" allow="taxi bus"/>
69
</edge>""", file=edges)
70
print("""<edge id="-cyberin" from="cyber0" to="cyber" numLanes="2" spreadType="center">
71
<lane index="0" allow="taxi bus"/>
72
<lane index="1" allow="taxi bus"/>
73
</edge>""", file=edges)
74
for row in range(DOUBLE_ROWS):
75
nodeID = "cyber%s" % row
76
x = row * ROW_DIST
77
print('<node id="%s" x="%s" y="%s"/>' % (nodeID, x, y), file=nodes)
78
if row > 0:
79
edgeID = "cyber%sto%s" % (row - 1, row)
80
print("""<edge id="%s" from="cyber%s" to="cyber%s" numLanes="3" spreadType="center">
81
<lane index="0" allow="pedestrian" width="2.00"/>
82
<lane index="1" allow="taxi bus"/>
83
<lane index="2" allow="taxi bus"/>
84
</edge>""" % (edgeID, row - 1, row), file=edges)
85
print("""<edge id="-%s" from="cyber%s" to="cyber%s" numLanes="2" spreadType="center">
86
<lane index="0" allow="taxi bus"/>
87
<lane index="1" allow="taxi bus"/>
88
</edge>""" % (edgeID, row, row - 1), file=edges)
89
print('<node id="cyberend" x="%s" y="%s"/>' % (x + 100, y), file=nodes)
90
print("""<edge id="cyberout" from="cyber%s" to="cyberend" numLanes="3" spreadType="center">
91
<lane index="0" allow="pedestrian" width="2.00"/>
92
<lane index="1" allow="taxi bus"/>
93
<lane index="2" allow="taxi bus"/>
94
</edge>""" % row, file=edges)
95
print("""<edge id="-cyberout" from="cyberend" to="cyber%s" numLanes="2" spreadType="center">
96
<lane index="0" allow="taxi bus"/>
97
<lane index="1" allow="taxi bus"/>
98
</edge>""" % row, file=edges)
99
100
# roads in the parking area
101
for row in range(DOUBLE_ROWS):
102
print("""<edge id="road%s" from="main%s" to="cyber%s" numLanes="2">
103
<lane index="0" allow="pedestrian" width="2.00"/>
104
<lane index="1" disallow="pedestrian"/>
105
</edge>""" % (row, row, row), file=edges)
106
print("""<edge id="-road%s" from="cyber%s" to="main%s" numLanes="2">
107
<lane index="0" allow="pedestrian" width="2.00"/>
108
<lane index="1" disallow="pedestrian"/>
109
</edge>""" % (row, row, row), file=edges)
110
111
print("</nodes>", file=nodes)
112
nodes.close()
113
print("</edges>", file=edges)
114
edges.close()
115
116
subprocess.call([sumolib.checkBinary('netconvert'),
117
'-n', '%s.nod.xml' % PREFIX,
118
'-e', '%s.edg.xml' % PREFIX,
119
'-o', '%s.net.xml' % PREFIX])
120
121
# Bus Stops / Parking Areas
122
stops = open("%s.add.xml" % PREFIX, "w")
123
sumolib.xml.writeHeader(stops, root="additional")
124
for row in range(DOUBLE_ROWS):
125
print(""" <parkingArea id="ParkArea%s" lane="road%s_1" roadsideCapacity="%s" angle="270" length="8"/>
126
<parkingArea id="ParkArea-%s" lane="-road%s_1" roadsideCapacity="%s" angle="270" length="8"/>""" %
127
(row, row, SLOTS_PER_ROW, row, row, SLOTS_PER_ROW), file=stops)
128
129
for row in range(DOUBLE_ROWS-1):
130
edgeID = "cyber%sto%s" % (row, row + 1)
131
print(' <busStop id="%sstop" lane="%s_1" startPos="2" endPos="12"/>' % (edgeID, edgeID), file=stops)
132
133
# vehicle types
134
print((""" <vType id="car" color="0.7,0.7,0.7"/>
135
<vType id="ped_pedestrian" vClass="pedestrian" color="1,0.2,0.2"/>
136
<vType id="cybercar" vClass="taxi" length="%s" minGap="1" guiShape="evehicle" maxSpeed="%s"
137
color="green" emissionClass="HBEFA2/P_7_7" personCapacity="%s">
138
<param key="has.taxi.device" value="true"/>
139
</vType>
140
<vType id="bus" vClass="bus" color="blue" personCapacity="%s"/>
141
""") % (CYBER_LENGTH, CYBER_SPEED, CYBER_CAPACITY, BUS_CAPACITY), file=stops)
142
143
print("</additional>", file=stops)
144
stops.close()
145
146
# demand building
147
# routes cybercar and bus
148
with open("%s_cyber.rou.xml" % PREFIX, "w") as routes:
149
sumolib.xml.writeHeader(routes, root="routes")
150
print(""" <flow id="c" type="cybercar" begin="50" period="100" number="%s" line="taxi">
151
<route edges="cyberin cyber0to1"/>
152
</flow>
153
</routes>""" % (TOTAL_CAPACITY // CYBER_CAPACITY), file=routes)
154
155
with open("%s_bus.rou.xml" % PREFIX, "w") as routes:
156
sumolib.xml.writeHeader(routes, root="routes")
157
print(""" <flow id="b" from="cyberin" to="cyberout" type="bus"
158
begin="50" period="100" number="%s" line="taxi">""" %
159
(TOTAL_CAPACITY // BUS_CAPACITY), file=routes)
160
for repeat in range(400 // TOTAL_CAPACITY):
161
for row in range(DOUBLE_ROWS-1):
162
edgeID = "cyber%sto%s" % (row, row + 1)
163
print(' <stop busStop="%sstop" duration="10"/>' % edgeID, file=routes)
164
print(' <stop edge="cyberout" duration="10"/>', file=routes)
165
print(" </flow>\n</routes>", file=routes)
166
167
# passenger car demand
168
for period in range(5, 50, 5):
169
# routes person
170
routes = open("%s_demand%02i.rou.xml" % (PREFIX, period), "w")
171
print("<routes>", file=routes)
172
for v in range(SLOTS_PER_ROW):
173
for idx in range(DOUBLE_ROWS):
174
busStop = "cyber%sto%sstop" % (idx, idx+1) if idx < DOUBLE_ROWS - 1 else "cyber%sto%sstop" % (idx-1, idx)
175
print(""" <trip id="v%s.%s" type="car" depart="%s" from="mainin" to="road%s">
176
<stop parkingArea="ParkArea%s" duration="1000"/>
177
</trip>""" % (idx, v, v * period, idx, idx), file=routes)
178
print(""" <trip id="v-%s.%s" type="car" depart="%s" from="mainin" to="-road%s">
179
<stop parkingArea="ParkArea-%s" duration="1000"/>
180
</trip>""" % (idx, v, v * period, idx, idx), file=routes)
181
for p in range(random.randint(1, CAR_CAPACITY)):
182
for infix in ("", "-"):
183
vehId = "v%s%s.%s" % (infix, idx, v)
184
print(""" <person id="%sp%s" type="ped_pedestrian" depart="triggered">
185
<ride from="mainin" to="%sroad%s" lines="%s"/>
186
<walk busStop="%s"/>
187
<ride to="cyberout" lines="taxi"/>
188
</person>""" % (vehId, p, infix, idx, vehId, busStop), file=routes)
189
print("</routes>", file=routes)
190
routes.close()
191
192
# sumo config, the "traditional" bus does not work currently
193
config = open("%s%02i.sumocfg" % (PREFIX, period), "w")
194
print("""<configuration>
195
<input>
196
<net-file value="%s.net.xml"/>
197
<route-files value="%s_bus.rou.xml,%s_demand%02i.rou.xml"/>
198
<additional-files value="%s.add.xml"/>
199
<no-step-log value="True"/>
200
<time-to-teleport value="0"/>
201
</input>
202
</configuration>""" % (PREFIX, PREFIX, PREFIX, period, PREFIX), file=config)
203
config.close()
204
205
config = open("%s%02i_cyber.sumocfg" % (PREFIX, period), "w")
206
print("""<configuration>
207
<input>
208
<net-file value="%s.net.xml"/>
209
<route-files value="%s_cyber.rou.xml,%s_demand%02i.rou.xml"/>
210
<additional-files value="%s.add.xml"/>
211
<no-step-log value="True"/>
212
<time-to-teleport value="0"/>
213
<device.taxi.dispatch-algorithm value="routeExtension"/>
214
</input>
215
</configuration>""" % (PREFIX, PREFIX, PREFIX, period, PREFIX), file=config)
216
config.close()
217
218