mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 12:09:20 +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)
 | 
					        traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6)
 | 
				
			||||||
 
 | 
					 
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
def adjust_traffic_light_phase_duration():
 | 
					def adjust_traffic_light_phase_duration(area,reduction_factor):
 | 
				
			||||||
    '''for tl_id in traci.trafficlight.getIDList():
 | 
					    for tl in area._tls:
 | 
				
			||||||
       print(traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id))'''
 | 
					        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 actions
 | 
				
			||||||
import config
 | 
					import config
 | 
				
			||||||
import sys
 | 
					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):
 | 
					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:
 | 
					            if vehicle.pos in area:
 | 
				
			||||||
                area.emissions += vehicle.emissions
 | 
					                area.emissions += vehicle.emissions
 | 
				
			||||||
        if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked:
 | 
					        if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked:
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
            actions.limit_speed_into_area(area, vehicles,30)
 | 
					            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.setColor(area.name, (255, 0, 0))
 | 
				
			||||||
            traci.polygon.setFilled(area.name, True)
 | 
					            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()
 | 
					    lanes = get_all_lanes()
 | 
				
			||||||
    for area in areas:
 | 
					    for area in areas:
 | 
				
			||||||
        for lane in lanes:
 | 
					        for lane in lanes:
 | 
				
			||||||
            if area.rectangle.intersects(lane.polygon):
 | 
					            if area.rectangle.intersects(lane.polygon):
 | 
				
			||||||
                area.add_lane(lane)
 | 
					                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():
 | 
					def main():
 | 
				
			||||||
@@ -75,9 +83,7 @@ def main():
 | 
				
			|||||||
    try:
 | 
					    try:
 | 
				
			||||||
        traci.start(config.sumo_cmd)
 | 
					        traci.start(config.sumo_cmd)
 | 
				
			||||||
        grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
 | 
					        grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
 | 
				
			||||||
        add_lanes_to_areas(grid)
 | 
					        add_data_to_areas(grid)
 | 
				
			||||||
                
 | 
					 | 
				
			||||||
        actions.adjust_traffic_light_phase_duration()
 | 
					 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
        step = 0 
 | 
					        step = 0 
 | 
				
			||||||
        while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
 | 
					        while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,15 @@ class Lane:
 | 
				
			|||||||
        """Overrides the default implementation"""
 | 
					        """Overrides the default implementation"""
 | 
				
			||||||
        return hash(self.lane_id)
 | 
					        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:
 | 
					class Area:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -24,6 +33,7 @@ class Area:
 | 
				
			|||||||
        self.name = name
 | 
					        self.name = name
 | 
				
			||||||
        self.emissions = 0.0
 | 
					        self.emissions = 0.0
 | 
				
			||||||
        self._lanes: Set[Lane] = set()
 | 
					        self._lanes: Set[Lane] = set()
 | 
				
			||||||
 | 
					        self._tls: Set[TrafficLight] = set() 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __eq__(self, other):
 | 
					    def __eq__(self, other):
 | 
				
			||||||
        return self.rectangle.__eq__(other)
 | 
					        return self.rectangle.__eq__(other)
 | 
				
			||||||
@@ -41,6 +51,9 @@ class Area:
 | 
				
			|||||||
    def add_lane(self, lane: Lane):
 | 
					    def add_lane(self, lane: Lane):
 | 
				
			||||||
        self._lanes.add(lane)
 | 
					        self._lanes.add(lane)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 | 
					    def add_tl(self, tl: TrafficLight):
 | 
				
			||||||
 | 
					        self._tls.add(tl)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def remove_lane(self, lane: Lane):
 | 
					    def remove_lane(self, lane: Lane):
 | 
				
			||||||
        self._lanes.remove(lane)
 | 
					        self._lanes.remove(lane)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user