2018-10-12 12:00:09 +00:00
|
|
|
import os, sys
|
2018-11-07 10:23:32 +00:00
|
|
|
from sumo_project.SUMOFactory import SUMOFactory
|
2018-10-12 12:00:09 +00:00
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
# Traci launch
|
2018-10-12 12:00:09 +00:00
|
|
|
if 'SUMO_HOME' in os.environ:
|
|
|
|
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
|
2018-11-07 10:23:32 +00:00
|
|
|
sys.path.append(os.path.join(os.environ['SUMO_HOME'], tools))
|
|
|
|
sumoBinary = os.path.join(os.environ['SUMO_HOME'], 'bin', 'sumo-gui')
|
|
|
|
sumoCmd = [sumoBinary, "-c", "mulhouse_simulation/osm.sumocfg"]
|
|
|
|
else:
|
2018-10-12 12:00:09 +00:00
|
|
|
sys.exit("please declare environment variable 'SUMO_HOME'")
|
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
import traci
|
|
|
|
from traci import polygon
|
2018-10-12 12:00:09 +00:00
|
|
|
|
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
# creating multiple zones of equal sizes
|
2018-10-12 12:00:09 +00:00
|
|
|
def init_grid():
|
2018-11-07 10:23:32 +00:00
|
|
|
default_color = (0, 255, 0)
|
2018-10-12 12:00:09 +00:00
|
|
|
for i in range(cells_step):
|
|
|
|
for j in range(cells_step):
|
|
|
|
area = ((i * width, j * height), (i * width, (j + 1) * height),
|
|
|
|
((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height))
|
|
|
|
areas[i][j] = area
|
|
|
|
polygon.add("area " + str(i) + "," + str(j), area, default_color, False, "rectangle")
|
2018-10-12 12:17:36 +00:00
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
|
|
|
|
# Emissions recovery by area
|
2018-10-12 12:00:09 +00:00
|
|
|
def getEmissionsByArea(i, j):
|
|
|
|
vehicles = []
|
2018-11-07 10:23:32 +00:00
|
|
|
|
|
|
|
# Vehicle IDs retrieving into this area
|
2018-10-12 12:00:09 +00:00
|
|
|
for veh_id in traci.vehicle.getIDList():
|
|
|
|
pos = traci.vehicle.getPosition(veh_id)
|
2018-11-07 10:23:32 +00:00
|
|
|
if((i * width < pos[0] and (i + 1) * width > pos[0])
|
2018-10-12 12:00:09 +00:00
|
|
|
and (j * height < pos[1] and (j + 1) * height > pos[1])):
|
|
|
|
vehicles.append(veh_id)
|
2018-11-07 10:23:32 +00:00
|
|
|
|
|
|
|
# Sum all emissions
|
2018-10-12 12:00:09 +00:00
|
|
|
emissions = 0.0
|
|
|
|
for veh_id in vehicles:
|
|
|
|
emission = traci.vehicle.getCO2Emission(veh_id)
|
2018-11-07 10:23:32 +00:00
|
|
|
emissions += emission
|
|
|
|
|
|
|
|
# Change area color if emisssions exceeds the threshold
|
2018-10-12 12:00:09 +00:00
|
|
|
emissionsArea[i][j] += emissions
|
|
|
|
if(emissionsArea[i][j] >= CO2_threshold):
|
|
|
|
red = (255, 0, 0)
|
2018-11-07 10:23:32 +00:00
|
|
|
factory.lock_area(areas[i][j])
|
2018-10-12 12:00:09 +00:00
|
|
|
polygon.setColor("area " + str(i) + "," + str(j), red)
|
|
|
|
polygon.setFilled("area " + str(i) + "," + str(j), True)
|
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
|
|
|
|
# Recover emissions from all areas
|
2018-10-12 12:00:09 +00:00
|
|
|
def getAllEmissions():
|
|
|
|
for i in range(cells_step):
|
|
|
|
for j in range(cells_step):
|
|
|
|
getEmissionsByArea(i, j)
|
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
|
|
|
|
# Display emissions information
|
2018-10-12 12:00:09 +00:00
|
|
|
def showEmissions():
|
|
|
|
for i in range(cells_step):
|
|
|
|
for j in range(cells_step):
|
|
|
|
print("Total CO2 emissions into Area " + str(i) + "," + str(j)
|
2018-11-07 10:23:32 +00:00
|
|
|
+" = " , str(emissionsArea[i][j]) + " mg")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Simulation launch
|
|
|
|
traci.start(sumoCmd)
|
2018-10-12 12:00:09 +00:00
|
|
|
|
2018-11-07 10:23:32 +00:00
|
|
|
# Variables and constants declaration
|
|
|
|
cells_step = 10
|
|
|
|
boundary = traci.simulation.getNetBoundary()
|
|
|
|
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
|
|
|
|
height = boundary[1][1] / cells_step # height/step
|
|
|
|
CO2_threshold = 500000
|
|
|
|
|
|
|
|
step = 0
|
|
|
|
init_grid()
|
|
|
|
factory = SUMOFactory()
|
|
|
|
|
|
|
|
#factory.lock_area(areas[5][5])
|
|
|
|
|
|
|
|
while traci.simulation.getMinExpectedNumber() > 0:
|
|
|
|
traci.simulationStep()
|
|
|
|
getAllEmissions()
|
|
|
|
step += 1
|
|
|
|
|
|
|
|
# Display emissions information and close simulation
|
|
|
|
showEmissions()
|
|
|
|
traci.close()
|
|
|
|
sys.stdout.flush()
|