diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 7c61488..298ebe2 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -1,44 +1,42 @@ -""" -Created on 17 oct. 2018 - -@author: Axel Huynh-Phuc, Thibaud Gasser -""" -from typing import Iterable - -import traci -from shapely.geometry.linestring import LineString - -from model import Area, Vehicle - - -def remove_vehicle(veh_id): - traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING) - - -def lanes_in_area(area): - for lane_id in traci.lane.getIDList(): - polygon_lane = LineString(traci.lane.getShape(lane_id)) - if area.rectangle.intersects(polygon_lane): - yield lane_id - - -def compute_edge_weight(edge_id): - return (traci.edge.getCOEmission(edge_id) - + traci.edge.getNOxEmission(edge_id) - + traci.edge.getHCEmission(edge_id) - + traci.edge.getPMxEmission(edge_id) - + traci.edge.getCO2Emission(edge_id)) - - -def adjust_edges_weights(): - for edge_id in traci.edge.getIDList(): - weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed - traci.edge.adaptTraveltime(edge_id, weight) - - -def lock_area(area: Area): - max_speed = 30 - print(f'Setting max speed into {area.name} to {max_speed} km/h') - area.locked = True - for lane in area._lanes: - traci.lane.setMaxSpeed(lane.lane_id, max_speed / 3.6) +""" +Created on 17 oct. 2018 + +@author: Axel Huynh-Phuc, Thibaud Gasser +""" +from typing import Iterable + +import traci +from shapely.geometry.linestring import LineString + +from model import Area, Vehicle + + +def remove_vehicle(veh_id): + traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING) + + +def compute_edge_weight(edge_id): + return (traci.edge.getCOEmission(edge_id) + + traci.edge.getNOxEmission(edge_id) + + traci.edge.getHCEmission(edge_id) + + traci.edge.getPMxEmission(edge_id) + + traci.edge.getCO2Emission(edge_id)) + + +def adjust_edges_weights(): + for edge_id in traci.edge.getIDList(): + weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed + traci.edge.adaptTraveltime(edge_id, weight) + + +def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], max_speed): + print(f'Setting max speed into {area.name} to {max_speed} km/h') + area.locked = True + for lane in area._lanes: + traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6) + + +def adjust_traffic_light_phase_duration(): + '''for tl_id in traci.trafficlight.getIDList(): + print(traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id))''' + \ No newline at end of file diff --git a/sumo_project/config.py b/sumo_project/config.py index a27bd0f..5b1beb8 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -1,26 +1,31 @@ -""" -Global configuration for the simulation -""" - -import os -import sys - -if 'SUMO_HOME' in os.environ: - tools = os.path.join(os.environ['SUMO_HOME'], 'tools') - sys.path.append(tools) -else: - sys.exit("please declare environment variable 'SUMO_HOME'") - -_SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI -_SUMOCFG = "mulhouse_simulation/osm.sumocfg" -CELLS_NUMBER = 10 -EMISSIONS_THRESHOLD = 500000 -n_steps = 200 - -lock_mode = True -routing_mode = False - -sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD) -sumo_cmd = [sumo_binary, "-c", _SUMOCFG] - - +""" +Global configuration for the simulation +""" + +import os +import sys + +if 'SUMO_HOME' in os.environ: + tools = os.path.join(os.environ['SUMO_HOME'], 'tools') + sys.path.append(tools) +else: + sys.exit("please declare environment variable 'SUMO_HOME'") + +_SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI +_SUMOCFG = "mulhouse_simulation/osm.sumocfg" +CELLS_NUMBER = 10 +EMISSIONS_THRESHOLD = 500000 +n_steps = 200 + +lock_mode = True +routing_mode = False + +sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD) +sumo_cmd = [sumo_binary, "-c", _SUMOCFG] + +def showConfig(): + return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n') + + str(f'step number = {n_steps}\n') + + str(f'lock mode = {lock_mode}\n') + + str(f'routing mode = {routing_mode}\n')) + diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 2c390c7..f199357 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -39,7 +39,6 @@ def get_all_vehicles() -> List[Vehicle]: veh_pos = traci.vehicle.getPosition(veh_id) vehicle = Vehicle(veh_id, veh_pos) vehicle.emissions = compute_vehicle_emissions(veh_id) - traci.vehicle.setRoutingMode(veh_id, traci.constants.ROUTING_MODE_AGGREGATED) vehicles.append(vehicle) return vehicles @@ -58,7 +57,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]): if vehicle.pos in area: area.emissions += vehicle.emissions if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked: - actions.lock_area(area) + actions.limit_speed_into_area(area, vehicles,30) traci.polygon.setColor(area.name, (255, 0, 0)) traci.polygon.setFilled(area.name, True) @@ -77,9 +76,11 @@ def main(): traci.start(config.sumo_cmd) grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER) add_lanes_to_areas(grid) - - step = 0 - while step < config.n_steps: # traci.simulation.getMinExpectedNumber() > 0: + + actions.adjust_traffic_light_phase_duration() + + step = 0 + while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() vehicles = get_all_vehicles() @@ -90,7 +91,8 @@ def main(): # actions.rerouteAllVehicles() step += 1 - sys.stdout.write(f'Simulation step = {step}/{config.n_steps}' + '\r') + progress = round(step/config.n_steps*100,2) + sys.stdout.write(f'Progress : {progress}%'+'\r') sys.stdout.flush() finally: @@ -99,14 +101,15 @@ def main(): total_emissions = 0 for area in grid: total_emissions += area.emissions - - # Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas - total_emissions200 = 43970763.15084749 - - print(f'\n**** Total emissions = {total_emissions} mg ****') - diff_with_lock = (total_emissions200 - total_emissions) / total_emissions200 - print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n') - - + + #Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas + total_emissions200 = 43970763.15084749 + + print("\n**** RESULTS ****") + print(f'Total emissions = {total_emissions} mg') + diff_with_lock = (total_emissions200 - total_emissions)/total_emissions200 + print(f'Reduction percentage of emissions = {diff_with_lock*100} %') + print("With the configuration :\n" + str(config.showConfig())) + if __name__ == '__main__': main()