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

Added a logs file output

This commit is contained in:
Ahp06 2018-12-07 16:30:23 +01:00
parent 70b29c47b2
commit 342320bd4a
4 changed files with 125 additions and 76 deletions

View File

@ -31,11 +31,10 @@ def adjust_edges_weights():
for veh_id in traci.vehicle.getIDList(): for veh_id in traci.vehicle.getIDList():
traci.vehicle.rerouteEffort(veh_id) traci.vehicle.rerouteEffort(veh_id)
def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], max_speed): def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], speed_rf):
print(f'Setting max speed into {area.name} to {max_speed} km/h')
area.limited_speed = True area.limited_speed = True
for lane in area._lanes: for lane in area._lanes:
traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6) traci.lane.setMaxSpeed(lane.lane_id, speed_rf * lane.initial_max_speed)
def modifyLogic(logic, rf): #rf for "reduction factor" def modifyLogic(logic, rf): #rf for "reduction factor"
new_phases = [] new_phases = []
@ -46,20 +45,20 @@ def modifyLogic(logic, rf): #rf for "reduction factor"
return traci.trafficlight.Logic("new-program", 0 , 0 , 0 , new_phases) return traci.trafficlight.Logic("new-program", 0 , 0 , 0 , new_phases)
def adjust_traffic_light_phase_duration(area, reduction_factor): def adjust_traffic_light_phase_duration(area, reduction_factor):
print(f'Decrease of traffic lights duration by a factor of {reduction_factor}') area.tls_adjusted = True
for tl in area._tls: for tl in area._tls:
for logic in tl._logics: for logic in tl._logics:
traci.trafficlights.setCompleteRedYellowGreenDefinition(tl.tl_id, modifyLogic(logic,reduction_factor)) traci.trafficlights.setCompleteRedYellowGreenDefinition(tl.tl_id, modifyLogic(logic,reduction_factor))
def lock_area(area): def count_vehicles_in_area(area):
#Trying to lock area #Trying to lock area
vehicles_in_area = 0 vehicles_in_area = 0
for lane in area._lanes: for lane in area._lanes:
vehicles_in_area += traci.lane.getLastStepVehicleNumber(lane.lane_id) vehicles_in_area += traci.lane.getLastStepVehicleNumber(lane.lane_id)
#Waiting for the area to be empty before blocking it return vehicles_in_area
if vehicles_in_area == 0:
def lock_area(area):
area.locked = True area.locked = True
print(f'Area locked : {area.name}')
for lane in area._lanes: for lane in area._lanes:
traci.lane.setDisallowed(lane.lane_id, 'passenger') traci.lane.setDisallowed(lane.lane_id, 'passenger')

View File

