2018-11-15 12:44:15 +00:00
|
|
|
from typing import List
|
|
|
|
|
2018-11-14 13:56:54 +00:00
|
|
|
import traci
|
2018-11-15 21:15:43 +00:00
|
|
|
from shapely.geometry import LineString
|
2018-11-14 13:40:55 +00:00
|
|
|
|
2018-11-15 21:15:43 +00:00
|
|
|
import actions
|
2018-11-15 12:44:15 +00:00
|
|
|
import config
|
2018-11-19 15:28:29 +00:00
|
|
|
import sys
|
2018-11-15 21:15:43 +00:00
|
|
|
from model import Area, Vehicle, Lane
|
2018-11-14 13:40:55 +00:00
|
|
|
|
2018-11-19 15:28:29 +00:00
|
|
|
areas = list()
|
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
|
|
|
|
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)
|
2018-11-19 15:28:29 +00:00
|
|
|
area.name = 'area {}/{}'.format(i, j)
|
2018-11-14 13:40:55 +00:00
|
|
|
areas.append(area)
|
|
|
|
traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
|
|
|
|
return areas
|
|
|
|
|
2018-11-20 12:52:23 +00:00
|
|
|
def compute_vehicle_emissions(veh_id):
|
|
|
|
return (traci.vehicle.getCOEmission(veh_id)
|
|
|
|
+ traci.vehicle.getNOxEmission(veh_id)
|
|
|
|
+ traci.vehicle.getHCEmission(veh_id)
|
|
|
|
+ traci.vehicle.getPMxEmission(veh_id)
|
|
|
|
+ traci.vehicle.getCO2Emission(veh_id))
|
|
|
|
|
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)
|
2018-11-20 12:52:23 +00:00
|
|
|
vehicle.emissions = compute_vehicle_emissions(veh_id)
|
2018-11-15 12:44:15 +00:00
|
|
|
vehicles.append(vehicle)
|
|
|
|
return vehicles
|
|
|
|
|
|
|
|
|
2018-11-15 21:15:43 +00:00
|
|
|
def get_all_lanes() -> List[Lane]:
|
|
|
|
lanes = []
|
|
|
|
for lane_id in traci.lane.getIDList():
|
|
|
|
polygon_lane = LineString(traci.lane.getShape(lane_id))
|
|
|
|
lanes.append(Lane(lane_id, polygon_lane))
|
|
|
|
return lanes
|
|
|
|
|
|
|
|
|
2018-11-15 12:44:15 +00:00
|
|
|
def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
|
|
|
|
for area in grid:
|
|
|
|
for vehicle in vehicles:
|
|
|
|
if vehicle.pos in area:
|
2018-11-20 12:52:23 +00:00
|
|
|
area.emissions += vehicle.emissions
|
2018-11-19 15:28:29 +00:00
|
|
|
if area.emissions > config.CO2_THRESHOLD and area.locked == False:
|
|
|
|
actions.lock_area(area, vehicles)
|
2018-11-14 13:40:55 +00:00
|
|
|
traci.polygon.setColor(area.name, (255, 0, 0))
|
|
|
|
traci.polygon.setFilled(area.name, True)
|
|
|
|
|
|
|
|
|
2018-11-15 21:15:43 +00:00
|
|
|
def add_lanes_to_areas(areas: List[Area]):
|
|
|
|
lanes = get_all_lanes()
|
|
|
|
for area in areas:
|
|
|
|
for lane in lanes:
|
|
|
|
if area.rectangle.intersects(lane.polygon):
|
|
|
|
area.add_lane(lane)
|
|
|
|
|
|
|
|
|
2018-11-14 13:40:55 +00:00
|
|
|
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-15 21:15:43 +00:00
|
|
|
add_lanes_to_areas(grid)
|
2018-11-19 15:28:29 +00:00
|
|
|
|
|
|
|
step = 0
|
|
|
|
while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
|
2018-11-14 13:40:55 +00:00
|
|
|
traci.simulationStep()
|
2018-11-19 15:28:29 +00:00
|
|
|
|
2018-11-15 12:44:15 +00:00
|
|
|
vehicles = get_all_vehicles()
|
|
|
|
get_emissions(grid, vehicles)
|
2018-11-19 15:28:29 +00:00
|
|
|
#actions.adjustEdgesWeight()
|
2018-11-20 12:52:23 +00:00
|
|
|
#actions.rerouteEffortVehicles()
|
2018-11-19 15:28:29 +00:00
|
|
|
|
|
|
|
step += 1
|
|
|
|
sys.stdout.write(f'Simulation step = {step}/{config.n_steps}'+'\r')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2018-11-14 13:40:55 +00:00
|
|
|
finally:
|
2018-11-19 15:28:29 +00:00
|
|
|
total_emissions = 0
|
|
|
|
for area in areas:
|
|
|
|
total_emissions += area.emissions
|
|
|
|
|
2018-11-20 12:52:23 +00:00
|
|
|
#Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
|
|
|
|
total_emissions200 = 43970763.15084749
|
|
|
|
|
|
|
|
print(f'\n**** Total emissions = {total_emissions} mg ****')
|
|
|
|
diff_with_lock = (total_emissions200 - total_emissions)/total_emissions200
|
|
|
|
print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n')
|
2018-11-19 15:28:29 +00:00
|
|
|
|
2018-11-14 13:40:55 +00:00
|
|
|
traci.close(False)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|