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
def test_encode_hand_inference(): hand = [(4, 3), (2, 0), (1, 1), (3, 3), (3, 2), (4, 2), (2, 2)] goal = np.zeros(32) goal[19] = 1 goal[8] = 1 goal[5] = 1 goal[15] = 1 goal[14] = 1 goal[10] = 1 goal[18] = 1 assert np.array_equal(enc.encode_hand_inference(hand), goal) sec_hand = [(2, 1), (0, 0), (7, 3), (7, 2)] sec_goal = np.zeros(32) sec_goal[0] = 1 sec_goal[9] = 1 sec_goal[31] = 1 sec_goal[30] = 1 assert np.array_equal(enc.encode_hand_inference(sec_hand), sec_goal)
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