Path: blob/main/tests/complex/tutorial/city_mobil/data/createNetTaxi.py
169689 views
#!/usr/bin/env python1# -*- coding: utf-8 -*-2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo3# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file createNetTaxi.py15# @author Michael Behrisch16# @author Daniel Krajzewicz17# @author Melanie Weber18# @date 2008-07-041920"""21Create the XML input files for the generation of the SUMO network22of the CityMobil parking lot.23"""24from __future__ import absolute_import25from __future__ import print_function26from __future__ import division27import random28import subprocess29import os30import sys3132from constants import PREFIX, DOUBLE_ROWS, ROW_DIST, SLOTS_PER_ROW, SLOT_WIDTH33from constants import CAR_CAPACITY, CYBER_CAPACITY, BUS_CAPACITY, TOTAL_CAPACITY34from constants import CYBER_SPEED, CYBER_LENGTH3536sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))37import sumolib # noqa3839random.seed(42)40# network building41nodes = open("%s.nod.xml" % PREFIX, "w")42sumolib.xml.writeHeader(nodes, root="nodes")43edges = open("%s.edg.xml" % PREFIX, "w")44sumolib.xml.writeHeader(edges, root="edges")4546# main street (passenger car inflow)47nodeID = "main0"48print('<node id="in" x="-100" y="0"/>', file=nodes)49print('<edge id="mainin" from="in" to="%s" numLanes="2"/>' % nodeID, file=edges)50for row in range(DOUBLE_ROWS):51nextNodeID = "main%s" % row52x = row * ROW_DIST53print('<node id="%s" x="%s" y="0"/>' % (nextNodeID, x), file=nodes)54if row > 0:55print('<edge id="main%sto%s" from="%s" to="%s" numLanes="2"/>' %56(row - 1, row, nodeID, nextNodeID), file=edges)57nodeID = nextNodeID58print('<node id="out" x="%s" y="0"/>' % (x + 100), file=nodes)59print('<edge id="mainout" from="%s" to="out" numLanes="2"/>' % nodeID, file=edges)6061# cybercar infrastructure (automated bus)62y = (SLOTS_PER_ROW + 3) * SLOT_WIDTH63print('<node id="cyber" x="-100" y="%s"/>' % y, file=nodes)64print("""<edge id="cyberin" from="cyber" to="cyber0" numLanes="3" spreadType="center">65<lane index="0" allow="pedestrian" width="2.00"/>66<lane index="1" allow="taxi bus"/>67<lane index="2" allow="taxi bus"/>68</edge>""", file=edges)69print("""<edge id="-cyberin" from="cyber0" to="cyber" numLanes="2" spreadType="center">70<lane index="0" allow="taxi bus"/>71<lane index="1" allow="taxi bus"/>72</edge>""", file=edges)73for row in range(DOUBLE_ROWS):74nodeID = "cyber%s" % row75x = row * ROW_DIST76print('<node id="%s" x="%s" y="%s"/>' % (nodeID, x, y), file=nodes)77if row > 0:78edgeID = "cyber%sto%s" % (row - 1, row)79print("""<edge id="%s" from="cyber%s" to="cyber%s" numLanes="3" spreadType="center">80<lane index="0" allow="pedestrian" width="2.00"/>81<lane index="1" allow="taxi bus"/>82<lane index="2" allow="taxi bus"/>83</edge>""" % (edgeID, row - 1, row), file=edges)84print("""<edge id="-%s" from="cyber%s" to="cyber%s" numLanes="2" spreadType="center">85<lane index="0" allow="taxi bus"/>86<lane index="1" allow="taxi bus"/>87</edge>""" % (edgeID, row, row - 1), file=edges)88print('<node id="cyberend" x="%s" y="%s"/>' % (x + 100, y), file=nodes)89print("""<edge id="cyberout" from="cyber%s" to="cyberend" numLanes="3" spreadType="center">90<lane index="0" allow="pedestrian" width="2.00"/>91<lane index="1" allow="taxi bus"/>92<lane index="2" allow="taxi bus"/>93</edge>""" % row, file=edges)94print("""<edge id="-cyberout" from="cyberend" to="cyber%s" numLanes="2" spreadType="center">95<lane index="0" allow="taxi bus"/>96<lane index="1" allow="taxi bus"/>97</edge>""" % row, file=edges)9899# roads in the parking area100for row in range(DOUBLE_ROWS):101print("""<edge id="road%s" from="main%s" to="cyber%s" numLanes="2">102<lane index="0" allow="pedestrian" width="2.00"/>103<lane index="1" disallow="pedestrian"/>104</edge>""" % (row, row, row), file=edges)105print("""<edge id="-road%s" from="cyber%s" to="main%s" numLanes="2">106<lane index="0" allow="pedestrian" width="2.00"/>107<lane index="1" disallow="pedestrian"/>108</edge>""" % (row, row, row), file=edges)109110print("</nodes>", file=nodes)111nodes.close()112print("</edges>", file=edges)113edges.close()114115subprocess.call([sumolib.checkBinary('netconvert'),116'-n', '%s.nod.xml' % PREFIX,117'-e', '%s.edg.xml' % PREFIX,118'-o', '%s.net.xml' % PREFIX])119120# Bus Stops / Parking Areas121stops = open("%s.add.xml" % PREFIX, "w")122sumolib.xml.writeHeader(stops, root="additional")123for row in range(DOUBLE_ROWS):124print(""" <parkingArea id="ParkArea%s" lane="road%s_1" roadsideCapacity="%s" angle="270" length="8"/>125<parkingArea id="ParkArea-%s" lane="-road%s_1" roadsideCapacity="%s" angle="270" length="8"/>""" %126(row, row, SLOTS_PER_ROW, row, row, SLOTS_PER_ROW), file=stops)127128for row in range(DOUBLE_ROWS-1):129edgeID = "cyber%sto%s" % (row, row + 1)130print(' <busStop id="%sstop" lane="%s_1" startPos="2" endPos="12"/>' % (edgeID, edgeID), file=stops)131132# vehicle types133print((""" <vType id="car" color="0.7,0.7,0.7"/>134<vType id="ped_pedestrian" vClass="pedestrian" color="1,0.2,0.2"/>135<vType id="cybercar" vClass="taxi" length="%s" minGap="1" guiShape="evehicle" maxSpeed="%s"136color="green" emissionClass="HBEFA2/P_7_7" personCapacity="%s">137<param key="has.taxi.device" value="true"/>138</vType>139<vType id="bus" vClass="bus" color="blue" personCapacity="%s"/>140""") % (CYBER_LENGTH, CYBER_SPEED, CYBER_CAPACITY, BUS_CAPACITY), file=stops)141142print("</additional>", file=stops)143stops.close()144145# demand building146# routes cybercar and bus147with open("%s_cyber.rou.xml" % PREFIX, "w") as routes:148sumolib.xml.writeHeader(routes, root="routes")149print(""" <flow id="c" type="cybercar" begin="50" period="100" number="%s" line="taxi">150<route edges="cyberin cyber0to1"/>151</flow>152</routes>""" % (TOTAL_CAPACITY // CYBER_CAPACITY), file=routes)153154with open("%s_bus.rou.xml" % PREFIX, "w") as routes:155sumolib.xml.writeHeader(routes, root="routes")156print(""" <flow id="b" from="cyberin" to="cyberout" type="bus"157begin="50" period="100" number="%s" line="taxi">""" %158(TOTAL_CAPACITY // BUS_CAPACITY), file=routes)159for repeat in range(400 // TOTAL_CAPACITY):160for row in range(DOUBLE_ROWS-1):161edgeID = "cyber%sto%s" % (row, row + 1)162print(' <stop busStop="%sstop" duration="10"/>' % edgeID, file=routes)163print(' <stop edge="cyberout" duration="10"/>', file=routes)164print(" </flow>\n</routes>", file=routes)165166# passenger car demand167for period in range(5, 50, 5):168# routes person169routes = open("%s_demand%02i.rou.xml" % (PREFIX, period), "w")170print("<routes>", file=routes)171for v in range(SLOTS_PER_ROW):172for idx in range(DOUBLE_ROWS):173busStop = "cyber%sto%sstop" % (idx, idx+1) if idx < DOUBLE_ROWS - 1 else "cyber%sto%sstop" % (idx-1, idx)174print(""" <trip id="v%s.%s" type="car" depart="%s" from="mainin" to="road%s">175<stop parkingArea="ParkArea%s" duration="1000"/>176</trip>""" % (idx, v, v * period, idx, idx), file=routes)177print(""" <trip id="v-%s.%s" type="car" depart="%s" from="mainin" to="-road%s">178<stop parkingArea="ParkArea-%s" duration="1000"/>179</trip>""" % (idx, v, v * period, idx, idx), file=routes)180for p in range(random.randint(1, CAR_CAPACITY)):181for infix in ("", "-"):182vehId = "v%s%s.%s" % (infix, idx, v)183print(""" <person id="%sp%s" type="ped_pedestrian" depart="triggered">184<ride from="mainin" to="%sroad%s" lines="%s"/>185<walk busStop="%s"/>186<ride to="cyberout" lines="taxi"/>187</person>""" % (vehId, p, infix, idx, vehId, busStop), file=routes)188print("</routes>", file=routes)189routes.close()190191# sumo config, the "traditional" bus does not work currently192config = open("%s%02i.sumocfg" % (PREFIX, period), "w")193print("""<configuration>194<input>195<net-file value="%s.net.xml"/>196<route-files value="%s_bus.rou.xml,%s_demand%02i.rou.xml"/>197<additional-files value="%s.add.xml"/>198<no-step-log value="True"/>199<time-to-teleport value="0"/>200</input>201</configuration>""" % (PREFIX, PREFIX, PREFIX, period, PREFIX), file=config)202config.close()203204config = open("%s%02i_cyber.sumocfg" % (PREFIX, period), "w")205print("""<configuration>206<input>207<net-file value="%s.net.xml"/>208<route-files value="%s_cyber.rou.xml,%s_demand%02i.rou.xml"/>209<additional-files value="%s.add.xml"/>210<no-step-log value="True"/>211<time-to-teleport value="0"/>212<device.taxi.dispatch-algorithm value="routeExtension"/>213</input>214</configuration>""" % (PREFIX, PREFIX, PREFIX, period, PREFIX), file=config)215config.close()216217218