2018-11-23 13:40:47 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import traci
|
2018-12-07 15:30:23 +00:00
|
|
|
import time
|
|
|
|
|
2018-11-23 13:40:47 +00:00
|
|
|
from shapely.geometry import LineString
|
2018-12-07 16:40:16 +00:00
|
|
|
from parse import search
|
2018-11-23 13:40:47 +00:00
|
|
|
|
|
|
|
import actions
|
2018-12-09 18:49:37 +00:00
|
|
|
from config import config
|
2018-11-23 13:40:47 +00:00
|
|
|
import sys
|
2018-12-03 20:05:01 +00:00
|
|
|
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic
|
2018-11-23 13:40:47 +00:00
|
|
|
from traci import trafficlight
|
|
|
|
|
2018-12-09 18:49:37 +00:00
|
|
|
config = config('C:\\Users\\Admin\\Desktop\\config.json')
|
2018-12-07 16:40:16 +00:00
|
|
|
logger = config.logger
|
2018-11-23 13:40:47 +00:00
|
|
|
|
2018-12-09 13:27:39 +00:00
|
|
|
def init_grid(simulation_bounds, areas_number):
|
2018-11-23 13:40:47 +00:00
|
|
|
grid = list()
|
2018-12-09 13:27:39 +00:00
|
|
|
width = simulation_bounds[1][0] / areas_number
|
|
|
|
height = simulation_bounds[1][1] / areas_number
|
|
|
|
for i in range(areas_number):
|
|
|
|
for j in range(areas_number):
|
2018-11-23 13:40:47 +00:00
|
|
|
# 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-12-07 15:30:23 +00:00
|
|
|
area.name = 'Area ({},{})'.format(i, j)
|
2018-11-23 13:40:47 +00:00
|
|
|
grid.append(area)
|
|
|
|
traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
|
|
|
|
return grid
|
|
|
|
|
|
|
|
def get_all_lanes() -> List[Lane]:
|
|
|
|
lanes = []
|
|
|
|
for lane_id in traci.lane.getIDList():
|
|
|
|
polygon_lane = LineString(traci.lane.getShape(lane_id))
|
2018-11-23 18:40:14 +00:00
|
|
|
initial_max_speed = traci.lane.getMaxSpeed(lane_id)
|
2018-12-03 20:05:01 +00:00
|
|
|
lanes.append(Lane(lane_id, polygon_lane, initial_max_speed))
|
2018-11-23 13:40:47 +00:00
|
|
|
return lanes
|
|
|
|
|
2018-12-07 16:16:26 +00:00
|
|
|
def parse_phase(phase_repr):
|
2018-12-03 20:05:01 +00:00
|
|
|
duration = search('duration: {:f}', phase_repr)
|
|
|
|
minDuration = search('minDuration: {:f}', phase_repr)
|
|
|
|
maxDuration = search('maxDuration: {:f}', phase_repr)
|
|
|
|
phaseDef = search('phaseDef: {}\n', phase_repr)
|
|
|
|
|
|
|
|
if phaseDef is None: phaseDef = ''
|
|
|
|
else : phaseDef = phaseDef[0]
|
|
|
|
|
|
|
|
return Phase(duration[0], minDuration[0], maxDuration[0], phaseDef)
|
2018-11-23 13:40:47 +00:00
|
|
|
|
|
|
|
def add_data_to_areas(areas: List[Area]):
|
|
|
|
lanes = get_all_lanes()
|
|
|
|
for area in areas:
|
2018-12-03 20:05:01 +00:00
|
|
|
for lane in lanes: # add lanes
|
2018-11-23 13:40:47 +00:00
|
|
|
if area.rectangle.intersects(lane.polygon):
|
2018-12-03 20:05:01 +00:00
|
|
|
area.add_lane(lane)
|
|
|
|
for tl_id in traci.trafficlight.getIDList(): # add traffic lights
|
2018-11-23 13:40:47 +00:00
|
|
|
if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id):
|
2018-12-03 20:05:01 +00:00
|
|
|
logics = []
|
|
|
|
for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics
|
|
|
|
phases = []
|
|
|
|
for phase in traci.trafficlight.Logic.getPhases(l): #add phases to logics
|
2018-12-07 16:16:26 +00:00
|
|
|
phases.append(parse_phase(phase.__repr__()))
|
2018-12-03 20:05:01 +00:00
|
|
|
logics.append(Logic(l,phases))
|
2018-11-29 10:48:07 +00:00
|
|
|
area.add_tl(TrafficLight(tl_id,logics))
|
2018-12-07 15:30: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))
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_vehicles() -> List[Vehicle]:
|
|
|
|
vehicles = list()
|
|
|
|
for veh_id in traci.vehicle.getIDList():
|
|
|
|
veh_pos = traci.vehicle.getPosition(veh_id)
|
|
|
|
vehicle = Vehicle(veh_id, veh_pos)
|
|
|
|
vehicle.emissions = compute_vehicle_emissions(veh_id)
|
|
|
|
vehicles.append(vehicle)
|
|
|
|
return vehicles
|
|
|
|
|
2018-12-09 13:27:39 +00:00
|
|
|
def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step):
|
2018-12-07 15:30:23 +00:00
|
|
|
for area in grid:
|
2018-12-09 13:27:39 +00:00
|
|
|
vehicle_emissions = 0
|
2018-12-07 15:30:23 +00:00
|
|
|
for vehicle in vehicles:
|
|
|
|
if vehicle.pos in area:
|
2018-12-09 13:27:39 +00:00
|
|
|
vehicle_emissions += vehicle.emissions
|
|
|
|
|
|
|
|
area.emissions_by_step.append(vehicle_emissions)
|
|
|
|
|
2018-12-09 14:46:22 +00:00
|
|
|
if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold:
|
2018-12-09 13:27:39 +00:00
|
|
|
|
2018-12-07 15:30:23 +00:00
|
|
|
if config.limit_speed_mode and not area.limited_speed:
|
2018-12-07 16:40:16 +00:00
|
|
|
logger.info(f'Action - Decreased max speed into {area.name} by {config.speed_rf*100}%')
|
2018-12-07 15:30:23 +00:00
|
|
|
actions.limit_speed_into_area(area, vehicles, config.speed_rf)
|
|
|
|
traci.polygon.setColor(area.name, (255, 0, 0))
|
|
|
|
traci.polygon.setFilled(area.name, True)
|
|
|
|
if config.adjust_traffic_light_mode and not area.tls_adjusted:
|
2018-12-07 16:40:16 +00:00
|
|
|
logger.info(f'Action - Decreased traffic lights duration by {config.trafficLights_duration_rf*100}%')
|
2018-12-07 15:30:23 +00:00
|
|
|
actions.adjust_traffic_light_phase_duration(area, config.trafficLights_duration_rf)
|
2018-12-09 13:27:39 +00:00
|
|
|
|
2018-12-07 15:30:23 +00:00
|
|
|
if config.lock_area_mode and not area.locked:
|
|
|
|
if actions.count_vehicles_in_area(area):
|
|
|
|
logger.info(f'Action - {area.name} blocked')
|
|
|
|
actions.lock_area(area)
|
2018-12-09 14:05:33 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
actions.reverse_actions(area)
|
2018-12-07 15:30:23 +00:00
|
|
|
|
2018-12-09 13:27:39 +00:00
|
|
|
|
2018-11-23 13:40:47 +00:00
|
|
|
def main():
|
|
|
|
grid = list()
|
|
|
|
try:
|
|
|
|
traci.start(config.sumo_cmd)
|
2018-12-09 13:27:39 +00:00
|
|
|
logger.info(f'Loaded simulation file : {config._SUMOCFG}')
|
2018-12-07 15:30:23 +00:00
|
|
|
logger.info('Loading data for the simulation')
|
|
|
|
start = time.perf_counter()
|
|
|
|
|
2018-12-09 13:27:39 +00:00
|
|
|
grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number)
|
2018-11-23 13:40:47 +00:00
|
|
|
add_data_to_areas(grid)
|
2018-12-07 15:30:23 +00:00
|
|
|
|
|
|
|
loading_time = round(time.perf_counter() - start,2)
|
|
|
|
logger.info(f'Data loaded ({loading_time}s)')
|
|
|
|
|
|
|
|
logger.info('Start of the simulation')
|
2018-11-23 13:40:47 +00:00
|
|
|
step = 0
|
|
|
|
while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
|
|
|
|
traci.simulationStep()
|
|
|
|
|
|
|
|
vehicles = get_all_vehicles()
|
2018-12-09 13:27:39 +00:00
|
|
|
get_emissions(grid, vehicles,step)
|
2018-11-23 13:40:47 +00:00
|
|
|
|
2018-11-23 18:40:14 +00:00
|
|
|
if config.weight_routing_mode:
|
2018-11-23 13:40:47 +00:00
|
|
|
actions.adjust_edges_weights()
|
|
|
|
|
|
|
|
step += 1
|
2018-12-03 20:05:01 +00:00
|
|
|
|
2018-11-23 13:40:47 +00:00
|
|
|
finally:
|
|
|
|
traci.close(False)
|
2018-12-07 15:46:54 +00:00
|
|
|
simulation_time = round(time.perf_counter() - start,2)
|
|
|
|
logger.info(f'End of the simulation ({simulation_time}s)')
|
|
|
|
|
2018-11-23 13:40:47 +00:00
|
|
|
total_emissions = 0
|
|
|
|
for area in grid:
|
2018-12-09 13:27:39 +00:00
|
|
|
total_emissions += area.sum_all_emissions()
|
|
|
|
|
2018-12-07 15:30:23 +00:00
|
|
|
logger.info(f'Total emissions = {total_emissions} mg')
|
2018-12-06 20:49:01 +00:00
|
|
|
|
2018-12-09 13:27:39 +00:00
|
|
|
if not config.without_actions_mode :
|
2018-12-07 16:16:26 +00:00
|
|
|
ref = config.get_basics_emissions()
|
2018-12-09 13:27:39 +00:00
|
|
|
if not (ref is None):
|
|
|
|
diff_with_actions = (ref - total_emissions)/ref
|
|
|
|
logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %')
|
2018-12-07 16:16:26 +00:00
|
|
|
|
|
|
|
logger.info('With the configuration : \n' + str(config.show_config()))
|
2018-12-07 15:30:23 +00:00
|
|
|
logger.info('Logs END')
|
2018-12-03 20:05:01 +00:00
|
|
|
|
2018-11-23 13:40:47 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|