mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Added TrafficLight model and actions on traffic lights
This commit is contained in:
		@@ -36,7 +36,9 @@ def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], max_speed):
 | 
			
		||||
        traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6)
 | 
			
		||||
 
 | 
			
		||||
        
 | 
			
		||||
def adjust_traffic_light_phase_duration():
 | 
			
		||||
    '''for tl_id in traci.trafficlight.getIDList():
 | 
			
		||||
       print(traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id))'''
 | 
			
		||||
def adjust_traffic_light_phase_duration(area,reduction_factor):
 | 
			
		||||
    for tl in area._tls:
 | 
			
		||||
        phaseDuration = traci.trafficlight.getPhaseDuration(tl.tl_id)
 | 
			
		||||
        traci.trafficlight.setPhaseDuration(tl.tl_id, phaseDuration*reduction_factor)
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
@@ -6,7 +6,8 @@ from shapely.geometry import LineString
 | 
			
		||||
import actions
 | 
			
		||||
import config
 | 
			
		||||
import sys
 | 
			
		||||
from model import Area, Vehicle, Lane
 | 
			
		||||
from model import Area, Vehicle, Lane , TrafficLight
 | 
			
		||||
from traci import trafficlight
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def init_grid(simulation_bounds, cells_number):
 | 
			
		||||
@@ -57,17 +58,24 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
 | 
			
		||||
            if vehicle.pos in area:
 | 
			
		||||
                area.emissions += vehicle.emissions
 | 
			
		||||
        if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked:
 | 
			
		||||
            
 | 
			
		||||
            actions.limit_speed_into_area(area, vehicles,30)
 | 
			
		||||
            actions.adjust_traffic_light_phase_duration(area, 0.5)
 | 
			
		||||
            
 | 
			
		||||
            traci.polygon.setColor(area.name, (255, 0, 0))
 | 
			
		||||
            traci.polygon.setFilled(area.name, True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def add_lanes_to_areas(areas: List[Area]):
 | 
			
		||||
def add_data_to_areas(areas: List[Area]):
 | 
			
		||||
    lanes = get_all_lanes()
 | 
			
		||||
    for area in areas:
 | 
			
		||||
        for lane in lanes:
 | 
			
		||||
            if area.rectangle.intersects(lane.polygon):
 | 
			
		||||
                area.add_lane(lane)
 | 
			
		||||
                for tl_id in traci.trafficlight.getIDList():
 | 
			
		||||
                    if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id):
 | 
			
		||||
                        area.add_tl(TrafficLight(tl_id))
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
@@ -75,9 +83,7 @@ def main():
 | 
			
		||||
    try:
 | 
			
		||||
        traci.start(config.sumo_cmd)
 | 
			
		||||
        grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
 | 
			
		||||
        add_lanes_to_areas(grid)
 | 
			
		||||
                
 | 
			
		||||
        actions.adjust_traffic_light_phase_duration()
 | 
			
		||||
        add_data_to_areas(grid)
 | 
			
		||||
                
 | 
			
		||||
        step = 0 
 | 
			
		||||
        while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,15 @@ class Lane:
 | 
			
		||||
        """Overrides the default implementation"""
 | 
			
		||||
        return hash(self.lane_id)
 | 
			
		||||
 | 
			
		||||
class TrafficLight: 
 | 
			
		||||
    
 | 
			
		||||
    def __init__(self, tl_id: str):
 | 
			
		||||
        self.tl_id = tl_id        
 | 
			
		||||
    
 | 
			
		||||
    def __hash__(self):
 | 
			
		||||
        """Overrides the default implementation"""
 | 
			
		||||
        return hash(self.tl_id)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
class Area:
 | 
			
		||||
 | 
			
		||||
@@ -24,6 +33,7 @@ class Area:
 | 
			
		||||
        self.name = name
 | 
			
		||||
        self.emissions = 0.0
 | 
			
		||||
        self._lanes: Set[Lane] = set()
 | 
			
		||||
        self._tls: Set[TrafficLight] = set() 
 | 
			
		||||
 | 
			
		||||
    def __eq__(self, other):
 | 
			
		||||
        return self.rectangle.__eq__(other)
 | 
			
		||||
@@ -41,6 +51,9 @@ class Area:
 | 
			
		||||
    def add_lane(self, lane: Lane):
 | 
			
		||||
        self._lanes.add(lane)
 | 
			
		||||
        
 | 
			
		||||
    def add_tl(self, tl: TrafficLight):
 | 
			
		||||
        self._tls.add(tl)
 | 
			
		||||
 | 
			
		||||
    def remove_lane(self, lane: Lane):
 | 
			
		||||
        self._lanes.remove(lane)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user