1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-24 20:46:29 +00:00
sumo-emissions/sumo_project/config.py

108 lines
3.8 KiB
Python
Raw Normal View History

2018-11-23 12:53:20 +00:00
"""
Global configuration for the simulation
"""
2018-12-07 15:30:23 +00:00
import datetime
import json
2018-12-07 16:40:16 +00:00
import logging
import os
import sys
from model import Emission
2018-12-16 14:33:10 +00:00
2018-12-16 20:43:21 +00:00
class Config:
# Total of emissions of all pollutants in mg for n steps of simulation without acting on areas
2018-12-09 19:22:29 +00:00
# These constants are simulation dependant, you must change them according to your simulation
2018-12-16 20:43:21 +00:00
ref200 = Emission(co2=42816869.05436445, co=1128465.0343051048, nox=18389.648337283958, hc=6154.330914019103,
pmx=885.0829265236318)
2018-12-10 12:33:53 +00:00
def __init__(self):
2018-12-16 20:43:21 +00:00
"""Default constructor"""
def import_config_file(self, config_file):
with open(config_file, 'r') as f:
2018-12-10 12:33:53 +00:00
data = json.load(f)
2018-12-16 20:43:21 +00:00
2018-12-10 12:33:53 +00:00
self._SUMOCMD = data["_SUMOCMD"]
self._SUMOCFG = data["_SUMOCFG"]
2018-12-16 20:43:21 +00:00
2018-12-10 12:33:53 +00:00
self.areas_number = data["areas_number"]
self.emissions_threshold = data["emissions_threshold"]
self.n_steps = data["n_steps"]
self.window_size = data["window_size"]
2018-12-16 20:43:21 +00:00
2018-12-10 12:33:53 +00:00
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"]
2018-12-16 20:43:21 +00:00
2018-12-10 12:33:53 +00:00
self.check_config()
2018-12-16 20:43:21 +00:00
2018-12-09 19:22:29 +00:00
def check_config(self):
2018-12-16 20:43:21 +00:00
# Weight routing mode cannot be combinated with other actions
2018-12-09 19:22:29 +00:00
if self.weight_routing_mode:
self.limit_speed_mode = False
self.adjust_traffic_light_mode = False
self.lock_area_mode = False
2018-12-16 20:43:21 +00:00
# If without_actions_mode is choosen
2018-12-09 19:22:29 +00:00
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
2018-12-16 20:43:21 +00:00
2018-12-09 19:22:29 +00:00
def __repr__(self) -> str:
2018-12-16 20:43:21 +00:00
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},'
2018-12-16 20:48:53 +00:00
f'RF = {self.trafficLights_duration_rf * 100}%\n'
2018-12-16 20:43:21 +00:00
)
2018-12-09 19:22:29 +00:00
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'")
2018-12-16 20:43:21 +00:00
2018-12-09 19:22:29 +00:00
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', self._SUMOCMD)
self.sumo_cmd = [sumo_binary, "-c", self._SUMOCFG]
2018-12-16 20:43:21 +00:00
def init_logger(self, save_logs=False):
2018-12-09 19:22:29 +00:00
now = datetime.datetime.now()
current_date = now.strftime("%Y_%m_%d_%H_%M_%S")
2018-12-16 20:43:21 +00:00
2018-12-10 12:33:53 +00:00
if not os.path.exists('logs'):
os.makedirs('logs')
2018-12-16 20:43:21 +00:00
2018-12-09 19:22:29 +00:00
log_filename = f'logs/sumo_logs_{current_date}.log'
2018-12-16 20:43:21 +00:00
2018-12-09 19:22:29 +00:00
logger = logging.getLogger("sumo_logger")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2018-12-16 20:43:21 +00:00
if save_logs:
2018-12-10 15:26:14 +00:00
file_handler = logging.FileHandler(log_filename)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
handler = logging.StreamHandler()
2018-12-09 19:22:29 +00:00
handler.setFormatter(formatter)
logger.addHandler(handler)
2018-12-16 20:43:21 +00:00
2018-12-09 19:22:29 +00:00
return logger
2018-12-07 15:30:23 +00:00
def get_ref_emissions(self):
2018-12-09 18:49:37 +00:00
if self.n_steps == 200:
return self.ref200