Path: blob/main/tests/complex/tutorial/traci_taxi/runner.py
169685 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 runner.py15# @author Michael Behrisch16# @author Daniel Krajzewicz17# @author Melanie Weber18# @author Lara Kaiser19# @date 2021-03-29202122from __future__ import absolute_import23from __future__ import print_function24import os25import sys26import optparse272829# we need to import python modules from the $SUMO_HOME/tools directory30if 'SUMO_HOME' in os.environ:31tools = os.path.join(os.environ['SUMO_HOME'], 'tools')32sys.path.append(tools)33else:34sys.exit("please declare environment variable 'SUMO_HOME'")3536from sumolib import checkBinary # noqa37import traci # noqa38import sumolib # noqa394041# Creating 4 taxis42def createTaxi(time):43for i in range(0, 20, 5):44# declaring the name(unique), route(from demand.route.xml), type of vehicle(declared in demand.route.xml),45# depart time, and line46traci.vehicle.add('taxiV%i' % i, 'route_0',47'taxi', depart=time, line='taxi')484950# Dispatching taxis to cater to people waiting at a busstop51def emergencyTaxi(busstopID):52# getting a Id-list of people waiting at the busstop53peopleWaiting = traci.busstop.getPersonIDs(busstopID)54print("PersonId: \n", peopleWaiting)55pickup = []56# creating a list with the taxi resevations57for i, val in enumerate(peopleWaiting):58pickup.append(traci.person.getTaxiReservations(0)[i].id)59# if one Taxi should pick up all customers, the list needs to clarify the drop off60# hence the pickup is extendet by the order of drop offs61# pickup.extend(pickup)62print("PickupList:\n", pickup)6364try:65fleet = traci.vehicle.getTaxiFleet(0)66except (traci.exceptions.FatalTraCIError):67print("No unoccupied taxi-fleet!")68# dispatching the unoccupied taxis to pick up their designated customers69for i, val in enumerate(peopleWaiting):70print("Taxifleet-Status")71print("empty: ", traci.vehicle.getTaxiFleet(0))72print("pickup: ", traci.vehicle.getTaxiFleet(1))73traci.vehicle.dispatchTaxi(fleet[i], pickup[i])747576# checks if busstop is "overflowing"77def busstopCheck():78# getting all bus stops on the map79busstops = traci.busstop.getIDList()80# checking the personcount of waiting people81for i in busstops:82if traci.busstop.getPersonCount(i) >= 4:83emergencyTaxi(i)848586def run():87"""execute the TraCI control loop"""88step = 089time = 9090while traci.simulation.getMinExpectedNumber() > 0:91traci.simulationStep()92step += 193if step == time:94createTaxi(time)95if step == 150:96busstopCheck()97if step == 5000:98break99traci.close()100sys.stdout.flush()101102103def get_options():104optParser = optparse.OptionParser()105optParser.add_option("--nogui", action="store_true",106default=False, help="run the commandline version of sumo")107options, args = optParser.parse_args()108return options109110111if __name__ == "__main__":112# first, generate the route file for this simulation113options = get_options()114# If you want to run this tutorial please uncomment following lines, that define the sumoBinary115# and delete the line before traci.start, to use the gui116# if options.nogui:117# sumoBinary = checkBinary('sumo')118# else:119# sumoBinary = checkBinary('sumo-gui')120# this is the normal way of using traci. sumo is started as a121# subprocess and then the python script connects and runs122sumoBinary = checkBinary('sumo')123traci.start([sumoBinary, "-c", "data/sumo.sumocfg",124"--tripinfo-output", "data/tripinfo.xml"])125run()126127128