diff --git a/sumo_project/config.py b/sumo_project/config.py index 8f331dd..de9727b 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -10,6 +10,7 @@ import sys from model import Emission + class Config: # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 5b7b7d7..b72792b 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -1,9 +1,12 @@ import argparse +import csv import sys import time from traci import trafficlight import traci from typing import List +import datetime +import os from parse import search from shapely.geometry import LineString @@ -121,6 +124,33 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi def get_reduction_percentage(ref,total): return (ref - total) / ref * 100 + +def export_data_to_csv(config, grid): + + now = datetime.datetime.now() + current_date = now.strftime("%Y_%m_%d_%H_%M_%S") + + '''if not os.path.exists(f'csv/{current_date}'): + os.makedirs(f'csv/{current_date}')''' + + '''with open(f'test.csv', mode='w', newline = '') as emission_file: + try: + csv_writer = csv.writer(emission_file, delimiter = '', quoting=csv.QUOTE_MINIMAL) + a = list(range(config.n_steps)) + csv_writer.writerow(['steps']) + for item in a: + csv_writer.writerow([item]) + + for area in grid: + writer = csv.writer(emission_file, delimiter = ' ', quoting=csv.QUOTE_MINIMAL) + writer.writerow([f'{area.name}']) + for emission in area.emissions_by_step: + writer.writerow([emission.value()]) + + + finally: + emission_file.close()''' + def run(config, logger): grid = list() @@ -131,6 +161,7 @@ def run(config, logger): start = time.perf_counter() grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) + export_data_to_csv(config,grid) add_data_to_areas(grid) loading_time = round(time.perf_counter() - start, 2) @@ -149,8 +180,10 @@ def run(config, logger): finally: traci.close(False) + simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') + logger.info(f'Real-time factor : {config.n_steps/simulation_time}') total_emissions = Emission() for area in grid: @@ -207,7 +240,7 @@ def main(args): config.check_config() logger.info(f'Loaded configuration file : {args.configfile}') - logger.info(f'Simulated time : {args.steps}') + logger.info(f'Simulated time : {args.steps}s') run(config, logger) if __name__ == '__main__': diff --git a/sumo_project/model.py b/sumo_project/model.py index 55d9f89..210918b 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -1,3 +1,4 @@ +import collections from traci._trafficlight import Logic as SUMO_Logic from typing import Tuple, Set @@ -5,8 +6,6 @@ from shapely.geometry import Point, LineString from shapely.geometry import Polygon from shapely.geometry.base import BaseGeometry -import collections - class Lane: @@ -107,9 +106,8 @@ class Area: return sum def sum_emissions_into_window(self, current_step, window_size): - #print(self.emissions_by_step) - em_obj = self.emissions_by_step[current_step] - self.window.appendleft(em_obj.value()) + + self.window.appendleft(self.emissions_by_step[current_step].value()) sum = 0 for i in range(self.window.__len__()):