1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 03:26:30 +00:00

Merge pull request #1 from Ahp06/dev

Change absolute to relative path using SUMO_HOME
This commit is contained in:
Thibaud Gasser 2018-10-19 08:43:08 +02:00 committed by GitHub
commit d8982b8c3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,92 +1,94 @@
''' '''
Created on 11 oct. 2018 Created on 11 oct. 2018
@author: Axel HUYNH-PHUC @author: Axel HUYNH-PHUC
''' '''
import os, sys import os, sys
import traci
from traci import polygon '''Launch traci'''
if 'SUMO_HOME' in os.environ:
'''Launch traci''' tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
if 'SUMO_HOME' in os.environ: sys.path.append(os.path.join(os.environ['SUMO_HOME'], tools))
tools = os.path.join(os.environ['SUMO_HOME'], 'tools') sumoBinary = os.path.join(os.environ['SUMO_HOME'], 'bin', 'sumo-gui')
sys.path.append(tools) sumoCmd = [sumoBinary, "-c", "mulhouse_simulation/osm.sumocfg"]
else: else:
sys.exit("please declare environment variable 'SUMO_HOME'") sys.exit("please declare environment variable 'SUMO_HOME'")
sumoBinary = "C:\\Users\\Admin\\AppData\\Roaming\\Microsoft\\Installer\\{A63B306E-2B15-11E1-88C8-028037EC0200}\\sumogui.exe" import traci
sumoCmd = [sumoBinary, "-c", "mulhouse_simulation\\osm.sumocfg"] from traci import polygon
traci.start(sumoCmd)
'''Variables and constants declaration''' '''creating multiple zones of equal sizes'''
cells_step = 10 def init_grid():
boundary = traci.simulation.getNetBoundary() default_color = (0, 255, 0)
areas = [[0] * cells_step for _ in range(cells_step)] for i in range(cells_step):
emissionsArea = [[0] * cells_step for _ in range(cells_step)] for j in range(cells_step):
width = boundary[1][0] / cells_step # width/step area = ((i * width, j * height), (i * width, (j + 1) * height),
height = boundary[1][1] / cells_step # height/step ((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height))
CO2_threshold = 500000 areas[i][j] = area
polygon.add("area " + str(i) + "," + str(j), area, default_color, False, "rectangle")
'''creating multiple zones of equal sizes'''
def init_grid(): '''Emissions recovery by area'''
default_color = (0, 255, 0) def getEmissionsByArea(i, j):
for i in range(cells_step): vehicles = []
for j in range(cells_step): '''Vehicle IDs retrieving into this area'''
area = ((i * width, j * height), (i * width, (j + 1) * height), for veh_id in traci.vehicle.getIDList():
((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height)) pos = traci.vehicle.getPosition(veh_id)
areas[i][j] = area if((i * width < pos[0] and (i + 1) * width > pos[0])
polygon.add("area " + str(i) + "," + str(j), area, default_color, False, "rectangle") and (j * height < pos[1] and (j + 1) * height > pos[1])):
vehicles.append(veh_id)
'''Emissions recovery by area''' '''Sum all emissions'''
def getEmissionsByArea(i, j): emissions = 0.0
vehicles = [] for veh_id in vehicles:
'''Vehicle IDs retrieving into this area''' emission = traci.vehicle.getCO2Emission(veh_id)
for veh_id in traci.vehicle.getIDList(): emissions += emission
pos = traci.vehicle.getPosition(veh_id)
if((i * width < pos[0] and (i + 1) * width > pos[0]) '''Change area color if emisssions exceeds the threshold'''
and (j * height < pos[1] and (j + 1) * height > pos[1])): emissionsArea[i][j] += emissions
vehicles.append(veh_id) if(emissionsArea[i][j] >= CO2_threshold):
red = (255, 0, 0)
'''Sum all emissions''' polygon.setColor("area " + str(i) + "," + str(j), red)
emissions = 0.0 polygon.setFilled("area " + str(i) + "," + str(j), True)
for veh_id in vehicles:
emission = traci.vehicle.getCO2Emission(veh_id) '''Recover emissions from all areas'''
emissions += emission def getAllEmissions():
for i in range(cells_step):
'''Change area color if emisssions exceeds the threshold''' for j in range(cells_step):
emissionsArea[i][j] += emissions getEmissionsByArea(i, j)
if(emissionsArea[i][j] >= CO2_threshold):
red = (255, 0, 0) '''Display emissions information'''
polygon.setColor("area " + str(i) + "," + str(j), red) def showEmissions():
polygon.setFilled("area " + str(i) + "," + str(j), True) for i in range(cells_step):
for j in range(cells_step):
'''Recover emissions from all areas''' print("Total CO2 emissions into Area " + str(i) + "," + str(j)
def getAllEmissions(): + " = " , str(emissionsArea[i][j]) + " mg" )
for i in range(cells_step):
for j in range(cells_step):
getEmissionsByArea(i, j) if __name__ == "__main__":
'''Simulation launch'''
'''Display emissions information''' traci.start(sumoCmd)
def showEmissions():
for i in range(cells_step): '''Variables and constants declaration'''
for j in range(cells_step): cells_step = 10
print("Total CO2 emissions into Area " + str(i) + "," + str(j) boundary = traci.simulation.getNetBoundary()
+ " = " , str(emissionsArea[i][j]) + " mg" ) areas = [[0] * cells_step for _ in range(cells_step)]
emissionsArea = [[0] * cells_step for _ in range(cells_step)]
width = boundary[1][0] / cells_step # width/step
'''Simulation launch''' height = boundary[1][1] / cells_step # height/step
step = 0 CO2_threshold = 500000
init_grid()
while step < 100: # while traci.simulation.getMinExpectedNumber() > 0: step = 0
traci.simulationStep() init_grid()
getAllEmissions() while traci.simulation.getMinExpectedNumber() > 0:
step += 1 print("Vehicle count :", traci.vehicle.getIDCount())
traci.simulationStep()
'''Display emissions information and close simulation''' getAllEmissions()
showEmissions() step += 1
traci.close()
sys.stdout.flush() '''Display emissions information and close simulation'''
showEmissions()
traci.close()
sys.stdout.flush()