Beispiel #1
0
def prepare_data_inference(game_data_dic, num_samples):
    """creates lists X, Y with X containing card_seq and Y containing encoded player_hands (starting player first)"""
    played_cards = game_data_dic['played_cards']
    player_hands = game_data_dic['player_hands']

    card_sequences = []
    hands_to_predict = []

    # create num_samples different sequences from one game
    seq_lenghts = random.sample(range(1, 27), num_samples)

    for seq_len in seq_lenghts:
        seq = played_cards[:seq_len]
        seq_encoded = enc.encode_played_cards(
            seq, next_rel_pos=played_cards[seq_len][1])
        card_sequences.append(seq_encoded)

        player_hands_to_predict = np.zeros(128)

        for index, hand in enumerate(player_hands):
            remaining_hand = [
                card for card in hand if card not in [c[0] for c in seq]
            ]
            rem_hand_encoded = enc.encode_hand_inference(remaining_hand)
            player_hands_to_predict[index * 32:index * 32 +
                                    32] = rem_hand_encoded

        hands_to_predict.append(player_hands_to_predict)

    return card_sequences, hands_to_predict
Beispiel #2
0
def prepare_extended_data_trickplay(game_data_dic, num_samples=1):
    played_cards = game_data_dic['played_cards']
    player_hands = game_data_dic['player_hands']

    card_sequences = []
    aux_input_hands = []
    cards_to_predict = []

    # create num_samples different sequences from one game
    seq_lenghts = random.sample(range(27), num_samples)

    for seq_len in seq_lenghts:
        seq = played_cards[:seq_len]
        seq_encoded = enc.encode_played_cards(
            seq, next_rel_pos=played_cards[seq_len][1])
        card_sequences.append(seq_encoded)

        next_player = played_cards[seq_len][1]
        pl_hand = player_hands[next_player]
        hand_enc = enc.encode_one_hot_hand(pl_hand)
        aux_input_hands.append(hand_enc)

        next_card = played_cards[seq_len][0]
        cards_to_predict.append(enc.encode_one_hot_card(next_card))

    return card_sequences, aux_input_hands, cards_to_predict
Beispiel #3
0
def test_encode_played_cards():
    seq = [((OBER, ACORNS), 1), ((UNTER, BELLS), 2), ((TEN, ACORNS), 3),
           ((EIGHT, HEARTS), 0), ((NINE, HEARTS), 1)]
    next_rel_pos = 2
    goal = np.zeros((28, 36))
    goal[0][33] = 1
    goal[1][34] = 1
    goal[1][19] = 1
    goal[2][35] = 1
    goal[2][12] = 1
    goal[3][32] = 1
    goal[3][27] = 1
    goal[4][33] = 1
    goal[4][5] = 1
    goal[5][34] = 1
    goal[5][9] = 1
    assert np.array_equal(enc.encode_played_cards(seq, next_rel_pos), goal)
    seq = []
    next_rel_pos = 3
    goal = np.zeros((28, 36))
    goal[0][35] = 1
    assert np.array_equal(enc.encode_played_cards(seq, next_rel_pos), goal)
Beispiel #4
0
def prepare_extended_data_inference(game_data_dic, num_samples):
    """creates lists [X1, X2], Y with X1 containing card_seq, X2 containing the player hand,
     and Y containing encoded opponent_hands (starting player first)"""
    played_cards = game_data_dic['played_cards']
    player_hands = game_data_dic['player_hands']

    card_sequences = []
    aux_input_hands = []
    hands_to_predict = []

    # create num_samples different sequences from one game
    seq_lenghts = random.sample(range(1, 27), num_samples)

    for seq_len in seq_lenghts:
        seq = played_cards[:seq_len]
        seq_encoded = enc.encode_played_cards(
            seq, next_rel_pos=played_cards[seq_len][1])
        card_sequences.append(seq_encoded)

        next_player = played_cards[seq_len][1]
        pl_hand = player_hands[next_player]
        hand_enc = enc.encode_one_hot_hand(pl_hand)
        aux_input_hands.append(hand_enc)

        player_hands_to_predict = np.zeros(96)

        shift = 0
        for index, hand in enumerate(player_hands):
            start_ind = (index + shift) * 32
            if index == next_player:
                shift = -1
                continue
            else:
                remaining_hand = [
                    card for card in hand if card not in [c[0] for c in seq]
                ]
                rem_hand_encoded = enc.encode_hand_inference(remaining_hand)
                player_hands_to_predict[start_ind:start_ind +
                                        32] = rem_hand_encoded

        hands_to_predict.append(player_hands_to_predict)

    return card_sequences, aux_input_hands, hands_to_predict
Beispiel #5
0
    def make_card_prediction(self, public_info):

        card_sequence = self.create_card_sequence(public_info)
        card_seq_switched_suits = self.switch_suits(card_sequence, public_info)
        rel_pos = (public_info['current_trick'].current_player_index -
                   public_info['declaring_player']) % 4
        card_seq_encoded = enc.encode_played_cards(card_seq_switched_suits,
                                                   rel_pos)

        if not self.use_extended_models:
            x = np.array([card_seq_encoded])
        else:
            hand_encoded = enc.encode_one_hot_hand(self.starting_hand)
            x = [np.array([card_seq_encoded]), np.array([hand_encoded])]

        if public_info['game_mode'][0] == PARTNER_MODE:
            pred = self.partner_nn.predict(x)[0]
        elif public_info['game_mode'][0] == WENZ:
            pred = self.wenz_nn.predict(x)[0]
        else:
            pred = self.solo_nn.predict(x)[0]

        return pred