mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Added reverse_actions method
This commit is contained in:
		@@ -11,11 +11,6 @@ from shapely.geometry.linestring import LineString
 | 
				
			|||||||
from model import Area, Vehicle
 | 
					from model import Area, Vehicle
 | 
				
			||||||
from traci._trafficlight import Logic
 | 
					from traci._trafficlight import Logic
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def remove_vehicles(vehicles):
 | 
					 | 
				
			||||||
    print(f'Removed {vehicles.size} vehicles from the simulation')
 | 
					 | 
				
			||||||
    for vehicle in vehicles:
 | 
					 | 
				
			||||||
        traci.vehicle.remove(vehicle.veh_id, traci.constants.REMOVE_PARKING)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def compute_edge_weight(edge_id):
 | 
					def compute_edge_weight(edge_id):
 | 
				
			||||||
    return (traci.edge.getCOEmission(edge_id)
 | 
					    return (traci.edge.getCOEmission(edge_id)
 | 
				
			||||||
            + traci.edge.getNOxEmission(edge_id)
 | 
					            + traci.edge.getNOxEmission(edge_id)
 | 
				
			||||||
@@ -60,3 +55,24 @@ def lock_area(area):
 | 
				
			|||||||
    area.locked = True
 | 
					    area.locked = True
 | 
				
			||||||
    for lane in area._lanes:
 | 
					    for lane in area._lanes:
 | 
				
			||||||
        traci.lane.setDisallowed(lane.lane_id, 'passenger')
 | 
					        traci.lane.setDisallowed(lane.lane_id, 'passenger')
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					def reverse_actions(area):
 | 
				
			||||||
 | 
					    #Reset max speed to original 
 | 
				
			||||||
 | 
					    if not area.limited_speed:
 | 
				
			||||||
 | 
					        area.limited_speed = False
 | 
				
			||||||
 | 
					        for lane in area.lanes:
 | 
				
			||||||
 | 
					            traci.lane.setMaxSpeed(lane.lane_id, lane.initial_max_speed / 3.6)
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    #Reset traffic lights initial duration  
 | 
				
			||||||
 | 
					    if not area.tls_adjusted:
 | 
				
			||||||
 | 
					        area.tls_adjusted = False
 | 
				
			||||||
 | 
					        for initial_logic in tl._logics:
 | 
				
			||||||
 | 
					            traci.trafficlights.setCompleteRedYellowGreenDefinition(tl.tl_id, initial_logic)
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    #Unlock the area 
 | 
				
			||||||
 | 
					    if not area.locked:
 | 
				
			||||||
 | 
					        area.locked = False
 | 
				
			||||||
 | 
					        for lane in area._lanes:
 | 
				
			||||||
 | 
					            traci.lane.setAllowed(lane.lane_id, '') #empty means all classes are allowed 
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
@@ -41,6 +41,9 @@ n_steps = 200
 | 
				
			|||||||
########################## ACTIONS CONFIGURATION ##############################
 | 
					########################## ACTIONS CONFIGURATION ##############################
 | 
				
			||||||
###############################################################################
 | 
					###############################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#Set this mode to True if you want running a basic simulation without actions 
 | 
				
			||||||
 | 
					without_actions_mode = False 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#Limit the speed into areas when the threshold is exceeded
 | 
					#Limit the speed into areas when the threshold is exceeded
 | 
				
			||||||
speed_rf = 0.1
 | 
					speed_rf = 0.1
 | 
				
			||||||
limit_speed_mode = True
 | 
					limit_speed_mode = True
 | 
				
			||||||
@@ -49,9 +52,6 @@ limit_speed_mode = True
 | 
				
			|||||||
trafficLights_duration_rf = 0.2
 | 
					trafficLights_duration_rf = 0.2
 | 
				
			||||||
adjust_traffic_light_mode = True
 | 
					adjust_traffic_light_mode = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#Immediately delete all vehicles in the simulation area
 | 
					 | 
				
			||||||
remove_vehicles_mode = False
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#Vehicles are routed according to the less polluted route (HEAVY)
 | 
					#Vehicles are routed according to the less polluted route (HEAVY)
 | 
				
			||||||
weight_routing_mode = False
 | 
					weight_routing_mode = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -63,6 +63,13 @@ if weight_routing_mode:
 | 
				
			|||||||
    limit_speed_mode = False
 | 
					    limit_speed_mode = False
 | 
				
			||||||
    adjust_traffic_light_mode = False
 | 
					    adjust_traffic_light_mode = False
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					#If without_actions_mode is choosen 
 | 
				
			||||||
 | 
					if without_actions_mode:
 | 
				
			||||||
 | 
					    limit_speed_mode = False
 | 
				
			||||||
 | 
					    adjust_traffic_light_mode = False
 | 
				
			||||||
 | 
					    weight_routing_mode = False
 | 
				
			||||||
 | 
					    lock_area_mode = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
###############################################################################
 | 
					###############################################################################
 | 
				
			||||||
########################## SIMULATION REFERENCES ##############################
 | 
					########################## SIMULATION REFERENCES ##############################
 | 
				
			||||||
###############################################################################
 | 
					###############################################################################
 | 
				
			||||||
@@ -70,6 +77,8 @@ if weight_routing_mode:
 | 
				
			|||||||
# Total of emissions of all pollutants in mg for n steps of simulation without locking areas
 | 
					# Total of emissions of all pollutants in mg for n steps of simulation without locking areas
 | 
				
			||||||
total_emissions200 = 43970763.15084749  
 | 
					total_emissions200 = 43970763.15084749  
 | 
				
			||||||
total_emissions300 = 87382632.08217141
 | 
					total_emissions300 = 87382632.08217141
 | 
				
			||||||
 | 
					total_emissions400 = 140757491.8489904
 | 
				
			||||||
 | 
					total_emissions500 = 202817535.43856794
 | 
				
			||||||
 | 
					
 | 
				
			||||||
###############################################################################
 | 
					###############################################################################
 | 
				
			||||||
########################## CONFIGURATION METHODS ##############################
 | 
					########################## CONFIGURATION METHODS ##############################
 | 
				
			||||||
@@ -80,8 +89,12 @@ def get_basics_emissions():
 | 
				
			|||||||
        return total_emissions200
 | 
					        return total_emissions200
 | 
				
			||||||
    if n_steps == 300:
 | 
					    if n_steps == 300:
 | 
				
			||||||
        return total_emissions300
 | 
					        return total_emissions300
 | 
				
			||||||
 | 
					    if n_steps == 400:
 | 
				
			||||||
 | 
					        return total_emissions400
 | 
				
			||||||
 | 
					    if n_steps == 500:
 | 
				
			||||||
 | 
					        return total_emissions500
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def showConfig():
 | 
					def show_config():
 | 
				
			||||||
    return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n')
 | 
					    return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n')
 | 
				
			||||||
    + str(f'step number = {n_steps}\n')
 | 
					    + str(f'step number = {n_steps}\n')
 | 
				
			||||||
    + str(f'weight routing mode = {weight_routing_mode}\n')
 | 
					    + str(f'weight routing mode = {weight_routing_mode}\n')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,7 +49,7 @@ def get_all_lanes() -> List[Lane]:
 | 
				
			|||||||
        lanes.append(Lane(lane_id, polygon_lane, initial_max_speed))
 | 
					        lanes.append(Lane(lane_id, polygon_lane, initial_max_speed))
 | 
				
			||||||
    return lanes
 | 
					    return lanes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def parsePhase(phase_repr):
 | 
					def parse_phase(phase_repr):
 | 
				
			||||||
    duration = search('duration: {:f}', phase_repr)
 | 
					    duration = search('duration: {:f}', phase_repr)
 | 
				
			||||||
    minDuration = search('minDuration: {:f}', phase_repr)
 | 
					    minDuration = search('minDuration: {:f}', phase_repr)
 | 
				
			||||||
    maxDuration = search('maxDuration: {:f}', phase_repr)
 | 
					    maxDuration = search('maxDuration: {:f}', phase_repr)
 | 
				
			||||||
@@ -72,7 +72,7 @@ def add_data_to_areas(areas: List[Area]):
 | 
				
			|||||||
                        for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics 
 | 
					                        for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics 
 | 
				
			||||||
                            phases = []
 | 
					                            phases = []
 | 
				
			||||||
                            for phase in traci.trafficlight.Logic.getPhases(l): #add phases to logics
 | 
					                            for phase in traci.trafficlight.Logic.getPhases(l): #add phases to logics
 | 
				
			||||||
                                phases.append(parsePhase(phase.__repr__()))
 | 
					                                phases.append(parse_phase(phase.__repr__()))
 | 
				
			||||||
                            logics.append(Logic(l,phases)) 
 | 
					                            logics.append(Logic(l,phases)) 
 | 
				
			||||||
                        area.add_tl(TrafficLight(tl_id,logics))
 | 
					                        area.add_tl(TrafficLight(tl_id,logics))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -153,11 +153,12 @@ def main():
 | 
				
			|||||||
                 
 | 
					                 
 | 
				
			||||||
        logger.info(f'Total emissions = {total_emissions} mg')
 | 
					        logger.info(f'Total emissions = {total_emissions} mg')
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 | 
					        if not config.without_actions_mode:
 | 
				
			||||||
            ref = config.get_basics_emissions()
 | 
					            ref = config.get_basics_emissions()
 | 
				
			||||||
            diff_with_actions = (ref - total_emissions)/ref    
 | 
					            diff_with_actions = (ref - total_emissions)/ref    
 | 
				
			||||||
            
 | 
					 | 
				
			||||||
            logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %')
 | 
					            logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %')
 | 
				
			||||||
        logger.info('With the configuration : \n' + str(config.showConfig()))
 | 
					        
 | 
				
			||||||
 | 
					        logger.info('With the configuration : \n' + str(config.show_config()))
 | 
				
			||||||
        logger.info('Logs END')
 | 
					        logger.info('Logs END')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user