1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 03:26:30 +00:00

Added Emission class for more details into logs

This commit is contained in:
Ahp06 2018-12-14 17:53:46 +01:00
parent 4519ce9d2c
commit 86f694756d
4 changed files with 59 additions and 32 deletions

View File

@ -20,7 +20,7 @@ def compute_edge_weight(edge_id):
hc = traci.edge.getHCEmission(edge_id) hc = traci.edge.getHCEmission(edge_id)
pmx = traci.edge.getPMxEmission(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): def adjust_edges_weights(area):
area.weight_adjusted = True area.weight_adjusted = True

View File

@ -8,14 +8,13 @@ import logging
import os import os
import sys import sys
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
# These constants are simulation dependant, you must change them according to your simulation # These constants are simulation dependant, you must change them according to your simulation
total_emissions100 = 13615949.148296086 ref200 = Emission(co2=42816869.05436445,co=1128465.0343051048,nox=18389.648337283958,hc=6154.330914019103,pmx=885.0829265236318)
total_emissions200 = 43970763.15084738
total_emissions300 = 87382632.0821697
def __init__(self): def __init__(self):
'''Default constructor''' '''Default constructor'''
@ -101,12 +100,8 @@ class Config:
return logger return logger
def get_basics_emissions(self): def get_ref_emissions(self):
if self.n_steps == 100:
return self.total_emissions100
if self.n_steps == 200: if self.n_steps == 200:
return self.total_emissions200 return self.ref200
if self.n_steps == 300:
return self.total_emissions300

View File

@ -10,7 +10,7 @@ from shapely.geometry import LineString
import actions import actions
from config import Config 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): 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): 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]: def get_all_vehicles() -> List[Vehicle]:
vehicles = list() 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): def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, config, logger):
for area in grid: for area in grid:
vehicle_emissions = 0 total_emissions = Emission()
for vehicle in vehicles: for vehicle in vehicles:
if vehicle.pos in area: if vehicle.pos in area:
vehicle_emissions += vehicle.emissions total_emissions += vehicle.emissions
area.emissions_by_step.append(vehicle_emissions) area.emissions_by_step.append(total_emissions)
if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold: if area.sum_emissions_into_window(current_step, config.window_size) >= config.emissions_threshold:
@ -117,6 +119,8 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
actions.reverse_actions(area) actions.reverse_actions(area)
traci.polygon.setFilled(area.name, False) traci.polygon.setFilled(area.name, False)
def get_reduction_percentage(ref,total):
return (ref - total) / ref * 100
def run(config, logger): def run(config, logger):
grid = list() grid = list()
@ -148,17 +152,24 @@ def run(config, logger):
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)')
total_emissions = 0 total_emissions = Emission()
for area in grid: for area in grid:
total_emissions += area.sum_all_emissions() 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 : if not config.without_actions_mode :
ref = config.get_basics_emissions() ref = config.get_ref_emissions()
if not (ref is None): if not (ref is None):
diff_with_actions = (ref - total_emissions) / ref global_diff = (ref.value() - total_emissions.value()) / ref.value()
logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %')
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): def add_options(parser):
parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False, parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False,

View File

@ -45,6 +45,25 @@ class TrafficLight:
"""Overrides the default implementation""" """Overrides the default implementation"""
return hash(self.tl_id) 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: class Area:
def __init__(self, coords, name, window_size): def __init__(self, coords, name, window_size):
@ -82,14 +101,16 @@ class Area:
self._lanes.remove(lane) self._lanes.remove(lane)
def sum_all_emissions(self): def sum_all_emissions(self):
sum = 0 sum = Emission()
for emission in self.emissions_by_step: for emission in self.emissions_by_step:
sum += emission sum += emission
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(em_obj.value())
self.window.appendleft(self.emissions_by_step[current_step])
sum = 0 sum = 0
for i in range(self.window.__len__()): for i in range(self.window.__len__()):
sum += self.window[i] sum += self.window[i]
@ -106,7 +127,7 @@ class Area:
class Vehicle: class Vehicle:
def __init__(self, veh_id: int, pos: Tuple[float, float]): 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.veh_id = veh_id
self.pos = Point(pos) self.pos = Point(pos)