mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-10-30 18:09:20 +00:00 
			
		
		
		
	Added data to csv export
This commit is contained in:
		| @@ -10,6 +10,7 @@ import sys | |||||||
|  |  | ||||||
| from model import Emission | from model import Emission | ||||||
|  |  | ||||||
|  |  | ||||||
| class Config:  | class Config:  | ||||||
|      |      | ||||||
|     # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas |     # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas | ||||||
|   | |||||||
| @@ -1,9 +1,12 @@ | |||||||
| import argparse | import argparse | ||||||
|  | import csv | ||||||
| import sys | import sys | ||||||
| import time | import time | ||||||
| from traci import trafficlight | from traci import trafficlight | ||||||
| import traci | import traci | ||||||
| from typing import List | from typing import List | ||||||
|  | import datetime | ||||||
|  | import os | ||||||
|  |  | ||||||
| from parse import search | from parse import search | ||||||
| from shapely.geometry import LineString | from shapely.geometry import LineString | ||||||
| @@ -122,6 +125,33 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi | |||||||
| def get_reduction_percentage(ref,total): | def get_reduction_percentage(ref,total): | ||||||
|     return (ref - total) / ref * 100 |     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): | def run(config, logger): | ||||||
|     grid = list() |     grid = list() | ||||||
|     try: |     try: | ||||||
| @@ -131,6 +161,7 @@ def run(config, logger): | |||||||
|         start = time.perf_counter() |         start = time.perf_counter() | ||||||
|         |         | ||||||
|         grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) |         grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) | ||||||
|  |         export_data_to_csv(config,grid) | ||||||
|         add_data_to_areas(grid) |         add_data_to_areas(grid) | ||||||
|          |          | ||||||
|         loading_time = round(time.perf_counter() - start, 2) |         loading_time = round(time.perf_counter() - start, 2) | ||||||
| @@ -149,8 +180,10 @@ def run(config, logger): | |||||||
|              |              | ||||||
|     finally: |     finally: | ||||||
|         traci.close(False) |         traci.close(False) | ||||||
|  |                  | ||||||
|         simulation_time = round(time.perf_counter() - start, 2) |         simulation_time = round(time.perf_counter() - start, 2) | ||||||
|         logger.info(f'End of the simulation ({simulation_time}s)') |         logger.info(f'End of the simulation ({simulation_time}s)') | ||||||
|  |         logger.info(f'Real-time factor : {config.n_steps/simulation_time}') | ||||||
|          |          | ||||||
|         total_emissions = Emission() |         total_emissions = Emission() | ||||||
|         for area in grid: |         for area in grid: | ||||||
| @@ -207,7 +240,7 @@ def main(args): | |||||||
|     config.check_config() |     config.check_config() | ||||||
|      |      | ||||||
|     logger.info(f'Loaded configuration file : {args.configfile}') |     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) |     run(config, logger) | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|   | |||||||
| @@ -1,3 +1,4 @@ | |||||||
|  | import collections  | ||||||
| from traci._trafficlight import Logic as SUMO_Logic | from traci._trafficlight import Logic as SUMO_Logic | ||||||
| from typing import Tuple, Set | from typing import Tuple, Set | ||||||
|  |  | ||||||
| @@ -5,8 +6,6 @@ from shapely.geometry import Point, LineString | |||||||
| from shapely.geometry import Polygon | from shapely.geometry import Polygon | ||||||
| from shapely.geometry.base import BaseGeometry | from shapely.geometry.base import BaseGeometry | ||||||
|  |  | ||||||
| import collections  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Lane: | class Lane: | ||||||
|  |  | ||||||
| @@ -107,9 +106,8 @@ class Area: | |||||||
|         return sum  |         return sum  | ||||||
|      |      | ||||||
|     def sum_emissions_into_window(self, current_step, window_size):  |     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(self.emissions_by_step[current_step].value()) | ||||||
|         self.window.appendleft(em_obj.value()) |  | ||||||
|          |          | ||||||
|         sum = 0 |         sum = 0 | ||||||
|         for i in range(self.window.__len__()): |         for i in range(self.window.__len__()): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user