Ejemplo n.º 1
0
 def get_location_as_int(self) -> int:
     """Get the vehicle location."""
     if self._vehicle_location is None:
         return -1
     start_node, end_node = dynamic_routing_utils._road_section_to_nodes(  # pylint:disable=protected-access
         self._vehicle_location)
     return self.get_game().network.get_action_id_from_movement(
         start_node, end_node)
Ejemplo n.º 2
0
    def _legal_actions(self, player: pyspiel.PlayerId) -> List[int]:
        """Return the legal actions of the vehicle.

    Legal actions are the succesor road section of the vehicle current road
    section.
    Args:
      player: the vehicle id.

    Returns:
      list_legal_actions: a list of legal actions. If the game is finished then
        the list is empty. If the vehicle is at its destination, has a positive
        waiting time or if it is on a node without successors then an empty list
        is returned. Otherwise the list of successors nodes of the current
        vehicle location is returned.
    """
        if self._is_terminal:
            return []
        if self.get_game().perform_sanity_checks:
            assert player == pyspiel.PlayerId.DEFAULT_PLAYER_ID, str(player)
        if self._vehicle_without_legal_action:
            # If the vehicle is at destination it cannot do anything.
            return [dynamic_routing_utils.NO_POSSIBLE_ACTION]
        if self._waiting_time > 0:
            return [dynamic_routing_utils.NO_POSSIBLE_ACTION]
        _, end_section_node = dynamic_routing_utils._road_section_to_nodes(  # pylint:disable=protected-access
            self._vehicle_location)
        successors = self.get_game().network.get_successors(end_section_node)
        if self.get_game().perform_sanity_checks:
            if not successors:
                raise ValueError(
                    ("If a vehicle is not without legal action, it"
                     " should have an action."))
            assert isinstance(successors, Iterable)
        actions = [
            self.get_game().network.get_action_id_from_movement(
                end_section_node, d) for d in successors
        ]
        map(self.get_game().network.assert_valid_action, actions)
        return sorted(actions)
Ejemplo n.º 3
0
  def _legal_actions(self, vehicle: int) -> List[int]:
    """Return the legal actions of the vehicle.

    Legal actions are the succesor road section of the vehicle current road
    section.
    Args:
      vehicle: the vehicle id.

    Returns:
      list_legal_actions: a list of legal actions. If the game is finished then
        the list is empty. If the vehicle is at its destination, has a positive
        waiting time or if it is on a node without successors then an empty list
        is returned. Otherwise the list of successors nodes of the current
        vehicle location is returned.
    """
    if self._is_terminal:
      return []
    if self.get_game().perform_sanity_checks:
      self.assert_valid_player(vehicle)
    if vehicle in self._vehicle_without_legal_actions:
      # If the vehicle is at destination it cannot do anything.
      return [dynamic_routing_utils.NO_POSSIBLE_ACTION]
    if self._waiting_times[vehicle] > 0:
      return [dynamic_routing_utils.NO_POSSIBLE_ACTION]
    _, end_section_node = dynamic_routing_utils._road_section_to_nodes(  # pylint:disable=protected-access
        self._vehicle_locations[vehicle])
    successors = self.get_game().network.get_successors(end_section_node)
    if successors:
      assert isinstance(successors, Iterable)
      actions = [
          self.get_game().network.get_action_id_from_movement(
              end_section_node, d) for d in successors
      ]
      if self.get_game().perform_sanity_checks:
        map(self.get_game().network.assert_valid_action, actions)
      return sorted(actions)
    return []
Ejemplo n.º 4
0
 def get_location_as_int(self, vehicle: int) -> int:
   """Get the vehicle location."""
   origin, destination = dynamic_routing_utils._road_section_to_nodes(  # pylint:disable=protected-access
       self._vehicle_locations[vehicle])
   return self.get_game().network.get_action_id_from_movement(
       origin, destination)