1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 11:36:29 +00:00
sumo-emissions/sumo_project/actions.py

85 lines
2.9 KiB
Python
Raw Normal View History

2018-11-23 12:53:20 +00:00
"""
Created on 17 oct. 2018
@author: Axel Huynh-Phuc, Thibaud Gasser
"""
import traci
from traci._trafficlight import Logic
2018-11-23 12:53:20 +00:00
from typing import Iterable
from shapely.geometry.linestring import LineString
from model import Area, Vehicle
2018-11-23 12:53:20 +00:00
def compute_edge_weight(edge_id):
2018-12-14 14:50:26 +00:00
co2 = traci.edge.getCO2Emission(edge_id)
co = traci.edge.getCOEmission(edge_id)
nox = traci.edge.getNOxEmission(edge_id)
hc = traci.edge.getHCEmission(edge_id)
pmx = traci.edge.getPMxEmission(edge_id)
return (co2 + co + nox + hc + pmx)
2018-12-14 14:50:26 +00:00
def adjust_edges_weights(area):
area.weight_adjusted = True
for lane in area._lanes:
edge_id = traci.lane.getEdgeID(lane.lane_id)
2018-11-23 12:53:20 +00:00
weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed
traci.edge.setEffort(edge_id, weight)
2018-12-14 14:50:26 +00:00
for veh_id in traci.vehicle.getIDList():
traci.vehicle.rerouteEffort(veh_id)
2018-12-07 15:30:23 +00:00
def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], speed_rf):
area.limited_speed = True
2018-11-23 12:53:20 +00:00
for lane in area._lanes:
2018-12-07 15:30:23 +00:00
traci.lane.setMaxSpeed(lane.lane_id, speed_rf * lane.initial_max_speed)
2018-12-14 14:50:26 +00:00
2018-12-03 20:05:01 +00:00
def modifyLogic(logic, rf): #rf for "reduction factor"
new_phases = []
for phase in logic._phases:
new_phase = traci.trafficlight.Phase(phase.duration*rf,phase.minDuration*rf,phase.maxDuration*rf,phase.phaseDef)
new_phases.append(new_phase)
2018-11-23 12:53:20 +00:00
2018-12-03 20:05:01 +00:00
return traci.trafficlight.Logic("new-program", 0 , 0 , 0 , new_phases)
def adjust_traffic_light_phase_duration(area, reduction_factor):
2018-12-07 15:30:23 +00:00
area.tls_adjusted = True
for tl in area._tls:
for logic in tl._logics:
2018-12-03 20:05:01 +00:00
traci.trafficlights.setCompleteRedYellowGreenDefinition(tl.tl_id, modifyLogic(logic,reduction_factor))
2018-12-07 15:30:23 +00:00
def count_vehicles_in_area(area):
vehicles_in_area = 0
for lane in area._lanes:
vehicles_in_area += traci.lane.getLastStepVehicleNumber(lane.lane_id)
2018-12-07 15:30:23 +00:00
return vehicles_in_area
def lock_area(area):
area.locked = True
for lane in area._lanes:
2018-12-07 16:16:26 +00:00
traci.lane.setDisallowed(lane.lane_id, 'passenger')
def reverse_actions(area):
#Reset max speed to original
2018-12-09 14:05:33 +00:00
if area.limited_speed:
2018-12-07 16:16:26 +00:00
area.limited_speed = False
2018-12-09 14:05:33 +00:00
for lane in area._lanes:
traci.lane.setMaxSpeed(lane.lane_id, lane.initial_max_speed)
2018-12-07 16:16:26 +00:00
#Reset traffic lights initial duration
2018-12-09 14:05:33 +00:00
if area.tls_adjusted:
2018-12-07 16:16:26 +00:00
area.tls_adjusted = False
2018-12-09 14:05:33 +00:00
for tl in area._tls:
for initial_logic in tl._logics:
traci.trafficlights.setCompleteRedYellowGreenDefinition(tl.tl_id, initial_logic._logic)
2018-12-07 16:16:26 +00:00
#Unlock the area
2018-12-09 14:05:33 +00:00
if area.locked:
2018-12-07 16:16:26 +00:00
area.locked = False
for lane in area._lanes:
traci.lane.setAllowed(lane.lane_id, '') #empty means all classes are allowed