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

Chache vehicle information locally at each step

This commit is contained in:
Thibaud Gasser 2018-11-15 13:44:15 +01:00
parent 09841d4cfb
commit 9600dee7e5
2 changed files with 35 additions and 16 deletions

View File

@ -1,8 +1,9 @@
import traci from typing import List
import config
from SUMOFactory import SUMOFactory import traci
from model import Area
import config
from model import Area, Vehicle
def init_grid(simulation_bounds, cells_number): def init_grid(simulation_bounds, cells_number):
@ -22,20 +23,24 @@ def init_grid(simulation_bounds, cells_number):
return areas return areas
def emission_for_area(area): def get_all_vehicles() -> List[Vehicle]:
# retrieve all vehicles into this area vehicles = list()
for veh_id in traci.vehicle.getIDList(): for veh_id in traci.vehicle.getIDList():
pos = traci.vehicle.getPosition(veh_id) veh_pos = traci.vehicle.getPosition(veh_id)
if pos in area: vehicle = Vehicle(veh_id, veh_pos)
area.emissions += traci.vehicle.getCO2Emission(veh_id) vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.id)
vehicles.append(vehicle)
return vehicles
def get_emissions(areas, factory): def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
for area in areas: for area in grid:
emission_for_area(area) for vehicle in vehicles:
if vehicle.pos in area:
area.emissions += vehicle.co2
if area.emissions > config.CO2_THRESHOLD: if area.emissions > config.CO2_THRESHOLD:
# print(f'Threshold exceeded in {area.name} : {area.emissions}') # print(f'Threshold exceeded in {area.name} : {area.emissions}')
factory.lock_area(area) # factory.lock_area(area)
traci.polygon.setColor(area.name, (255, 0, 0)) traci.polygon.setColor(area.name, (255, 0, 0))
traci.polygon.setFilled(area.name, True) traci.polygon.setFilled(area.name, True)
@ -46,7 +51,9 @@ def main():
grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER) grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
while traci.simulation.getMinExpectedNumber() > 0: while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep() traci.simulationStep()
get_emissions(grid, SUMOFactory()) # get_emissions(grid, SUMOFactory())
vehicles = get_all_vehicles()
get_emissions(grid, vehicles)
finally: finally:
traci.close(False) traci.close(False)

View File

@ -1,5 +1,7 @@
from shapely.geometry import Polygon from typing import Tuple
from shapely.geometry import Point from shapely.geometry import Point
from shapely.geometry import Polygon
class Area: class Area:
@ -13,7 +15,7 @@ class Area:
return self.rectangle.__eq__(other) return self.rectangle.__eq__(other)
def __contains__(self, item): def __contains__(self, item):
return self.rectangle.contains(Point(item)) return self.rectangle.contains(item)
@property @property
def bounds(self): def bounds(self):
@ -26,3 +28,13 @@ class Area:
(xmin, ymax), (xmin, ymax),
(xmax, ymax), (xmax, ymax),
(xmax, ymin))) (xmax, ymin)))
class Vehicle:
def __init__(self, id: int, pos: Tuple[float, float]):
self.id = id
self.pos = Point(pos)
def __repr__(self) -> str:
return str(self.__dict__)