From 70b29c47b25f538272c4d0c4277e974ffc9f1344 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Thu, 6 Dec 2018 21:49:01 +0100 Subject: [PATCH] Changed config and added lock area mode --- sumo_project/actions.py | 27 ++++++++++++++++++++------- sumo_project/config.py | 23 ++++++++++++++++++----- sumo_project/emissions.py | 25 ++++++++++++++----------- sumo_project/model.py | 1 + 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 2a9dc60..483abb4 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -21,16 +21,19 @@ def compute_edge_weight(edge_id): + traci.edge.getNOxEmission(edge_id) + traci.edge.getHCEmission(edge_id) + traci.edge.getPMxEmission(edge_id) - + traci.edge.getCO2Emission(edge_id)) + + traci.edge.getCO2Emission(edge_id))/(traci.edge.getLaneNumber(edge_id)) -def adjust_edges_weights(): +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) - + traci.edge.setEffort(edge_id, weight) + + for veh_id in traci.vehicle.getIDList(): + traci.vehicle.rerouteEffort(veh_id) + 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 + area.limited_speed = True for lane in area._lanes: traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6) @@ -48,5 +51,15 @@ def adjust_traffic_light_phase_duration(area, reduction_factor): for logic in tl._logics: traci.trafficlights.setCompleteRedYellowGreenDefinition(tl.tl_id, modifyLogic(logic,reduction_factor)) - #phaseDuration = traci.trafficlight.getPhaseDuration(tl.tl_id) - #traci.trafficlight.setPhaseDuration(tl.tl_id, phaseDuration*reduction_factor) \ No newline at end of file +def lock_area(area): + #Trying to lock area + vehicles_in_area = 0 + for lane in area._lanes: + vehicles_in_area += traci.lane.getLastStepVehicleNumber(lane.lane_id) + + #Waiting for the area to be empty before blocking it + if vehicles_in_area == 0: + area.locked = True + print(f'Area locked : {area.name}') + for lane in area._lanes: + traci.lane.setDisallowed(lane.lane_id, 'passenger') \ No newline at end of file diff --git a/sumo_project/config.py b/sumo_project/config.py index 1538099..51fe185 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -17,19 +17,22 @@ CELLS_NUMBER = 10 EMISSIONS_THRESHOLD = 500000 n_steps = 200 -#Vehicles are routed according to the less polluted route -weight_routing_mode = False - #Limit the speed into areas when the threshold is exceeded limited_speed = 30 limit_speed_mode = False #Decrease all traffic lights duration into the area when the threshold is exceeded -rf_trafficLights_duration = 0.2 +rf_trafficLights_duration = 1.4 adjust_traffic_light_mode = False #Immediately delete all vehicles in the simulation area -remove_vehicles_mode = True +remove_vehicles_mode = False + +#Vehicles are routed according to the less polluted route (HEAVY) +weight_routing_mode = True + +#Lock the area when the threshold is exceeded (NOT FIXED) +lock_area_mode = False #Weight routing mode cannot be combinated with other actions if weight_routing_mode: @@ -39,6 +42,16 @@ if weight_routing_mode: sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD) sumo_cmd = [sumo_binary, "-c", _SUMOCFG] +# Total of emissions of all pollutants in mg for n steps of simulation without locking areas +total_emissions200 = 43970763.15084749 +total_emissions300 = 87382632.08217141 + +def get_basics_emissions(): + if n_steps == 200: + return total_emissions200 + if n_steps == 300: + return total_emissions300 + def showConfig(): return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n') + str(f'step number = {n_steps}\n') diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 127cfa6..be52a1f 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -1,3 +1,5 @@ +# -*- coding: latin-1 -*- + from typing import List import traci @@ -59,15 +61,17 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]): for vehicle in vehicles: if vehicle.pos in area: area.emissions += vehicle.emissions - if area.emissions > config.EMISSIONS_THRESHOLD and not area.locked: - if config.limit_speed_mode: + if area.emissions > config.EMISSIONS_THRESHOLD: + + if config.limit_speed_mode and not area.limited_speed: actions.limit_speed_into_area(area, vehicles, config.limited_speed) traci.polygon.setColor(area.name, (255, 0, 0)) traci.polygon.setFilled(area.name, True) if config.adjust_traffic_light_mode: actions.adjust_traffic_light_phase_duration(area, config.rf_trafficLights_duration) - '''if config.remove_vehicles_mode: - actions.remove_vehicles(vehicles)''' #Véhicules à mettre en donnée membre car variable + + if config.lock_area_mode and not area.locked: + actions.lock_area(area) def parsePhase(phase_repr): @@ -127,15 +131,14 @@ def main(): total_emissions = 0 for area in grid: total_emissions += area.emissions - - # Total of emissions of all pollutants in mg for n steps of simulation without locking areas - total_emissions200 = 43970763.15084749 - total_emissions300 = 87382632.08217141 - + 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} %') + + ref = config.get_basics_emissions() + diff_with_actions = (ref - total_emissions)/ref + + print(f'Reduction percentage of emissions = {diff_with_actions*100} %') print("**** With the configuration : ****\n" + str(config.showConfig())) diff --git a/sumo_project/model.py b/sumo_project/model.py index 9fde8eb..736e3e6 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -46,6 +46,7 @@ class TrafficLight: class Area: def __init__(self, coords, name=''): + self.limited_speed = False self.locked = False self.rectangle = Polygon(coords) self.name = name