mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Changed code structure
This commit is contained in:
		@@ -15,13 +15,6 @@ def remove_vehicle(veh_id):
 | 
				
			|||||||
    traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING)
 | 
					    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):
 | 
					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)
 | 
				
			||||||
@@ -36,9 +29,14 @@ def adjust_edges_weights():
 | 
				
			|||||||
        traci.edge.adaptTraveltime(edge_id, weight)
 | 
					        traci.edge.adaptTraveltime(edge_id, weight)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def lock_area(area: Area):
 | 
					def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], max_speed):
 | 
				
			||||||
    max_speed = 30
 | 
					 | 
				
			||||||
    print(f'Setting max speed into {area.name} to {max_speed} km/h')
 | 
					    print(f'Setting max speed into {area.name} to {max_speed} km/h')
 | 
				
			||||||
    area.locked = True
 | 
					    area.locked = True
 | 
				
			||||||
    for lane in area._lanes:
 | 
					    for lane in area._lanes:
 | 
				
			||||||
        traci.lane.setMaxSpeed(lane.lane_id, max_speed / 3.6)
 | 
					        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))'''
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
@@ -23,4 +23,9 @@ routing_mode = False
 | 
				
			|||||||
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
 | 
					sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
 | 
				
			||||||
sumo_cmd = [sumo_binary, "-c", _SUMOCFG]
 | 
					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'))
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,7 +39,6 @@ def get_all_vehicles() -> List[Vehicle]:
 | 
				
			|||||||
        veh_pos = traci.vehicle.getPosition(veh_id)
 | 
					        veh_pos = traci.vehicle.getPosition(veh_id)
 | 
				
			||||||
        vehicle = Vehicle(veh_id, veh_pos)
 | 
					        vehicle = Vehicle(veh_id, veh_pos)
 | 
				
			||||||
        vehicle.emissions = compute_vehicle_emissions(veh_id)
 | 
					        vehicle.emissions = compute_vehicle_emissions(veh_id)
 | 
				
			||||||
        traci.vehicle.setRoutingMode(veh_id, traci.constants.ROUTING_MODE_AGGREGATED)
 | 
					 | 
				
			||||||
        vehicles.append(vehicle)
 | 
					        vehicles.append(vehicle)
 | 
				
			||||||
    return vehicles
 | 
					    return vehicles
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -58,7 +57,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
 | 
				
			|||||||
            if vehicle.pos in area:
 | 
					            if vehicle.pos in area:
 | 
				
			||||||
                area.emissions += vehicle.emissions
 | 
					                area.emissions += vehicle.emissions
 | 
				
			||||||
        if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked:
 | 
					        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.setColor(area.name, (255, 0, 0))
 | 
				
			||||||
            traci.polygon.setFilled(area.name, True)
 | 
					            traci.polygon.setFilled(area.name, True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -78,8 +77,10 @@ def main():
 | 
				
			|||||||
        grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
 | 
					        grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
 | 
				
			||||||
        add_lanes_to_areas(grid)
 | 
					        add_lanes_to_areas(grid)
 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
 | 
					        actions.adjust_traffic_light_phase_duration()
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        step = 0 
 | 
					        step = 0 
 | 
				
			||||||
        while step < config.n_steps:  # traci.simulation.getMinExpectedNumber() > 0:
 | 
					        while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
 | 
				
			||||||
            traci.simulationStep()
 | 
					            traci.simulationStep()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            vehicles = get_all_vehicles()
 | 
					            vehicles = get_all_vehicles()
 | 
				
			||||||
@@ -90,7 +91,8 @@ def main():
 | 
				
			|||||||
                # actions.rerouteAllVehicles()
 | 
					                # actions.rerouteAllVehicles()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            step += 1
 | 
					            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()
 | 
					            sys.stdout.flush()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    finally:
 | 
					    finally:
 | 
				
			||||||
@@ -100,13 +102,14 @@ def main():
 | 
				
			|||||||
        for area in grid:
 | 
					        for area in grid:
 | 
				
			||||||
            total_emissions += area.emissions
 | 
					            total_emissions += area.emissions
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        # Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
 | 
					         #Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
 | 
				
			||||||
        total_emissions200 = 43970763.15084749  
 | 
					        total_emissions200 = 43970763.15084749  
 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
        print(f'\n**** Total emissions = {total_emissions} mg ****')
 | 
					        print("\n**** RESULTS ****")
 | 
				
			||||||
        diff_with_lock = (total_emissions200 - total_emissions) / total_emissions200
 | 
					        print(f'Total emissions = {total_emissions} mg')
 | 
				
			||||||
        print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n')
 | 
					        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__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    main()
 | 
					    main()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user