1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-21 19:16:30 +00:00

Update existing code to use the new area data model

This commit is contained in:
Thibaud Gasser 2018-11-14 14:40:55 +01:00
parent 5a79858d04
commit 6c36eafe09
2 changed files with 82 additions and 25 deletions

View File

@ -1,40 +1,29 @@
'''
Created on 17 oct. 2018
@author: Admin
@author: Axel Huynh-Phuc, Thibaud Gasser
'''
import os, sys
import traci
from shapely.geometry import Polygon
from shapely.geometry import Point
import traci
from shapely.geometry.linestring import LineString
class SUMOFactory(object):
def __init__(self):
'''Constructor'''
def stopVehicle(self, veh_id):
def stop_vehicle(self, veh_id):
traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING)
def getLanesIntoArea(self, area):
polygon_area = Polygon(area)
lanes = []
def lanes_in_area(self, area):
polygon_area = area.rectangle
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
yield lane_id
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)
for lane_id in self.lanes_in_area(area):
print(f'Setting max speed of {lane_id} to 30.')
traci.lane.setMaxSpeed(lane_id, 30)
for veh_id in traci.vehicle.getIDList():
traci.vehicle.rerouteTraveltime(veh_id, True)

68
sumo_project/emissions.py Normal file
View File

@ -0,0 +1,68 @@
import os
import sys
from SUMOFactory import SUMOFactory
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
import traci
from area import Area
sumoBinary = os.path.join(os.environ['SUMO_HOME'], 'bin', 'sumo-gui')
sumoCmd = [sumoBinary, "-c", "mulhouse_simulation/map.sumocfg"]
CELLS_NUMBER = 10
CO2_THRESHOLD = 500000
def init_grid(simulation_bounds, cells_number):
width = simulation_bounds[1][0] / cells_number
height = simulation_bounds[1][1] / cells_number
# TODO: change data structure?
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)
areas.append(area)
traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
return areas
def emission_for_area(area):
# retrieve all vehicles into this area
for veh_id in traci.vehicle.getIDList():
pos = traci.vehicle.getPosition(veh_id)
if area.contains(pos):
area.emissions += traci.vehicle.getCO2Emission(veh_id)
def get_emissions(areas, factory):
for area in areas:
emission_for_area(area)
if area.emissions > CO2_THRESHOLD:
# print(f'Threshold exceeded in {area.name} : {area.emissions}')
factory.lock_area(area)
traci.polygon.setColor(area.name, (255, 0, 0))
traci.polygon.setFilled(area.name, True)
def main():
try:
traci.start(sumoCmd)
grid = init_grid(traci.simulation.getNetBoundary(), CELLS_NUMBER)
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
get_emissions(grid, SUMOFactory())
finally:
traci.close(False)
if __name__ == '__main__':
main()