@ -4,6 +4,11 @@ Global configuration for the simulation
import os import os
import sys import sys
import datetime
###############################################################################
############################# SIMULATION FILE #################################
###############################################################################
if 'SUMO_HOME' in os.environ: if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools') tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
@ -13,39 +18,63 @@ else:
_SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI _SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI
_SUMOCFG = "mulhouse_simulation/osm.sumocfg" _SUMOCFG = "mulhouse_simulation/osm.sumocfg"
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
sumo_cmd = [sumo_binary, "-c", _SUMOCFG]
###############################################################################
############################# LOGS OUTPUT #####################################
###############################################################################
now = datetime.datetime.now()
current_date = now.strftime("%Y_%m_%d_%H_%M")
LOG_FILENAME = f'sumo_logs_{current_date}.log'
###############################################################################
########################## SIMULATION CONFIGURATION ###########################
###############################################################################
CELLS_NUMBER = 10 CELLS_NUMBER = 10
EMISSIONS_THRESHOLD = 500000 EMISSIONS_THRESHOLD = 500000
n_steps = 200 n_steps = 200
###############################################################################
########################## ACTIONS CONFIGURATION ##############################
###############################################################################
#Limit the speed into areas when the threshold is exceeded #Limit the speed into areas when the threshold is exceeded
limited_speed = 30 speed_rf = 0.1
limit_speed_mode = False limit_speed_mode = False
#Decrease all traffic lights duration into the area when the threshold is exceeded #Decrease all traffic lights duration into the area when the threshold is exceeded
rf_trafficLights_duration = 1.4 trafficLights_duration_rf = 0.2
adjust_traffic_light_mode = False adjust_traffic_light_mode = False
#Immediately delete all vehicles in the simulation area #Immediately delete all vehicles in the simulation area
remove_vehicles_mode = False remove_vehicles_mode = False
#Vehicles are routed according to the less polluted route (HEAVY) #Vehicles are routed according to the less polluted route (HEAVY)
weight_routing_mode = True weight_routing_mode = False
#Lock the area when the threshold is exceeded (NOT FIXED) #Lock the area when the threshold is exceeded (NOT FIXED)
lock_area_mode = False lock_area_mode = True
#Weight routing mode cannot be combinated with other actions #Weight routing mode cannot be combinated with other actions
if weight_routing_mode: if weight_routing_mode:
limit_speed_mode = False limit_speed_mode = False
adjust_traffic_light_mode = False adjust_traffic_light_mode = False
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD) ###############################################################################
sumo_cmd = [sumo_binary, "-c", _SUMOCFG] ########################## SIMULATION REFERENCES ##############################
###############################################################################
# Total of emissions of all pollutants in mg for n steps of simulation without locking areas # Total of emissions of all pollutants in mg for n steps of simulation without locking areas
total_emissions200 = 43970763.15084749 total_emissions200 = 43970763.15084749
total_emissions300 = 87382632.08217141 total_emissions300 = 87382632.08217141
###############################################################################
########################## CONFIGURATION METHODS ##############################
###############################################################################
def get_basics_emissions(): def get_basics_emissions():
if n_steps == 200: if n_steps == 200:
return total_emissions200 return total_emissions200
@ -55,7 +84,8 @@ def get_basics_emissions():
def showConfig(): def showConfig():
return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n') return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n')
+ str(f'step number = {n_steps}\n') + str(f'step number = {n_steps}\n')
+ str(f'weight routing mode= {weight_routing_mode}\n') + str(f'weight routing mode = {weight_routing_mode}\n')
+ str(f'limit speed mode = {limit_speed_mode}, limited speed to {limited_speed}\n') + str(f'lock area mode = {lock_area_mode}\n')
+ str(f'adjust traffic light mode = {adjust_traffic_light_mode} , RF = {rf_trafficLights_duration}\n')) + str(f'limit speed mode = {limit_speed_mode}, RF = {speed_rf*100}%\n')
+ str(f'adjust traffic light mode = {adjust_traffic_light_mode} , RF = {trafficLights_duration_rf*100}%\n'))

View File

