From 28be0142fb2c1db3e97310a1a7e3f55ad4cdb3dc Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Mon, 10 Dec 2018 13:33:53 +0100 Subject: [PATCH] Changed config class --- sumo_project/config.py | 46 +++++++++++-------- .../default_config.json} | 6 +-- sumo_project/emissions.py | 18 ++++++-- 3 files changed, 42 insertions(+), 28 deletions(-) rename sumo_project/{config.json => configs/default_config.json} (71%) diff --git a/sumo_project/config.py b/sumo_project/config.py index 9019c38..6703b69 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -8,7 +8,7 @@ import os import sys import json -class config: +class Config: # Total of emissions of all pollutants in mg for n steps of simulation without locking areas # These constants are simulation dependant, you must change them according to your simulation @@ -16,26 +16,28 @@ class config: total_emissions200 = 43970763.15084738 total_emissions300 = 87382632.0821697 - def __init__(self, config_file = None): - if not (config_file is None): - with open(config_file) as f: - data = json.load(f) + def __init__(self): + '''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"] - self.adjust_traffic_light_mode = data["adjust_traffic_light_mode"] - 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._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"] + self.adjust_traffic_light_mode = data["adjust_traffic_light_mode"] + 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() + self.check_config() def check_config(self): #Weight routing mode cannot be combinated with other actions @@ -53,7 +55,7 @@ class config: def __repr__(self) -> str: - return (str(f'Grid : {self.areas_number}x{self.areas_number}\n') + 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') @@ -75,6 +77,10 @@ class config: def init_logger(self): 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") diff --git a/sumo_project/config.json b/sumo_project/configs/default_config.json similarity index 71% rename from sumo_project/config.json rename to sumo_project/configs/default_config.json index ffd59bc..1608a2a 100644 --- a/sumo_project/config.json +++ b/sumo_project/configs/default_config.json @@ -7,12 +7,12 @@ "n_steps": 200, "window_size":100, - "without_actions_mode": false, + "without_actions_mode": true, - "limit_speed_mode": true, + "limit_speed_mode": false, "speed_rf": 0.1, - "adjust_traffic_light_mode": true, + "adjust_traffic_light_mode": false, "trafficLights_duration_rf": 0.2, "weight_routing_mode": false, diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 7c12945..8a49cbc 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -2,18 +2,18 @@ from typing import List import traci import time +import argparse from shapely.geometry import LineString from parse import search import actions -from config import config +from config import Config import sys from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic from traci import trafficlight -config = config('config.json') -config.init_traci() +config = Config() logger = config.init_logger() def init_grid(simulation_bounds, areas_number): @@ -112,7 +112,15 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step): actions.reverse_actions(area) -def main(): +def main(args): + + parser = argparse.ArgumentParser(description="") + parser.add_argument("-f", "--configfile", type=str, default= 'configs/default_config.json', required=False) + args = parser.parse_args(args) + + config.import_config_file(args.configfile) + config.init_traci() + grid = list() try: traci.start(config.sumo_cmd) @@ -161,4 +169,4 @@ def main(): if __name__ == '__main__': - main() + main(sys.argv[1:])