def evaluateFromState(self, state, playerid): # print("state",state.player_states[playerid].hand) evaluator = Evaluator() hand = [] board = [] # p1_score = evaluator.evaluate(board, player1_hand) for i in state.player_states[playerid].hand: hand.append(Card.new(card_to_normal_str(i))) # print(card_to_normal_str(i)) # print(hand) for j in state.community_card: if j != -1: # print(card_to_normal_str(j)) board.append(Card.new(card_to_normal_str(j))) # print(board) if len(board) == 0: rank = evaluator.evaluate(hand, []) elif len(board) == 3: rank = evaluator.evaluate(hand, board[:3]) elif len(board) == 4: rank = evaluator.evaluate(hand, board[:4]) elif len(board) == 5: rank = evaluator.evaluate(hand, board[:5]) rank_class = evaluator.get_rank_class(rank) class_string = evaluator.class_to_string(rank_class) percentage = 1.0 - evaluator.get_five_card_rank_percentage(rank) # higher better here return [rank,percentage]
def _getObservation(self, state): # [monteCarlo, remainChips, investChips, pot] cards = [ card_to_normal_str(c).upper() for c in state.player_states[self.playerid].hand ] boards = [ card_to_normal_str(c).upper() for c in state.community_card if c != -1 ] self.playerCount = len( [p for p in state.player_states if p.playing_hand]) start = time.time() self.winRate = self.monteCarlo.calc_win_rate(cards, boards, self.playerCount, self.montecarloTimes) remainChips = state.player_states[self.playerid].stack / self.BB investChips = state.player_states[self.playerid].betting / self.BB pot = state.community_state.totalpot / self.BB toCallChips = state.community_state.to_call / self.BB # state = [self.winRate, remainChips, investChips, pot] # ver1 state = [ self.winRate, self.playerCount, remainChips, investChips, pot, toCallChips ] # ver2 # print 'time_getObs', (time.time() - start), state, self.playerid return np.array(state)
def card_list_to_str(cards): s = "" for c in cards: if c == -1: break s += (card_to_normal_str(c) + " ") return s.strip()
def __turn_card_to_one_hot(self, card): if card == -1: return [0] * 52 # " {'23456789TJQKA'} + {'shdc''} (note: lower case) " card_info = card_to_normal_str(card) card_idx = CHAR_NUM_TO_INT[card_info[:1]] + CHAR_SUIT_TO_INT[card_info[1:]] card_hot = [0]*52 card_hot[card_idx] = 1 return card_hot
def __turn_card_to_one_hot_returnIndx(self, card, card_hot): if card == -1: return card_hot else: card_info = card_to_normal_str(card) card_idx = CHAR_NUM_TO_INT[card_info[:1]] + CHAR_SUIT_TO_INT[card_info[1:]] if card_hot[card_idx] == 1: input("Error!!!!!! card_hot cann't duplicate") else: card_hot[card_idx] = 1 return card_hot
def __turn_observation_to_state(self, observation, playerid): my_card = observation.player_states[playerid].hand community_card = observation.community_card round_num = observation.community_state.round round_one_hot = [0, 0, 0, 0] if round_num == 0: round_one_hot = [0, 0, 0, 1] elif round_num == 1: round_one_hot = [0, 0, 1, 0] elif round_num == 2: round_one_hot = [0, 1, 0, 0] elif round_num == 3: round_one_hot = [1, 0, 0, 0] my_card_list = [card_to_normal_str(card) for card in my_card] community_card_list = [ card_to_normal_str(card) for card in community_card if card != -1 ] self.bot.reset() self.bot.assign_hand(my_card_list) if len(community_card_list) > 0: # get board cards self.bot.assign_board(community_card_list) # get win rate self.win_rate = self.bot.estimate_winrate(n=200) player_cnt = observation.community_state.num_not_fold_player my_stack = observation.player_states[playerid].stack total_pot = observation.community_state.totalpot to_call = observation.community_state.to_call return self.__turn_card_to_one_hot(my_card[0]) + \ self.__turn_card_to_one_hot(my_card[1])+ \ self.__turn_card_to_one_hot(community_card[0])+ \ self.__turn_card_to_one_hot(community_card[1])+ \ self.__turn_card_to_one_hot(community_card[2])+ \ self.__turn_card_to_one_hot(community_card[3])+ \ self.__turn_card_to_one_hot(community_card[4])+ \ [player_cnt, total_pot, to_call, self.win_rate] + round_one_hot
def __turn_card_to_one_hot(self, card): if card == -1: return [0] * 52 # " {'23456789TJQKA'} + {'shdc''} (note: lower case) " card_info = card_to_normal_str(card) # for j in ['s','h','d','c']: # for i in ['A','2','3','4','5','6','7','8','9','T','J','Q','K']: # card_hot = [0]*52 # print('{0}'.format(i+j)) # card_idx = CHAR_NUM_TO_INT[i] + CHAR_SUIT_TO_INT[j] # print(card_idx) # card_hot[card_idx] = 1 # print(card_hot) # input('pause') card_idx = CHAR_NUM_TO_INT[card_info[:1]] + CHAR_SUIT_TO_INT[card_info[1:]] card_hot = [0]*52 card_hot[card_idx] = 1 return card_hot
def takeAction(self, state, playerid): round = state.community_state.round # 0-3 hands = [ card_to_normal_str(c).upper() for c in state.player_states[playerid].hand ] table_cards = [ card_to_normal_str(c).upper() for c in state.community_card if c != -1 ] min_bet = state.community_state.to_call chips = state.player_states[playerid].stack bigblind = state.community_state.bigblind betting = state.player_states[playerid].betting num_in_game_player = len( [p for p in state.player_states if p.playing_hand]) allin_threshold = self.preflop_win_rate.quantile( axis=0, q=0.9)[num_in_game_player - 2] call_threshold = self.preflop_win_rate.quantile( axis=0, q=0.6)[num_in_game_player - 2] raise_action_amount = min_bet * 2 allin_action_amount = chips call_action_amount = min_bet action_amount = 0 if round == 0: win_rate = self._get_preflop_win_rate(hands, num_in_game_player) if win_rate >= allin_threshold: if min_bet * 4 > chips: action_amount = allin_action_amount else: action_amount = max(min_bet, 0.4 * chips * random.random()) elif win_rate >= call_threshold: action_amount = call_action_amount elif win_rate >= call_threshold and chips < 3 * bigblind: action_amount = allin_action_amount elif min_bet <= 0.05 * chips: action_amount = call_action_amount else: win_rate = self.mcg.calc_win_rate(hands, table_cards, num_in_game_player, 1000) if win_rate >= 0.9: action_amount = allin_action_amount elif win_rate >= 0.75: if betting > 0.3 * chips: action_amount = call_action_amount else: action_amount = max(min_bet, 0.3 * chips * random.random()) elif win_rate >= 0.6: if betting > 0.1 * chips: action_amount = call_action_amount else: action_amount = max(min_bet, 0.1 * chips * random.random()) if action_amount >= allin_action_amount: action = ACTION(action_table.RAISE, chips) elif abs(action_amount - raise_action_amount) <= 0.5 * bigblind: action = ACTION(action_table.RAISE, raise_action_amount) elif abs(action_amount - call_action_amount) <= 0.5 * bigblind: action = ACTION(action_table.CALL, min_bet) elif action_amount >= call_action_amount: action = ACTION(action_table.RAISE, action_amount) else: action = ACTION(action_table.FOLD, action_amount) return action
def takeAction(self, state, playerid): my_card = state.player_states[playerid].hand my_card = [card_to_normal_str(card) for card in my_card] community_card = state.community_card community_card = [ card_to_normal_str(card) for card in community_card if card != -1 ] self.bot.reset() self.bot.assign_hand(my_card) if len(community_card) > 0: # get board cards self.bot.assign_board(community_card) # get win rate win_rate = self.bot.estimate_winrate(n=200) bet_amount = state.community_state.smallblind action = action_table.FOLD round_bet = state.community_state.total_preround_pot total_bet = state.community_state.totalpot big_blind = state.community_state.bigblind min_bet = state.community_state.call_price player_cnt = state.community_state.num_not_fold_player my_chips = state.player_states[playerid].stack if min_bet == 0: action = action_table.CHECK if round_bet == total_bet: #nobody bet in this round, default check action = action_table.CHECK if total_bet < big_blind * 1.5: total_bet = big_blind * 1.5 min_bet_ratio = float(min_bet) / total_bet try: total_win_rate = win_rate**(player_cnt - 1) except ZeroDivisionError: total_win_rate = 0 print("win_rate: {}".format(win_rate)) print("total_win_rate: {}".format(total_win_rate)) max_call = 0 if state.community_state.round == 0: #round_bet=0 #TODO: consider which round of Deal if total_win_rate > 0.8 and win_rate > 0.92: print("Deal#1 rule hit, try to attract others to call") action = action_table.BET bet_amount = random.randint(4, 7) * big_blind max_call = max(0.5 * my_chips, 20 * big_blind) elif total_win_rate > 0.7 and win_rate > 0.8: print("Deal#2 rule hit") action = action_table.BET bet_amount = random.randint(2, 5) * big_blind max_call = max(0.5 * my_chips, 20 * big_blind) print("Deal#2: max_call:{}".format(max_call)) elif my_chips < 10 * big_blind: print("Deal#3 rule hit: be conservative because too poor") if min_bet < big_blind and win_rate > 0.3: #smallBlind case action = action_table.CHECK elif win_rate > 0.6: print("Deal#4 rule hit") action = action_table.BET bet_amount = random.randint(1, 3) * big_blind max_call = random.randint(4, 6) * big_blind elif win_rate > 0.5: print("Deal#5 rule hit") action = action_table.CHECK max_call = random.randint(2, 3) * big_blind elif win_rate > 0.3: print("Deal#6 rule hit") action = action_table.CHECK max_call = big_blind else: print("Deal: no rule hit, fold") else: if total_win_rate > 0.8 and win_rate > 0.98: print("#1 rule hit, try to attract others to call") if win_rate == 1: action = action_table.BET bet_amount = my_chips elif state.community_state.round == 1: action = action_table.BET bet_amount = 0.6 * round_bet elif state.community_state.round == 2: action = action_table.BET bet_amount = round_bet elif my_chips > round_bet: #river action = action_table.BET bet_amount = round_bet else: action = action_table.BET bet_amount = my_chips elif total_win_rate > 0.7 and win_rate > 0.8: print("#2 rule hit") action = action_table.BET bet_amount = 0.5 * round_bet max_call = round_bet elif total_win_rate > 0.5: print("#4 rule hit") if big_blind == min_bet: action = action_table.BET bet_amount = min_bet action = action_table.CHECK max_call = round_bet * win_rate elif win_rate > 0.5: print("#6 rule hit") action = action_table.CHECK max_call = round_bet * 0.5 elif win_rate > 0.3: print("#7 rule hit") action = action_table.CHECK max_call = big_blind else: print("no rule hit, fold") if bet_amount < min_bet: bet_amount = min_bet if max_call != 0 and max_call < min_bet: print("fold because someone bet too much: min_bet:{} max_call:{}". format(min_bet, max_call)) action = action_table.FOLD bet_amount = 0 print("decide_action: action:{}, amount:{}, max_call:{}".format( action, bet_amount, max_call)) return ACTION(action, bet_amount)