2019-01-17 17:02:24 +00:00
|
|
|
"""
|
|
|
|
Created on 17 oct. 2018
|
|
|
|
|
|
|
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
|
|
|
"""
|
|
|
|
|
2019-01-30 16:57:29 +00:00
|
|
|
"""
|
|
|
|
This module defines how pollutant emissions are recovered and how we act on the areas
|
|
|
|
"""
|
|
|
|
|
2019-01-19 18:56:59 +00:00
|
|
|
import traci
|
2019-01-17 17:02:24 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import actions
|
2019-01-30 16:57:29 +00:00
|
|
|
from model import Vehicle, Emission
|
2019-01-22 10:56:44 +00:00
|
|
|
from runner import RunProcess
|
2019-01-17 17:02:24 +00:00
|
|
|
|
2019-01-30 16:57:29 +00:00
|
|
|
|
2019-01-17 17:02:24 +00:00
|
|
|
def compute_vehicle_emissions(veh_id):
|
|
|
|
"""
|
|
|
|
Recover the emissions of different pollutants from a vehicle and create an Emission instance
|
2019-01-18 22:01:38 +00:00
|
|
|
:param veh_id: The vehicle ID
|
2019-01-17 17:02:24 +00:00
|
|
|
:return: A new Emission instance
|
|
|
|
"""
|
|
|
|
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]:
|
|
|
|
"""
|
|
|
|
Recover all useful information about vehicles and creates a vehicles list
|
|
|
|
:return: A list of vehicles instances
|
|
|
|
"""
|
|
|
|
vehicles = list()
|
|
|
|
for veh_id in traci.vehicle.getIDList():
|
|
|
|
veh_pos = traci.vehicle.getPosition(veh_id)
|
|
|
|
vehicle = Vehicle(veh_id, veh_pos)
|
|
|
|
vehicle.emissions = compute_vehicle_emissions(veh_id)
|
|
|
|
vehicles.append(vehicle)
|
|
|
|
return vehicles
|
|
|
|
|
2019-01-22 10:56:44 +00:00
|
|
|
def get_emissions(p : RunProcess, vehicles: List[Vehicle], current_step):
|
2019-01-17 17:02:24 +00:00
|
|
|
"""
|
|
|
|
For each area retrieves the acquired emissions in the window,
|
|
|
|
and acts according to the configuration chosen by the user
|
2019-01-30 16:57:29 +00:00
|
|
|
:param p: The current process
|
2019-01-17 17:02:24 +00:00
|
|
|
:param vehicles: The list of vehicles
|
|
|
|
:param current_step: The simulation current step
|
|
|
|
:return:
|
|
|
|
"""
|
2019-01-22 10:56:44 +00:00
|
|
|
for area in p.data.grid:
|
2019-01-17 17:02:24 +00:00
|
|
|
total_emissions = Emission()
|
|
|
|
for vehicle in vehicles:
|
|
|
|
if vehicle.pos in area:
|
|
|
|
total_emissions += vehicle.emissions
|
|
|
|
|
2019-01-18 22:01:38 +00:00
|
|
|
# Adding of the total of emissions pollutant at the current step into memory
|
2019-01-17 17:02:24 +00:00
|
|
|
area.emissions_by_step.append(total_emissions)
|
2019-01-22 10:56:44 +00:00
|
|
|
|
2019-01-18 22:01:38 +00:00
|
|
|
# If the sum of pollutant emissions (in mg) exceeds the threshold
|
2019-01-22 10:56:44 +00:00
|
|
|
if area.sum_emissions_into_window(current_step) >= p.config.emissions_threshold:
|
2019-01-17 17:02:24 +00:00
|
|
|
|
2019-01-22 10:56:44 +00:00
|
|
|
if p.config.limit_speed_mode and not area.limited_speed:
|
|
|
|
p.logger.info(f'Action - Decreased max speed into {area.name} by {p.config.speed_rf * 100}%')
|
|
|
|
actions.limit_speed_into_area(area, p.config.speed_rf)
|
|
|
|
if p.config.adjust_traffic_light_mode and not area.tls_adjusted:
|
|
|
|
p.logger.info(
|
|
|
|
f'Action - Decreased traffic lights duration by {p.config.trafficLights_duration_rf * 100}%')
|
|
|
|
actions.adjust_traffic_light_phase_duration(area, p.config.trafficLights_duration_rf)
|
2019-01-17 17:02:24 +00:00
|
|
|
|
2019-01-22 10:56:44 +00:00
|
|
|
if p.config.lock_area_mode and not area.locked:
|
2019-01-17 17:02:24 +00:00
|
|
|
if actions.count_vehicles_in_area(area):
|
2019-01-22 10:56:44 +00:00
|
|
|
p.logger.info(f'Action - {area.name} blocked')
|
2019-01-17 17:02:24 +00:00
|
|
|
actions.lock_area(area)
|
|
|
|
|
2019-01-22 10:56:44 +00:00
|
|
|
if p.config.weight_routing_mode and not area.weight_adjusted:
|
2019-01-17 17:02:24 +00:00
|
|
|
actions.adjust_edges_weights(area)
|
|
|
|
|
|
|
|
traci.polygon.setFilled(area.name, True)
|
|
|
|
|
|
|
|
else:
|
2019-01-30 16:57:29 +00:00
|
|
|
if area.infrastructure_changed():
|
|
|
|
p.logger.info(f'Action - Reversed actions into area {area.name}')
|
|
|
|
actions.reverse_actions(area)
|
|
|
|
traci.polygon.setFilled(area.name, False)
|
2019-01-17 17:02:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_reduction_percentage(ref, total):
|
|
|
|
"""
|
|
|
|
Return the reduction percentage of total emissions between reference and an other simulation
|
2019-01-18 22:01:38 +00:00
|
|
|
:param ref: The sum of all pollutant emissions (in mg) for the simulation of reference
|
|
|
|
:param total: The sum of all pollutant emissions (in mg) for the current simulation launched
|
2019-01-17 17:02:24 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
return (ref - total) / ref * 100
|
|
|
|
|