Path: blob/main/tools/contributed/sumopy/examples/scripts/sumopy_sim.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# 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 sumopy_sim.py15# @author Joerg Schweizer16# @date 20121718"""19sumopy_sim is a script to open and run a SUMOPy scenario2021Usage: python sumopy_sim.py scenario.obj2223"""24from agilepy.lib_base.logger import Logger25from coremodules.simulation import sumo26from coremodules.scenario import scenario27import sys28import os29import numpy as np3031# point to the SUMOPy directory here32sys.path.append(os.path.join('..', '..'))333435resultfilepath = None36if len(sys.argv) >= 2:37simfilepath = sys.argv[1]38if len(sys.argv) == 3:39resultfilepath = sys.argv[2]40else:41print __doc__42sys.exit(0)4344myscenario = scenario.load_scenario(simfilepath)45rootfilepath = myscenario.get_rootfilepath()46if resultfilepath is None:47resultfilepath = rootfilepath+'.res.obj'4849mylogger = Logger( # filepath = os.path.join(dirpath,logfilepath),50is_stdout=True, # False51)5253microsim = sumo.Sumo(myscenario,54guimode='nogui', # 'sumopy','sumopy+map','native','openscene','nogui',55simtime_start=0,56simtime_end=600,57time_to_teleport=-1,58time_step=0.2, # s59is_ballistic_integrator=True,60#61# routing options62#63is_dynaroute=True, # = one shot assignment64is_rerouting=False, # enable rerouting devices65probability_rerouting=0.4,66is_deterministic_rerouting=False,67period_rerouting=180,68preperiod_rerouting=180,69adaptationinterval_rerouting=180,70adaptationweight_rerouting=0.0,71adaptationsteps_rerouting=10,72#73# decide what results are needed74#75is_edgedata=True,76is_tripdata=True,77is_edgenoise=True,78is_edgesemissions=True,79time_warmup=0, # s start sampling at this time80time_sample=60, # s result sampling81#82# run options83#84# is_runnow = False,# run immediately after init85is_start=True, # True,86is_quit_on_end=True,87#is_run_background = False,88#is_nohup = False,89#90# export options91#92is_export_net=True,93is_export_poly=False,94is_export_rou=True,95is_export_ptstops=True,96#97# other options98#99seed=1234,100is_collission_check_junctions=True,101collission_action='warn',102is_exclude_emptyedges=True,103is_exclude_emptylanes=True,104is_include_poly=True,105#logfilepath = sumologfilepath,106logger=mylogger,107)108109microsim.do() # now open SUMO and run simulation110111112if resultfilepath is not None:113print 'saving results in', resultfilepath114myscenario.simulation.results.save(resultfilepath)115116# import all results from xml and put them into myscenario.simulation.results117microsim.import_results()118edgeres = myscenario.simulation.results.edgeresults119tripres = myscenario.simulation.results.tripresults120121edgeres.export_csv(rootfilepath+'.edgeres.csv',122sep=',',123# ids = [ list wit ids to export],124show_parentesis=True,125# name_id='ID',126is_header=True)127128tripres.export_csv(rootfilepath+'.tripres.csv',129sep=',',130# ids = [ list wit ids to export],131show_parentesis=True,132# name_id='ID',133is_header=True)134135# do some analyses136ids_tripres = tripres.get_ids()137print 'numer of arrived vehicles:', len(ids_tripres)138print 'Total triplength: %.2fKm' % (0.001*np.mean(tripres.routeLength[ids_tripres]))139print 'Average speed: %.2fKm/s' % (3.6*np.mean(tripres.routeLength[ids_tripres]/tripres.duration[ids_tripres]))140141ids_edgeres = edgeres.get_ids()142print 'Total fuel consumption: %.2f liter' % (0.001*np.sum(edgeres.fuel_abs[ids_edgeres]))143144145