mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-24 20:46:29 +00:00
Add sumo factory to interact with the simulation
This commit is contained in:
parent
3db7647d0b
commit
572812c5fe
17
sumo_project/Area.py
Normal file
17
sumo_project/Area.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
'''
|
||||||
|
Created on 6 nov. 2018
|
||||||
|
|
||||||
|
@author: Admin
|
||||||
|
'''
|
||||||
|
|
||||||
|
class Area:
|
||||||
|
|
||||||
|
locked = False
|
||||||
|
|
||||||
|
def __init__(self, coords):
|
||||||
|
self.coords = coords
|
||||||
|
|
||||||
|
|
||||||
|
def getCoords(self):
|
||||||
|
return self.coords
|
||||||
|
|
@ -1,35 +1,20 @@
|
|||||||
'''
|
|
||||||
Created on 11 oct. 2018
|
|
||||||
|
|
||||||
@author: Axel HUYNH-PHUC
|
|
||||||
'''
|
|
||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
import traci
|
from sumo_project.SUMOFactory import SUMOFactory
|
||||||
from traci import polygon
|
|
||||||
|
|
||||||
'''Launch traci'''
|
# Traci launch
|
||||||
if 'SUMO_HOME' in os.environ:
|
if 'SUMO_HOME' in os.environ:
|
||||||
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
|
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
|
||||||
sys.path.append(tools)
|
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:
|
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'''
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
'''creating multiple zones of equal sizes'''
|
# creating multiple zones of equal sizes
|
||||||
def init_grid():
|
def init_grid():
|
||||||
default_color = (0, 255, 0)
|
default_color = (0, 255, 0)
|
||||||
for i in range(cells_step):
|
for i in range(cells_step):
|
||||||
@ -40,36 +25,40 @@ def init_grid():
|
|||||||
polygon.add("area " + str(i) + "," + str(j), area, default_color, False, "rectangle")
|
polygon.add("area " + str(i) + "," + str(j), area, default_color, False, "rectangle")
|
||||||
|
|
||||||
|
|
||||||
'''Emissions recovery by area'''
|
# Emissions recovery by area
|
||||||
def getEmissionsByArea(i, j):
|
def getEmissionsByArea(i, j):
|
||||||
vehicles = []
|
vehicles = []
|
||||||
'''Vehicle IDs retrieving into this area'''
|
|
||||||
|
# Vehicle IDs retrieving into this area
|
||||||
for veh_id in traci.vehicle.getIDList():
|
for veh_id in traci.vehicle.getIDList():
|
||||||
pos = traci.vehicle.getPosition(veh_id)
|
pos = traci.vehicle.getPosition(veh_id)
|
||||||
if((i * width < pos[0] and (i + 1) * width > pos[0])
|
if((i * width < pos[0] and (i + 1) * width > pos[0])
|
||||||
and (j * height < pos[1] and (j + 1) * height > pos[1])):
|
and (j * height < pos[1] and (j + 1) * height > pos[1])):
|
||||||
vehicles.append(veh_id)
|
vehicles.append(veh_id)
|
||||||
|
|
||||||
'''Sum all emissions'''
|
# Sum all emissions
|
||||||
emissions = 0.0
|
emissions = 0.0
|
||||||
for veh_id in vehicles:
|
for veh_id in vehicles:
|
||||||
emission = traci.vehicle.getCO2Emission(veh_id)
|
emission = traci.vehicle.getCO2Emission(veh_id)
|
||||||
emissions += emission
|
emissions += emission
|
||||||
|
|
||||||
'''Change area color if emisssions exceeds the threshold'''
|
# Change area color if emisssions exceeds the threshold
|
||||||
emissionsArea[i][j] += emissions
|
emissionsArea[i][j] += emissions
|
||||||
if(emissionsArea[i][j] >= CO2_threshold):
|
if(emissionsArea[i][j] >= CO2_threshold):
|
||||||
red = (255, 0, 0)
|
red = (255, 0, 0)
|
||||||
|
factory.lock_area(areas[i][j])
|
||||||
polygon.setColor("area " + str(i) + "," + str(j), red)
|
polygon.setColor("area " + str(i) + "," + str(j), red)
|
||||||
polygon.setFilled("area " + str(i) + "," + str(j), True)
|
polygon.setFilled("area " + str(i) + "," + str(j), True)
|
||||||
|
|
||||||
'''Recover emissions from all areas'''
|
|
||||||
|
# Recover emissions from all areas
|
||||||
def getAllEmissions():
|
def getAllEmissions():
|
||||||
for i in range(cells_step):
|
for i in range(cells_step):
|
||||||
for j in range(cells_step):
|
for j in range(cells_step):
|
||||||
getEmissionsByArea(i, j)
|
getEmissionsByArea(i, j)
|
||||||
|
|
||||||
'''Display emissions information'''
|
|
||||||
|
# Display emissions information
|
||||||
def showEmissions():
|
def showEmissions():
|
||||||
for i in range(cells_step):
|
for i in range(cells_step):
|
||||||
for j in range(cells_step):
|
for j in range(cells_step):
|
||||||
@ -77,16 +66,31 @@ def showEmissions():
|
|||||||
+" = " , str(emissionsArea[i][j]) + " mg")
|
+" = " , str(emissionsArea[i][j]) + " mg")
|
||||||
|
|
||||||
|
|
||||||
'''Simulation launch'''
|
if __name__ == "__main__":
|
||||||
|
# Simulation launch
|
||||||
|
traci.start(sumoCmd)
|
||||||
|
|
||||||
|
# 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
|
step = 0
|
||||||
init_grid()
|
init_grid()
|
||||||
while step < 100: # while traci.simulation.getMinExpectedNumber() > 0:
|
factory = SUMOFactory()
|
||||||
|
|
||||||
|
#factory.lock_area(areas[5][5])
|
||||||
|
|
||||||
|
while traci.simulation.getMinExpectedNumber() > 0:
|
||||||
traci.simulationStep()
|
traci.simulationStep()
|
||||||
getAllEmissions()
|
getAllEmissions()
|
||||||
step += 1
|
step += 1
|
||||||
|
|
||||||
'''Display emissions information and close simulation'''
|
# Display emissions information and close simulation
|
||||||
showEmissions()
|
showEmissions()
|
||||||
traci.close()
|
traci.close()
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
40
sumo_project/SUMOFactory.py
Normal file
40
sumo_project/SUMOFactory.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
'''
|
||||||
|
Created on 17 oct. 2018
|
||||||
|
|
||||||
|
@author: Admin
|
||||||
|
'''
|
||||||
|
import os, sys
|
||||||
|
import traci
|
||||||
|
from shapely.geometry import Polygon
|
||||||
|
from shapely.geometry import Point
|
||||||
|
from shapely.geometry.linestring import LineString
|
||||||
|
|
||||||
|
|
||||||
|
class SUMOFactory(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
'''Constructor'''
|
||||||
|
|
||||||
|
def stopVehicle(self, veh_id):
|
||||||
|
traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING)
|
||||||
|
|
||||||
|
def getLanesIntoArea(self, area):
|
||||||
|
polygon_area = Polygon(area)
|
||||||
|
lanes = []
|
||||||
|
for lane_id in traci.lane.getIDList():
|
||||||
|
polygon_lane = LineString(traci.lane.getShape(lane_id))
|
||||||
|
if polygon_area.intersects(polygon_lane):
|
||||||
|
print("lane is in area : ", polygon_lane)
|
||||||
|
lanes.append(lane_id)
|
||||||
|
return lanes
|
||||||
|
|
||||||
|
def lock_area(self, area):
|
||||||
|
lanes = self.getLanesIntoArea(area)
|
||||||
|
for lane_id in lanes:
|
||||||
|
'''print("Vehicles number into lane = ", traci.lane.getLastStepVehicleNumber(lane_id))
|
||||||
|
if traci.lane.getLastStepVehicleNumber(lane_id) == 0:
|
||||||
|
traci.lane.setDisallowed(lane_id, "passenger")
|
||||||
|
print("lane blocked : ", lane_id)'''
|
||||||
|
traci.lane.setMaxSpeed(lane_id, 30)
|
||||||
|
for veh_id in traci.vehicle.getIDList():
|
||||||
|
traci.vehicle.rerouteTraveltime(veh_id, True)
|
Loading…
Reference in New Issue
Block a user