From 86f694756d9efcad1fed08d458ba9f402f4c3a55 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Fri, 14 Dec 2018 17:53:46 +0100 Subject: [PATCH] Added Emission class for more details into logs --- sumo_project/actions.py | 2 +- sumo_project/config.py | 13 ++++------- sumo_project/emissions.py | 47 ++++++++++++++++++++++++--------------- sumo_project/model.py | 29 ++++++++++++++++++++---- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 4a7a854..302a9ce 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -20,7 +20,7 @@ def compute_edge_weight(edge_id): hc = traci.edge.getHCEmission(edge_id) pmx = traci.edge.getPMxEmission(edge_id) - return (co2 + co + nox + hc + pmx)/traci.edge.getLaneNumber(edge_id) + return (co2 + co + nox + hc + pmx) def adjust_edges_weights(area): area.weight_adjusted = True diff --git a/sumo_project/config.py b/sumo_project/config.py index e139ffb..8f331dd 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -8,14 +8,13 @@ import logging import os 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 # These constants are simulation dependant, you must change them according to your simulation - total_emissions100 = 13615949.148296086 - total_emissions200 = 43970763.15084738 - total_emissions300 = 87382632.0821697 + ref200 = Emission(co2=42816869.05436445,co=1128465.0343051048,nox=18389.648337283958,hc=6154.330914019103,pmx=885.0829265236318) def __init__(self): '''Default constructor''' @@ -101,12 +100,8 @@ class Config: return logger - def get_basics_emissions(self): - if self.n_steps == 100: - return self.total_emissions100 + def get_ref_emissions(self): if self.n_steps == 200: - return self.total_emissions200 - if self.n_steps == 300: - return self.total_emissions300 + return self.ref200 diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index b0da161..5b7b7d7 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -10,7 +10,7 @@ from shapely.geometry import LineString import actions from config import Config -from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic +from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic, Emission def init_grid(simulation_bounds, areas_number,window_size): @@ -68,12 +68,14 @@ def add_data_to_areas(areas: List[Area]): def compute_vehicle_emissions(veh_id): - return (traci.vehicle.getCOEmission(veh_id) - +traci.vehicle.getNOxEmission(veh_id) - +traci.vehicle.getHCEmission(veh_id) - +traci.vehicle.getPMxEmission(veh_id) - +traci.vehicle.getCO2Emission(veh_id)) - + + co2 = traci.vehicle.getCO2Emission(veh_id) + co = traci.vehicle.getCOEmission(veh_id) + nox = traci.vehicle.getNOxEmission(veh_id) + hc = traci.vehicle.getHCEmission(veh_id) + pmx = traci.vehicle.getPMxEmission(veh_id) + + return Emission(co2,co,nox,hc,pmx) def get_all_vehicles() -> List[Vehicle]: vehicles = list() @@ -87,12 +89,12 @@ def get_all_vehicles() -> List[Vehicle]: def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, config, logger): for area in grid: - vehicle_emissions = 0 + total_emissions = Emission() for vehicle in vehicles: if vehicle.pos in area: - vehicle_emissions += vehicle.emissions - - area.emissions_by_step.append(vehicle_emissions) + total_emissions += vehicle.emissions + + area.emissions_by_step.append(total_emissions) if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold: @@ -117,7 +119,9 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi actions.reverse_actions(area) traci.polygon.setFilled(area.name, False) - +def get_reduction_percentage(ref,total): + return (ref - total) / ref * 100 + def run(config, logger): grid = list() try: @@ -148,17 +152,24 @@ def run(config, logger): simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') - total_emissions = 0 + total_emissions = Emission() for area in grid: total_emissions += area.sum_all_emissions() - - logger.info(f'Total emissions = {total_emissions} mg') + + logger.info(f'Total emissions = {total_emissions.value()} mg') if not config.without_actions_mode : - ref = config.get_basics_emissions() + ref = config.get_ref_emissions() if not (ref is None): - diff_with_actions = (ref - total_emissions) / ref - logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %') + global_diff = (ref.value() - total_emissions.value()) / ref.value() + + logger.info(f'Global reduction percentage of emissions = {global_diff*100} %') + logger.info(f'-> CO2 emissions = {get_reduction_percentage(ref.co2, total_emissions.co2)} %') + logger.info(f'-> CO emissions = {get_reduction_percentage(ref.co, total_emissions.co)} %') + logger.info(f'-> Nox emissions = {get_reduction_percentage(ref.nox, total_emissions.nox)} %') + logger.info(f'-> HC emissions = {get_reduction_percentage(ref.hc, total_emissions.hc)} %') + logger.info(f'-> PMx emissions = {get_reduction_percentage(ref.pmx, total_emissions.pmx)} %') + def add_options(parser): parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False, diff --git a/sumo_project/model.py b/sumo_project/model.py index 68101b4..55d9f89 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -44,7 +44,26 @@ class TrafficLight: def __hash__(self): """Overrides the default implementation""" return hash(self.tl_id) + +class Emission: + def __init__(self, co2 = 0, co = 0 , nox = 0, hc = 0, pmx = 0): + self.co2 = co2 + self.co = co + self.nox = nox + self.hc = hc + self.pmx = pmx + def __add__(self,other): + return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc, self.pmx + other.pmx) + + + def value(self): + return self.co2 + self.co + self.nox + self.hc + self.pmx + + def __repr__(self) -> str: + repr = f'Emission(co2={self.co2},co={self.co},nox={self.nox},hc={self.hc},pmx={self.pmx})' + return str(repr) + class Area: def __init__(self, coords, name, window_size): @@ -82,14 +101,16 @@ class Area: self._lanes.remove(lane) def sum_all_emissions(self): - sum = 0 + sum = Emission() for emission in self.emissions_by_step: sum += emission return sum def sum_emissions_into_window(self, current_step, window_size): - - self.window.appendleft(self.emissions_by_step[current_step]) + #print(self.emissions_by_step) + em_obj = self.emissions_by_step[current_step] + self.window.appendleft(em_obj.value()) + sum = 0 for i in range(self.window.__len__()): sum += self.window[i] @@ -106,7 +127,7 @@ class Area: class Vehicle: def __init__(self, veh_id: int, pos: Tuple[float, float]): - self.emissions: float = 0.0 + self.emissions: Emission = Emission() self.veh_id = veh_id self.pos = Point(pos)