Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/examples/scripts/sumopy_sim.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
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 sumopy_sim.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
"""
20
sumopy_sim is a script to open and run a SUMOPy scenario
21
22
Usage: python sumopy_sim.py scenario.obj
23
24
"""
25
from agilepy.lib_base.logger import Logger
26
from coremodules.simulation import sumo
27
from coremodules.scenario import scenario
28
import sys
29
import os
30
import numpy as np
31
32
# point to the SUMOPy directory here
33
sys.path.append(os.path.join('..', '..'))
34
35
36
resultfilepath = None
37
if len(sys.argv) >= 2:
38
simfilepath = sys.argv[1]
39
if len(sys.argv) == 3:
40
resultfilepath = sys.argv[2]
41
else:
42
print __doc__
43
sys.exit(0)
44
45
myscenario = scenario.load_scenario(simfilepath)
46
rootfilepath = myscenario.get_rootfilepath()
47
if resultfilepath is None:
48
resultfilepath = rootfilepath+'.res.obj'
49
50
mylogger = Logger( # filepath = os.path.join(dirpath,logfilepath),
51
is_stdout=True, # False
52
)
53
54
microsim = sumo.Sumo(myscenario,
55
guimode='nogui', # 'sumopy','sumopy+map','native','openscene','nogui',
56
simtime_start=0,
57
simtime_end=600,
58
time_to_teleport=-1,
59
time_step=0.2, # s
60
is_ballistic_integrator=True,
61
#
62
# routing options
63
#
64
is_dynaroute=True, # = one shot assignment
65
is_rerouting=False, # enable rerouting devices
66
probability_rerouting=0.4,
67
is_deterministic_rerouting=False,
68
period_rerouting=180,
69
preperiod_rerouting=180,
70
adaptationinterval_rerouting=180,
71
adaptationweight_rerouting=0.0,
72
adaptationsteps_rerouting=10,
73
#
74
# decide what results are needed
75
#
76
is_edgedata=True,
77
is_tripdata=True,
78
is_edgenoise=True,
79
is_edgesemissions=True,
80
time_warmup=0, # s start sampling at this time
81
time_sample=60, # s result sampling
82
#
83
# run options
84
#
85
# is_runnow = False,# run immediately after init
86
is_start=True, # True,
87
is_quit_on_end=True,
88
#is_run_background = False,
89
#is_nohup = False,
90
#
91
# export options
92
#
93
is_export_net=True,
94
is_export_poly=False,
95
is_export_rou=True,
96
is_export_ptstops=True,
97
#
98
# other options
99
#
100
seed=1234,
101
is_collission_check_junctions=True,
102
collission_action='warn',
103
is_exclude_emptyedges=True,
104
is_exclude_emptylanes=True,
105
is_include_poly=True,
106
#logfilepath = sumologfilepath,
107
logger=mylogger,
108
)
109
110
microsim.do() # now open SUMO and run simulation
111
112
113
if resultfilepath is not None:
114
print 'saving results in', resultfilepath
115
myscenario.simulation.results.save(resultfilepath)
116
117
# import all results from xml and put them into myscenario.simulation.results
118
microsim.import_results()
119
edgeres = myscenario.simulation.results.edgeresults
120
tripres = myscenario.simulation.results.tripresults
121
122
edgeres.export_csv(rootfilepath+'.edgeres.csv',
123
sep=',',
124
# ids = [ list wit ids to export],
125
show_parentesis=True,
126
# name_id='ID',
127
is_header=True)
128
129
tripres.export_csv(rootfilepath+'.tripres.csv',
130
sep=',',
131
# ids = [ list wit ids to export],
132
show_parentesis=True,
133
# name_id='ID',
134
is_header=True)
135
136
# do some analyses
137
ids_tripres = tripres.get_ids()
138
print 'numer of arrived vehicles:', len(ids_tripres)
139
print 'Total triplength: %.2fKm' % (0.001*np.mean(tripres.routeLength[ids_tripres]))
140
print 'Average speed: %.2fKm/s' % (3.6*np.mean(tripres.routeLength[ids_tripres]/tripres.duration[ids_tripres]))
141
142
ids_edgeres = edgeres.get_ids()
143
print 'Total fuel consumption: %.2f liter' % (0.001*np.sum(edgeres.fuel_abs[ids_edgeres]))
144
145