Esempio n. 1
0
def create_first_partial_observation(complete_state_delta: state_delta.StateDelta) -> PartialObservation:
    initial_follower = complete_state_delta.follower

    # Figure out which hexes can be seen from this configuration
    visible_positions: List[position.Position] = VISIBILITY_MAP[(initial_follower.get_position(),
                                                                 initial_follower.get_rotation())]

    # Construct a state delta with the dynamic information
    leader = complete_state_delta.leader
    if leader.get_position() not in visible_positions:
        leader = None

    card_beliefs: List[card.Card] = list()
    for state_card in complete_state_delta.cards:
        if state_card.get_position() in visible_positions:
            # Update the selection: invalid sets can't be determined!
            selection: card.CardSelection = state_card.get_selection()
            if selection == card.CardSelection.INVALID:
                selection = card.CardSelection.SELECTED

            new_card = card.Card(state_card.get_position(),
                                 card.CARD_ROTATION,
                                 state_card.get_color(),
                                 state_card.get_count(),
                                 state_card.get_shape(),
                                 selection)
            card_beliefs.append(new_card)

    # The initial observation history is an age of 0 (currently observed) for all visible positions.
    return PartialObservation(state_delta.StateDelta(leader, initial_follower, card_beliefs),
                              {pos: 0 for pos in visible_positions})
Esempio n. 2
0
def update_observation(current_observation: PartialObservation,
                       complete_state_delta: state_delta.StateDelta) -> PartialObservation:
    """Given a current observation, this creates a new observation after the follower moves."""
    new_follower: agent.Agent = complete_state_delta.follower

    now_visible_positions: List[position.Position] = VISIBILITY_MAP[(new_follower.get_position(),
                                                                     new_follower.get_rotation())]

    # Update the previous observation ages.
    previous_observation_ages = current_observation.get_observation_ages()
    new_observation_ages: Dict[position.Position, int] = dict()
    for previous_observation, previous_age in previous_observation_ages.items():
        new_observation_ages[previous_observation] = previous_age + 1

    # Make sure all the currently-visible positions get an age of zero.
    for pos in now_visible_positions:
        new_observation_ages[pos] = 0

    # Leader: if it's now in view, update it, otherwise it will stay where it was.
    current_leader: agent.Agent = complete_state_delta.leader
    if current_leader.get_position() not in now_visible_positions:
        current_leader = current_observation.get_leader()

    # Cards:

    # First, make sure everything in view is correct.
    actual_cards = complete_state_delta.cards
    actual_card_dict: Dict[position.Position, card.Card] = dict()
    for actual_card in actual_cards:
        actual_card_dict[actual_card.get_position()] = actual_card

    new_card_beliefs = list()
    for observed_position in now_visible_positions:
        # If the position contains a now-visible card, add it.
        if observed_position in actual_card_dict:
            actual_card = actual_card_dict[observed_position]
            selection: card.CardSelection = actual_card.get_selection()
            if selection == card.CardSelection.INVALID:
                selection = card.CardSelection.SELECTED

            new_card = card.Card(actual_card.get_position(),
                                 card.CARD_ROTATION,
                                 actual_card.get_color(),
                                 actual_card.get_count(),
                                 actual_card.get_shape(),
                                 selection)

            new_card_beliefs.append(new_card)

    # Then add all the cards that were previously observed but no longer observed.
    previously_observed_cards = current_observation.get_card_beliefs()

    for card_belief in previously_observed_cards:
        if card_belief.get_position() not in now_visible_positions:
            new_card_beliefs.append(card_belief)

    return PartialObservation(state_delta.StateDelta(current_leader, new_follower, new_card_beliefs),
                              new_observation_ages)
Esempio n. 3
0
    def _set_environment_info(
            self, dictionary_representation: Dict[str, Any]) -> None:
        if self._hexes:
            raise ValueError('Already set the hex information.')
        for hex_cell in dictionary_representation['hexCellInfo']:
            hex_position: position.Position = position.v3_pos_to_position(
                eval(hex_cell['posV3']), environment_util.HEX_WIDTH,
                environment_util.HEX_DEPTH)
            hex_terrain: terrain.Terrain = terrain.cell_name_to_terrain(
                hex_cell['lType'], float(eval(hex_cell['posV3'])[1]))

            self._hexes.append((hex_terrain, hex_position))

        card_list: List[card.Card] = \
            environment_util.interpret_card_info(dictionary_representation['cardInfo'],
                                                 environment_util.HEX_WIDTH,
                                                 environment_util.HEX_DEPTH)
        leader: agent.Agent = None
        follower: agent.Agent = None

        for environment_object in dictionary_representation[
                'propPlacementInfo']:
            prop_name: str = environment_object['pName']
            object_position: position.Position = position.v3_pos_to_position(
                eval(environment_object['posV3']), environment_util.HEX_WIDTH,
                environment_util.HEX_DEPTH)

            constructed_object = environment_util.construct_object(
                prop_name, object_position, eval(environment_object['rotV3']),
                card_list)
            if not isinstance(constructed_object, agent.Agent):
                self._objects.append(constructed_object)
            elif constructed_object.get_type(
            ) == environment_objects.ObjectType.LEADER:
                leader = constructed_object
            elif constructed_object.get_type(
            ) == environment_objects.ObjectType.FOLLOWER:
                follower = constructed_object
            else:
                raise ValueError(
                    'Unrecognized object type with Agent object: %r' %
                    constructed_object.get_type())

        if not leader or not follower:
            raise ValueError(
                'Didn\'t find leader or follower in the object list.')

        self._current_state_delta: state_delta.StateDelta = state_delta.StateDelta(
            leader, follower, card_list)