def do_test(weights, test, min_game, current_iteration, current_weight):

    new_weights = copy.deepcopy(weights)
    weight = test['weight']
    value = test['testValue']
    new_weights[weight] = value

    config = setup_config(max_round=max_round,
                          initial_stack=initial_stack,
                          small_blind_amount=smallblind_amount)
    # Register players
    config.register_player(name="agent1",
                           algorithm=OurPlayerNoMwu(new_weights))
    config.register_player(name="agent2", algorithm=OurPlayerNoMwu(weights))

    # Start playing num_game games
    agent1_pot = 0
    agent2_pot = 0

    # init count of games and threads
    return_queue = Queue()
    num_threads = multiprocessing.cpu_count() - 1
    games = min_game + num_threads - (min_game % num_threads)
    num_games_ran = 0
    while games > 0:
        # start a thread for each remaining game up to threadcount limit
        threads = []
        game_runners = []
        for i in range(0, num_threads):
            if games > 0:
                games -= 1
                num_games_ran += 1
                game_runners += [GameRunner(config, return_queue)]
                threads += [Process(target=game_runners[i].start_game)]
                threads[i].start()

        # join all threads
        for i in range(0, len(threads)):
            threads[i].join()

        # process returned data
        while not return_queue.empty():
            result = return_queue.get()
            agent1_pot = agent1_pot + result[0]
            agent2_pot = agent2_pot + result[1]

        status = get_status()
        if status['iteration'] != current_iteration or status[
                'weight'] != current_weight:
            print(
                "Current iteration/weight changed. Stopping current training.")
            games = 0

    performance = (agent1_pot - agent2_pot) / num_games_ran
    return performance
def do_benchmark(weights1, weights2, min_game):
    config = setup_config(max_round=max_round,
                          initial_stack=initial_stack,
                          small_blind_amount=smallblind_amount)
    # Register players
    config.register_player(name="agent1", algorithm=OurPlayerNoMwu(weights1))
    config.register_player(name="agent2", algorithm=OurPlayerNoMwu(weights2))

    # Start playing num_game games
    agent1_pot = 0
    agent2_pot = 0

    # init count of games and threads
    return_queue = Queue()
    num_threads = multiprocessing.cpu_count() - 1
    games = min_game + num_threads - (min_game % num_threads)
    num_games_ran = 0
    while games > 0:
        # start a thread for each remaining game up to threadcount limit
        threads = []
        game_runners = []
        for i in range(0, num_threads):
            if games > 0:
                games -= 1
                num_games_ran += 1
                game_runners += [GameRunner(config, return_queue)]
                threads += [Process(target=game_runners[i].start_game)]
                threads[i].start()

        # join all threads
        for i in range(0, len(threads)):
            threads[i].join()

        # process returned data
        while not return_queue.empty():
            result = return_queue.get()
            agent1_pot = agent1_pot + result[0]
            agent2_pot = agent2_pot + result[1]

    performance = (agent1_pot - agent2_pot) / num_games_ran
    return performance
Exemple #3
0
def check_perf(weights, last_net_winnings):
    # Init to play 500 games of 1000 rounds
    num_game = 1
    max_round = 10
    initial_stack = 10000
    smallblind_amount = 0

    # Init pot of players
    agent1_pot = 0
    agent2_pot = 0

    # Setting configuration
    config = setup_config(max_round=max_round,
                          initial_stack=initial_stack,
                          small_blind_amount=smallblind_amount)

    # Register players
    config.register_player(name="agent_name1", algorithm=OurPlayerNoMwu())
    config.register_player(name="agent_name2", algorithm=BootStrapBot())

    # Configuring other weights
    config.players_info[0]['algorithm'].w = weights

    # Start playing num_game games
    for game in range(1, num_game + 1):
        game_result = start_poker(config, verbose=0)
        agent1_pot = agent1_pot + game_result[0]['players'][0]['stack']
        agent2_pot = agent2_pot + game_result[0]['players'][1]['stack']

    curr_net_winnings = agent1_pot - agent2_pot
    performance_gain = 0
    if not last_net_winnings == 0:
        performance_gain = curr_net_winnings - last_net_winnings / float(
            last_net_winnings)
    print("Performance changed by %d percent" % (performance_gain * 100))

    return curr_net_winnings
Exemple #4
0
    results.to_csv("outcome data")

    print("\n After playing {} games of {} rounds, the results are: ".format(
        num_game, max_round))
    print("\n " + agent_name1 + "'s final pot: ", agent1_pot)
    print("\n " + agent_name2 + "'s final pot: ", agent2_pot)

    if (agent1_pot < agent2_pot):
        print("\n Congratulations! " + agent_name2 + " has won.")
    elif (agent1_pot > agent2_pot):
        print("\n Congratulations! " + agent_name1 + " has won.")
    else:
        print("\n It's a draw!")


if __name__ == '__main__':
    my_name = "ME"
    my_agent = MY_PLAYER
    MY_PLAYER.name = my_name

    players = {"no_mwu": OurPlayerNoMwu()}
    for name, base_agent in players.items():
        base_agent.name = name
        start = time.time()

        testperf(my_name, my_agent, name, base_agent)
        end = time.time()

        print("\n Time taken to play: %.4f seconds" % (end - start))