@ -1,8 +1,9 @@
# -*- coding: latin-1 -*-
from typing import List from typing import List
import traci import traci
import logging
import time
from shapely.geometry import LineString from shapely.geometry import LineString
from parse import * from parse import *
@ -12,6 +13,18 @@ import sys
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic
from traci import trafficlight from traci import trafficlight
# create logger
logger = logging.getLogger("sumo_logger")
logger.setLevel(logging.INFO)
# create console handler and set level to info
handler = logging.FileHandler(config.LOG_FILENAME)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter to handler
handler.setFormatter(formatter)
# add handler to logger
logger.addHandler(handler)
def init_grid(simulation_bounds, cells_number): def init_grid(simulation_bounds, cells_number):
grid = list() grid = list()
@ -23,11 +36,47 @@ def init_grid(simulation_bounds, cells_number):
ar_bounds = ((i * width, j * height), (i * width, (j + 1) * height), ar_bounds = ((i * width, j * height), (i * width, (j + 1) * height),
((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height)) ((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height))
area = Area(ar_bounds) area = Area(ar_bounds)
area.name = 'area ({},{})'.format(i, j) area.name = 'Area ({},{})'.format(i, j)
grid.append(area) grid.append(area)
traci.polygon.add(area.name, ar_bounds, (0, 255, 0)) traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
return grid return grid
def get_all_lanes() -> List[Lane]:
lanes = []
for lane_id in traci.lane.getIDList():
polygon_lane = LineString(traci.lane.getShape(lane_id))
initial_max_speed = traci.lane.getMaxSpeed(lane_id)
lanes.append(Lane(lane_id, polygon_lane, initial_max_speed))
return lanes
def parsePhase(phase_repr):
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)
def add_data_to_areas(areas: List[Area]):
lanes = get_all_lanes()
for area in areas:
for lane in lanes: # add lanes
if area.rectangle.intersects(lane.polygon):
area.add_lane(lane)
for tl_id in traci.trafficlight.getIDList(): # add traffic lights
if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id):
logics = []
for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics
phases = []
for phase in traci.trafficlight.Logic.getPhases(l): #add phases to logics
phases.append(parsePhase(phase.__repr__()))
logics.append(Logic(l,phases))
area.add_tl(TrafficLight(tl_id,logics))
def compute_vehicle_emissions(veh_id): def compute_vehicle_emissions(veh_id):
return (traci.vehicle.getCOEmission(veh_id) return (traci.vehicle.getCOEmission(veh_id)
@ -46,16 +95,6 @@ def get_all_vehicles() -> List[Vehicle]:
vehicles.append(vehicle) vehicles.append(vehicle)
return vehicles return vehicles
def get_all_lanes() -> List[Lane]:
lanes = []
for lane_id in traci.lane.getIDList():
polygon_lane = LineString(traci.lane.getShape(lane_id))
initial_max_speed = traci.lane.getMaxSpeed(lane_id)
lanes.append(Lane(lane_id, polygon_lane, initial_max_speed))
return lanes
def get_emissions(grid: List[Area], vehicles: List[Vehicle]): def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
for area in grid: for area in grid:
for vehicle in vehicles: for vehicle in vehicles:
@ -64,52 +103,34 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
if area.emissions > config.EMISSIONS_THRESHOLD: if area.emissions > config.EMISSIONS_THRESHOLD:
if config.limit_speed_mode and not area.limited_speed: if config.limit_speed_mode and not area.limited_speed:
actions.limit_speed_into_area(area, vehicles, config.limited_speed) logger.info(f'Action - Decrease of max speed into {area.name} by {config.speed_rf*100}%')
actions.limit_speed_into_area(area, vehicles, config.speed_rf)
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)
if config.adjust_traffic_light_mode: if config.adjust_traffic_light_mode and not area.tls_adjusted:
actions.adjust_traffic_light_phase_duration(area, config.rf_trafficLights_duration) logger.info(f'Action - Decrease of traffic lights duration by {config.trafficLights_duration_rf*100}%')
actions.adjust_traffic_light_phase_duration(area, config.trafficLights_duration_rf)
if config.lock_area_mode and not area.locked: 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) actions.lock_area(area)
def parsePhase(phase_repr):
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)
def add_data_to_areas(areas: List[Area]):
lanes = get_all_lanes()
for area in areas:
for lane in lanes: # add lanes
if area.rectangle.intersects(lane.polygon):
area.add_lane(lane)
for tl_id in traci.trafficlight.getIDList(): # add traffic lights
if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id):
logics = []
for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics
phases = []
for phase in traci.trafficlight.Logic.getPhases(l): #add phases to logics
phases.append(parsePhase(phase.__repr__()))
logics.append(Logic(l,phases))
area.add_tl(TrafficLight(tl_id,logics))
def main(): def main():
grid = list() grid = list()
try: try:
traci.start(config.sumo_cmd) traci.start(config.sumo_cmd)
logger.info('Loading data for the simulation')
start = time.perf_counter()
grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER) grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
add_data_to_areas(grid) add_data_to_areas(grid)
loading_time = round(time.perf_counter() - start,2)
logger.info(f'Data loaded ({loading_time}s)')
logger.info('Start of the simulation')
step = 0 step = 0
while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0: while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep() traci.simulationStep()
@ -118,28 +139,26 @@ def main():
get_emissions(grid, vehicles) get_emissions(grid, vehicles)
if config.weight_routing_mode: if config.weight_routing_mode:
logger.info('Action - Lane weights adjusted')
actions.adjust_edges_weights() actions.adjust_edges_weights()
step += 1 step += 1
progress = round(step/config.n_steps*100,2)
sys.stdout.write(f'Progress : {progress}%'+'\r')
sys.stdout.flush()
finally: finally:
traci.close(False) traci.close(False)
logger.info('End of the simulation')
total_emissions = 0 total_emissions = 0
for area in grid: for area in grid:
total_emissions += area.emissions total_emissions += area.emissions
print("\n**** RESULTS ****") logger.info(f'Total emissions = {total_emissions} mg')
print(f'Total emissions = {total_emissions} mg')
ref = config.get_basics_emissions() ref = config.get_basics_emissions()
diff_with_actions = (ref - total_emissions)/ref diff_with_actions = (ref - total_emissions)/ref
print(f'Reduction percentage of emissions = {diff_with_actions*100} %') logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %')
print("**** With the configuration : ****\n" + str(config.showConfig())) logger.info('With the configuration : \n' + str(config.showConfig()))
logger.info('Logs END')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -48,6 +48,7 @@ class Area:
def __init__(self, coords, name=''): def __init__(self, coords, name=''):
self.limited_speed = False self.limited_speed = False
self.locked = False self.locked = False
self.tls_adjusted = False
self.rectangle = Polygon(coords) self.rectangle = Polygon(coords)
self.name = name self.name = name
self.emissions = 0.0 self.emissions = 0.0