def Fold(self): if self.state.community_state.total_preround_pot == self.state.community_state.totalpot: print('CHECK') return 2, ACTION(action_table.CHECK, 0) else: print('FOLD') return 0, ACTION(action_table.FOLD, 0)
def Call(self): #print("Call") if self.state.community_state.to_call == 0: return ACTION(action_table.CHECK, 0) else: return ACTION(action_table.CALL, self.state.community_state.to_call)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' if state.community_state.to_call == state.player_states[ playerid].betting: return ACTION(action_table.CHECK, 0) else: return ACTION(action_table.CALL, state.community_state.to_call)
def Call(self, playerid): if self.state.community_state.to_call == 0: return ACTION(action_table.CHECK, 0) elif self.state.community_state.to_call > self.state.player_states[ playerid].stack / 20: return self.Fold() else: return ACTION(action_table.CALL, self.state.community_state.to_call)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' rank, percentage = self.evaluateFromState(state, playerid) # expert and probability rule if state.community_card[0] == -1: if percentage > 0: if random.random() > 0.3 : return ACTION(action_table.RAISE, state.player_states[playerid].stack) else: return ACTION(action_table.RAISE, 50) else: if random.random() > 0.5 : return ACTION(action_table.FOLD, 0) else: return ACTION(action_table.CALL, state.community_state.to_call) else: if percentage > 0.8: return ACTION(action_table.RAISE, state.player_states[playerid].stack) elif percentage > 0.5: return ACTION(action_table.RAISE, 50) elif percentage > 0.3: return ACTION(action_table.CALL, state.community_state.to_call) else: return ACTION(action_table.FOLD, 0)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' if state.community_state.to_call > 0: if random.random() > 0.7: return ACTION(action_table.FOLD, 0) else: return ACTION(action_table.CALL, state.community_state.to_call) else: if random.random() > 0.7: return ACTION(action_table.RAISE, 50) elif random.random() > 0.9: return ACTION(action_table.RAISE, state.player_states[playerid].stack) else: return ACTION(action_table.CHECK, 0)
def _Raise(self, raise_upper, min_raise, raise_amount): if min([raise_upper, min_raise, raise_amount]) == raise_upper: # fold #print("Raise amount too high") return self.Fold() if min_raise > raise_amount: # call #print("minimum raise more than amount to be raised") return self.Call() else: return ACTION(action_table.RAISE, raise_amount)
def reset_state(self): self.hand_odds = 0.0 self.lastboard = "" self.round = 0 self.n_opponent = 0 self.call_risk = 0.0 self._roundRaiseCount = 0 self.stack = 0 self.lastaction = ACTION(action_table.NA, 0)
def takeExptectedAction(self, state, playerid): action = self.model.take_action(state.community_card, state.player_states[playerid].hand) if action == 3: self.model.fold_count += 1 table_length = sum(1 for x in state.community_card if x != -1) if table_length == 3 or table_length == 4: self.action_history.append((action, state.community_card, state.player_states[playerid].hand)) return ACTION(action, state.community_state.bigblind)
def Check(self, round_bet, big_blind, min_bet, round_id, win_rate): if round_id == 0: max_call = random.randint(1, 3) * big_blind else: max_call = win_rate * round_bet if max_call < min_bet: return self.Fold() else: print("CHECK") return 2, ACTION(action_table.CHECK, min_bet)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' # print("state => ",state) # print("playerid => ",playerid) _stateCards = self.__turn_observation_to_stateJust52(state, playerid) print("Test State => ", _stateCards) # print("Test State => ", self.__turn_observation_to_state(state, playerid)) # input("pause") if state.community_state.to_call > 0: if random.random() > 0.7: return ACTION(action_table.FOLD, 0) else: return ACTION(action_table.CALL, state.community_state.to_call) else: if random.random() > 0.7: return ACTION(action_table.RAISE, 50) elif random.random() > 0.9: return ACTION(action_table.RAISE, state.player_states[playerid].stack) else: return ACTION(action_table.CHECK, 0)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' # print("state => ",state) # print("playerid => ",playerid) rank, percentage = self.evaluateFromState(state, playerid) _stateCards = self.turn_observation_to_stateJust52_plus2dim( state, playerid) _stateCards.append(rank) _stateCards.append(percentage) # expert and probability rule # if state.community_card[0] == -1: # if percentage > 0: # return ACTION(action_table.RAISE, state.player_states[playerid].stack) # else: # if random.random() > 0.3 : # return ACTION(action_table.FOLD, 0) # else: # return ACTION(action_table.CALL, state.community_state.to_call) # else: # if percentage > 0.8: # return ACTION(action_table.RAISE, state.player_states[playerid].stack) # elif percentage > 0.5: # return ACTION(action_table.RAISE, 50) # elif percentage > 0.3: # return ACTION(action_table.CALL, state.community_state.to_call) # else: # return ACTION(action_table.FOLD, 0) action = self.model.predict( np.array(_stateCards).reshape(1, self.state_dim)) best_action = np.argmax(action) if best_action == 0: # play the game, CALL return ACTION(action_table.CALL, state.community_state.to_call) else: # not play the game, FOLD return ACTION(action_table.FOLD, 0)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' print("debug >>>") for p in state.player_states: print(p) print(state.community_state) print(state.community_card) print(playerid) print("<<< ") # print("Test State : ", self.__turn_observation_to_state(state, playerid)) if state.community_state.to_call > 0: if random.random() > 0.7 : return ACTION(action_table.FOLD, 0) else: return ACTION(action_table.CALL, state.community_state.to_call) else: if random.random() > 0.7: return ACTION(action_table.RAISE, 50) elif random.random() > 0.9: return ACTION(action_table.RAISE, state.player_states[playerid].stack) else: return ACTION(action_table.CHECK, 0)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' if self.lastaction.action != action_table.NA: I = self.state2index() R = state.player_states[playerid].stack - self.stack A = self.lastaction.action - 1 I += (A, ) self.logger.info( "sarsa2Model: previous stack={}, previous call_level={}". format(self.stack, self.call_level)) available_actions = self.readState(state, playerid) self.logger.info( "sarsa2Model: takeAction: round {}, available_actions={}".format( self.round, available_actions)) q = self.getActionValues() max_a = 0 max_q = -100000 for a in available_actions: if q[a - 1] > max_q: max_a = a max_q = q[a - 1] assert len(available_actions) > 0 self.logger.info("sarsa2Model: max_a={}, max_q={}".format( max_a, max_q)) # Q-learning (off policy TD control) if self.lastaction.action != action_table.NA: E = R + DISCOUNT * max_q - self.Q[I] self.logger.info("sarsa2Model: reward {}, error {}".format(R, E)) self.Q[I] = self.Q[I] + STEP_SIZE * E self.Q_hit[I] += 1 self.Q_err[I] = 0.9 * self.Q_err[I] + 0.1 * np.absolute(E) # behaviour is epsilon greedy action = max_a if np.random.random() < EPSILON: # exploration action = np.random.choice(available_actions) self.logger.info("sarsa2Model: explore action={}".format(action)) amount = 0.0 if action == action_table.RAISE: self._roundRaiseCount += 1 amount = state.community_state.to_call elif action == action_table.CALL and state.community_state.to_call <= state.player_states[ playerid].betting: action = action_table.CHECK self.lastaction = ACTION(action, amount) return self.lastaction
def Bet(self, round_bet, big_blind, min_bet, my_chips, round_id, win_rate): if round_id == 0: bet_amount = random.randint(2, 7) * big_blind max_call = max(0.5 * my_chips, 20 * big_blind) elif round_id == 1: max_call = win_rate * round_bet bet_amount = win_rate * 0.6 * round_bet else: max_call = win_rate * round_bet bet_amount = win_rate * round_bet if bet_amount < min_bet: bet_amount = min_bet if max_call < min_bet: return self.Fold() else: print("BET") return 1, ACTION(action_table.BET, bet_amount)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' return ACTION(action_table.RAISE, state.community_state.to_call * 2)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' self.playerid = playerid actionID, amount = self._doAction(state) return ACTION(actionID, amount)
def takeRuleAction(self, table, hand, trainModel, state, blindid_info, playerid): if len(table) == 0: if state.community_state.current_player in blindid_info: # inblind, always call return ACTION(action_table.CALL, state.community_state.to_call) else: """ Do rule based 1) one pair, call 2) has J, Q, K, A in hand, call 3) if same suit, 50% call """ if hand[0][0] == hand[1][0]: return ACTION(action_table.CALL, state.community_state.to_call) elif hand[0][0] in 'JQKA' or hand[1][0] in 'JQKA': return ACTION(action_table.CALL, state.community_state.to_call) elif hand[0][1] == hand[1][1]: if random.random() > 0.5: return ACTION(action_table.CALL, state.community_state.to_call) return ACTION(action_table.FOLD, 0) else: rank, value = trainModel.get_expected_value(table, hand) if len(table) == 5: other_rank, other_value = trainModel.get_expected_value( table[:3], table[3:]) if other_rank > rank * 1.5: if random.random() < 0.5: return ACTION(action_table.FOLD, 0) else: return ACTION(action_table.CALL, state.community_state.to_call) else: return ACTION(action_table.CALL, state.community_state.to_call) else: if rank > 18: return ACTION(action_table.RAISE, state.player_states[playerid].stack) elif rank > 15: if random.random() < 0.5: bet = min(state.community_state.to_call * 2, state.player_states[playerid].stack) return ACTION(action_table.RAISE, bet) else: return ACTION(action_table.RAISE, state.player_states[playerid].stack) elif rank > 10: if random.random() < 0.8: bet = min(state.community_state.to_call * 2, state.player_states[playerid].stack) return ACTION(action_table.RAISE, bet) else: return ACTION(action_table.RAISE, state.player_states[playerid].stack) elif rank > 5: if random.random() < 0.1: return ACTION(action_table.RAISE, state.player_states[playerid].stack) else: return ACTION(action_table.CALL, state.community_state.to_call) elif rank > 2: return ACTION(action_table.CALL, state.community_state.to_call) else: if random.random() > 0.7: return ACTION(action_table.CALL, state.community_state.to_call) else: return ACTION(action_table.FOLD, 0)
def AllIn(self, playerid): #print("All in") return ACTION(action_table.RAISE, self.state.player_states[playerid].stack) # all in
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)
def Fold(self): #print('FOLD') if self.state.community_state.to_call == 0: return ACTION(action_table.CHECK, 0) else: return ACTION(action_table.FOLD, 0)
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): ''' (Predict/ Policy) Select Action under state''' return ACTION(action_table.RAISE, state.player_states[playerid].stack)
def takeAction(self, state, playerid): ''' (Predict/ Policy) Select Action under state''' if state.community_state.to_call == 0: return ACTION(action_table.CHECK, 0) else: return ACTION(action_table.FOLD, 0)
def Raise(self, raise_upper, raise_amount): if raise_amount > raise_upper: # call return ACTION(action_table.CALL, raise_upper) else: return ACTION(action_table.RAISE, raise_amount)