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

Fixed runner

This commit is contained in:
Ahp06 2019-01-19 21:26:37 +01:00
parent 1e8d0b0199
commit 401a5c43dc
5 changed files with 39 additions and 48 deletions

View File

@ -38,7 +38,7 @@ class Config:
:param config_file: The path to your configuration file :param config_file: The path to your configuration file
:return: :return:
""" """
with open(config_file, 'r') as f: with open(f'files/configs/{config_file}.json', 'r') as f:
data = json.load(f) data = json.load(f)
for option in data: for option in data:
@ -102,10 +102,10 @@ class Config:
now = datetime.datetime.now() now = datetime.datetime.now()
current_date = now.strftime("%Y_%m_%d_%H_%M_%S") 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') 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 = logging.getLogger("sumo_logger")
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)

View File

@ -46,7 +46,6 @@ class Data:
:param window_size: The size of the acquisition window :param window_size: The size of the acquisition window
:return: A list of areas :return: A list of areas
""" """
from distutils.command.config import config
self.grid = list() self.grid = list()
areas_number = self.config.areas_number areas_number = self.config.areas_number
window_size = self.config.window_size window_size = self.config.window_size
@ -61,7 +60,6 @@ class Data:
name = 'Area ({},{})'.format(i, j) name = 'Area ({},{})'.format(i, j)
area = Area(ar_bounds, name, window_size) area = Area(ar_bounds, name, window_size)
self.grid.append(area) self.grid.append(area)
traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) # Add polygon for UI
return self.grid return self.grid
def get_all_lanes(self) -> List[Lane]: def get_all_lanes(self) -> List[Lane]:

View File

@ -120,13 +120,13 @@ def export_data_to_csv(config, grid):
:param grid: The list of areas :param grid: The list of areas
:return: :return:
""" """
csv_dir = 'csv' csv_dir = 'files/csv'
if not os.path.exists(csv_dir): if not os.path.exists(csv_dir):
os.mkdir(csv_dir) os.mkdir(csv_dir)
now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") 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) writer = csv.writer(f)
# Write CSV headers # Write CSV headers
writer.writerow(itertools.chain(('Step',), (a.name for a in grid))) writer.writerow(itertools.chain(('Step',), (a.name for a in grid)))

View File

@ -150,7 +150,7 @@ class Emission:
class Area: 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): def __init__(self, coords, name, window_size):

View File

@ -31,12 +31,6 @@ def add_options(parser):
parser.add_argument("-save", "--save", action="store_true", parser.add_argument("-save", "--save", action="store_true",
help='Save the logs into the logs folder') 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", parser.add_argument("-csv", "--csv", action="store_true",
help="Export all data emissions into a CSV file") help="Export all data emissions into a CSV file")
@ -48,10 +42,10 @@ def create_dump(config_file, dump_name):
config.check_config() config.check_config()
config.init_traci() config.init_traci()
"""with open(f'dump/{dump_name}.json', 'r') as f: sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', 'sumo')
data = jsonpickle.decode(f.read())""" sumo_cmd = [sumo_binary, "-c", config._SUMOCFG]
traci.start(config.sumo_cmd) traci.start(sumo_cmd)
if not os.path.isfile(f'files/dump/{dump_name}.json'): if not os.path.isfile(f'files/dump/{dump_name}.json'):
start = time.perf_counter() start = time.perf_counter()
data = Data(traci.simulation.getNetBoundary(), config) data = Data(traci.simulation.getNetBoundary(), config)
@ -71,6 +65,10 @@ def create_dump(config_file, dump_name):
def run(data : Data, config : Config, logger): def run(data : Data, config : Config, logger):
try: try:
traci.start(config.sumo_cmd) 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(f'Loaded simulation file : {config._SUMOCFG}')
logger.info('Loading data for the simulation') logger.info('Loading data for the simulation')
start = time.perf_counter() start = time.perf_counter()
@ -105,18 +103,12 @@ def main(args):
if args.run is not None: if args.run is not None:
dump_path = f'files/dump/{args.run}.json' 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: with open(dump_path, 'r') as f:
data = jsonpickle.decode(f.read()) data = jsonpickle.decode(f.read())
config = data.config config = data.config
logger = config.init_logger(save_logs=args.save) logger = config.init_logger(save_logs=args.save)
logger.info(f'Running simulation dump {args.run}...') 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() start = time.perf_counter()
run(data, config, logger) run(data, config, logger)
@ -148,5 +140,6 @@ def main(args):
logger.info(f'-> HC emissions = {emissions.get_reduction_percentage(ref.hc, total_emissions.hc)} %') 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'-> PMx emissions = {emissions.get_reduction_percentage(ref.pmx, total_emissions.pmx)} %')
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) main(sys.argv[1:])