示例#1
0
def main():
    # Parse command line arguments into hole cards and create deck
    (hole_cards, num_iterations,
                    exact, given_board, deck, num_players) = holdem_argparser.parse_args()
    assigned_num_players, unassigned_num_players = len(hole_cards), num_players - len(hole_cards)
    if(num_players == 0):
        num_players = len(hole_cards)
    # Create results data structures which tracks results of comparisons
    # 1) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    # 2) winner_list: number of times each player wins the given round
    # 3) result_list: list of the best possible poker hand for each pair of
    #    hole cards for a given board
    result_list, winner_list = [None] * num_players, [0] * (num_players + 1)
    result_histograms = []
    for player in xrange(num_players):
        result_histograms.append([0] * 10)
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board == None else len(given_board)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    generate_boards = holdem_functions.generate_random_boards

    # Run simulations
    for generated_cards in generate_boards(deck, num_iterations, board_length, unassigned_num_players):
        # Assign cards to unassigned players
        for i in range(unassigned_num_players):
            hole_cards.append((generated_cards[0], generated_cards[1]))
            del generated_cards[0:2]

        hole_card = tuple(hole_cards)
        remaining_board = generated_cards
        # Generate a new board
        if given_board:
            board = given_board[:]
            board.extend(remaining_board)
        else:
            board = remaining_board
        # Find the best possible poker hand given the created board and the
        # hole cards and save them in the results data structures
        (suit_histogram,histogram, max_suit) = holdem_functions.preprocess_board(board)
        for index, hole_card in enumerate(hole_cards):
            result_list[index] = holdem_functions.detect_hand(hole_card, board,suit_histogram, histogram, max_suit)
        # Find the winner of the hand and tabulate results
        winner_index = holdem_functions.compare_hands(result_list)
        winner_list[winner_index] += 1
        if winner_index != 0:
            print "Winner:" + str(winner_index) + " With cards " + str(hole_cards[winner_index - 1][0]) + " | "+ str(hole_cards[winner_index - 1][1])
        # Increment what hand each player made
        for index, result in enumerate(result_list):
            result_histograms[index][result[0]] += 1

        # Dump appended random hole_cards
        hole_cards = list(hole_cards)
        for i in range(unassigned_num_players):
            del hole_cards[assigned_num_players]
    holdem_functions.print_results(hole_cards, winner_list, result_histograms)
示例#2
0
def run_simulation(hole_cards, num, exact, given_board, deck, verbose):
    num_players = len(hole_cards)
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board is None else len(given_board)
    # Create data structures to manage multiple processes:
    # 1) winner_list: number of times each player wins a hand
    # 2) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    num_processes = multiprocessing.cpu_count()
    num_poker_hands = len(holdem_functions.hand_rankings)
    num_histograms = num_processes * num_players * num_poker_hands
    winner_list = multiprocessing.Array('i', num_processes * (num_players + 1))
    result_histograms = multiprocessing.Array('i', num_histograms)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if exact or given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    if (None, None) in hole_cards:
        hole_cards_list = list(hole_cards)
        unknown_index = hole_cards.index((None, None))
        deck_list = list(deck)
        pool = multiprocessing.Pool(processes=num_processes,
                                    initializer=unknown_simulation_init,
                                    initargs=(hole_cards_list, unknown_index,
                                              deck_list, generate_boards, num,
                                              board_length, given_board,
                                              winner_list, result_histograms))
        pool.map(unknown_simulation,
                 holdem_functions.generate_hole_cards(deck))
    else:
        find_winner(generate_boards, deck, hole_cards, num, board_length,
                    given_board, winner_list, result_histograms)
    # Go through each parallel data structure and aggregate results
    combined_winner_list, combined_histograms = [0] * (num_players + 1), []
    for _ in range(num_players):
        combined_histograms.append([0] * len(holdem_functions.hand_rankings))
    for index, element in enumerate(winner_list):
        combined_winner_list[index % (num_players + 1)] += element
    for index, element in enumerate(result_histograms):
        combined_histograms[(index / num_poker_hands) %
                            num_players][(index % num_poker_hands)] += element
    if verbose:
        holdem_functions.print_results(hole_cards, combined_winner_list,
                                       combined_histograms)
    return holdem_functions.find_winning_percentage(combined_winner_list)
