diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 2f80bfc..b2e9001 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -1,12 +1,15 @@ -''' +""" Created on 17 oct. 2018 @author: Axel Huynh-Phuc, Thibaud Gasser -''' +""" +from typing import Iterable import traci from shapely.geometry.linestring import LineString +from model import Area, Vehicle + def stop_vehicle(veh_id): traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING) @@ -19,10 +22,10 @@ def lanes_in_area(area): yield lane_id -def lock_area(area): - for lane_id in lanes_in_area(area): - print(f'Setting max speed of {lane_id} to 30.') - traci.lane.setMaxSpeed(lane_id, 30) - - for veh_id in traci.vehicle.getIDList(): - traci.vehicle.rerouteTraveltime(veh_id, True) +def lock_area(area: Area, vehicles: Iterable[Vehicle]): + for lane in area._lanes: + print(f'Setting max speed of {lane.lane_id} to 30.') + traci.lane.setMaxSpeed(lane.lane_id, 30) + area.locked = True + for vehicle in vehicles: + traci.vehicle.rerouteTraveltime(vehicle.veh_id, True) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 0ecbfe2..bfdb484 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -1,15 +1,16 @@ from typing import List import traci +from shapely.geometry import LineString +import actions import config -from model import Area, Vehicle +from model import Area, Vehicle, Lane def init_grid(simulation_bounds, cells_number): width = simulation_bounds[1][0] / cells_number height = simulation_bounds[1][1] / cells_number - # TODO: change data structure? areas = list() for i in range(cells_number): for j in range(cells_number): @@ -28,11 +29,19 @@ def get_all_vehicles() -> List[Vehicle]: for veh_id in traci.vehicle.getIDList(): veh_pos = traci.vehicle.getPosition(veh_id) vehicle = Vehicle(veh_id, veh_pos) - vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.id) + vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.veh_id) vehicles.append(vehicle) return vehicles +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)) + return lanes + + def get_emissions(grid: List[Area], vehicles: List[Vehicle]): for area in grid: for vehicle in vehicles: @@ -40,18 +49,27 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]): area.emissions += vehicle.co2 if area.emissions > config.CO2_THRESHOLD: # print(f'Threshold exceeded in {area.name} : {area.emissions}') - # factory.lock_area(area) + if not area.locked: + actions.lock_area(area, vehicles) traci.polygon.setColor(area.name, (255, 0, 0)) traci.polygon.setFilled(area.name, True) +def add_lanes_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) + + def main(): try: traci.start(config.sumo_cmd) grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER) + add_lanes_to_areas(grid) while traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() - # get_emissions(grid, SUMOFactory()) vehicles = get_all_vehicles() get_emissions(grid, vehicles) finally: diff --git a/sumo_project/model.py b/sumo_project/model.py index 541e3c1..5f6e69d 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -1,15 +1,29 @@ -from typing import Tuple +from typing import Tuple, Set -from shapely.geometry import Point +from shapely.geometry import Point, LineString from shapely.geometry import Polygon +from shapely.geometry.base import BaseGeometry + + +class Lane: + + def __init__(self, lane_id: str, polygon: LineString): + self.polygon = polygon + self.lane_id = lane_id + + def __hash__(self): + """Overrides the default implementation""" + return hash(self.lane_id) class Area: def __init__(self, coords, name=''): + self.locked = False self.rectangle = Polygon(coords) self.name = name self.emissions = 0.0 + self._lanes: Set[Lane] = set() def __eq__(self, other): return self.rectangle.__eq__(other) @@ -21,6 +35,15 @@ class Area: def bounds(self): return self.rectangle.bounds + def intersects(self, other: BaseGeometry) -> bool: + return self.rectangle.intersects(other) + + def add_lane(self, lane: Lane): + self._lanes.add(lane) + + def remove_lane(self, lane: Lane): + self._lanes.remove(lane) + @classmethod def from_bounds(cls, xmin, ymin, xmax, ymax): return cls(( @@ -32,8 +55,9 @@ class Area: class Vehicle: - def __init__(self, id: int, pos: Tuple[float, float]): - self.id = id + def __init__(self, veh_id: int, pos: Tuple[float, float]): + self.co2: float = None + self.veh_id = veh_id self.pos = Point(pos) def __repr__(self) -> str: