Exemplo n.º 1
0
    def is_admissible(
        self, vehicle_id: str, index: VehicleIndex, prev_cursors, running_cursors
    ):
        """The vehicle_id we are querying for and the `other_vehicle_ids` _presently in
        this `sstudio.types.Bubble`_.
        """
        for prefix in self.exclusion_prefixes:
            if vehicle_id.startswith(prefix):
                return False

        if self._limit is not None:
            # Already hijacked (according to VehicleIndex) + to be hijacked (running cursors)
            current_hijacked_or_shadowed_vehicle_ids = {
                v_id
                for v_id in BubbleManager.vehicle_ids_in_bubble(self, prev_cursors)
                if index.vehicle_is_hijacked(v_id) or index.vehicle_is_shadowed(v_id)
            }
            running_hijacked_or_shadowed_vehicle_ids = BubbleManager.vehicle_ids_in_bubble(
                self, running_cursors
            )
            all_hijacked_or_shadowed_vehicle_ids = (
                current_hijacked_or_shadowed_vehicle_ids
                | running_hijacked_or_shadowed_vehicle_ids
            )
            if len(all_hijacked_or_shadowed_vehicle_ids) >= self._limit:
                return False

        return True
Exemplo n.º 2
0
    def is_admissible(
        self,
        vehicle_id: str,
        index: VehicleIndex,
        vehicle_ids_in_bubbles: Dict["Bubble", Set[str]],
        running_cursors: Set["Cursor"],
    ):
        """The vehicle_id we are querying for and the `other_vehicle_ids` _presently in
        this `sstudio.types.Bubble`_.
        """
        for prefix in self.exclusion_prefixes:
            if vehicle_id.startswith(prefix):
                return False

        if self._limit is not None:
            # Already hijacked (according to VehicleIndex) + to be hijacked (running cursors)
            current_hijacked_or_shadowed_vehicle_ids = {
                v_id
                for v_id in vehicle_ids_in_bubbles[self]
                if index.vehicle_is_hijacked(v_id)
                or index.vehicle_is_shadowed(v_id)
            }
            per_bubble_veh_ids = BubbleManager.vehicle_ids_per_bubble(
                frozenset(running_cursors))
            running_hijacked_or_shadowed_vehicle_ids = per_bubble_veh_ids[self]

            all_hijacked_or_shadowed_vehicle_ids = (
                current_hijacked_or_shadowed_vehicle_ids
                | running_hijacked_or_shadowed_vehicle_ids) - {vehicle_id}

            if len(all_hijacked_or_shadowed_vehicle_ids) >= self._limit:
                return False

        return True
Exemplo n.º 3
0
    def admissibility(
        self,
        vehicle_id: str,
        index: VehicleIndex,
        vehicle_ids_in_bubbles: Dict["Bubble", Set[str]],
        running_cursors: Set["Cursor"],
    ):
        """The vehicle_id we are querying for and the `other_vehicle_ids` _presently in
        this `sstudio.types.Bubble`_.
        """
        for prefix in self.exclusion_prefixes:
            if vehicle_id.startswith(prefix):
                return False, False

        hijackable, shadowable = True, True
        if self._limit is not None:
            # Already hijacked (according to VehicleIndex) + to be hijacked (running cursors)
            current_hijacked_vehicle_ids = {
                v_id
                for v_id in vehicle_ids_in_bubbles[self]
                if index.vehicle_is_hijacked(v_id)
            }
            current_shadowed_vehicle_ids = {
                v_id
                for v_id in vehicle_ids_in_bubbles[self]
                if index.vehicle_is_shadowed(v_id)
            }
            vehicle_ids_by_bubble_state = (
                BubbleManager._vehicle_ids_divided_by_bubble_state(
                    frozenset(running_cursors)
                )
            )

            # pytype: disable=unsupported-operands
            all_hijacked_vehicle_ids = (
                current_hijacked_vehicle_ids
                | vehicle_ids_by_bubble_state[BubbleState.InAirlock][self]
            ) - {vehicle_id}

            all_shadowed_vehicle_ids = (
                current_shadowed_vehicle_ids
                | vehicle_ids_by_bubble_state[BubbleState.InBubble][self]
            ) - {vehicle_id}
            # pytype: enable=unsupported-operands

            hijackable = len(all_hijacked_vehicle_ids) < (
                self._limit.hijack_limit or maxsize
            )
            shadowable = len(all_shadowed_vehicle_ids) + len(
                all_hijacked_vehicle_ids
            ) < (self._limit.shadow_limit or maxsize)

        return hijackable, shadowable
