1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 11:36:29 +00:00
sumo-emissions/sumo_project/model.py

140 lines
3.9 KiB
Python
Raw Normal View History

2018-12-16 20:43:21 +00:00
import collections
from typing import Tuple, Set
from shapely.geometry import Point, LineString
from shapely.geometry import Polygon
from shapely.geometry.base import BaseGeometry
2018-12-16 20:43:21 +00:00
from traci._trafficlight import Logic as SUMO_Logic
class Lane:
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"""
return hash(self.lane_id)
2018-12-16 20:43:21 +00:00
2018-12-03 20:05:01 +00:00
class Phase:
2018-12-16 20:43:21 +00:00
def __init__(self, duration: float, minDuration: float, maxDuration: float, phaseDef: str):
self.duration = duration
2018-12-03 20:05:01 +00:00
self.minDuration = minDuration
self.maxDuration = maxDuration
2018-12-16 20:43:21 +00:00
self.phaseDef = phaseDef
2018-12-03 20:05:01 +00:00
def __repr__(self) -> str:
repr = f'Phase(duration:{self.duration},minDuration:{self.minDuration},maxDuration:{self.maxDuration},phaseDef:{self.phaseDef})'
return str(repr)
2018-12-16 20:43:21 +00:00
2018-12-03 20:05:01 +00:00
class Logic:
def __init__(self, logic: SUMO_Logic, phases: Set[Phase]):
self._logic = logic
self._phases: Set[Phase] = phases
2018-12-16 20:43:21 +00:00
class TrafficLight:
def __init__(self, tl_id: str, logics: Set[Logic]):
2018-12-16 20:43:21 +00:00
self.tl_id = tl_id
self._logics: Set[Logic] = logics
2018-12-16 20:43:21 +00:00
def __hash__(self):
"""Overrides the default implementation"""
return hash(self.tl_id)
2018-12-16 20:43:21 +00:00
class Emission:
2018-12-16 20:43:21 +00:00
def __init__(self, co2=0, co=0, nox=0, hc=0, pmx=0):
self.co2 = co2
self.co = co
self.nox = nox
self.hc = hc
self.pmx = pmx
2018-12-16 20:43:21 +00:00
def __add__(self, other):
return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc,
self.pmx + other.pmx)
def value(self):
return self.co2 + self.co + self.nox + self.hc + self.pmx
2018-12-16 20:43:21 +00:00
def __repr__(self) -> str:
repr = f'Emission(co2={self.co2},co={self.co},nox={self.nox},hc={self.hc},pmx={self.pmx})'
return str(repr)
2018-12-16 20:43:21 +00:00
class Area:
2018-12-14 14:50:26 +00:00
def __init__(self, coords, name, window_size):
self.limited_speed = False
self.locked = False
2018-12-07 15:30:23 +00:00
self.tls_adjusted = False
2018-12-14 14:50:26 +00:00
self.weight_adjusted = False
self.rectangle = Polygon(coords)
self.name = name
2018-12-14 14:50:26 +00:00
self.emissions_by_step = []
2018-12-16 20:43:21 +00:00
self.window = collections.deque(maxlen=window_size)
self._lanes: Set[Lane] = set()
2018-12-16 20:43:21 +00:00
self._tls: Set[TrafficLight] = set()
def __eq__(self, other):
return self.rectangle.__eq__(other)
def __contains__(self, item):
return self.rectangle.contains(item)
@property
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)
2018-12-16 20:43:21 +00:00
def add_tl(self, tl: TrafficLight):
self._tls.add(tl)
def remove_lane(self, lane: Lane):
self._lanes.remove(lane)
2018-12-16 20:43:21 +00:00
def sum_all_emissions(self):
sum = Emission()
for emission in self.emissions_by_step:
sum += emission
2018-12-16 20:43:21 +00:00
return sum
def sum_emissions_into_window(self, current_step, window_size):
2018-12-16 14:33:10 +00:00
self.window.appendleft(self.emissions_by_step[current_step].value())
2018-12-16 20:43:21 +00:00
sum = 0
2018-12-14 14:50:26 +00:00
for i in range(self.window.__len__()):
sum += self.window[i]
return sum
@classmethod
def from_bounds(cls, xmin, ymin, xmax, ymax):
return cls((
(xmin, ymin),
(xmin, ymax),
(xmax, ymax),
(xmax, ymin)))
2018-12-16 20:43:21 +00:00
class Vehicle:
def __init__(self, veh_id: int, pos: Tuple[float, float]):
self.emissions: Emission = Emission()
self.veh_id = veh_id
self.pos = Point(pos)
def __repr__(self) -> str:
return str(self.__dict__)