def testperf(agent_name1, agent1, agent_name2, agent2):

    # Init to play 500 games of 1000 rounds
    num_game = 100
    max_round = 1000
    initial_stack = 10000
    smallblind_amount = 20

    # Init pot of players
    agent1_pot = 0
    agent2_pot = 0

    # Setting configuration
    config = setup_config(max_round=max_round,
                          initial_stack=initial_stack,
                          small_blind_amount=smallblind_amount)

    # Register players
    config.register_player(name=agent_name1, algorithm=OurPlayerNoMwu())
    #config.register_player(name=agent_name2, algorithm=OurPlayerNoMwu([0.63,0.83,0.4,1,0.75,0.49,0.38,0.87,0.21,0,0,0.14,0.44]))
    config.register_player(name=agent_name2, algorithm=Group01Player())
    # config.register_player(name=agent_name1, algorithm=agent1())
    # config.register_player(name=agent_name2, algorithm=agent2())

    return_queue = Queue()
    num_threads = multiprocessing.cpu_count() - 1
    games = num_game
    num_games_ran = 0
    while games > 0:
        # start a thread for each remaining game up to threadcount limit
        threads = []
        game_runners = []
        for i in range(0, num_threads):
            if games > 0:
                games -= 1
                num_games_ran += 1
                game_runners += [GameRunner(config, return_queue)]
                threads += [Process(target=game_runners[i].start_game)]
                print("Game Number " + str(num_games_ran))
                threads[i].start()

        # join all threads
        for i in range(0, len(threads)):
            threads[i].join()

        # process returned data
        while not return_queue.empty():
            result = return_queue.get()
            agent1_pot = agent1_pot + result[0]
            agent2_pot = agent2_pot + result[1]
            print(str(agent1_pot) + " vs " + str(agent2_pot))

    print("\n After playing {} games of {} rounds, the results are: ".format(
        num_game, max_round))
    # print("\n Agent 1's final pot: ", agent1_pot)
    print("\n " + agent_name1 + "'s final pot: ", agent1_pot)
    print("\n " + agent_name2 + "'s final pot: ", agent2_pot)

    # print("\n ", game_result)
    # print("\n Random player's final stack: ", game_result['players'][0]['stack'])
    # print("\n " + agent_name + "'s final stack: ", game_result['players'][1]['stack'])

    if (agent1_pot < agent2_pot):
        print("\n Congratulations! " + agent_name2 + " has won.")
    elif (agent1_pot > agent2_pot):
        print("\n Congratulations! " + agent_name1 + " has won.")
        # print("\n Random Player has won!")
    else:
        Print("\n It's a draw!")
Exemple #6
0
def train_bots(agent_name1, agent1, agent_name2, agent2, initial_weights):

    # Init to play 500 games of 1000 rounds
    num_game = 10
    max_round = 100
    initial_stack = 10000
    smallblind_amount = 20

    trial_weights = []
    initial_weights = initial_weights
    weight_count = len(initial_weights)
    partition_interval_number = 100
    last_net_winnings = 0
    offset = 5  # Use this to adjust which weight starts being trained first. E.g. offset = 6 implies w6 trained first.

    # Partitioning step to get the weights to be tested
    for p in range(0, partition_interval_number + 1):
        trial_weights.append(p * float(1) / partition_interval_number)

    for i in range(weight_count):
        updated_index = (i + offset) % weight_count
        print("Testing weight %d ..." % (updated_index))
        print("Current w%d is now: %d" %
              (updated_index, initial_weights[updated_index]))
        print("Current weights are now %s" % (str(initial_weights)))

        current_best = (float("-inf"), 0)

        for weight in trial_weights:
            # Init pot of players
            agent1_pot = 0
            agent2_pot = 0

            # Setting configuration
            config = setup_config(max_round=max_round,
                                  initial_stack=initial_stack,
                                  small_blind_amount=smallblind_amount)

            # Register players
            config.register_player(name=agent_name1,
                                   algorithm=OurPlayerNoMwu())
            config.register_player(name=agent_name2, algorithm=BootStrapBot())
            # config.register_player(name=agent_name1, algorithm=agent1())
            # config.register_player(name=agent_name2, algorithm=agent2())

            # Configuring other weights
            config.players_info[0]['algorithm'].w = initial_weights

            # Set trainedplayer weights
            config.players_info[0]['algorithm'].w[updated_index] = weight

            # Start playing num_game games

            # init count of games and threads
            game_queue = Queue()
            for game in range(1, num_game + 1):
                game_queue.put(game)
            return_queue = Queue()
            num_threads = multiprocessing.cpu_count() - 1
            while not game_queue.empty():
                # start a thread for each remaining game up to threadcount limit
                threads = []
                game_runners = []
                for i in range(0, num_threads):
                    if not game_queue.empty():
                        game = game_queue.get()
                        game_runners += [GameRunner(config, return_queue)]
                        threads += [Process(target=game_runners[i].start_game)]
                        threads[i].start()

                # join all threads
                for i in range(0, len(threads)):
                    threads[i].join()

                # process returned data
                while not return_queue.empty():
                    result = return_queue.get()
                    agent1_pot = agent1_pot + result[0]
                    agent2_pot = agent2_pot + result[1]

            if agent1_pot - agent2_pot > current_best[0]:
                current_best = (agent1_pot - agent2_pot, weight)
                print("Current best w%d is now: %s" %
                      (updated_index, str(current_best)))

        initial_weights[updated_index] = current_best[1]

    last_net_winnings = check_perf(initial_weights, last_net_winnings)
    return initial_weights