mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Changed config and added lock area mode
This commit is contained in:
		@@ -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():     
 | 
			
		||||
    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)
 | 
			
		||||
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')
 | 
			
		||||
@@ -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')
 | 
			
		||||
 
 | 
			
		||||
@@ -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 <20> mettre en donn<6E>e membre car variable 
 | 
			
		||||
            
 | 
			
		||||
            if config.lock_area_mode and not area.locked:
 | 
			
		||||
                actions.lock_area(area)
 | 
			
		||||
 | 
			
		||||
                
 | 
			
		||||
def parsePhase(phase_repr):
 | 
			
		||||
@@ -128,14 +132,13 @@ def main():
 | 
			
		||||
        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()))
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user