From c719ba0068bc81fa47d74ad4a849f255510be4d5 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Sat, 19 Jan 2019 17:22:45 +0100 Subject: [PATCH 1/3] Changed code structure by adding Data class for map loading --- sumo_project/configs/default_config.json | 2 +- sumo_project/data.py | 131 +++++++++++++++++++++++ sumo_project/emissions.py | 103 +++--------------- 3 files changed, 147 insertions(+), 89 deletions(-) create mode 100644 sumo_project/data.py diff --git a/sumo_project/configs/default_config.json b/sumo_project/configs/default_config.json index 4565a2a..425d1b9 100644 --- a/sumo_project/configs/default_config.json +++ b/sumo_project/configs/default_config.json @@ -7,7 +7,7 @@ "n_steps": 200, "window_size":100, - "without_actions_mode": false, + "without_actions_mode": true, "limit_speed_mode": true, "speed_rf": 0.1, diff --git a/sumo_project/data.py b/sumo_project/data.py new file mode 100644 index 0000000..3696c7a --- /dev/null +++ b/sumo_project/data.py @@ -0,0 +1,131 @@ +""" +Created on 17 oct. 2018 + +@author: Axel Huynh-Phuc, Thibaud Gasser +""" + +import argparse +import csv +import datetime +import itertools +import os +import sys +import time +from typing import List +import json +import jsonpickle +import pprint + +import actions +import traci +from config import Config +from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission +from parse import search +from shapely.geometry import LineString + +""" +This module is used for loading simulation data +""" + + +class Data: + + def __init__(self, map_bounds, config : Config): + """ + Data constructor + """ + self.map_bounds = map_bounds + self.config = config + + def init_grid(self): + """ + Initialize the grid of the loaded map from the configuration + :param self.map_bounds: The map bounds + :param areas_number: The number of areas + :param window_size: The size of the acquisition window + :return: A list of areas + """ + from distutils.command.config import config + self.grid = list() + areas_number = self.config.areas_number + window_size = self.config.window_size + + width = self.map_bounds[1][0] / areas_number + height = self.map_bounds[1][1] / areas_number + for i in range(areas_number): + for j in range(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)) + name = 'Area ({},{})'.format(i, j) + area = Area(ar_bounds, name, window_size) + self.grid.append(area) + traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) # Add polygon for UI + return self.grid + + def get_all_lanes(self) -> List[Lane]: + """ + Recover and creates a list of Lane objects + :return: The lanes list + """ + 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 parse_phase(self, phase_repr): + """ + Because the SUMO object Phase does not contain accessors, + we parse the string representation to retrieve data members. + :param phase_repr: The Phase string representation + :return: An new Phase instance + """ + duration = search('duration: {:f}', 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 = '' + else: + phase_def = phase_def[0] + + return Phase(duration[0], min_duration[0], max_duration[0], phase_def) + + def add_data_to_areas(self): + """ + Adds all recovered data to different areas + :param areas: The list of areas + :return: + """ + lanes = self.get_all_lanes() + for area in self.grid: + 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(self.parse_phase(phase.__repr__())) + logics.append(Logic(l, phases)) + area.add_tl(TrafficLight(tl_id, logics)) + + def save(self,dump_name): + """ + Save simulation data into a json file + :param dump_name: The name of your data dump + :return: + """ + dump_dir = 'dump' + if not os.path.exists(dump_dir): + os.mkdir(dump_dir) + + s = json.dumps(json.loads(jsonpickle.encode(self)), indent=4) # for pretty JSON + with open(f'{dump_dir}/{dump_name}.json', 'w') as f: + f.write(s) + diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 74c7c28..6c8a1c3 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -13,6 +13,8 @@ import sys import time from typing import List + +from data import Data import actions import traci from config import Config @@ -24,85 +26,6 @@ from shapely.geometry import LineString This module defines the entry point of the application """ - -def init_grid(simulation_bounds, areas_number, window_size): - """ - Initialize the grid of the loaded map from the configuration - :param simulation_bounds: The map bounds - :param areas_number: The number of areas - :param window_size: The size of the acquisition window - :return: A list of areas - """ - grid = list() - 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): - # 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)) - name = 'Area ({},{})'.format(i, j) - area = Area(ar_bounds, name, window_size) - grid.append(area) - traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) # Add polygon for UI - return grid - - -def get_all_lanes() -> List[Lane]: - """ - Recover and creates a list of Lane objects - :return: The lanes list - """ - 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 parse_phase(phase_repr): - """ - Because the SUMO object Phase does not contain accessors, - we parse the string representation to retrieve data members. - :param phase_repr: The Phase string representation - :return: An new Phase instance - """ - duration = search('duration: {:f}', 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 = '' - else: - phase_def = phase_def[0] - - return Phase(duration[0], min_duration[0], max_duration[0], phase_def) - - -def add_data_to_areas(areas: List[Area]): - """ - Adds all recovered data to different areas - :param areas: The list of areas - :return: - """ - 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(parse_phase(phase.__repr__())) - logics.append(Logic(l, phases)) - area.add_tl(TrafficLight(tl_id, logics)) - - def compute_vehicle_emissions(veh_id): """ Recover the emissions of different pollutants from a vehicle and create an Emission instance @@ -211,22 +134,24 @@ def export_data_to_csv(config, grid): writer.writerow(itertools.chain((step,), em_for_step)) -def run(config, logger, csv_export): +def run(config, logger, csv_export, dump_name): """ Run the simulation with the configuration chosen :param config: The simulation configuration :param logger: The simulation logger :return: """ - grid = list() + try: traci.start(config.sumo_cmd) 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) + data = Data(traci.simulation.getNetBoundary(), config) + data.init_grid() + data.add_data_to_areas() + data.save(dump_name) loading_time = round(time.perf_counter() - start, 2) logger.info(f'Data loaded ({loading_time}s)') @@ -237,16 +162,16 @@ def run(config, logger, csv_export): traci.simulationStep() vehicles = get_all_vehicles() - get_emissions(grid, vehicles, step, config, logger) + get_emissions(data.grid, vehicles, step, config, logger) step += 1 print(f'step = {step}/{config.n_steps}', end='\r') finally: traci.close(False) - + if csv_export: - export_data_to_csv(config, grid) + export_data_to_csv(config, data.grid) logger.info(f'Exported data into the csv folder') simulation_time = round(time.perf_counter() - start, 2) @@ -256,7 +181,7 @@ def run(config, logger, csv_export): logger.info(f'Real-time factor : {config.n_steps / simulation_time}') total_emissions = Emission() - for area in grid: + for area in data.grid: total_emissions += area.sum_all_emissions() logger.info(f'Total emissions = {total_emissions.value()} mg') @@ -280,6 +205,8 @@ def add_options(parser): :param parser: The command line parser :return: """ + 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("-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", @@ -327,7 +254,7 @@ def main(args): logger.info(f'Loaded configuration file : {args.configfile}') logger.info(f'Simulated time : {args.steps}s') - run(config, logger, csv_export) + run(config, logger, csv_export, dump_name) if __name__ == '__main__': From 1e8d0b01997fc85ed833e22b3a68ddc81f3e7878 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Sat, 19 Jan 2019 19:56:59 +0100 Subject: [PATCH 2/3] Added runner module --- sumo_project/actions.py | 10 +- sumo_project/config.py | 8 +- sumo_project/data.py | 29 ++-- sumo_project/emissions.py | 146 ++--------------- .../{ => files}/configs/default_config.json | 2 +- .../{ => files}/imgs/simulation_example.PNG | Bin .../simulations/mulhouse_simulation/build.bat | 0 .../mulhouse_simulation/osm.net.xml | 0 .../mulhouse_simulation/osm.netccfg | 0 .../osm.passenger.rou.alt.xml | 0 .../mulhouse_simulation/osm.passenger.rou.xml | 0 .../osm.passenger.trips.xml | 0 .../mulhouse_simulation/osm.poly.xml | 0 .../mulhouse_simulation/osm.polycfg | 0 .../mulhouse_simulation/osm.sumocfg | 0 .../mulhouse_simulation/osm.view.xml | 0 .../mulhouse_simulation/osm_bbox.osm.xml | 0 .../simulations/mulhouse_simulation/run.bat | 0 sumo_project/model.py | 10 +- sumo_project/runner.py | 152 ++++++++++++++++++ 20 files changed, 193 insertions(+), 164 deletions(-) rename sumo_project/{ => files}/configs/default_config.json (83%) rename sumo_project/{ => files}/imgs/simulation_example.PNG (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/build.bat (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.net.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.netccfg (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.passenger.rou.alt.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.passenger.rou.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.passenger.trips.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.poly.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.polycfg (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.sumocfg (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm.view.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/osm_bbox.osm.xml (100%) rename sumo_project/{ => files}/simulations/mulhouse_simulation/run.bat (100%) create mode 100644 sumo_project/runner.py diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 9a4ed15..058c3ed 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -4,15 +4,15 @@ Created on 17 oct. 2018 @author: Axel Huynh-Phuc, Thibaud Gasser """ -from typing import Iterable - -import traci -from model import Area, Vehicle - """ This module defines all possible actions on the simulation """ +import traci +from typing import Iterable + +from model import Area, Vehicle + def compute_edge_weight(edge_id): """ diff --git a/sumo_project/config.py b/sumo_project/config.py index 0286699..8227e1a 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -4,6 +4,10 @@ Created on 17 oct. 2018 @author: Axel Huynh-Phuc, Thibaud Gasser """ +""" +This module defines the global configuration for the simulation +""" + import datetime import json import logging @@ -12,10 +16,6 @@ import sys from model import Emission -""" -This module defines the global configuration for the simulation -""" - class Config: """ diff --git a/sumo_project/data.py b/sumo_project/data.py index 3696c7a..12266bf 100644 --- a/sumo_project/data.py +++ b/sumo_project/data.py @@ -4,28 +4,29 @@ Created on 17 oct. 2018 @author: Axel Huynh-Phuc, Thibaud Gasser """ +""" +This module is used for loading simulation data +""" + import argparse import csv import datetime import itertools +import json import os +import pprint import sys import time -from typing import List -import json -import jsonpickle -import pprint - -import actions import traci -from config import Config -from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission +from typing import List + +import jsonpickle from parse import search from shapely.geometry import LineString -""" -This module is used for loading simulation data -""" +import actions +from config import Config +from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission class Data: @@ -115,17 +116,17 @@ class Data: logics.append(Logic(l, phases)) area.add_tl(TrafficLight(tl_id, logics)) - def save(self,dump_name): + def save(self, dump_name): """ Save simulation data into a json file :param dump_name: The name of your data dump :return: """ - dump_dir = 'dump' + dump_dir = 'files/dump' if not os.path.exists(dump_dir): os.mkdir(dump_dir) - s = json.dumps(json.loads(jsonpickle.encode(self)), indent=4) # for pretty JSON + s = json.dumps(json.loads(jsonpickle.encode(self)), indent=4) # for pretty JSON with open(f'{dump_dir}/{dump_name}.json', 'w') as f: f.write(s) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 6c8a1c3..2aa5e15 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -4,6 +4,10 @@ Created on 17 oct. 2018 @author: Axel Huynh-Phuc, Thibaud Gasser """ +""" +This module defines the entry point of the application +""" + import argparse import csv import datetime @@ -11,20 +15,18 @@ import itertools import os import sys import time +import traci from typing import List - -from data import Data -import actions -import traci -from config import Config -from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission +import jsonpickle from parse import search from shapely.geometry import LineString -""" -This module defines the entry point of the application -""" +import actions +from config import Config +from data import Data +from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission + def compute_vehicle_emissions(veh_id): """ @@ -133,129 +135,3 @@ def export_data_to_csv(config, grid): 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, csv_export, dump_name): - """ - Run the simulation with the configuration chosen - :param config: The simulation configuration - :param logger: The simulation logger - :return: - """ - - try: - traci.start(config.sumo_cmd) - logger.info(f'Loaded simulation file : {config._SUMOCFG}') - logger.info('Loading data for the simulation') - start = time.perf_counter() - - data = Data(traci.simulation.getNetBoundary(), config) - data.init_grid() - data.add_data_to_areas() - data.save(dump_name) - - 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.simulationStep() - - vehicles = get_all_vehicles() - get_emissions(data.grid, vehicles, step, config, logger) - step += 1 - - print(f'step = {step}/{config.n_steps}', end='\r') - - finally: - traci.close(False) - - if csv_export: - export_data_to_csv(config, data.grid) - logger.info(f'Exported data into the csv folder') - - simulation_time = round(time.perf_counter() - start, 2) - logger.info(f'End of the simulation ({simulation_time}s)') - - # 1 step is equal to one second simulated - logger.info(f'Real-time factor : {config.n_steps / simulation_time}') - - total_emissions = Emission() - for area in data.grid: - total_emissions += area.sum_all_emissions() - - logger.info(f'Total emissions = {total_emissions.value()} mg') - - if not config.without_actions_mode: # If it's not a simulation without actions - ref = config.get_ref_emissions() - if not (ref is None): # If a reference value exist (add yours into config.py) - 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): - """ - Add command line options - :param parser: The command line parser - :return: - """ - 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("-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="Show UI") - parser.add_argument("-csv", "--csv", action="store_true", - help="Export all data emissions into a CSV file") - - -def main(args): - """ - The entry point of the application - :param args: Command line options - :return: - """ - parser = argparse.ArgumentParser(description="") - add_options(parser) - args = parser.parse_args(args) - - config = Config() - config.import_config_file(args.configfile) # By default the configfile is default_config.json - config.init_traci() - logger = config.init_logger(save_logs=args.save) - csv_export = False - - if args.ref: - config.without_actions_mode = True - logger.info(f'Reference simulation') - - if args.steps: - config.n_steps = args.steps - - if args.gui: - config._SUMOCMD = "sumo-gui" - - if args.csv: - csv_export = True - - config.check_config() - - logger.info(f'Loaded configuration file : {args.configfile}') - logger.info(f'Simulated time : {args.steps}s') - run(config, logger, csv_export, dump_name) - - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/sumo_project/configs/default_config.json b/sumo_project/files/configs/default_config.json similarity index 83% rename from sumo_project/configs/default_config.json rename to sumo_project/files/configs/default_config.json index 425d1b9..ddb2037 100644 --- a/sumo_project/configs/default_config.json +++ b/sumo_project/files/configs/default_config.json @@ -1,6 +1,6 @@ { "_SUMOCMD": "sumo", - "_SUMOCFG": "simulations/mulhouse_simulation/osm.sumocfg", + "_SUMOCFG": "files/simulations/mulhouse_simulation/osm.sumocfg", "areas_number": 10, "emissions_threshold": 500000, diff --git a/sumo_project/imgs/simulation_example.PNG b/sumo_project/files/imgs/simulation_example.PNG similarity index 100% rename from sumo_project/imgs/simulation_example.PNG rename to sumo_project/files/imgs/simulation_example.PNG diff --git a/sumo_project/simulations/mulhouse_simulation/build.bat b/sumo_project/files/simulations/mulhouse_simulation/build.bat similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/build.bat rename to sumo_project/files/simulations/mulhouse_simulation/build.bat diff --git a/sumo_project/simulations/mulhouse_simulation/osm.net.xml b/sumo_project/files/simulations/mulhouse_simulation/osm.net.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.net.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm.net.xml diff --git a/sumo_project/simulations/mulhouse_simulation/osm.netccfg b/sumo_project/files/simulations/mulhouse_simulation/osm.netccfg similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.netccfg rename to sumo_project/files/simulations/mulhouse_simulation/osm.netccfg diff --git a/sumo_project/simulations/mulhouse_simulation/osm.passenger.rou.alt.xml b/sumo_project/files/simulations/mulhouse_simulation/osm.passenger.rou.alt.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.passenger.rou.alt.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm.passenger.rou.alt.xml diff --git a/sumo_project/simulations/mulhouse_simulation/osm.passenger.rou.xml b/sumo_project/files/simulations/mulhouse_simulation/osm.passenger.rou.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.passenger.rou.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm.passenger.rou.xml diff --git a/sumo_project/simulations/mulhouse_simulation/osm.passenger.trips.xml b/sumo_project/files/simulations/mulhouse_simulation/osm.passenger.trips.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.passenger.trips.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm.passenger.trips.xml diff --git a/sumo_project/simulations/mulhouse_simulation/osm.poly.xml b/sumo_project/files/simulations/mulhouse_simulation/osm.poly.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.poly.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm.poly.xml diff --git a/sumo_project/simulations/mulhouse_simulation/osm.polycfg b/sumo_project/files/simulations/mulhouse_simulation/osm.polycfg similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.polycfg rename to sumo_project/files/simulations/mulhouse_simulation/osm.polycfg diff --git a/sumo_project/simulations/mulhouse_simulation/osm.sumocfg b/sumo_project/files/simulations/mulhouse_simulation/osm.sumocfg similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.sumocfg rename to sumo_project/files/simulations/mulhouse_simulation/osm.sumocfg diff --git a/sumo_project/simulations/mulhouse_simulation/osm.view.xml b/sumo_project/files/simulations/mulhouse_simulation/osm.view.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm.view.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm.view.xml diff --git a/sumo_project/simulations/mulhouse_simulation/osm_bbox.osm.xml b/sumo_project/files/simulations/mulhouse_simulation/osm_bbox.osm.xml similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/osm_bbox.osm.xml rename to sumo_project/files/simulations/mulhouse_simulation/osm_bbox.osm.xml diff --git a/sumo_project/simulations/mulhouse_simulation/run.bat b/sumo_project/files/simulations/mulhouse_simulation/run.bat similarity index 100% rename from sumo_project/simulations/mulhouse_simulation/run.bat rename to sumo_project/files/simulations/mulhouse_simulation/run.bat diff --git a/sumo_project/model.py b/sumo_project/model.py index cf5ae04..ddc79d8 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -4,17 +4,17 @@ Created on 17 oct. 2018 @author: Axel Huynh-Phuc, Thibaud Gasser """ +""" +This module defines the business model of our application +""" + import collections +from traci._trafficlight import Logic as SUMO_Logic 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 - -""" -This module defines the business model of our application -""" class Lane: diff --git a/sumo_project/runner.py b/sumo_project/runner.py new file mode 100644 index 0000000..1494517 --- /dev/null +++ b/sumo_project/runner.py @@ -0,0 +1,152 @@ +''' +Created on 19 janv. 2019 + +@author: Admin +''' + +import argparse +import os +import sys +import time +import traci + +import jsonpickle + +from config import Config +from data import Data +import emissions +from model import Emission + + +def add_options(parser): + """ + Add command line options + :param parser: The command line parser + :return: + """ + parser.add_argument("-new_dump", "--new_dump", metavar=('config_file', 'dump_name'), nargs=2, type=str, + required=False, help='Load an create a new data dump with the configuration file chosen') + parser.add_argument("-run", "--run", type=str, required=False, + help='Run a simulation with the dump chosen') + + 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="Show UI") + parser.add_argument("-csv", "--csv", action="store_true", + help="Export all data emissions into a CSV file") + + +def create_dump(config_file, dump_name): + + config = Config() + config.import_config_file(config_file) + config.check_config() + config.init_traci() + + """with open(f'dump/{dump_name}.json', 'r') as f: + data = jsonpickle.decode(f.read())""" + + traci.start(config.sumo_cmd) + if not os.path.isfile(f'files/dump/{dump_name}.json'): + start = time.perf_counter() + data = Data(traci.simulation.getNetBoundary(), config) + data.init_grid() + data.add_data_to_areas() + data.save(dump_name) + + loading_time = round(time.perf_counter() - start, 2) + print(f'Data loaded ({loading_time}s)') + print(f'Dump {dump_name} created') + else: + print(f'Dump with name {dump_name} already exist') + + traci.close(False) + + +def run(data : Data, config : Config, logger): + try: + traci.start(config.sumo_cmd) + logger.info(f'Loaded simulation file : {config._SUMOCFG}') + logger.info('Loading data for the simulation') + start = time.perf_counter() + + logger.info('Simulation started...') + step = 0 + while step < config.n_steps: + traci.simulationStep() + + vehicles = emissions.get_all_vehicles() + emissions.get_emissions(data.grid, vehicles, step, config, logger) + step += 1 + + print(f'step = {step}/{config.n_steps}', end='\r') + + finally: + traci.close(False) + + +def main(args): + """ + The entry point of the application + :param args: Command line options + :return: + """ + parser = argparse.ArgumentParser() + add_options(parser) + args = parser.parse_args(args) + + if args.new_dump is not None: + create_dump(args.new_dump[0], args.new_dump[1]) + + if args.run is not None: + dump_path = f'files/dump/{args.run}.json' + if not os.path.isfile(dump_path): + with open(dump_path, 'r') as f: + data = jsonpickle.decode(f.read()) + config = data.config + logger = config.init_logger(save_logs=args.save) + logger.info(f'Running simulation dump {args.run}...') + if args.ref: + config.without_actions_mode = True + logger.info(f'Reference simulation') + if args.steps: config.n_steps = args.steps + if args.gui: config._SUMOCMD = "sumo-gui" + + start = time.perf_counter() + run(data, config, logger) + + if args.csv: + emissions.export_data_to_csv(config, data.grid) + logger.info(f'Exported data into the csv folder') + + simulation_time = round(time.perf_counter() - start, 2) + logger.info(f'End of the simulation ({simulation_time}s)') + + # 1 step is equal to one second simulated + logger.info(f'Real-time factor : {config.n_steps / simulation_time}') + + total_emissions = Emission() + for area in data.grid: + total_emissions += area.sum_all_emissions() + + logger.info(f'Total emissions = {total_emissions.value()} mg') + + if not config.without_actions_mode: # If it's not a simulation without actions + ref = config.get_ref_emissions() + if not (ref is None): # If a reference value exist (add yours into config.py) + 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 = {emissions.get_reduction_percentage(ref.co2, total_emissions.co2)} %') + logger.info(f'-> CO emissions = {emissions.get_reduction_percentage(ref.co, total_emissions.co)} %') + logger.info(f'-> Nox emissions = {emissions.get_reduction_percentage(ref.nox, total_emissions.nox)} %') + logger.info(f'-> HC emissions = {emissions.get_reduction_percentage(ref.hc, total_emissions.hc)} %') + logger.info(f'-> PMx emissions = {emissions.get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') + +if __name__ == '__main__': + main(sys.argv[1:]) From 401a5c43dc02b48c35edc67745ef8dad5e9a639c Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Sat, 19 Jan 2019 21:26:37 +0100 Subject: [PATCH 3/3] Fixed runner --- sumo_project/config.py | 6 ++-- sumo_project/data.py | 2 -- sumo_project/emissions.py | 4 +-- sumo_project/model.py | 2 +- sumo_project/runner.py | 73 ++++++++++++++++++--------------------- 5 files changed, 39 insertions(+), 48 deletions(-) diff --git a/sumo_project/config.py b/sumo_project/config.py index 8227e1a..236f408 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -38,7 +38,7 @@ class Config: :param config_file: The path to your configuration file :return: """ - with open(config_file, 'r') as f: + with open(f'files/configs/{config_file}.json', 'r') as f: data = json.load(f) for option in data: @@ -102,10 +102,10 @@ class Config: now = datetime.datetime.now() current_date = now.strftime("%Y_%m_%d_%H_%M_%S") - if not os.path.exists('logs'): + if not os.path.exists('files/logs'): os.makedirs('logs') - log_filename = f'logs/sumo_logs_{current_date}_{self.config_filename}.log' + log_filename = f'files/logs/sumo_logs_{current_date}_{self.config_filename}.log' logger = logging.getLogger("sumo_logger") logger.setLevel(logging.INFO) diff --git a/sumo_project/data.py b/sumo_project/data.py index 12266bf..039d4f7 100644 --- a/sumo_project/data.py +++ b/sumo_project/data.py @@ -46,7 +46,6 @@ class Data: :param window_size: The size of the acquisition window :return: A list of areas """ - from distutils.command.config import config self.grid = list() areas_number = self.config.areas_number window_size = self.config.window_size @@ -61,7 +60,6 @@ class Data: name = 'Area ({},{})'.format(i, j) area = Area(ar_bounds, name, window_size) self.grid.append(area) - traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) # Add polygon for UI return self.grid def get_all_lanes(self) -> List[Lane]: diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 2aa5e15..8bfd259 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -120,13 +120,13 @@ def export_data_to_csv(config, grid): :param grid: The list of areas :return: """ - csv_dir = 'csv' + csv_dir = 'files/csv' if not os.path.exists(csv_dir): os.mkdir(csv_dir) now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") - with open(f'csv/{now}.csv', 'w') as f: + with open(f'files/csv/{now}.csv', 'w') as f: writer = csv.writer(f) # Write CSV headers writer.writerow(itertools.chain(('Step',), (a.name for a in grid))) diff --git a/sumo_project/model.py b/sumo_project/model.py index ddc79d8..25cea9c 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -150,7 +150,7 @@ class Emission: class Area: """ - The Area class defines a grid area of ​​the map + The Area class defines a grid area of the simulation map """ def __init__(self, coords, name, window_size): diff --git a/sumo_project/runner.py b/sumo_project/runner.py index 1494517..661bf67 100644 --- a/sumo_project/runner.py +++ b/sumo_project/runner.py @@ -31,12 +31,6 @@ def add_options(parser): 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="Show UI") parser.add_argument("-csv", "--csv", action="store_true", help="Export all data emissions into a CSV file") @@ -48,10 +42,10 @@ def create_dump(config_file, dump_name): config.check_config() config.init_traci() - """with open(f'dump/{dump_name}.json', 'r') as f: - data = jsonpickle.decode(f.read())""" - - traci.start(config.sumo_cmd) + sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', 'sumo') + sumo_cmd = [sumo_binary, "-c", config._SUMOCFG] + + traci.start(sumo_cmd) if not os.path.isfile(f'files/dump/{dump_name}.json'): start = time.perf_counter() data = Data(traci.simulation.getNetBoundary(), config) @@ -71,6 +65,10 @@ def create_dump(config_file, dump_name): def run(data : Data, config : Config, logger): try: traci.start(config.sumo_cmd) + + for area in data.grid: + traci.polygon.add(area.name, area.rectangle.exterior.coords, (255, 0, 0)) # Add polygon for UI + logger.info(f'Loaded simulation file : {config._SUMOCFG}') logger.info('Loading data for the simulation') start = time.perf_counter() @@ -105,48 +103,43 @@ def main(args): if args.run is not None: dump_path = f'files/dump/{args.run}.json' - if not os.path.isfile(dump_path): + if os.path.isfile(dump_path): with open(dump_path, 'r') as f: data = jsonpickle.decode(f.read()) - config = data.config - logger = config.init_logger(save_logs=args.save) - logger.info(f'Running simulation dump {args.run}...') - if args.ref: - config.without_actions_mode = True - logger.info(f'Reference simulation') - if args.steps: config.n_steps = args.steps - if args.gui: config._SUMOCMD = "sumo-gui" - - start = time.perf_counter() - run(data, config, logger) + config = data.config + logger = config.init_logger(save_logs=args.save) + logger.info(f'Running simulation dump {args.run}...') + start = time.perf_counter() + run(data, config, logger) if args.csv: emissions.export_data_to_csv(config, data.grid) logger.info(f'Exported data into the csv folder') - simulation_time = round(time.perf_counter() - start, 2) - logger.info(f'End of the simulation ({simulation_time}s)') + simulation_time = round(time.perf_counter() - start, 2) + logger.info(f'End of the simulation ({simulation_time}s)') - # 1 step is equal to one second simulated - logger.info(f'Real-time factor : {config.n_steps / simulation_time}') + # 1 step is equal to one second simulated + logger.info(f'Real-time factor : {config.n_steps / simulation_time}') - total_emissions = Emission() - for area in data.grid: - total_emissions += area.sum_all_emissions() + total_emissions = Emission() + for area in data.grid: + total_emissions += area.sum_all_emissions() - logger.info(f'Total emissions = {total_emissions.value()} mg') + logger.info(f'Total emissions = {total_emissions.value()} mg') - if not config.without_actions_mode: # If it's not a simulation without actions - ref = config.get_ref_emissions() - if not (ref is None): # If a reference value exist (add yours into config.py) - global_diff = (ref.value() - total_emissions.value()) / ref.value() + if not config.without_actions_mode: # If it's not a simulation without actions + ref = config.get_ref_emissions() + if not (ref is None): # If a reference value exist (add yours into config.py) + 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 = {emissions.get_reduction_percentage(ref.co2, total_emissions.co2)} %') - logger.info(f'-> CO emissions = {emissions.get_reduction_percentage(ref.co, total_emissions.co)} %') - logger.info(f'-> Nox emissions = {emissions.get_reduction_percentage(ref.nox, total_emissions.nox)} %') - logger.info(f'-> HC emissions = {emissions.get_reduction_percentage(ref.hc, total_emissions.hc)} %') - logger.info(f'-> PMx emissions = {emissions.get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') + logger.info(f'Global reduction percentage of emissions = {global_diff * 100} %') + logger.info(f'-> CO2 emissions = {emissions.get_reduction_percentage(ref.co2, total_emissions.co2)} %') + logger.info(f'-> CO emissions = {emissions.get_reduction_percentage(ref.co, total_emissions.co)} %') + logger.info(f'-> Nox emissions = {emissions.get_reduction_percentage(ref.nox, total_emissions.nox)} %') + logger.info(f'-> HC emissions = {emissions.get_reduction_percentage(ref.hc, total_emissions.hc)} %') + logger.info(f'-> PMx emissions = {emissions.get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') + if __name__ == '__main__': main(sys.argv[1:])