mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Fixed a bug about grid initialization
This commit is contained in:
		@@ -11,21 +11,32 @@ from shapely.geometry.linestring import LineString
 | 
			
		||||
from model import Area, Vehicle
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def stop_vehicle(veh_id):
 | 
			
		||||
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 computeEdgeWeight(edge_id):
 | 
			
		||||
        return traci.edge.getCO2Emission(edge_id)
 | 
			
		||||
    
 | 
			
		||||
#Useless because vehicles reroute automatically by travelTime       
 | 
			
		||||
def rerouteVehicles():
 | 
			
		||||
    for veh_id in traci.vehicle.getIDList():
 | 
			
		||||
        traci.vehicle.rerouteTraveltime(veh_id,True)
 | 
			
		||||
                
 | 
			
		||||
def adjustEdgesWeight():
 | 
			
		||||
    for edge_id in traci.edge.getIDList():
 | 
			
		||||
        weight = computeEdgeWeight(edge_id) #by default edges weight = length/mean speed
 | 
			
		||||
        traci.edge.adaptTraveltime(edge_id, weight) 
 | 
			
		||||
 | 
			
		||||
def lock_area(area: Area, vehicles: Iterable[Vehicle]):
 | 
			
		||||
    for lane in area._lanes:
 | 
			
		||||
        print(f'Setting max speed of {lane.lane_id} to 30.')
 | 
			
		||||
        traci.lane.setMaxSpeed(lane.lane_id, 30)
 | 
			
		||||
    max_speed = 30
 | 
			
		||||
    print(f' Setting max speed into {area.name} to {max_speed} km/h')
 | 
			
		||||
    area.locked = True
 | 
			
		||||
    for vehicle in vehicles:
 | 
			
		||||
        traci.vehicle.rerouteTraveltime(vehicle.veh_id, True)
 | 
			
		||||
    for lane in area._lanes:
 | 
			
		||||
        traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6)
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
@@ -11,10 +11,11 @@ if 'SUMO_HOME' in os.environ:
 | 
			
		||||
else:
 | 
			
		||||
    sys.exit("please declare environment variable 'SUMO_HOME'")
 | 
			
		||||
 | 
			
		||||
_SUMOCMD = 'sumo-gui'
 | 
			
		||||
_SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI 
 | 
			
		||||
_SUMOCFG = "mulhouse_simulation/osm.sumocfg"
 | 
			
		||||
CELLS_NUMBER = 10
 | 
			
		||||
CO2_THRESHOLD = 500000
 | 
			
		||||
n_steps = 400 
 | 
			
		||||
 | 
			
		||||
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
 | 
			
		||||
sumo_cmd = [sumo_binary, "-c", _SUMOCFG]
 | 
			
		||||
 
 | 
			
		||||
@@ -5,20 +5,21 @@ from shapely.geometry import LineString
 | 
			
		||||
 | 
			
		||||
import actions
 | 
			
		||||
import config
 | 
			
		||||
import sys
 | 
			
		||||
from model import Area, Vehicle, Lane
 | 
			
		||||
 | 
			
		||||
areas = list()
 | 
			
		||||
 | 
			
		||||
def init_grid(simulation_bounds, cells_number):
 | 
			
		||||
    width = simulation_bounds[1][0] / cells_number
 | 
			
		||||
    height = simulation_bounds[1][1] / cells_number
 | 
			
		||||
    areas = list()
 | 
			
		||||
    for i in range(cells_number):
 | 
			
		||||
        for j in range(cells_number):
 | 
			
		||||
            # bounds coordinates for the area : (xmin, ymin, xmax, ymax)
 | 
			
		||||
            ar_bounds = ((i * width, j * height), (i * width, (j + 1) * height),
 | 
			
		||||
                         ((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height))
 | 
			
		||||
            area = Area(ar_bounds)
 | 
			
		||||
            area.name = 'area{}{}'.format(i, j)
 | 
			
		||||
            area.name = 'area {}/{}'.format(i, j)
 | 
			
		||||
            areas.append(area)
 | 
			
		||||
            traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
 | 
			
		||||
    return areas
 | 
			
		||||
@@ -47,9 +48,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
 | 
			
		||||
        for vehicle in vehicles:
 | 
			
		||||
            if vehicle.pos in area:
 | 
			
		||||
                area.emissions += vehicle.co2
 | 
			
		||||
        if area.emissions > config.CO2_THRESHOLD:
 | 
			
		||||
            # print(f'Threshold exceeded in {area.name} : {area.emissions}')
 | 
			
		||||
            if not area.locked:
 | 
			
		||||
        if area.emissions > config.CO2_THRESHOLD and area.locked == False:
 | 
			
		||||
            actions.lock_area(area, vehicles)
 | 
			
		||||
            traci.polygon.setColor(area.name, (255, 0, 0))
 | 
			
		||||
            traci.polygon.setFilled(area.name, True)
 | 
			
		||||
@@ -68,11 +67,32 @@ def main():
 | 
			
		||||
        traci.start(config.sumo_cmd)
 | 
			
		||||
        grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
 | 
			
		||||
        add_lanes_to_areas(grid)
 | 
			
		||||
        while traci.simulation.getMinExpectedNumber() > 0:
 | 
			
		||||
        
 | 
			
		||||
        step = 0 
 | 
			
		||||
        while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
 | 
			
		||||
            traci.simulationStep()
 | 
			
		||||
            
 | 
			
		||||
            vehicles = get_all_vehicles()
 | 
			
		||||
            get_emissions(grid, vehicles)
 | 
			
		||||
            #actions.adjustEdgesWeight()
 | 
			
		||||
 | 
			
		||||
            
 | 
			
		||||
            step += 1 
 | 
			
		||||
            sys.stdout.write(f'Simulation step =  {step}/{config.n_steps}'+'\r')
 | 
			
		||||
            sys.stdout.flush()
 | 
			
		||||
            
 | 
			
		||||
    finally:
 | 
			
		||||
        total_emissions = 0 
 | 
			
		||||
        for area in areas:
 | 
			
		||||
            total_emissions += area.emissions
 | 
			
		||||
        
 | 
			
		||||
        #For 200 steps, total emissions = 42816869.054364316 mg
 | 
			
		||||
        #For 400 steps, total emissions = 136020579.71122485 mg
 | 
			
		||||
        
 | 
			
		||||
        print(f'\n**** Total emissions (CO2) = {total_emissions} mg')
 | 
			
		||||
        diff_with_lock = (136020579.71122485 - total_emissions)/136020579.71122485
 | 
			
		||||
        print(f'**** Reduction percentage of CO2 emissions = {diff_with_lock*100} % ****\n')
 | 
			
		||||
        
 | 
			
		||||
        traci.close(False)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user