From 4519ce9d2c2ed64311378a964efb9d79e5777dda Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Fri, 14 Dec 2018 15:50:26 +0100 Subject: [PATCH 01/10] Fixed floating window with deque list --- sumo_project/actions.py | 25 +++++++++------- sumo_project/emissions.py | 63 +++++++++++++++++++++++---------------- sumo_project/model.py | 17 +++++++---- 3 files changed, 64 insertions(+), 41 deletions(-) diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 484d7f5..4a7a854 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -13,17 +13,22 @@ from model import Area, Vehicle def compute_edge_weight(edge_id): - return (traci.edge.getCOEmission(edge_id) - + traci.edge.getNOxEmission(edge_id) - + traci.edge.getHCEmission(edge_id) - + traci.edge.getPMxEmission(edge_id) - + traci.edge.getCO2Emission(edge_id))/(traci.edge.getLaneNumber(edge_id)) - -def adjust_edges_weights(): - for edge_id in traci.edge.getIDList(): + + co2 = traci.edge.getCO2Emission(edge_id) + co = traci.edge.getCOEmission(edge_id) + nox = traci.edge.getNOxEmission(edge_id) + hc = traci.edge.getHCEmission(edge_id) + pmx = traci.edge.getPMxEmission(edge_id) + + return (co2 + co + nox + hc + pmx)/traci.edge.getLaneNumber(edge_id) + +def adjust_edges_weights(area): + area.weight_adjusted = True + for lane in area._lanes: + edge_id = traci.lane.getEdgeID(lane.lane_id) weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed traci.edge.setEffort(edge_id, weight) - + for veh_id in traci.vehicle.getIDList(): traci.vehicle.rerouteEffort(veh_id) @@ -31,7 +36,7 @@ def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], speed_rf): area.limited_speed = True for lane in area._lanes: traci.lane.setMaxSpeed(lane.lane_id, speed_rf * lane.initial_max_speed) - + def modifyLogic(logic, rf): #rf for "reduction factor" new_phases = [] for phase in logic._phases: diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index dcb5792..b0da161 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -13,7 +13,7 @@ from config import Config from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic -def init_grid(simulation_bounds, areas_number): +def init_grid(simulation_bounds, areas_number,window_size): grid = list() width = simulation_bounds[1][0] / areas_number height = simulation_bounds[1][1] / areas_number @@ -22,10 +22,10 @@ def init_grid(simulation_bounds, areas_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) + name = 'Area ({},{})'.format(i, j) + area = Area(ar_bounds, name, window_size) grid.append(area) - traci.polygon.add(area.name, ar_bounds, (0, 255, 0)) + traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) return grid @@ -108,11 +108,14 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi logger.info(f'Action - {area.name} blocked') actions.lock_area(area) - traci.polygon.setColor(area.name, (255, 0, 0)) + if config.weight_routing_mode and not area.weight_adjusted: + actions.adjust_edges_weights(area) + traci.polygon.setFilled(area.name, True) else: actions.reverse_actions(area) + traci.polygon.setFilled(area.name, False) def run(config, logger): @@ -123,25 +126,23 @@ def run(config, logger): logger.info('Loading data for the simulation') start = time.perf_counter() - grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number) + grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) 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') + logger.info('Simulation started...') step = 0 while step < config.n_steps : # traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() - + vehicles = get_all_vehicles() get_emissions(grid, vehicles, step, config, logger) - - if config.weight_routing_mode: - actions.adjust_edges_weights() - step += 1 + print(f'step = {step}/{config.n_steps}', end='\r') + finally: traci.close(False) simulation_time = round(time.perf_counter() - start, 2) @@ -158,33 +159,45 @@ def run(config, logger): if not (ref is None): diff_with_actions = (ref - total_emissions) / ref logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %') - + +def add_options(parser): + parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False, + help='Choose your configuration file from your working directory') + parser.add_argument("-save", "--save", action="store_true", + help = 'Save the logs into the logs folder') + parser.add_argument("-steps", "--steps", type= int, default= 200, required = False, + help='Choose the simulated time (in seconds)') + parser.add_argument("-ref", "--ref", action="store_true", + help='Launch a reference simulation (without acting on areas)') + parser.add_argument("-gui", "--gui", action ="store_true", + help= "Set GUI mode") def main(args): + parser = argparse.ArgumentParser(description="") - parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False) - parser.add_argument("-save", "--save", action="store_true") - parser.add_argument("-ref", "--ref", action="store_true") + add_options(parser) args = parser.parse_args(args) - # > py ./emissions.py -f configs/config1.json -save - # will load the configuration file "config1.json" and save logs into the logs directory - - # > py ./emissions.py -f configs/config1.json -save -ref & py ./emissions.py -f configs/config1.json -save - # same as above but also launches a reference simulation by using -ref option - config = Config() config.import_config_file(args.configfile) config.init_traci() logger = config.init_logger(save_logs=args.save) + if args.ref: config.without_actions_mode = True - config.check_config() logger.info(f'Reference simulation') - logger.info(f'Loaded configuration file : {args.configfile}') + if args.steps: + config.n_steps = args.steps + + if args.gui: + config._SUMOCMD = "sumo-gui" + + config.check_config() + + logger.info(f'Loaded configuration file : {args.configfile}') + logger.info(f'Simulated time : {args.steps}') run(config, logger) - if __name__ == '__main__': main(sys.argv[1:]) diff --git a/sumo_project/model.py b/sumo_project/model.py index e910840..68101b4 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -5,6 +5,8 @@ from shapely.geometry import Point, LineString from shapely.geometry import Polygon from shapely.geometry.base import BaseGeometry +import collections + class Lane: @@ -45,13 +47,15 @@ class TrafficLight: class Area: - def __init__(self, coords, name=''): + def __init__(self, coords, name, window_size): self.limited_speed = False self.locked = False self.tls_adjusted = False + self.weight_adjusted = False self.rectangle = Polygon(coords) self.name = name - self.emissions_by_step = [] + self.emissions_by_step = [] + self.window = collections.deque(maxlen = window_size) self._lanes: Set[Lane] = set() self._tls: Set[TrafficLight] = set() @@ -83,11 +87,12 @@ class Area: sum += emission return sum - def sum_emissions_into_window(self, current_step, window_size): + def sum_emissions_into_window(self, current_step, window_size): + + self.window.appendleft(self.emissions_by_step[current_step]) sum = 0 - q = current_step // window_size #Returns the integral part of the quotient - for i in range(q*window_size, current_step): - sum += self.emissions_by_step[i] + for i in range(self.window.__len__()): + sum += self.window[i] return sum @classmethod From 86f694756d9efcad1fed08d458ba9f402f4c3a55 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Fri, 14 Dec 2018 17:53:46 +0100 Subject: [PATCH 02/10] Added Emission class for more details into logs --- sumo_project/actions.py | 2 +- sumo_project/config.py | 13 ++++------- sumo_project/emissions.py | 47 ++++++++++++++++++++++++--------------- sumo_project/model.py | 29 ++++++++++++++++++++---- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 4a7a854..302a9ce 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -20,7 +20,7 @@ def compute_edge_weight(edge_id): hc = traci.edge.getHCEmission(edge_id) pmx = traci.edge.getPMxEmission(edge_id) - return (co2 + co + nox + hc + pmx)/traci.edge.getLaneNumber(edge_id) + return (co2 + co + nox + hc + pmx) def adjust_edges_weights(area): area.weight_adjusted = True diff --git a/sumo_project/config.py b/sumo_project/config.py index e139ffb..8f331dd 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -8,14 +8,13 @@ import logging import os import sys +from model import Emission class Config: # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas # These constants are simulation dependant, you must change them according to your simulation - total_emissions100 = 13615949.148296086 - total_emissions200 = 43970763.15084738 - total_emissions300 = 87382632.0821697 + ref200 = Emission(co2=42816869.05436445,co=1128465.0343051048,nox=18389.648337283958,hc=6154.330914019103,pmx=885.0829265236318) def __init__(self): '''Default constructor''' @@ -101,12 +100,8 @@ class Config: return logger - def get_basics_emissions(self): - if self.n_steps == 100: - return self.total_emissions100 + def get_ref_emissions(self): if self.n_steps == 200: - return self.total_emissions200 - if self.n_steps == 300: - return self.total_emissions300 + return self.ref200 diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index b0da161..5b7b7d7 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -10,7 +10,7 @@ from shapely.geometry import LineString import actions from config import Config -from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic +from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission def init_grid(simulation_bounds, areas_number,window_size): @@ -68,12 +68,14 @@ def add_data_to_areas(areas: List[Area]): 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)) - + + co2 = traci.vehicle.getCO2Emission(veh_id) + co = traci.vehicle.getCOEmission(veh_id) + nox = traci.vehicle.getNOxEmission(veh_id) + hc = traci.vehicle.getHCEmission(veh_id) + pmx = traci.vehicle.getPMxEmission(veh_id) + + return Emission(co2,co,nox,hc,pmx) def get_all_vehicles() -> List[Vehicle]: vehicles = list() @@ -87,12 +89,12 @@ def get_all_vehicles() -> List[Vehicle]: def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, config, logger): for area in grid: - vehicle_emissions = 0 + total_emissions = Emission() for vehicle in vehicles: if vehicle.pos in area: - vehicle_emissions += vehicle.emissions - - area.emissions_by_step.append(vehicle_emissions) + total_emissions += vehicle.emissions + + area.emissions_by_step.append(total_emissions) if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold: @@ -117,7 +119,9 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi actions.reverse_actions(area) traci.polygon.setFilled(area.name, False) - +def get_reduction_percentage(ref,total): + return (ref - total) / ref * 100 + def run(config, logger): grid = list() try: @@ -148,17 +152,24 @@ def run(config, logger): simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') - total_emissions = 0 + total_emissions = Emission() for area in grid: total_emissions += area.sum_all_emissions() - - logger.info(f'Total emissions = {total_emissions} mg') + + logger.info(f'Total emissions = {total_emissions.value()} mg') if not config.without_actions_mode : - ref = config.get_basics_emissions() + ref = config.get_ref_emissions() if not (ref is None): - diff_with_actions = (ref - total_emissions) / ref - logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %') + global_diff = (ref.value() - total_emissions.value()) / ref.value() + + logger.info(f'Global reduction percentage of emissions = {global_diff*100} %') + logger.info(f'-> CO2 emissions = {get_reduction_percentage(ref.co2, total_emissions.co2)} %') + logger.info(f'-> CO emissions = {get_reduction_percentage(ref.co, total_emissions.co)} %') + logger.info(f'-> Nox emissions = {get_reduction_percentage(ref.nox, total_emissions.nox)} %') + logger.info(f'-> HC emissions = {get_reduction_percentage(ref.hc, total_emissions.hc)} %') + logger.info(f'-> PMx emissions = {get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') + def add_options(parser): parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False, diff --git a/sumo_project/model.py b/sumo_project/model.py index 68101b4..55d9f89 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -44,7 +44,26 @@ class TrafficLight: def __hash__(self): """Overrides the default implementation""" return hash(self.tl_id) + +class Emission: + def __init__(self, co2 = 0, co = 0 , nox = 0, hc = 0, pmx = 0): + self.co2 = co2 + self.co = co + self.nox = nox + self.hc = hc + self.pmx = pmx + def __add__(self,other): + return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc, self.pmx + other.pmx) + + + def value(self): + return self.co2 + self.co + self.nox + self.hc + self.pmx + + def __repr__(self) -> str: + repr = f'Emission(co2={self.co2},co={self.co},nox={self.nox},hc={self.hc},pmx={self.pmx})' + return str(repr) + class Area: def __init__(self, coords, name, window_size): @@ -82,14 +101,16 @@ class Area: self._lanes.remove(lane) def sum_all_emissions(self): - sum = 0 + sum = Emission() for emission in self.emissions_by_step: sum += emission return sum def sum_emissions_into_window(self, current_step, window_size): - - self.window.appendleft(self.emissions_by_step[current_step]) + #print(self.emissions_by_step) + em_obj = self.emissions_by_step[current_step] + self.window.appendleft(em_obj.value()) + sum = 0 for i in range(self.window.__len__()): sum += self.window[i] @@ -106,7 +127,7 @@ class Area: class Vehicle: def __init__(self, veh_id: int, pos: Tuple[float, float]): - self.emissions: float = 0.0 + self.emissions: Emission = Emission() self.veh_id = veh_id self.pos = Point(pos) From c76eea9d764aa497e458bb40bfed631e183fde01 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Sun, 16 Dec 2018 15:33:10 +0100 Subject: [PATCH 03/10] Added data to csv export --- sumo_project/config.py | 1 + sumo_project/emissions.py | 35 ++++++++++++++++++++++++++++++++++- sumo_project/model.py | 8 +++----- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/sumo_project/config.py b/sumo_project/config.py index 8f331dd..de9727b 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -10,6 +10,7 @@ import sys from model import Emission + class Config: # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 5b7b7d7..b72792b 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -1,9 +1,12 @@ import argparse +import csv import sys import time from traci import trafficlight import traci from typing import List +import datetime +import os from parse import search from shapely.geometry import LineString @@ -121,6 +124,33 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi def get_reduction_percentage(ref,total): return (ref - total) / ref * 100 + +def export_data_to_csv(config, grid): + + now = datetime.datetime.now() + current_date = now.strftime("%Y_%m_%d_%H_%M_%S") + + '''if not os.path.exists(f'csv/{current_date}'): + os.makedirs(f'csv/{current_date}')''' + + '''with open(f'test.csv', mode='w', newline = '') as emission_file: + try: + csv_writer = csv.writer(emission_file, delimiter = '', quoting=csv.QUOTE_MINIMAL) + a = list(range(config.n_steps)) + csv_writer.writerow(['steps']) + for item in a: + csv_writer.writerow([item]) + + for area in grid: + writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL) + writer.writerow([f'{area.name}']) + for emission in area.emissions_by_step: + writer.writerow([emission.value()]) + + + finally: + emission_file.close()''' + def run(config, logger): grid = list() @@ -131,6 +161,7 @@ def run(config, logger): start = time.perf_counter() grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) + export_data_to_csv(config,grid) add_data_to_areas(grid) loading_time = round(time.perf_counter() - start, 2) @@ -149,8 +180,10 @@ def run(config, logger): finally: traci.close(False) + simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') + logger.info(f'Real-time factor : {config.n_steps/simulation_time}') total_emissions = Emission() for area in grid: @@ -207,7 +240,7 @@ def main(args): config.check_config() logger.info(f'Loaded configuration file : {args.configfile}') - logger.info(f'Simulated time : {args.steps}') + logger.info(f'Simulated time : {args.steps}s') run(config, logger) if __name__ == '__main__': diff --git a/sumo_project/model.py b/sumo_project/model.py index 55d9f89..210918b 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -1,3 +1,4 @@ +import collections from traci._trafficlight import Logic as SUMO_Logic from typing import Tuple, Set @@ -5,8 +6,6 @@ from shapely.geometry import Point, LineString from shapely.geometry import Polygon from shapely.geometry.base import BaseGeometry -import collections - class Lane: @@ -107,9 +106,8 @@ class Area: return sum def sum_emissions_into_window(self, current_step, window_size): - #print(self.emissions_by_step) - em_obj = self.emissions_by_step[current_step] - self.window.appendleft(em_obj.value()) + + self.window.appendleft(self.emissions_by_step[current_step].value()) sum = 0 for i in range(self.window.__len__()): From 229ab72279c4058ef5c120270ed2337ed95c2078 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Sun, 16 Dec 2018 15:44:33 +0100 Subject: [PATCH 04/10] Added data to csv export --- sumo_project/emissions.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index b72792b..375d57f 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -133,23 +133,21 @@ def export_data_to_csv(config, grid): '''if not os.path.exists(f'csv/{current_date}'): os.makedirs(f'csv/{current_date}')''' - '''with open(f'test.csv', mode='w', newline = '') as emission_file: + with open(f'test.csv', mode='w', newline = '') as emission_file: try: - csv_writer = csv.writer(emission_file, delimiter = '', quoting=csv.QUOTE_MINIMAL) - a = list(range(config.n_steps)) + csv_writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL) csv_writer.writerow(['steps']) + a = list(range(config.n_steps)) for item in a: csv_writer.writerow([item]) for area in grid: - writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL) - writer.writerow([f'{area.name}']) + csv_writer.writerow([f'{area.name}']) for emission in area.emissions_by_step: - writer.writerow([emission.value()]) - + csv_writer.writerow([emission.value()]) finally: - emission_file.close()''' + emission_file.close() def run(config, logger): @@ -161,7 +159,6 @@ def run(config, logger): start = time.perf_counter() grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) - export_data_to_csv(config,grid) add_data_to_areas(grid) loading_time = round(time.perf_counter() - start, 2) @@ -180,7 +177,8 @@ def run(config, logger): finally: traci.close(False) - + export_data_to_csv(config,grid) + simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') logger.info(f'Real-time factor : {config.n_steps/simulation_time}') From 2a4ce7f935112d6f7a8722b7482a57856cb7f60f Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sun, 16 Dec 2018 21:35:37 +0100 Subject: [PATCH 05/10] Fix CSV export code --- sumo_project/emissions.py | 45 ++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 375d57f..a125f43 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -1,5 +1,6 @@ import argparse import csv +import itertools import sys import time from traci import trafficlight @@ -15,6 +16,9 @@ import actions from config import Config from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission +# Absolute path of the directory the script is in +SCRIPTDIR = os.path.dirname(__file__) + def init_grid(simulation_bounds, areas_number,window_size): grid = list() @@ -125,31 +129,24 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi def get_reduction_percentage(ref,total): return (ref - total) / ref * 100 + def export_data_to_csv(config, grid): - - now = datetime.datetime.now() - current_date = now.strftime("%Y_%m_%d_%H_%M_%S") - - '''if not os.path.exists(f'csv/{current_date}'): - os.makedirs(f'csv/{current_date}')''' - - with open(f'test.csv', mode='w', newline = '') as emission_file: - try: - csv_writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL) - csv_writer.writerow(['steps']) - a = list(range(config.n_steps)) - for item in a: - csv_writer.writerow([item]) - - for area in grid: - csv_writer.writerow([f'{area.name}']) - for emission in area.emissions_by_step: - csv_writer.writerow([emission.value()]) - - finally: - emission_file.close() - - + csv_dir = os.path.join(SCRIPTDIR, 'csv') + if not os.path.exists(csv_dir): + os.mkdir(csv_dir) + now = datetime.datetime.utcnow().isoformat() + + with open(os.path.join(csv_dir, f'{now}.csv'), 'w') as f: + writer = csv.writer(f) + # Write CSV headers + writer.writerow(itertools.chain(('Step',), (a.name for a in grid))) + emissions = (a.emissions_by_step for a in grid) + step = 0 + for em in emissions: + writer.writerow(itertools.chain((step,), (e.value() for e in em))) + step += 1 + + def run(config, logger): grid = list() try: From 61da2d730368c3c8f2b92bcc66e0a4c1ceeedd50 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sun, 16 Dec 2018 21:43:21 +0100 Subject: [PATCH 06/10] Reformat code :rocket: --- sumo_project/config.py | 75 +++++++++++---------- sumo_project/emissions.py | 136 ++++++++++++++++++++------------------ sumo_project/model.py | 56 +++++++++------- 3 files changed, 138 insertions(+), 129 deletions(-) diff --git a/sumo_project/config.py b/sumo_project/config.py index de9727b..ebceed5 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -11,27 +11,27 @@ import sys from model import Emission -class Config: - +class Config: # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas # These constants are simulation dependant, you must change them according to your simulation - ref200 = Emission(co2=42816869.05436445,co=1128465.0343051048,nox=18389.648337283958,hc=6154.330914019103,pmx=885.0829265236318) - + ref200 = Emission(co2=42816869.05436445, co=1128465.0343051048, nox=18389.648337283958, hc=6154.330914019103, + pmx=885.0829265236318) + def __init__(self): - '''Default constructor''' - - def import_config_file(self,config_file): - with open(config_file,'r') as f: + """Default constructor""" + + def import_config_file(self, config_file): + with open(config_file, 'r') as f: data = json.load(f) - + self._SUMOCMD = data["_SUMOCMD"] self._SUMOCFG = data["_SUMOCFG"] - + self.areas_number = data["areas_number"] self.emissions_threshold = data["emissions_threshold"] self.n_steps = data["n_steps"] self.window_size = data["window_size"] - + self.without_actions_mode = data["without_actions_mode"] self.limit_speed_mode = data["limit_speed_mode"] self.speed_rf = data["speed_rf"] @@ -39,58 +39,59 @@ class Config: self.trafficLights_duration_rf = data["trafficLights_duration_rf"] self.weight_routing_mode = data["weight_routing_mode"] self.lock_area_mode = data["lock_area_mode"] - + self.check_config() - + def check_config(self): - #Weight routing mode cannot be combinated with other actions + # Weight routing mode cannot be combinated with other actions if self.weight_routing_mode: self.limit_speed_mode = False self.adjust_traffic_light_mode = False self.lock_area_mode = False - - #If without_actions_mode is choosen + + # If without_actions_mode is choosen if self.without_actions_mode: self.limit_speed_mode = False self.adjust_traffic_light_mode = False self.weight_routing_mode = False self.lock_area_mode = False - - + def __repr__(self) -> str: - return (str(f'grid : {self.areas_number}x{self.areas_number}\n') - + str(f'step number = {self.n_steps}\n') - + str(f'window size = {self.window_size}\n') - + str(f'weight routing mode = {self.weight_routing_mode}\n') - + str(f'lock area mode = {self.lock_area_mode}\n') - + str(f'limit speed mode = {self.limit_speed_mode}, RF = {self.speed_rf*100}%\n') - + str(f'adjust traffic light mode = {self.adjust_traffic_light_mode} , RF = {self.trafficLights_duration_rf*100}%\n')) - - + return ( + f'grid : {self.areas_number}x{self.areas_number}\n' + f'step number = {self.n_steps}\n' + f'window size = {self.window_size}\n' + f'weight routing mode = {self.weight_routing_mode}\n' + f'lock area mode = {self.lock_area_mode}\n' + f'limit speed mode = {self.limit_speed_mode}, RF = {self.speed_rf * 100}%\n' + f'adjust traffic light mode = {self.adjust_traffic_light_mode},' + 'RF = {self.trafficLights_duration_rf * 100}%\n' + ) + def init_traci(self): if 'SUMO_HOME' in os.environ: tools = os.path.join(os.environ['SUMO_HOME'], 'tools') sys.path.append(tools) else: sys.exit("please declare environment variable 'SUMO_HOME'") - + sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', self._SUMOCMD) self.sumo_cmd = [sumo_binary, "-c", self._SUMOCFG] - - def init_logger(self, save_logs = False): + + def init_logger(self, save_logs=False): now = datetime.datetime.now() current_date = now.strftime("%Y_%m_%d_%H_%M_%S") - + if not os.path.exists('logs'): os.makedirs('logs') - + log_filename = f'logs/sumo_logs_{current_date}.log' - + logger = logging.getLogger("sumo_logger") logger.setLevel(logging.INFO) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") - - if save_logs : + + if save_logs: file_handler = logging.FileHandler(log_filename) file_handler.setFormatter(formatter) logger.addHandler(file_handler) @@ -98,11 +99,9 @@ class Config: handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) - + return logger def get_ref_emissions(self): if self.n_steps == 200: return self.ref200 - - diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index a125f43..4ef581e 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -1,26 +1,25 @@ import argparse import csv +import datetime import itertools +import os import sys import time -from traci import trafficlight -import traci from typing import List -import datetime -import os +import traci from parse import search from shapely.geometry import LineString import actions from config import Config -from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission +from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission # Absolute path of the directory the script is in SCRIPTDIR = os.path.dirname(__file__) -def init_grid(simulation_bounds, areas_number,window_size): +def init_grid(simulation_bounds, areas_number, window_size): grid = list() width = simulation_bounds[1][0] / areas_number height = simulation_bounds[1][1] / areas_number @@ -47,14 +46,16 @@ def get_all_lanes() -> List[Lane]: def parse_phase(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) + min_duration = search('min_duration: {:f}', phase_repr) + max_duration = search('max_duration: {:f}', phase_repr) + phase_def = search('phase_def: {}\n', phase_repr) - if phaseDef is None: phaseDef = '' - else : phaseDef = phaseDef[0] + if phase_def is None: + phase_def = '' + else: + phase_def = phase_def[0] - return Phase(duration[0], minDuration[0], maxDuration[0], phaseDef) + return Phase(duration[0], min_duration[0], max_duration[0], phase_def) def add_data_to_areas(areas: List[Area]): @@ -70,19 +71,19 @@ def add_data_to_areas(areas: List[Area]): phases = [] for phase in traci.trafficlight.Logic.getPhases(l): # add phases to logics phases.append(parse_phase(phase.__repr__())) - logics.append(Logic(l, phases)) + logics.append(Logic(l, phases)) area.add_tl(TrafficLight(tl_id, logics)) def compute_vehicle_emissions(veh_id): - co2 = traci.vehicle.getCO2Emission(veh_id) co = traci.vehicle.getCOEmission(veh_id) nox = traci.vehicle.getNOxEmission(veh_id) hc = traci.vehicle.getHCEmission(veh_id) pmx = traci.vehicle.getPMxEmission(veh_id) - - return Emission(co2,co,nox,hc,pmx) + + return Emission(co2, co, nox, hc, pmx) + def get_all_vehicles() -> List[Vehicle]: vehicles = list() @@ -100,33 +101,35 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi for vehicle in vehicles: if vehicle.pos in area: total_emissions += vehicle.emissions - + area.emissions_by_step.append(total_emissions) - - if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold: - + + if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold: + if config.limit_speed_mode and not area.limited_speed: - logger.info(f'Action - Decreased max speed into {area.name} by {config.speed_rf*100}%') + logger.info(f'Action - Decreased max speed into {area.name} by {config.speed_rf * 100}%') actions.limit_speed_into_area(area, vehicles, config.speed_rf) if config.adjust_traffic_light_mode and not area.tls_adjusted: - logger.info(f'Action - Decreased traffic lights duration by {config.trafficLights_duration_rf*100}%') + logger.info( + f'Action - Decreased 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 actions.count_vehicles_in_area(area): logger.info(f'Action - {area.name} blocked') actions.lock_area(area) - + if config.weight_routing_mode and not area.weight_adjusted: actions.adjust_edges_weights(area) - + traci.polygon.setFilled(area.name, True) - + else: actions.reverse_actions(area) traci.polygon.setFilled(area.name, False) -def get_reduction_percentage(ref,total): + +def get_reduction_percentage(ref, total): return (ref - total) / ref * 100 @@ -154,89 +157,90 @@ def run(config, logger): logger.info(f'Loaded simulation file : {config._SUMOCFG}') logger.info('Loading data for the simulation') start = time.perf_counter() - + grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) add_data_to_areas(grid) - + loading_time = round(time.perf_counter() - start, 2) logger.info(f'Data loaded ({loading_time}s)') - + logger.info('Simulation started...') - step = 0 - while step < config.n_steps : # traci.simulation.getMinExpectedNumber() > 0: + step = 0 + while step < config.n_steps: # traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() - + vehicles = get_all_vehicles() get_emissions(grid, vehicles, step, config, logger) step += 1 - + print(f'step = {step}/{config.n_steps}', end='\r') - + finally: traci.close(False) - export_data_to_csv(config,grid) - + export_data_to_csv(config, grid) + simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') - logger.info(f'Real-time factor : {config.n_steps/simulation_time}') - + logger.info(f'Real-time factor : {config.n_steps / simulation_time}') + total_emissions = Emission() for area in grid: total_emissions += area.sum_all_emissions() - + logger.info(f'Total emissions = {total_emissions.value()} mg') - - if not config.without_actions_mode : + + if not config.without_actions_mode: ref = config.get_ref_emissions() if not (ref is None): - global_diff = (ref.value() - total_emissions.value()) / ref.value() - - logger.info(f'Global reduction percentage of emissions = {global_diff*100} %') + global_diff = (ref.value() - total_emissions.value()) / ref.value() + + logger.info(f'Global reduction percentage of emissions = {global_diff * 100} %') logger.info(f'-> CO2 emissions = {get_reduction_percentage(ref.co2, total_emissions.co2)} %') logger.info(f'-> CO emissions = {get_reduction_percentage(ref.co, total_emissions.co)} %') - logger.info(f'-> Nox emissions = {get_reduction_percentage(ref.nox, total_emissions.nox)} %') - logger.info(f'-> HC emissions = {get_reduction_percentage(ref.hc, total_emissions.hc)} %') - logger.info(f'-> PMx emissions = {get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') + logger.info(f'-> Nox emissions = {get_reduction_percentage(ref.nox, total_emissions.nox)} %') + logger.info(f'-> HC emissions = {get_reduction_percentage(ref.hc, total_emissions.hc)} %') + logger.info(f'-> PMx emissions = {get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') def add_options(parser): parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False, help='Choose your configuration file from your working directory') - parser.add_argument("-save", "--save", action="store_true", - help = 'Save the logs into the logs folder') - parser.add_argument("-steps", "--steps", type= int, default= 200, required = False, - help='Choose the simulated time (in seconds)') - parser.add_argument("-ref", "--ref", action="store_true", + parser.add_argument("-save", "--save", action="store_true", + help='Save the logs into the logs folder') + parser.add_argument("-steps", "--steps", type=int, default=200, required=False, + help='Choose the simulated time (in seconds)') + parser.add_argument("-ref", "--ref", action="store_true", help='Launch a reference simulation (without acting on areas)') - parser.add_argument("-gui", "--gui", action ="store_true", - help= "Set GUI mode") - + parser.add_argument("-gui", "--gui", action="store_true", + help="Set GUI mode") + + def main(args): - parser = argparse.ArgumentParser(description="") add_options(parser) args = parser.parse_args(args) - + config = Config() config.import_config_file(args.configfile) config.init_traci() logger = config.init_logger(save_logs=args.save) - - if args.ref: + + if args.ref: config.without_actions_mode = True logger.info(f'Reference simulation') - - if args.steps: - config.n_steps = args.steps - + + if args.steps: + config.n_steps = args.steps + if args.gui: config._SUMOCMD = "sumo-gui" - + config.check_config() - + logger.info(f'Loaded configuration file : {args.configfile}') logger.info(f'Simulated time : {args.steps}s') run(config, logger) + if __name__ == '__main__': main(sys.argv[1:]) diff --git a/sumo_project/model.py b/sumo_project/model.py index 210918b..3b4bc82 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -1,10 +1,10 @@ -import collections -from traci._trafficlight import Logic as SUMO_Logic +import collections from typing import Tuple, Set from shapely.geometry import Point, LineString from shapely.geometry import Polygon from shapely.geometry.base import BaseGeometry +from traci._trafficlight import Logic as SUMO_Logic class Lane: @@ -18,51 +18,56 @@ class Lane: """Overrides the default implementation""" return hash(self.lane_id) + class Phase: - def __init__(self, duration: float, minDuration: float, maxDuration : float, phaseDef: str): - self.duration = duration + def __init__(self, duration: float, minDuration: float, maxDuration: float, phaseDef: str): + self.duration = duration self.minDuration = minDuration self.maxDuration = maxDuration - self.phaseDef = phaseDef - + self.phaseDef = phaseDef + def __repr__(self) -> str: repr = f'Phase(duration:{self.duration},minDuration:{self.minDuration},maxDuration:{self.maxDuration},phaseDef:{self.phaseDef})' return str(repr) + class Logic: def __init__(self, logic: SUMO_Logic, phases: Set[Phase]): self._logic = logic self._phases: Set[Phase] = phases -class TrafficLight: - + +class TrafficLight: + def __init__(self, tl_id: str, logics: Set[Logic]): - self.tl_id = tl_id + self.tl_id = tl_id self._logics: Set[Logic] = logics - + def __hash__(self): """Overrides the default implementation""" return hash(self.tl_id) + class Emission: - def __init__(self, co2 = 0, co = 0 , nox = 0, hc = 0, pmx = 0): + def __init__(self, co2=0, co=0, nox=0, hc=0, pmx=0): self.co2 = co2 self.co = co self.nox = nox self.hc = hc self.pmx = pmx - - def __add__(self,other): - return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc, self.pmx + other.pmx) - - + + def __add__(self, other): + return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc, + self.pmx + other.pmx) + def value(self): return self.co2 + self.co + self.nox + self.hc + self.pmx - + def __repr__(self) -> str: repr = f'Emission(co2={self.co2},co={self.co},nox={self.nox},hc={self.hc},pmx={self.pmx})' return str(repr) + class Area: def __init__(self, coords, name, window_size): @@ -73,9 +78,9 @@ class Area: self.rectangle = Polygon(coords) self.name = name self.emissions_by_step = [] - self.window = collections.deque(maxlen = window_size) + self.window = collections.deque(maxlen=window_size) self._lanes: Set[Lane] = set() - self._tls: Set[TrafficLight] = set() + self._tls: Set[TrafficLight] = set() def __eq__(self, other): return self.rectangle.__eq__(other) @@ -92,23 +97,23 @@ class Area: def add_lane(self, lane: Lane): self._lanes.add(lane) - + def add_tl(self, tl: TrafficLight): self._tls.add(tl) def remove_lane(self, lane: Lane): self._lanes.remove(lane) - + def sum_all_emissions(self): sum = Emission() for emission in self.emissions_by_step: sum += emission - return sum - - def sum_emissions_into_window(self, current_step, window_size): + return sum + + def sum_emissions_into_window(self, current_step, window_size): self.window.appendleft(self.emissions_by_step[current_step].value()) - + sum = 0 for i in range(self.window.__len__()): sum += self.window[i] @@ -122,6 +127,7 @@ class Area: (xmax, ymax), (xmax, ymin))) + class Vehicle: def __init__(self, veh_id: int, pos: Tuple[float, float]): From ba3e3d160b0ec1be431fba175e8d1f7e67a9a435 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sun, 16 Dec 2018 21:48:53 +0100 Subject: [PATCH 07/10] Fix small typo in Config __repr__ --- sumo_project/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sumo_project/config.py b/sumo_project/config.py index ebceed5..ef2fa0c 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -65,7 +65,7 @@ class Config: f'lock area mode = {self.lock_area_mode}\n' f'limit speed mode = {self.limit_speed_mode}, RF = {self.speed_rf * 100}%\n' f'adjust traffic light mode = {self.adjust_traffic_light_mode},' - 'RF = {self.trafficLights_duration_rf * 100}%\n' + f'RF = {self.trafficLights_duration_rf * 100}%\n' ) def init_traci(self): From d6436a941f17259b811b42e6711cad34943045bd Mon Sep 17 00:00:00 2001 From: Thibaud Date: Tue, 18 Dec 2018 10:57:11 +0100 Subject: [PATCH 08/10] Rollback phases names in parse_phase --- sumo_project/emissions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 4ef581e..5c11a41 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -46,9 +46,9 @@ def get_all_lanes() -> List[Lane]: def parse_phase(phase_repr): duration = search('duration: {:f}', phase_repr) - min_duration = search('min_duration: {:f}', phase_repr) - max_duration = search('max_duration: {:f}', phase_repr) - phase_def = search('phase_def: {}\n', phase_repr) + min_duration = search('minDuration: {:f}', phase_repr) + max_duration = search('maxDuration: {:f}', phase_repr) + phase_def = search('phaseDef: {}\n', phase_repr) if phase_def is None: phase_def = '' From 6042134412e84aae1c371072f2209542c79dd788 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 19 Dec 2018 11:02:19 +0100 Subject: [PATCH 09/10] Fix CSV generation --- sumo_project/emissions.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 5c11a41..d155679 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -143,11 +143,10 @@ def export_data_to_csv(config, grid): writer = csv.writer(f) # Write CSV headers writer.writerow(itertools.chain(('Step',), (a.name for a in grid))) - emissions = (a.emissions_by_step for a in grid) - step = 0 - for em in emissions: - writer.writerow(itertools.chain((step,), (e.value() for e in em))) - step += 1 + # Write all areas emission value for each step + for step in range(config.n_steps): + em_for_step = (f'{a.emissions_by_step[step].value():.3f}' for a in grid) + writer.writerow(itertools.chain((step,), em_for_step)) def run(config, logger): From 390ce6ef2c17e7632258394f74aaddf320d04a20 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Wed, 16 Jan 2019 17:26:02 +0100 Subject: [PATCH 10/10] Fixed csv export --- sumo_project/emissions.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 5c11a41..1293e80 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -15,10 +15,6 @@ import actions from config import Config from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission -# Absolute path of the directory the script is in -SCRIPTDIR = os.path.dirname(__file__) - - def init_grid(simulation_bounds, areas_number, window_size): grid = list() width = simulation_bounds[1][0] / areas_number @@ -137,17 +133,17 @@ def export_data_to_csv(config, grid): csv_dir = os.path.join(SCRIPTDIR, 'csv') if not os.path.exists(csv_dir): os.mkdir(csv_dir) - now = datetime.datetime.utcnow().isoformat() - - with open(os.path.join(csv_dir, f'{now}.csv'), 'w') as f: + + now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + + with open(f'csv/{now}.csv', 'w') as f: writer = csv.writer(f) # Write CSV headers writer.writerow(itertools.chain(('Step',), (a.name for a in grid))) - emissions = (a.emissions_by_step for a in grid) - step = 0 - for em in emissions: - writer.writerow(itertools.chain((step,), (e.value() for e in em))) - step += 1 + # Write all areas emission value for each step + for step in range(config.n_steps): + em_for_step = (f'{a.emissions_by_step[step].value():.3f}' for a in grid) + writer.writerow(itertools.chain((step,), em_for_step)) def run(config, logger):