|
| 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_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(['sumo', '-c', 'test_scenario/sumo.simple.cfg'], port=42044) |
| 41 | + |
| 42 | + parking_monitor_options = { |
| 43 | + 'seed': 42, |
| 44 | + 'addStepListener': True, |
| 45 | + 'logging': { |
| 46 | + 'stdout': False, |
| 47 | + 'filename': 'uncert.example.log', |
| 48 | + 'level': logging.DEBUG, |
| 49 | + }, |
| 50 | + 'sumo_parking_file': 'test_scenario/parkings.small.add.xml', |
| 51 | + 'blacklist': [], |
| 52 | + 'vclasses': {'truck', 'passenger', 'motorcycle'}, |
| 53 | + 'generic_conf': [ |
| 54 | + { |
| 55 | + 'cond': ['<', 'total_capacity', 25], |
| 56 | + 'set_to': [ |
| 57 | + ['uncertainty', { |
| 58 | + 'mu': 0.0, |
| 59 | + 'sigma': ['*', 'total_capacity', 0.10] |
| 60 | + } |
| 61 | + ], |
| 62 | + ], |
| 63 | + }, |
| 64 | + ], |
| 65 | + 'specific_conf': {}, |
| 66 | + 'subscriptions': { |
| 67 | + 'only_parkings': True, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + monitor = ParkingMonitor(traci, parking_monitor_options) |
| 72 | + # parking travel time structure initialized |
| 73 | + monitor.compute_parking_travel_time() |
| 74 | + |
| 75 | + while traci.simulation.getMinExpectedNumber() > 0: |
| 76 | + traci.simulationStep() |
| 77 | + |
| 78 | + ## PARKING OPTIMIZATION |
| 79 | + for vehicle in monitor.get_vehicle_iterator(): |
| 80 | + if vehicle['arrived']: |
| 81 | + ## the vehicle is not in the simulation anymore |
| 82 | + continue |
| 83 | + if vehicle['stopped']: |
| 84 | + ## the vehicle is stopped and it does not require additional |
| 85 | + ## parking changes at least for the moment |
| 86 | + continue |
| 87 | + if not vehicle['edge'] or ':' in vehicle['edge']: |
| 88 | + ## the vehicle is on an intersection and the change would not be safe. |
| 89 | + continue |
| 90 | + if vehicle['stops']: |
| 91 | + _, _, stopping_place, stop_flags, _, _ = vehicle['stops'][0] |
| 92 | + if monitor.is_parking_area(stop_flags): |
| 93 | + ### OPTIMIZE VEHICLE |
| 94 | + availability = monitor.get_free_places(stopping_place, |
| 95 | + vclass=vehicle['vClass'], |
| 96 | + with_projections=False, |
| 97 | + with_uncertainty=True) |
| 98 | + if availability < 1: |
| 99 | + alternatives = monitor.get_closest_parkings(stopping_place, num=25) |
| 100 | + for trtime, alt in alternatives: |
| 101 | + alt_availability = monitor.get_free_places( |
| 102 | + alt, vclass=vehicle['vClass'], |
| 103 | + with_projections=False, with_uncertainty=True) |
| 104 | + print(trtime, alt, alt_availability) |
| 105 | + if alt_availability > 1: |
| 106 | + ## reroute vehicle |
| 107 | + route = None |
| 108 | + try: |
| 109 | + edge = monitor.get_parking_access(alt).split('_')[0] |
| 110 | + route = traci.simulation.findRoute( |
| 111 | + vehicle['edge'], edge, vType=vehicle['vClass']) |
| 112 | + except traci.exceptions.TraCIException: |
| 113 | + route = None |
| 114 | + |
| 115 | + if route and len(route.edges) >= 2: |
| 116 | + try: |
| 117 | + traci.vehicle.rerouteParkingArea(vehicle['id'], alt) |
| 118 | + print("""Vehicle {} is going to be rerouted from {} """ |
| 119 | + """[{}] to {} [{}].""".format(vehicle['id'], |
| 120 | + stopping_place, |
| 121 | + availability, alt, |
| 122 | + alt_availability)) |
| 123 | + except traci.exceptions.TraCIException: |
| 124 | + print([monitor.get_parking_access(alt), route]) |
| 125 | + raise |
| 126 | + break |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + ## ======================== PROFILER ======================== ## |
| 130 | + import cProfile, pstats, io |
| 131 | + profiler = cProfile.Profile() |
| 132 | + profiler.enable() |
| 133 | + ## ======================== PROFILER ======================== ## |
| 134 | + |
| 135 | + try: |
| 136 | + _main() |
| 137 | + except traci.exceptions.TraCIException: |
| 138 | + exc_type, exc_value, exc_traceback = sys.exc_info() |
| 139 | + traceback.print_exception(exc_type, exc_value, exc_traceback, limit=10, file=sys.stdout) |
| 140 | + finally: |
| 141 | + traci.close() |
| 142 | + ## ======================== PROFILER ======================== ## |
| 143 | + profiler.disable() |
| 144 | + results = io.StringIO() |
| 145 | + pstats.Stats(profiler, stream=results).sort_stats('cumulative').print_stats(25) |
| 146 | + print(results.getvalue()) |
| 147 | + ## ======================== PROFILER ======================== ## |
0 commit comments