def run_simulation(hole_cards, num, exact, given_board, deck, verbose):
    num_players = len(hole_cards)
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board is None else len(given_board)
    # Create data structures to manage multiple processes:
    # 1) winner_list: number of times each player wins a hand
    # 2) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    num_processes = multiprocessing.cpu_count()
    num_poker_hands = len(holdem_functions.hand_rankings)
    num_histograms = num_processes * num_players * num_poker_hands
    winner_list = multiprocessing.Array('i', num_processes * (num_players + 1))
    result_histograms = multiprocessing.Array('i', num_histograms)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if exact or given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    if (None, None) in hole_cards:
        hole_cards_list = list(hole_cards)
        unknown_index = hole_cards.index((None, None))
        deck_list = list(deck)
        pool = multiprocessing.Pool(processes=num_processes,
                                    initializer=unknown_simulation_init,
                                    initargs=(hole_cards_list, unknown_index,
                                              deck_list, generate_boards,
                                              num, board_length, given_board,
                                              winner_list, result_histograms))
        pool.map(unknown_simulation, holdem_functions.generate_hole_cards(deck))
    else:
        find_winner(generate_boards, deck, hole_cards, num, board_length,
                    given_board, winner_list, result_histograms)
    # Go through each parallel data structure and aggregate results
    combined_winner_list, combined_histograms = [0] * (num_players + 1), []
    for _ in xrange(num_players):
        combined_histograms.append([0] * len(holdem_functions.hand_rankings))
    for index, element in enumerate(winner_list):
        combined_winner_list[index % (num_players + 1)] += element
    for index, element in enumerate(result_histograms):
        combined_histograms[(index / num_poker_hands) % num_players][
            (index % num_poker_hands)] += element
    if verbose:
        holdem_functions.print_results(hole_cards, combined_winner_list,
                                       combined_histograms)
    return holdem_functions.find_winning_percentage(combined_winner_list)
示例#4
0
def run_simulation(hole_cards, num, exact, given_board, deck, verbose, pad_opp=True):
    num_players = len(hole_cards)
    if pad_opp:
        num_players += 1
    #import pdb
    #pdb.set_trace()
    # Create results data structures which track results of comparisons
    # 1) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    # 2) winner_list: number of times each player wins the given round
    # 3) result_list: list of the best possible poker hand for each pair of
    #    hole cards for a given board
    result_histograms, winner_list = [], [0] * (num_players + 1)
    for _ in range(num_players):
        result_histograms.append([0] * len(holdem_functions.hand_rankings))
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board is None else len(given_board)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if exact or given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    if (None, None) in hole_cards:
        hole_cards_list = list(hole_cards)
        unknown_index = hole_cards.index((None, None))
        for filler_hole_cards in holdem_functions.generate_hole_cards(deck):
            hole_cards_list[unknown_index] = filler_hole_cards
            deck_list = list(deck)
            deck_list.remove(filler_hole_cards[0])
            deck_list.remove(filler_hole_cards[1])
            holdem_functions.find_winner(generate_boards, tuple(deck_list),
                                         tuple(hole_cards_list), num,
                                         board_length, given_board, winner_list,
                                         result_histograms)
    else:
        holdem_functions.find_winner(generate_boards, deck, hole_cards, num,
                                     board_length, given_board, winner_list,
                                     result_histograms,pad_opp = pad_opp)
    if verbose:
        holdem_functions.print_results(hole_cards, winner_list,
                                       result_histograms)
    return holdem_functions.return_results(hole_cards, winner_list,
                                       result_histograms,pad_opp = pad_opp, board = given_board )
