def montecarlo_simulation_hs(self, nb_player, hole_card, community_card, ahead_tied_behind, agent_rank): community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [ unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1) ] # get rank of hole_card with the community_card that are faced up opponents_rank = [ HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole ] if agent_rank > max(opponents_rank): # if win add increment ahead ahead_tied_behind['ahead'] += 1 elif agent_rank == max(opponents_rank): # if tie increment tied ahead_tied_behind['tied'] += 1 else: # if lose increment behind ahead_tied_behind['behind'] += 1
def montecarlo_simulation_hp(self, nb_player, hole_card, community_card, hp, total_hp, agent_rank, num_simulation): unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1)] # get rank of hole_card with the community_card that are faced up opponents_rank = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] index = 'ahead' if agent_rank > max(opponents_rank): index = 'ahead' elif agent_rank == max(opponents_rank): index = 'tied' else: index = 'behind' total_hp[index] += 1 for i in range(num_simulation): all_community_cards = _fill_community_card(community_card, used_card=hole_card + community_card) agent_best_rank = HandEvaluator.eval_hand(hole_card, all_community_cards) opp_best_rank = HandEvaluator.eval_hand(hole_card, all_community_cards) if agent_best_rank > opp_best_rank: hp[index]['ahead'] += (1 / num_simulation) # normalize so that output of ppot and npot is from 0 to 1 elif agent_best_rank == opp_best_rank: hp[index]['tied'] += (1 / num_simulation) # normalize so that output of ppot and npot is from 0 to 1 else: hp[index]['behind'] += (1 / num_simulation) # normalize so that output of ppot and npot is from 0 to 1
def evaluate_chance_node(self,player_pos,street,small_blind_pos, player_committed_amt, \ oppo_committed_amt, total_num_raises, hole_card, community_card,big_blind_has_spoken, \ total_num_player_raise, total_num_oppo_raise,num_player_raise, num_oppo_raise): if self.LOG: output_log_message_for_evaluate_chance_node(player_pos,street,small_blind_pos, \ player_committed_amt, oppo_committed_amt, total_num_raises, hole_card, community_card, \ big_blind_has_spoken, total_num_player_raise, total_num_oppo_raise,num_player_raise,\ num_oppo_raise) if street == 4: return self.evaluate_terminal_node(player_pos,street,small_blind_pos, player_committed_amt, \ oppo_committed_amt, total_num_raises, hole_card, community_card,big_blind_has_spoken, \ total_num_player_raise, total_num_oppo_raise,num_player_raise, num_oppo_raise) eva = 0 num_cards_left = 50 - len(community_card) cards = [c.to_id() for c in community_card] for i in _pick_unused_card(1, hole_card + community_card): cards.append(i.to_id()) cc = [Card.from_id(c) for c in cards] if player_pos == small_blind_pos: if self.LOG: print("chance 1.1") eva = self.evaluate_player_node(player_pos,street+1,small_blind_pos,player_committed_amt, \ oppo_committed_amt,total_num_raises,hole_card,cc,False, total_num_player_raise, \ total_num_oppo_raise,0,0) + eva else: if self.LOG: print("chance 1.2") eva = self.evaluate_oppo_node(player_pos,street+1,small_blind_pos,player_committed_amt, \ oppo_committed_amt,total_num_raises,hole_card,cc,False,total_num_player_raise, \ total_num_oppo_raise,0,0) + eva return eva / num_cards_left
def HandStrength(weight, hole_card, community_card): """Evaluate hand strength = probability that our hand is the strongest hand based on given pre-determined weights""" No_of_times = 5 # numround = 35 -> run 3 times for running alone without timeout, 2 for running together Ahead = Tied = Behind = 1 ourrank = HandEvaluator.eval_hand(hole_card, community_card) # Consider all two card combinations of the remaining cards. unused_cards = _pick_unused_card(45, hole_card + community_card) # Run defined number of simulations while (No_of_times > 0): oppcard1 = random.choice(unused_cards) oppcard2 = random.choice(unused_cards) if (oppcard1 != oppcard2): # initial opponent hand value oppcard = [oppcard1, oppcard2] opprank = HandEvaluator.eval_hand(oppcard, community_card) # enemy card weight oppweight = (Map_169(oppcard1, oppcard2, weight) / 3364.0) # print(ourrank, opprank, oppweight) if (ourrank > opprank): Ahead += oppweight elif (ourrank == opprank): Tied += oppweight else: Behind += oppweight # < No_of_times = No_of_times - 1 handstrength = (Ahead + Tied / 2) / (Ahead + Tied + Behind) return handstrength
def _montecarlo_simulation(nb_player, hole_card, community_card): community_card = _fill_community_card(community_card, used_card=hole_card+community_card) unused_cards = _pick_unused_card((nb_player-1)*2, hole_card + community_card) opponents_hole = [unused_cards[2*i:2*i+2] for i in range(nb_player-1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def montecarlo_simulation(self, nb_player, hole_card, community_card): # Do a Monte Carlo simulation given the current state of the game by evaluating the hands # get all the community cards, include your own hole card, put it in the community_card array community_card = _fill_community_card(community_card, used_card=hole_card + community_card) # get all the unused cards based on the community card currently (deck - community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) # get 100 possibilities of opponent's hole opponents_hole = [ unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1) ] # calculate the score of opponent's hand based on the possibilities of opponent's hole opponents_score = [ HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole ] # calculate your current hand's score my_score = HandEvaluator.eval_hand(hole_card, community_card) # if my current hand is better than all the possibilities of opponent's hand if my_score >= max(opponents_score): return 1 else: return 0
def montecarlo_simulation(self, nb_player, hole_card, community_card): # Do a Monte Carlo simulation given the current state of the game by evaluating the hands community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def montecarlo_simulation(nb_player, hole_card, community_card): # Do a Monte Carlo simulation given the current state of the game by evaluating the hands community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card((nb_player - 1) * 2, hole_card + community_card) opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(nb_player - 1)] opponents_score = [HandEvaluator.eval_hand(hole, community_card) for hole in opponents_hole] my_score = HandEvaluator.eval_hand(hole_card, community_card) return 1 if my_score >= max(opponents_score) else 0
def HandPotential(weight, hole_card, community_card): """Evaluate hand potential = potential that our hand can become better""" ahead = 0 tied = 1 behind = 2 No_of_times = 5 # numround = 35 -> run 2 times -> good for running alone and together without timeout but 3 has few errors # Hand potential array, each index represents ahead, tied, and behind. HP = [[0.01 for x in range(3)] for y in range(3)] # initialize to low value 0.01 HPTotal = [0 for x in range(3)] # initialize to 0 ourrank = HandEvaluator.eval_hand(hole_card, community_card) # Consider all two card combinations of the remaining cards for the opponent. community_card = _fill_community_card(community_card, used_card=hole_card + community_card) unused_cards = _pick_unused_card(45, hole_card + community_card) # Run defined number of simulations while (No_of_times > 0): oppcard1 = random.choice(unused_cards) oppcard2 = random.choice(unused_cards) turn = random.choice(unused_cards) river = random.choice(unused_cards) if (oppcard1 != oppcard2 != turn != river): # initial opponent hand value oppcard = [oppcard1, oppcard2] opprank = HandEvaluator.eval_hand(oppcard, community_card) # enemy card weight oppweight = (Map_169(oppcard1, oppcard2, weight) / 3364.0) if (ourrank > opprank): index = ahead elif (ourrank == opprank): index = tied else: index = behind # < HPTotal[index] += oppweight # Final 5-card board board = community_card board.append(turn) board.append(river) ourbest = HandEvaluator.eval_hand(hole_card, board) oppbest = HandEvaluator.eval_hand(oppcard, board) if (ourbest > oppbest): HP[index][ahead] += oppweight elif (ourbest == oppbest): HP[index][tied] += oppweight else: HP[index][behind] += oppweight # < No_of_times = No_of_times - 1 sumBehind = HP[behind][ahead] + HP[behind][tied] + HP[behind][behind] sumTied = HP[tied][ahead] + HP[tied][tied] + HP[tied][behind] sumAhead = HP[ahead][ahead] + HP[ahead][tied] + HP[ahead][behind] # Ppot: were behind but moved ahead. Ppot = (HP[behind][ahead] + HP[behind][tied] / 2 + HP[tied][ahead] / 2) / (sumBehind + sumTied / 2) # Npot: were ahead but fell behind. # Npot = (HP[ahead][behind]+HP[tied][behind]/2+HP[ahead][tied]/2)/ (sumAhead+sumTied/2) return Ppot
def generate_ars_table(street, num_simulation, num_simulation_hs): ars_table = {} ars_table_final = {} ars_table_sorted = {} init_ars_table(ars_table, ars_table_final, ars_table_sorted, street) if street == 'flop': num_community_cards = 3 elif street == 'turn': num_community_cards = 4 elif street == 'river': num_community_cards = 5 else: print("incorrect round name, run app again") return for i in range(num_simulation): hole_card = _pick_unused_card(2, []) # generate 2 hole cards # generate num of community card based on street community_card = _pick_unused_card(num_community_cards, hole_card) # initialization of table -------------------------------------------------------------- hand_rank = HandEvaluator.eval_hand(hole_card, community_card) # get current hand rank if hand_rank not in ars_table[street]: ars_table[street][hand_rank] = {} ars_table_final[street][hand_rank] = {} ars_table[street][hand_rank]["strength"] = 0 ars_table[street][hand_rank]["total_iter"] = 0 ars_table_final[street][hand_rank] = 0 # end initialization of table ---------------------------------------------------------- strength = get_hand_strength(hole_card, community_card, num_simulation_hs) # get hand strength ars_table[street][hand_rank]["strength"] += strength ars_table[street][hand_rank]["total_iter"] += 1 if i % 1000 == 0: print("pid: {} street: {} round: {}".format(getpid(), street, i)) # get average of all strengths and store it in ars_table_final for hand_rank_key in ars_table[street]: average_strength = ars_table[street][hand_rank_key]["strength"] / \ ars_table[street][hand_rank_key]["total_iter"] ars_table_final[street][hand_rank_key] = round(average_strength, 3) # 3 dec-place return ars_table
def get_opponent_hand_strength(self, hole_card, community_card): unused_cards = _pick_unused_card(2, hole_card + community_card) # get 100 possibilities of opponent's hole opponents_hole = [unused_cards[2 * i:2 * i + 2] for i in range(1)] # calculate the total score of opponent's hand based on the 100 possibilities of opponent's hole opponents_score = 0 for hole in opponents_hole: opponents_score += HandEvaluator.eval_hand(hole, community_card) opponents_avg_score = opponents_score / opponents_hole.__sizeof__() return opponents_avg_score
table[i] = {} # read table files = [ "./hs_data/flop_2500000_500.txt", "./hs_data/river_2500000_500.txt", "./hs_data/turn_2500000_500.txt", "./hs_data/preflop_10000.txt" ] for f in files: file = open(f) street = file.readline().strip() for i in file: # print(i.strip().split(" ")) score, strength = i.strip().split(" ") table[street][score] = strength for i in range(num_sim): opp_hole_card = _pick_unused_card(2, hole_card + community_card) opp_score = HandEvaluator.eval_hand(opp_hole_card, community_card) attrs = { 'street': 'river', 'ehs': table['river'][agent_score], 'action': 'raise' } if agent_score > opp_score: reward = 1 elif agent_score == opp_score: reward = 0 else: reward = -1 update_table(attrs, reward)
def assuming_card(my_hole_card): return _pick_unused_card(2, my_hole_card)