1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 03:26:30 +00:00

Improve speed of the actions

This commit is contained in:
Thibaud Gasser 2018-11-15 22:15:43 +01:00
parent 1cf12fec81
commit e058f06928
3 changed files with 63 additions and 18 deletions

View File

@ -1,12 +1,15 @@
''' """
Created on 17 oct. 2018 Created on 17 oct. 2018
@author: Axel Huynh-Phuc, Thibaud Gasser @author: Axel Huynh-Phuc, Thibaud Gasser
''' """
from typing import Iterable
import traci import traci
from shapely.geometry.linestring import LineString from shapely.geometry.linestring import LineString
from model import Area, Vehicle
def stop_vehicle(veh_id): def stop_vehicle(veh_id):
traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING) traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING)
@ -19,10 +22,10 @@ def lanes_in_area(area):
yield lane_id yield lane_id
def lock_area(area): def lock_area(area: Area, vehicles: Iterable[Vehicle]):
for lane_id in lanes_in_area(area): for lane in area._lanes:
print(f'Setting max speed of {lane_id} to 30.') print(f'Setting max speed of {lane.lane_id} to 30.')
traci.lane.setMaxSpeed(lane_id, 30) traci.lane.setMaxSpeed(lane.lane_id, 30)
area.locked = True
for veh_id in traci.vehicle.getIDList(): for vehicle in vehicles:
traci.vehicle.rerouteTraveltime(veh_id, True) traci.vehicle.rerouteTraveltime(vehicle.veh_id, True)

View File

@ -1,15 +1,16 @@
from typing import List from typing import List
import traci import traci
from shapely.geometry import LineString
import actions
import config import config
from model import Area, Vehicle from model import Area, Vehicle, Lane
def init_grid(simulation_bounds, cells_number): def init_grid(simulation_bounds, cells_number):
width = simulation_bounds[1][0] / cells_number width = simulation_bounds[1][0] / cells_number
height = simulation_bounds[1][1] / cells_number height = simulation_bounds[1][1] / cells_number
# TODO: change data structure?
areas = list() areas = list()
for i in range(cells_number): for i in range(cells_number):
for j 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(): for veh_id in traci.vehicle.getIDList():
veh_pos = traci.vehicle.getPosition(veh_id) veh_pos = traci.vehicle.getPosition(veh_id)
vehicle = Vehicle(veh_id, veh_pos) vehicle = Vehicle(veh_id, veh_pos)
vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.id) vehicle.co2 = traci.vehicle.getCO2Emission(vehicle.veh_id)
vehicles.append(vehicle) vehicles.append(vehicle)
return vehicles 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]): def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
for area in grid: for area in grid:
for vehicle in vehicles: for vehicle in vehicles:
@ -40,18 +49,27 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
area.emissions += vehicle.co2 area.emissions += vehicle.co2
if area.emissions > config.CO2_THRESHOLD: if area.emissions > config.CO2_THRESHOLD:
# print(f'Threshold exceeded in {area.name} : {area.emissions}') # 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.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]):
lanes = get_all_lanes()
for area in areas:
for lane in lanes:
if area.rectangle.intersects(lane.polygon):
area.add_lane(lane)
def main(): 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)
while traci.simulation.getMinExpectedNumber() > 0: while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep() traci.simulationStep()
# get_emissions(grid, SUMOFactory())
vehicles = get_all_vehicles() vehicles = get_all_vehicles()
get_emissions(grid, vehicles) get_emissions(grid, vehicles)
finally: finally:

View File

@ -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 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: class Area:
def __init__(self, coords, name=''): def __init__(self, coords, name=''):
self.locked = False
self.rectangle = Polygon(coords) self.rectangle = Polygon(coords)
self.name = name self.name = name
self.emissions = 0.0 self.emissions = 0.0
self._lanes: Set[Lane] = set()
def __eq__(self, other): def __eq__(self, other):
return self.rectangle.__eq__(other) return self.rectangle.__eq__(other)
@ -21,6 +35,15 @@ class Area:
def bounds(self): def bounds(self):
return self.rectangle.bounds 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 @classmethod
def from_bounds(cls, xmin, ymin, xmax, ymax): def from_bounds(cls, xmin, ymin, xmax, ymax):
return cls(( return cls((
@ -32,8 +55,9 @@ class Area:
class Vehicle: class Vehicle:
def __init__(self, id: int, pos: Tuple[float, float]): def __init__(self, veh_id: int, pos: Tuple[float, float]):
self.id = id self.co2: float = None
self.veh_id = veh_id
self.pos = Point(pos) self.pos = Point(pos)
def __repr__(self) -> str: def __repr__(self) -> str: