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 _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 hand_strength_with_belief(my_cards, community_cards, belief): overall_strength = 0 nb_simulation = 100 num_cards = len(belief['Cards']) for i in range(num_cards): opp_cards = belief['Cards'][i][0] prob = belief['Probability'][i] ahead = 0 behind = 0 tied = 0 for i in range(nb_simulation): community_cards_ = _fill_community_card( community_cards, used_card=my_cards + community_cards + opp_cards) opp_stength = HandEvaluator.eval_hand(opp_cards, community_cards_) my_strength = HandEvaluator.eval_hand(my_cards, community_cards_) if (my_strength > opp_stength): ahead = ahead + 1 elif (my_strength < opp_stength): behind = behind + 1 else: tied = tied + 1 overall_strength += prob * (ahead + tied / 2) / (ahead + tied + behind) return overall_strength
def simulation_with_opponent(cards, community_cards, opponent_cards): community_card = _fill_community_card(community_cards, used_card=cards + community_cards) opponents_hole = opponent_cards opponents_score = HandEvaluator.eval_hand(opponents_hole, community_cards) my_score = HandEvaluator.eval_hand(cards, community_cards) return 1 if my_score >= (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 win_rate(cards, community_cards, opp_cards): nb_simulation = 100 ahead = 0 behind = 0 tied = 0 for i in range(nb_simulation): community_cards_ = _fill_community_card(community_cards, used_card=cards + community_cards + opp_cards) opp_stength = HandEvaluator.eval_hand(opp_cards, community_cards_) my_strength = HandEvaluator.eval_hand(cards, community_cards_) if (my_strength > opp_stength): ahead = ahead + 1 elif (my_strength < opp_stength): behind = behind + 1 else: tied = tied + 1 return float((ahead + tied / 2)) / (ahead + tied + behind)