From 9a964c97e9469e30fa02411da03b51181029b8c1 Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Fri, 23 Nov 2018 19:40:14 +0100 Subject: [PATCH] Added initial max speed information into Lane class --- sumo_project/actions.py | 1 - sumo_project/config.py | 10 ++++++---- sumo_project/emissions.py | 19 +++++++++---------- sumo_project/model.py | 3 ++- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/sumo_project/actions.py b/sumo_project/actions.py index a0f6df9..5453e1b 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -22,7 +22,6 @@ def compute_edge_weight(edge_id): + traci.edge.getPMxEmission(edge_id) + traci.edge.getCO2Emission(edge_id)) - def adjust_edges_weights(): for edge_id in traci.edge.getIDList(): weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed diff --git a/sumo_project/config.py b/sumo_project/config.py index 5b1beb8..ae002a2 100644 --- a/sumo_project/config.py +++ b/sumo_project/config.py @@ -17,8 +17,9 @@ CELLS_NUMBER = 10 EMISSIONS_THRESHOLD = 500000 n_steps = 200 -lock_mode = True -routing_mode = False +limit_speed_mode = True +weight_routing_mode = False +adjust_traffic_light_mode = True sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD) sumo_cmd = [sumo_binary, "-c", _SUMOCFG] @@ -26,6 +27,7 @@ sumo_cmd = [sumo_binary, "-c", _SUMOCFG] def showConfig(): return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n') + str(f'step number = {n_steps}\n') - + str(f'lock mode = {lock_mode}\n') - + str(f'routing mode = {routing_mode}\n')) + + str(f'limit speed mode = {limit_speed_mode}\n') + + str(f'weight routing mode= {weight_routing_mode}\n') + + str(f'adjust traffic light mode = {adjust_traffic_light_mode}\n')) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index c4f7742..ad461e8 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -48,7 +48,8 @@ def get_all_lanes() -> List[Lane]: lanes = [] for lane_id in traci.lane.getIDList(): polygon_lane = LineString(traci.lane.getShape(lane_id)) - lanes.append(Lane(lane_id, polygon_lane)) + initial_max_speed = traci.lane.getMaxSpeed(lane_id) + lanes.append(Lane(lane_id, polygon_lane,initial_max_speed)) return lanes @@ -57,22 +58,21 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]): for vehicle in vehicles: if vehicle.pos in area: area.emissions += vehicle.emissions - if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked: - + if config.limit_speed_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) + if config.adjust_traffic_light_mode: + actions.adjust_traffic_light_phase_duration(area, 0.75) def add_data_to_areas(areas: List[Area]): lanes = get_all_lanes() for area in areas: - for lane in lanes: + for lane in lanes: #add lanes if area.rectangle.intersects(lane.polygon): - area.add_lane(lane) - for tl_id in traci.trafficlight.getIDList(): + area.add_lane(lane) + for tl_id in traci.trafficlight.getIDList(): #add traffic lights if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id): area.add_tl(TrafficLight(tl_id)) @@ -92,9 +92,8 @@ def main(): vehicles = get_all_vehicles() get_emissions(grid, vehicles) - if config.routing_mode: + if config.weight_routing_mode: actions.adjust_edges_weights() - # actions.rerouteAllVehicles() step += 1 progress = round(step/config.n_steps*100,2) diff --git a/sumo_project/model.py b/sumo_project/model.py index 9c3e9eb..5a90846 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -7,9 +7,10 @@ from shapely.geometry.base import BaseGeometry class Lane: - def __init__(self, lane_id: str, polygon: LineString): + def __init__(self, lane_id: str, polygon: LineString, initial_max_speed: float): self.polygon = polygon self.lane_id = lane_id + self.initial_max_speed = initial_max_speed def __hash__(self): """Overrides the default implementation"""