def test_one_hot_encoding_hand(): hand = [(NINE, HEARTS), (SEVEN, ACORNS), (OBER, BELLS), (UNTER, HEARTS), (EIGHT, LEAVES), (TEN, LEAVES), (ACE, HEARTS), (KING, BELLS)] first_card_enc = np.zeros(32) first_card_enc[16] = 1 assert np.array_equal(enc.encode_one_hot_hand(hand)[0], first_card_enc) sec_card_enc = np.zeros(32) sec_card_enc[13] = 1 assert np.array_equal(enc.encode_one_hot_hand(hand)[1], sec_card_enc)
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
def create_bidding_example(declaring_player, game_mode, hand, player_pos): x = enc.encode_one_hot_hand(hand) if player_pos == declaring_player: y = enc.encode_one_hot_game_mode(game_mode) else: y = enc.encode_one_hot_game_mode((NO_GAME, None)) return x, y
def choose_game_mode(self, public_info, options): hand_encoded = enc.encode_one_hot_hand(self.hand) pred = self.game_mode_nn.predict(np.array([hand_encoded]))[0] # remove not possible modes for mode in GAME_MODES: if mode not in options: pred[GAME_MODES.index(mode)] = 0 max_index = np.argmax(pred) return enc.decode_mode_index(max_index)
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
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
def main(): modelpath_partner = 'trickplay_model_partner_extended.hdf5' modelpath_solo = 'trickplay_model_solo_extended.hdf5' modelpath_wenz = 'trickplay_model_went_extended.hdf5' model = keras.models.load_model(modelpath_partner) # search hand_search = [(KING, HEARTS), (EIGHT, HEARTS), (TEN, ACORNS), (NINE, ACORNS), (ACE, LEAVES), (EIGHT, LEAVES), (SEVEN, LEAVES), (KING, BELLS)] hand_partnersearch = enc.encode_one_hot_hand(hand_search) card_seq_partnersearch = np.zeros((28, 36)) card_seq_partnersearch[0][34] = 1 x = [np.array([card_seq_partnersearch]), np.array([hand_partnersearch])] predictions = model.predict(x)[0] print('Search') print(predictions) print(np.argmax(predictions)) print(predictions[np.argmax(predictions)]) for index in range(32): card_decoded = (index // 4, index % 4) if card_decoded not in hand_search: predictions[index] = 0 print(predictions[np.argmax(predictions)] / sum(predictions)) # offensive partner hand_run_away = [(OBER, LEAVES), (UNTER, HEARTS), (UNTER, BELLS), (ACE, ACORNS), (EIGHT, ACORNS), (ACE, LEAVES), (SEVEN, LEAVES), (EIGHT, BELLS)] hand_partner_run_away = enc.encode_one_hot_hand(hand_run_away) card_seq_partner_run_away = np.zeros((28, 36)) card_seq_partner_run_away[0][34] = 1 x = [ np.array([card_seq_partner_run_away]), np.array([hand_partner_run_away]) ] predictions = model.predict(x)[0] print('Offensive partner') print(predictions) print(np.argmax(predictions)) print(predictions[np.argmax(predictions)]) for index in range(32): card_decoded = (index // 4, index % 4) if card_decoded not in hand_run_away: predictions[index] = 0 print(predictions[np.argmax(predictions)] / sum(predictions)) # declaring player hand_partner_decl = [(OBER, ACORNS), (OBER, BELLS), (UNTER, ACORNS), (ACE, HEARTS), (SEVEN, HEARTS), (EIGHT, ACORNS), (SEVEN, LEAVES), (ACE, BELLS)] hand_partner_decl_enc = enc.encode_one_hot_hand(hand_partner_decl) card_seq_partner_decl = np.zeros((28, 36)) card_seq_partner_decl[0][32] = 1 x = [np.array([card_seq_partner_decl]), np.array([hand_partner_decl_enc])] predictions = model.predict(x)[0] print('Offensive partner') print(predictions) print(np.argmax(predictions)) print(predictions[np.argmax(predictions)]) print('sum', sum(predictions)) for index in range(32): card_decoded = (index // 4, index % 4) if card_decoded not in hand_partner_decl: predictions[index] = 0 print(predictions) print(sum(predictions)) print(predictions[np.argmax(predictions)] / sum(predictions))
import keras import numpy as np from schafkopf.ranks import SEVEN, EIGHT, NINE, TEN, UNTER, OBER, KING, ACE from schafkopf.suits import BELLS, ACORNS, HEARTS, LEAVES import schafkopf.players.data.encodings as enc filepath = "bigger_classifier50.hdf5" model = keras.models.load_model(filepath) hand_wenz = enc.encode_one_hot_hand([(UNTER, ACORNS), (UNTER, HEARTS), (ACE, BELLS), (ACE, LEAVES), (EIGHT, LEAVES), (ACE, ACORNS), (EIGHT, BELLS), (SEVEN, BELLS)]) hand_solo_leaves = enc.encode_one_hot_hand([(OBER, ACORNS), (OBER, HEARTS), (OBER, BELLS), (UNTER, ACORNS), (UNTER, BELLS), (TEN, LEAVES), (NINE, LEAVES), (SEVEN, HEARTS)]) hand_partner_leaves = enc.encode_one_hot_hand([(OBER, ACORNS), (OBER, HEARTS), (UNTER, BELLS), (ACE, HEARTS), (NINE, HEARTS), (EIGHT, BELLS), (TEN, LEAVES), (ACE, ACORNS)]) hand_no_game = enc.encode_one_hot_hand([(OBER, ACORNS), (KING, HEARTS), (ACE, BELLS), (TEN, BELLS), (EIGHT, BELLS), (SEVEN, BELLS), (KING, ACORNS), (NINE, ACORNS)]) hands = np.array(