def store_handstrengths(self, board): board_cards = [[card.rank, card.suit] for card in board] en_board = [encode(c) for c in board_cards] for player in range(self.n_players): position = self.initial_positions[player] hand_cards = [[card.rank, card.suit] for card in self.players[position].hand] en_hand = [encode(c) for c in hand_cards] self.player_handstrength[position] = holdem_hand_rank( en_hand, en_board)
def build_hand_ranks_five(self, reduce_suits=True, valset=False): """ rank 5 card hands input 5 cards target = {0-7462} """ switcher = { 0: straight_flushes, 1: quads, 2: full_houses, 3: flushes, 4: straights, 5: trips, 6: two_pairs, 7: one_pairs, 8: high_cards } # if you want to make up for the samples repeats = {0: 10, 1: 4, 2: 10, 3: 4, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1} X = [] y = [] for category in dt.Globals.HAND_TYPE_DICT.keys(): if valset: five_hands = switcher[category]() for hand in five_hands: sorted_hand = np.transpose(sort_hand(hand)) en_hand = [encode(c) for c in sorted_hand] X.append(sorted_hand) y.append(rank(en_hand)) else: for _ in range(repeats[category]): five_hands = switcher[category]() for hand in five_hands: hero_hands = hero_5_cards(hand) for h in hero_hands: en_hand = [encode(c) for c in h] X.append(np.transpose(sort_hand(np.transpose(h)))) y.append(rank(en_hand)) # hero = hand[:2] # board = hand[2:] # hero = hero[np.argsort(hero[:,1]),:] # board = board[np.argsort(board[:,1]),:] # hero = hero[np.argsort(hero[:,0]),:] # board = board[np.argsort(board[:,0]),:] # hand = np.concatenate([hero,board]) # if reduce_suits: # hand = swap_suits(hand) # hand = hand[np.argsort(hand[:,0]),:] # lost blocker info X = np.stack(X) y = np.stack(y) return X, y
def build_partial(self, iterations): """ inputs consistenting of hand + board during all streets 4+padding all the way to the full 9 cards. Always evaluated vs random hand. inputs are sorted for data effeciency target = {-1,0,1} """ X = [] y = [] for category in dt.Globals.HAND_TYPE_DICT.keys(): for _ in range(iterations // 9): hero_hand, board, _ = self.create_ninecard_handtypes(category) ninecards = np.concatenate([hero_hand, board], axis=0) flat_card_vector = to_52_vector(ninecards) available_cards = list(set(self.deck) - set(flat_card_vector)) flat_vil_hand = np.random.choice(available_cards, 4, replace=False) vil_hand = np.array(to_2d(flat_vil_hand)) en_hand = [encode(c) for c in hero_hand] en_vil = [encode(c) for c in vil_hand] en_board = [encode(c) for c in board] result = winner(en_hand, en_vil, en_board) # hand + board at all stages. Shuffle cards so its more difficult for network np.random.shuffle(hero_hand) pure_hand = np.concatenate( [hero_hand, np.zeros((5, 2))], axis=0) np.random.shuffle(hero_hand) hand_flop = np.concatenate( [hero_hand, board[:3], np.zeros((2, 2))], axis=0) np.random.shuffle(hero_hand) hand_turn = np.concatenate( [hero_hand, board[:4], np.zeros((1, 2))], axis=0) X.append(pure_hand) X.append(hand_flop) X.append(hand_turn) X.append(ninecards) y.append(result) y.append(result) y.append(result) y.append(result) X = np.stack(X) y = np.stack(y)[:, None] return X, y
def create_ninecard_handtypes(self, category): """ Grab 5 cards representing that handtype, then split into hand/board and add cards, if handtype hasn't changed store hand. """ initial_cards = self.create_handtypes(category) flat_card_vector = to_52_vector(initial_cards) remaining_deck = list(set(self.deck) - set(flat_card_vector)) extra_cards_52 = np.random.choice(remaining_deck, 4, replace=False) extra_cards_2d = to_2d(extra_cards_52) hand = np.concatenate([initial_cards[:2], extra_cards_2d[:2]], axis=0) board = np.concatenate([initial_cards[2:], extra_cards_2d[2:]], axis=0) assert (False not in [(s[1] > dt.SUITS.LOW - 1 and s[1] < dt.SUITS.HIGH) == True for s in hand]), f'hand outside range {hand}' assert (False not in [(s[1] > dt.SUITS.LOW - 1 and s[1] < dt.SUITS.HIGH) == True for s in board]), f'board outside range {board}' en_hand = [encode(c) for c in hand] en_board = [encode(c) for c in board] hand_strength = hand_rank(en_hand, en_board) hand_type = CardDataset.find_strength(hand_strength) while hand_type != category: extra_cards_52 = np.random.choice(remaining_deck, 4, replace=False) extra_cards_2d = to_2d(extra_cards_52) hand = np.concatenate([initial_cards[:2], extra_cards_2d[:2]], axis=0) board = np.concatenate([initial_cards[2:], extra_cards_2d[2:]], axis=0) assert (False not in [(s[1] > dt.SUITS.LOW - 1 and s[1] < dt.SUITS.HIGH) == True for s in hand]), f'hand outside range {hand}' assert (False not in [(s[1] > dt.SUITS.LOW - 1 and s[1] < dt.SUITS.HIGH) == True for s in board]), f'board outside range {board}' en_hand = [encode(c) for c in hand] en_board = [encode(c) for c in board] hand_strength = hand_rank(en_hand, en_board) hand_type = CardDataset.find_strength(hand_strength) return hand, board, hand_strength
def build_10card(self, iterations, encoding): """ Generates X = (i,10,2) y = [-1,0,1] """ X, y = [], [] for i in range(iterations): category = np.random.choice(np.arange(9)) hand1 = self.create_handtypes(category) hand2 = self.create_handtypes(category) encoded_hand1 = [encode(c) for c in hand1] encoded_hand2 = [encode(c) for c in hand2] hand1_rank = rank(encoded_hand1) hand2_rank = rank(encoded_hand2) if hand1_rank > hand2_rank: result = 1 elif hand1_rank < hand2_rank: result = -1 else: result = 0 X.append(np.vstack((hand1, hand2))) y.append(result) X = np.stack(X) y = np.stack(y)[:, None] return X, y
def build_13card(self, iterations, encoding): """ Generates X = (i,13,2) y = [-1,0,1] """ X, y = [], [] for i in range(iterations): cards = np.random.choice(self.deck, 13, replace=False) rust_cards = convert_numpy_to_rust(cards) encoded_cards = [encode(c) for c in rust_cards] hand1 = encoded_cards[:4] hand2 = encoded_cards[4:8] board = encoded_cards[8:] result = winner(hand1, hand2, board) if encoding == '2d': cards2d = convert_numpy_to_2d(cards) X.append(cards2d) else: X.append(cards) y.append(result) X = np.stack(X) y = np.stack(y)[:, None] return X, y
def encode(hand): newhand = [cb.encode(c) for c in hand] return newhand