Exemplo n.º 4
0
 def __init__(self, bubbles: Sequence[SSBubble], road_network: SumoRoadNetwork):
     self._log = logging.getLogger(self.__class__.__name__)
     self._cursors = []
     self._last_vehicle_index = VehicleIndex.identity()
     self._bubbles = [Bubble(b, road_network) for b in bubbles]
Exemplo n.º 5
0
 def __init__(self, bubbles: Sequence[SSBubble], road_map: RoadMap):
     self._log = logging.getLogger(self.__class__.__name__)
     self._cursors = set()
     self._last_vehicle_index = VehicleIndex.identity()
     self._bubbles = [Bubble(b, road_map) for b in bubbles]
Exemplo n.º 6
0
    def from_pos(
        cls,
        pos: Point,
        vehicle: Vehicle,
        bubble: Bubble,
        index: VehicleIndex,
        vehicle_ids_per_bubble: Dict[Bubble, Set[str]],
        running_cursors: Set["Cursor"],
    ) -> "Cursor":
        """Generate a cursor.
        Args:
            pos (Point):
                The shapely position of the vehicle.
            vehicle (Vehicle):
                The vehicle that is to be tracked.
            bubble (Bubble):
                The bubble that the vehicle is interacting with.
            index (VehicleIndex):
                The vehicle index the vehicle is in.
            vehicle_ids_per_bubble (Dict[Bubble, Set[str]]):
                Bubbles associated with vehicle ids.
            running_cursors (Set["Cursor"]):
                A set of existing cursors.
        """
        in_bubble_zone, in_airlock_zone = bubble.in_bubble_or_airlock(pos)
        is_social = vehicle.id in index.social_vehicle_ids()
        is_hijacked, is_shadowed = index.vehicle_is_hijacked_or_shadowed(
            vehicle.id)
        is_hijack_admissible, is_airlock_admissible = bubble.admissibility(
            vehicle.id, index, vehicle_ids_per_bubble, running_cursors)
        was_in_this_bubble = vehicle.id in vehicle_ids_per_bubble[bubble]

        # XXX: When a travelling bubble disappears and an agent is airlocked or
        #      hijacked. It remains in that state.
        # TODO: Depending on step size, we could potentially skip transitions (e.g.
        #       go straight to relinquish w/o hijacking first). This may be solved by
        #       time-based airlocking. For robust code we'll want to handle these
        #       scenarios (e.g. hijacking if didn't airlock first)
        transition = None
        if is_social and not is_shadowed and is_airlock_admissible and in_airlock_zone:
            transition = BubbleTransition.AirlockEntered
        elif is_shadowed and is_hijack_admissible and in_bubble_zone:
            transition = BubbleTransition.Entered
        elif is_hijacked and in_airlock_zone:
            # XXX: This may get called repeatedly because we don't actually change
            #      any state when this happens.
            transition = BubbleTransition.Exited
        elif (was_in_this_bubble and (is_shadowed or is_hijacked)
              and not (in_airlock_zone or in_bubble_zone)):
            transition = BubbleTransition.AirlockExited

        state = None
        if is_hijack_admissible and in_bubble_zone:
            state = BubbleState.InBubble
        elif is_airlock_admissible and in_airlock_zone:
            state = BubbleState.InAirlock

        return cls(vehicle_id=vehicle.id,
                   transition=transition,
                   state=state,
                   bubble=bubble)