示例#5
0
def _parallel_evaluate(hole_cards,
                       deck,
                       given_board=None,
                       num_iterations=1000):
    num_players = len(hole_cards)
    # Create data structures to manage multiple processes:
    # 1) winner_list: number of times each player wins a hand
    # 2) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    num_processes = multiprocessing.cpu_count()
    winner_list = multiprocessing.Array('i', num_processes * (num_players + 1))
    result_histograms = multiprocessing.Array('i',
                                              num_processes * num_players * 10)
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board == None else len(given_board)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    # Create threadpool and use it to perform hand detection over all boards
    pool = multiprocessing.Pool(processes=num_processes,
                                initializer=_simulation_init,
                                initargs=(given_board, hole_cards, winner_list,
                                          result_histograms, num_players))
    pool.map(_simulation, generate_boards(deck, num_iterations, board_length))
    # Tallying and printing results
    combined_winner_list, combined_histograms = [0] * (num_players + 1), []
    for player in xrange(num_players):
        combined_histograms.append([0] * 10)
    # Go through each parallel data structure and aggregate results
    for index, element in enumerate(winner_list):
        combined_winner_list[index % (num_players + 1)] += element
    for index, element in enumerate(result_histograms):
        combined_histograms[(index // 10) % num_players][index % 10] += element
    # Print results
    holdem_functions.print_results(hole_cards, combined_winner_list,
                                   combined_histograms)

    float_iterations = float(sum(winner_list))
    return winner_list[1] / float_iterations, winner_list[2] / float_iterations
def main():
    # Parse command line arguments into hole cards and create deck
    (hole_cards, num_iterations,
                    exact, given_board, deck) = holdem_argparser.parse_args()
    num_players = len(hole_cards)
    # Create data structures to manage multiple processes:
    # 1) winner_list: number of times each player wins a hand
    # 2) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    num_processes = multiprocessing.cpu_count()
    winner_list = multiprocessing.Array('i', num_processes * (num_players + 1))
    result_histograms = multiprocessing.Array('i',
                                              num_processes * num_players * 10)
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board == None else len(given_board)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if exact or given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    # Create threadpool and use it to perform hand detection over all boards
    pool = multiprocessing.Pool(processes=num_processes,
                                initializer=simulation_init,
                                initargs=(given_board, hole_cards, winner_list,
                                          result_histograms, num_players))
    pool.map(simulation, generate_boards(deck, num_iterations, board_length))
    # Tallying and printing results
    combined_winner_list, combined_histograms = [0] * (num_players + 1), []
    for player in xrange(num_players):
        combined_histograms.append([0] * 10)
    # Go through each parallel data structure and aggregate results
    for index, element in enumerate(winner_list):
        combined_winner_list[index % (num_players + 1)] += element
    for index, element in enumerate(result_histograms):
        combined_histograms[(index // 10) % num_players][index % 10] += element
    # Print results
    holdem_functions.print_results(hole_cards, combined_winner_list,
                                                        combined_histograms)
示例#7
0
def run_simulation(hole_cards, num, exact, given_board, deck, verbose):
    num_players = len(hole_cards)
    # Create results data structures which track results of comparisons
    # 1) result_histograms: a list for each player that shows the number of
    #    times each type of poker hand (e.g. flush, straight) was gotten
    # 2) winner_list: number of times each player wins the given round
    # 3) result_list: list of the best possible poker hand for each pair of
    #    hole cards for a given board
    result_histograms, winner_list = [], [0] * (num_players + 1)
    for _ in xrange(num_players):
        result_histograms.append([0] * len(holdem_functions.hand_rankings))
    # Choose whether we're running a Monte Carlo or exhaustive simulation
    board_length = 0 if given_board is None else len(given_board)
    # When a board is given, exact calculation is much faster than Monte Carlo
    # simulation, so default to exact if a board is given
    if exact or given_board is not None:
        generate_boards = holdem_functions.generate_exhaustive_boards
    else:
        generate_boards = holdem_functions.generate_random_boards
    if (None, None) in hole_cards:
        hole_cards_list = list(hole_cards)
        unknown_index = hole_cards.index((None, None))
        for filler_hole_cards in holdem_functions.generate_hole_cards(deck):
            hole_cards_list[unknown_index] = filler_hole_cards
            deck_list = list(deck)
            deck_list.remove(filler_hole_cards[0])
            deck_list.remove(filler_hole_cards[1])
            holdem_functions.find_winner(generate_boards, tuple(deck_list),
                                         tuple(hole_cards_list), num,
                                         board_length, given_board, winner_list,
                                         result_histograms)
    else:
        holdem_functions.find_winner(generate_boards, deck, hole_cards, num,
                                     board_length, given_board, winner_list,
                                     result_histograms)
    if verbose:
        holdem_functions.print_results(hole_cards, winner_list,
                                       result_histograms)
    return holdem_functions.find_winning_percentage(winner_list)