2018-11-15 12:44:15 +00:00
|
|
|
from typing import List
|
|
|
|
|
2018-11-14 13:56:54 +00:00
|
|
|
import traci
|
2018-11-14 13:40:55 +00:00
|
|
|
|
2018-11-15 12:44:15 +00:00
|
|
|
import config
|
|
|
|
from model import Area, Vehicle
|
2018-11-14 13:40:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-11-15 12:44:15 +00:00
|
|
|
def get_all_vehicles() -> List[Vehicle]:
|
|
|
|
vehicles = list()
|
2018-11-14 13:40:55 +00:00
|
|
|
for veh_id in traci.vehicle.getIDList():
|
2018-11-15 12:44:15 +00:00
|
|
|
veh_pos = traci.vehicle.getPosition(veh_id)
|
|
|
|
vehicle = Vehicle(veh_id, veh_pos)
|
|
|
|
vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.id)
|
|
|
|
vehicles.append(vehicle)
|
|
|
|
return vehicles
|
|
|
|
|
|
|
|
|
|
|
|
def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
|
|
|
|
for area in grid:
|
|
|
|
for vehicle in vehicles:
|
|
|
|
if vehicle.pos in area:
|
|
|
|
area.emissions += vehicle.co2
|
2018-11-14 13:56:54 +00:00
|
|
|
if area.emissions > config.CO2_THRESHOLD:
|
2018-11-14 13:40:55 +00:00
|
|
|
# print(f'Threshold exceeded in {area.name} : {area.emissions}')
|
2018-11-15 12:44:15 +00:00
|
|
|
# factory.lock_area(area)
|
2018-11-14 13:40:55 +00:00
|
|
|
traci.polygon.setColor(area.name, (255, 0, 0))
|
|
|
|
traci.polygon.setFilled(area.name, True)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
2018-11-14 13:56:54 +00:00
|
|
|
traci.start(config.sumo_cmd)
|
|
|
|
grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
|
2018-11-14 13:40:55 +00:00
|
|
|
while traci.simulation.getMinExpectedNumber() > 0:
|
|
|
|
traci.simulationStep()
|
2018-11-15 12:44:15 +00:00
|
|
|
# get_emissions(grid, SUMOFactory())
|
|
|
|
vehicles = get_all_vehicles()
|
|
|
|
get_emissions(grid, vehicles)
|
2018-11-14 13:40:55 +00:00
|
|
|
finally:
|
|
|
|
traci.close(False)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|