mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Changed vehicle class
This commit is contained in:
		@@ -23,15 +23,14 @@ def lanes_in_area(area):
 | 
			
		||||
def computeEdgeWeight(edge_id):
 | 
			
		||||
        return traci.edge.getCO2Emission(edge_id)
 | 
			
		||||
    
 | 
			
		||||
#Useless because vehicles reroute automatically by travelTime       
 | 
			
		||||
def rerouteVehicles():
 | 
			
		||||
def rerouteEffortVehicles():
 | 
			
		||||
    for veh_id in traci.vehicle.getIDList():
 | 
			
		||||
        traci.vehicle.rerouteTraveltime(veh_id,True)
 | 
			
		||||
        traci.vehicle.rerouteEffort(veh_id)
 | 
			
		||||
                
 | 
			
		||||
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) 
 | 
			
		||||
        traci.edge.setEffort(edge_id, weight) 
 | 
			
		||||
 | 
			
		||||
def lock_area(area: Area, vehicles: Iterable[Vehicle]):
 | 
			
		||||
    max_speed = 30
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ _SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI
 | 
			
		||||
_SUMOCFG = "mulhouse_simulation/osm.sumocfg"
 | 
			
		||||
CELLS_NUMBER = 10
 | 
			
		||||
CO2_THRESHOLD = 500000
 | 
			
		||||
n_steps = 400 
 | 
			
		||||
n_steps = 200 
 | 
			
		||||
 | 
			
		||||
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
 | 
			
		||||
sumo_cmd = [sumo_binary, "-c", _SUMOCFG]
 | 
			
		||||
 
 | 
			
		||||
@@ -24,13 +24,19 @@ def init_grid(simulation_bounds, cells_number):
 | 
			
		||||
            traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
 | 
			
		||||
    return areas
 | 
			
		||||
 | 
			
		||||
def compute_vehicle_emissions(veh_id):
 | 
			
		||||
    return (traci.vehicle.getCOEmission(veh_id)
 | 
			
		||||
    + traci.vehicle.getNOxEmission(veh_id)
 | 
			
		||||
    + traci.vehicle.getHCEmission(veh_id)
 | 
			
		||||
    + traci.vehicle.getPMxEmission(veh_id)
 | 
			
		||||
    + traci.vehicle.getCO2Emission(veh_id))
 | 
			
		||||
    
 | 
			
		||||
def get_all_vehicles() -> List[Vehicle]:
 | 
			
		||||
    vehicles = list()
 | 
			
		||||
    for veh_id in traci.vehicle.getIDList():
 | 
			
		||||
        veh_pos = traci.vehicle.getPosition(veh_id)
 | 
			
		||||
        vehicle = Vehicle(veh_id, veh_pos)
 | 
			
		||||
        vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.veh_id)
 | 
			
		||||
        vehicle.emissions = compute_vehicle_emissions(veh_id)
 | 
			
		||||
        vehicles.append(vehicle)
 | 
			
		||||
    return vehicles
 | 
			
		||||
 | 
			
		||||
@@ -47,7 +53,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
 | 
			
		||||
    for area in grid:
 | 
			
		||||
        for vehicle in vehicles:
 | 
			
		||||
            if vehicle.pos in area:
 | 
			
		||||
                area.emissions += vehicle.co2
 | 
			
		||||
                area.emissions += vehicle.emissions
 | 
			
		||||
        if area.emissions > config.CO2_THRESHOLD and area.locked == False:
 | 
			
		||||
            actions.lock_area(area, vehicles)
 | 
			
		||||
            traci.polygon.setColor(area.name, (255, 0, 0))
 | 
			
		||||
@@ -75,7 +81,7 @@ def main():
 | 
			
		||||
            vehicles = get_all_vehicles()
 | 
			
		||||
            get_emissions(grid, vehicles)
 | 
			
		||||
            #actions.adjustEdgesWeight()
 | 
			
		||||
 | 
			
		||||
            #actions.rerouteEffortVehicles()
 | 
			
		||||
            
 | 
			
		||||
            step += 1 
 | 
			
		||||
            sys.stdout.write(f'Simulation step =  {step}/{config.n_steps}'+'\r')
 | 
			
		||||
@@ -86,12 +92,12 @@ def main():
 | 
			
		||||
        for area in areas:
 | 
			
		||||
            total_emissions += area.emissions
 | 
			
		||||
        
 | 
			
		||||
        #For 200 steps, total emissions = 42816869.054364316 mg
 | 
			
		||||
        #For 400 steps, total emissions = 136020579.71122485 mg
 | 
			
		||||
        #Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
 | 
			
		||||
        total_emissions200 = 43970763.15084749  
 | 
			
		||||
                
 | 
			
		||||
        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')
 | 
			
		||||
        print(f'\n**** Total emissions = {total_emissions} mg ****')
 | 
			
		||||
        diff_with_lock = (total_emissions200 - total_emissions)/total_emissions200
 | 
			
		||||
        print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n')
 | 
			
		||||
        
 | 
			
		||||
        traci.close(False)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -56,9 +56,10 @@ class Area:
 | 
			
		||||
class Vehicle:
 | 
			
		||||
 | 
			
		||||
    def __init__(self, veh_id: int, pos: Tuple[float, float]):
 | 
			
		||||
        self.co2: float  = None
 | 
			
		||||
        self.emissions: float  = None
 | 
			
		||||
        self.veh_id = veh_id
 | 
			
		||||
        self.pos = Point(pos)
 | 
			
		||||
 | 
			
		||||
    def __repr__(self) -> str:
 | 
			
		||||
        return str(self.__dict__)
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user