Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/traci_taxi/runner.py
169685 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 runner.py
16
# @author Michael Behrisch
17
# @author Daniel Krajzewicz
18
# @author Melanie Weber
19
# @author Lara Kaiser
20
# @date 2021-03-29
21
22
23
from __future__ import absolute_import
24
from __future__ import print_function
25
import os
26
import sys
27
import optparse
28
29
30
# we need to import python modules from the $SUMO_HOME/tools directory
31
if 'SUMO_HOME' in os.environ:
32
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
33
sys.path.append(tools)
34
else:
35
sys.exit("please declare environment variable 'SUMO_HOME'")
36
37
from sumolib import checkBinary # noqa
38
import traci # noqa
39
import sumolib # noqa
40
41
42
# Creating 4 taxis
43
def createTaxi(time):
44
for i in range(0, 20, 5):
45
# declaring the name(unique), route(from demand.route.xml), type of vehicle(declared in demand.route.xml),
46
# depart time, and line
47
traci.vehicle.add('taxiV%i' % i, 'route_0',
48
'taxi', depart=time, line='taxi')
49
50
51
# Dispatching taxis to cater to people waiting at a busstop
52
def emergencyTaxi(busstopID):
53
# getting a Id-list of people waiting at the busstop
54
peopleWaiting = traci.busstop.getPersonIDs(busstopID)
55
print("PersonId: \n", peopleWaiting)
56
pickup = []
57
# creating a list with the taxi resevations
58
for i, val in enumerate(peopleWaiting):
59
pickup.append(traci.person.getTaxiReservations(0)[i].id)
60
# if one Taxi should pick up all customers, the list needs to clarify the drop off
61
# hence the pickup is extendet by the order of drop offs
62
# pickup.extend(pickup)
63
print("PickupList:\n", pickup)
64
65
try:
66
fleet = traci.vehicle.getTaxiFleet(0)
67
except (traci.exceptions.FatalTraCIError):
68
print("No unoccupied taxi-fleet!")
69
# dispatching the unoccupied taxis to pick up their designated customers
70
for i, val in enumerate(peopleWaiting):
71
print("Taxifleet-Status")
72
print("empty: ", traci.vehicle.getTaxiFleet(0))
73
print("pickup: ", traci.vehicle.getTaxiFleet(1))
74
traci.vehicle.dispatchTaxi(fleet[i], pickup[i])
75
76
77
# checks if busstop is "overflowing"
78
def busstopCheck():
79
# getting all bus stops on the map
80
busstops = traci.busstop.getIDList()
81
# checking the personcount of waiting people
82
for i in busstops:
83
if traci.busstop.getPersonCount(i) >= 4:
84
emergencyTaxi(i)
85
86
87
def run():
88
"""execute the TraCI control loop"""
89
step = 0
90
time = 90
91
while traci.simulation.getMinExpectedNumber() > 0:
92
traci.simulationStep()
93
step += 1
94
if step == time:
95
createTaxi(time)
96
if step == 150:
97
busstopCheck()
98
if step == 5000:
99
break
100
traci.close()
101
sys.stdout.flush()
102
103
104
def get_options():
105
optParser = optparse.OptionParser()
106
optParser.add_option("--nogui", action="store_true",
107
default=False, help="run the commandline version of sumo")
108
options, args = optParser.parse_args()
109
return options
110
111
112
if __name__ == "__main__":
113
# first, generate the route file for this simulation
114
options = get_options()
115
# If you want to run this tutorial please uncomment following lines, that define the sumoBinary
116
# and delete the line before traci.start, to use the gui
117
# if options.nogui:
118
# sumoBinary = checkBinary('sumo')
119
# else:
120
# sumoBinary = checkBinary('sumo-gui')
121
# this is the normal way of using traci. sumo is started as a
122
# subprocess and then the python script connects and runs
123
sumoBinary = checkBinary('sumo')
124
traci.start([sumoBinary, "-c", "data/sumo.sumocfg",
125
"--tripinfo-output", "data/tripinfo.xml"])
126
run()
127
128