Beispiel #1
0
class Area:
    """
    The Area class defines a grid area of the simulation map
    """
    def __init__(self, coords, name):
        """
        Area constructor
        :param coords: The coordinates of the zone,
        defined by the bounds coordinates of this area : (xmin, ymin, xmax, ymax)
        :param name: The Area name
        """
        self.limited_speed = False
        self.locked = False
        self.tls_adjusted = False
        self.weight_adjusted = False
        self.rectangle = Polygon(coords)
        self.name = name
        self.emissions_by_step = []
        self._lanes: Set[Lane] = set()
        self._tls: Set[TrafficLight] = set()

    def set_window_size(self, window_size):
        self.window = collections.deque(maxlen=window_size)

    def __eq__(self, other):
        """
        Overrides the equal definition
        :param other: The other Area object
        :return: True if the two rectangles are equals
        """
        return self.rectangle.__eq__(other)

    def __contains__(self, item):
        """
        :param item: A position on the map
        :return: True if the area contains the item
        """
        return self.rectangle.contains(item)

    @property
    def bounds(self):
        """
        Return the bounds rectangle of this area
        :return:
        """
        return self.rectangle.bounds

    def intersects(self, other: BaseGeometry) -> bool:
        """
        :param other: A BaseGeometry object
        :return: True if this area intersects with other
        """
        return self.rectangle.intersects(other)

    def add_lane(self, lane: Lane):
        """
        Add a new lane object into lanes list
        :param lane: A Lane object
        :return:
        """
        self._lanes.add(lane)

    def add_tl(self, tl: TrafficLight):
        """
        Add a new trafficLight object into lanes list
        :param tl: A TrafficLight object
        :return:
        """
        self._tls.add(tl)

    def remove_lane(self, lane: Lane):
        """
        Remove a lane from lanes list
        :param lane: The Lane object to remove
        :return:
        """
        self._lanes.remove(lane)

    def sum_all_emissions(self):
        """
        Sum all Emissions object from initial step to final step
        :return: The sum Emission object
        """
        sum = Emission()
        for emission in self.emissions_by_step:
            sum += emission
        return sum

    def sum_emissions_into_window(self, current_step):
        """
        Sum all Emissions object into the acquisition window
        :param current_step: The current step of the simulation
        :return:
        """
        self.window.appendleft(self.emissions_by_step[current_step].value())

        sum = 0
        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)))

    def infrastructure_changed(self):
        return (self.limited_speed or self.locked or self.tls_adjusted
                or self.weight_adjusted)