2019-01-17 17:02:24 +00:00
|
|
|
"""
|
|
|
|
Created on 17 oct. 2018
|
|
|
|
|
|
|
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
|
|
|
"""
|
|
|
|
|
2019-01-19 18:56:59 +00:00
|
|
|
"""
|
|
|
|
This module defines the global configuration for the simulation
|
|
|
|
"""
|
|
|
|
|
2019-01-17 17:02:24 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2019-01-22 12:19:46 +00:00
|
|
|
from data import Data
|
2019-01-30 16:57:29 +00:00
|
|
|
from model import Emission
|
2019-01-17 17:02:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
"""
|
|
|
|
The Config class defines all simulation properties that can be changed
|
|
|
|
"""
|
|
|
|
|
2019-01-22 12:19:46 +00:00
|
|
|
def __init__(self,config_file, data : Data):
|
2019-01-17 17:02:24 +00:00
|
|
|
"""
|
|
|
|
Default constructor
|
|
|
|
"""
|
2019-01-22 12:19:46 +00:00
|
|
|
self.import_config_file(config_file)
|
|
|
|
self.init_traci(data.dir)
|
|
|
|
self.check_config()
|
2019-01-20 14:56:05 +00:00
|
|
|
|
2019-01-17 17:02:24 +00:00
|
|
|
def import_config_file(self, config_file):
|
|
|
|
"""
|
|
|
|
Import your configuration file in JSON format
|
|
|
|
:param config_file: The path to your configuration file
|
|
|
|
:return:
|
|
|
|
"""
|
2019-02-06 14:06:41 +00:00
|
|
|
with open(f'{config_file}', 'r') as f:
|
2019-01-17 17:02:24 +00:00
|
|
|
data = json.load(f)
|
|
|
|
|
2019-01-18 22:01:38 +00:00
|
|
|
for option in data:
|
|
|
|
self.__setattr__(option, data[option])
|
|
|
|
self.config_filename = os.path.basename(f.name)
|
2019-01-17 17:02:24 +00:00
|
|
|
self.check_config()
|
|
|
|
|
|
|
|
def check_config(self):
|
|
|
|
"""
|
|
|
|
Check the relevance of user configuration choices
|
|
|
|
:return:
|
|
|
|
"""
|
2019-01-18 22:01:38 +00:00
|
|
|
# Weight routing mode cannot be combined with other actions
|
2019-01-17 17:02:24 +00:00
|
|
|
if self.weight_routing_mode:
|
|
|
|
self.limit_speed_mode = False
|
|
|
|
self.adjust_traffic_light_mode = False
|
|
|
|
self.lock_area_mode = False
|
|
|
|
|
2019-01-18 22:01:38 +00:00
|
|
|
# If without_actions_mode is chosen
|
2019-01-17 17:02:24 +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
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""
|
|
|
|
:return: All properties chosen by the user
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
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},'
|
|
|
|
f'RF = {self.trafficLights_duration_rf * 100}%\n'
|
|
|
|
)
|
2019-01-20 14:56:05 +00:00
|
|
|
|
|
|
|
def init_traci(self, simulation_dir):
|
2019-01-17 17:02:24 +00:00
|
|
|
"""
|
|
|
|
Init the Traci API
|
2019-01-21 10:25:45 +00:00
|
|
|
:param simulation_dir: The path to the simulation directory
|
2019-01-17 17:02:24 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2019-02-06 16:06:48 +00:00
|
|
|
simdir = os.path.join(os.path.dirname(__file__), f'{simulation_dir}')
|
2019-02-06 15:29:43 +00:00
|
|
|
|
2019-02-06 13:20:45 +00:00
|
|
|
for f in os.listdir(simdir):
|
|
|
|
if f.endswith('.sumocfg'):
|
|
|
|
self._SUMOCFG = os.path.join(simdir, f)
|
2019-01-17 17:02:24 +00:00
|
|
|
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', self._SUMOCMD)
|
2019-02-10 14:30:25 +00:00
|
|
|
self.sumo_cmd = [sumo_binary, "-c", self._SUMOCFG]
|