Skip to content

Commit c9c1c1a

Browse files
author
Lara CODECA
committed
Examples update
1 parent 8855bfb commit c9c1c1a

3 files changed

Lines changed: 134 additions & 6 deletions

File tree

examples/random.grid.example.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env python3
2+
3+
""" Example of usage of PyPML.
4+
5+
Python Parking Monitor Library (PyPML)
6+
Copyright (C) 2018
7+
Lara CODECA
8+
9+
This program is free software: you can redistribute it and/or modify
10+
it under the terms of the GNU General Public License as published by
11+
the Free Software Foundation, either version 3 of the License, or
12+
(at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
"""
22+
23+
import logging
24+
import os
25+
import sys
26+
import traceback
27+
from pypml import ParkingMonitor
28+
29+
# """ Import SUMO library """
30+
if 'SUMO_TOOLS' in os.environ:
31+
sys.path.append(os.environ['SUMO_110_TOOLS'])
32+
import traci
33+
else:
34+
sys.exit("Please declare environment variable 'SUMO_TOOLS'")
35+
36+
def _main():
37+
""" Example of parking management in SUMO. """
38+
39+
## TESTED WITH: SUMO 1.1.0
40+
traci.start(['/home/drone/Applications/SUMO/sumo-1.1.0/bin/sumo',
41+
'-c', 'random_grid/random.sumocfg'], port=42041)
42+
## Running with the last-monday development version
43+
# traci.start(['sumo-gui', '-c', 'test_scenario/sumo.simple.cfg'], port=42041)
44+
45+
parking_monitor_options = {
46+
'addStepListener': True,
47+
'logging': {
48+
'stdout': False,
49+
'filename': 'random.example.log',
50+
'level': logging.DEBUG,
51+
},
52+
'sumo_parking_file': 'random_grid/parkingArea.add.xml',
53+
'blacklist': [],
54+
'vclasses': {'delivery', 'motorcycle', 'passenger'},
55+
'generic_conf': [],
56+
'specific_conf': {},
57+
'subscriptions': {
58+
'only_parkings': True,
59+
},
60+
}
61+
62+
monitor = ParkingMonitor(traci, parking_monitor_options, 383.0)
63+
# parking travel time structure initialized
64+
monitor.compute_parking_travel_time()
65+
66+
while traci.simulation.getMinExpectedNumber() > 0:
67+
traci.simulationStep()
68+
69+
## PARKING OPTIMIZATION
70+
for vehicle in monitor.get_vehicle_iterator():
71+
if vehicle['arrived']:
72+
## the vehicle is not in the simulation anymore
73+
continue
74+
if not vehicle['edge'] or ':' in vehicle['edge']:
75+
## the vehicle is on an intersection and the change would not be safe.
76+
continue
77+
if vehicle['stops']:
78+
_, _, stopping_place, stop_flags, _, _ = vehicle['stops'][0]
79+
if monitor.is_parking_area(stop_flags):
80+
### OPTIMIZE VEHICLE
81+
availability = monitor.get_free_places(stopping_place,
82+
vclass=vehicle['vClass'],
83+
with_projections=False,
84+
with_uncertainty=False)
85+
if availability < 1:
86+
alternatives = monitor.get_closest_parkings(stopping_place, num=25)
87+
for trtime, alt in alternatives:
88+
alt_availability = monitor.get_free_places(
89+
alt, vclass=vehicle['vClass'],
90+
with_projections=False, with_uncertainty=False)
91+
print(trtime, alt, alt_availability)
92+
if alt_availability > 1:
93+
## reroute vehicle
94+
route = None
95+
try:
96+
edge = monitor.get_parking_access(alt).split('_')[0]
97+
route = traci.simulation.findRoute(
98+
vehicle['edge'], edge, vType=vehicle['vClass'])
99+
except traci.exceptions.TraCIException:
100+
route = None
101+
102+
if route and len(route.edges) >= 2:
103+
try:
104+
traci.vehicle.rerouteParkingArea(vehicle['id'], alt)
105+
print("""Vehicle {} is going to be rerouted from {} """
106+
"""[{}] to {} [{}].""".format(vehicle['id'],
107+
stopping_place,
108+
availability, alt,
109+
alt_availability))
110+
except traci.exceptions.TraCIException:
111+
print([monitor.get_parking_access(alt), route])
112+
raise
113+
break
114+
115+
if __name__ == '__main__':
116+
## ======================== PROFILER ======================== ##
117+
import cProfile, pstats, io
118+
profiler = cProfile.Profile()
119+
profiler.enable()
120+
## ======================== PROFILER ======================== ##
121+
122+
try:
123+
_main()
124+
except traci.exceptions.TraCIException:
125+
exc_type, exc_value, exc_traceback = sys.exc_info()
126+
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=10, file=sys.stdout)
127+
finally:
128+
traci.close()
129+
## ======================== PROFILER ======================== ##
130+
profiler.disable()
131+
results = io.StringIO()
132+
pstats.Stats(profiler, stream=results).sort_stats('cumulative').print_stats(25)
133+
print(results.getvalue())
134+
## ======================== PROFILER ======================== ##

examples/simple.example.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ def _main():
3737
""" Example of parking management in SUMO. """
3838

3939
## TESTED WITH: SUMO 1.1.0
40-
# traci.start(['/home/drone/Applications/SUMO/sumo-1.1.0/bin/sumo',
41-
# '-c', 'test_scenario/sumo.simple.cfg'], port=42041)
42-
## Running with the last-monday development version
4340
traci.start(['sumo', '-c', 'test_scenario/sumo.simple.cfg'], port=42041)
4441

4542
parking_monitor_options = {

examples/subscriptions.example.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ def _main():
3838
""" Example of parking management in SUMO. """
3939

4040
## TESTED WITH: SUMO 1.1.0
41-
# traci.start(['/home/drone/Applications/SUMO/sumo-1.1.0/bin/sumo',
42-
# '-c', 'test_scenario/sumo.subscriptions.cfg'], port=42042)
43-
## Running with the last-monday development version
4441
traci.start(['sumo', '-c', 'test_scenario/sumo.subscriptions.cfg'], port=42042)
4542

4643
parking_monitor_options = {

0 commit comments

Comments
 (0)