src/quagen/simulator.py
def simulate(
    setup_callback,
    number_games=DEFAULT_SIMULATION_COUNT,
    concurrency=DEFAULT_SIMULATION_CONCURRENCY,
):
    """
    User entry point for simulating games.

    Args:
        setup_callback (function): Hook called before a game starts simulating
            to give the user a chance to tweak the game settings and add AI
            players. Called with parameters setup_callback(simluation).
        number_games (int): Optional number of games to simulate.
        concurrency (int): Optional number of concurrent simulations.

    Returns:
        (dict) Tallied scores for each AI player

    """

    logging.info(f"Simulating {number_games} with concurrency of {concurrency}")
    simulations = []
    results = {}

    # Create all our simulations and call the user's hook for further setup
    for i in range(number_games):  # pylint: disable=unused-variable
        simulation = Simulation()
        setup_callback(simulation)
        simulations.append(simulation)

    # Run through the simulations concurrently
    with Pool(concurrency) as pool:
        for simulation in pool.map(run_simulation, simulations):

            scores = simulation.game.scores
            leaders = simulation.game.get_leaders()

            # Keep a record of every score for every game for each player
            for color in range(len(scores)):  # pylint: disable=consider-using-enumerate
                if color not in results.keys():
                    results[color] = {"wins": 0, "scores": []}

                results[color]["scores"].append(scores[color]["controlled"])

            # Add a win to the player's tally for outright wins (ignore ties).
            if len(leaders) == 1:
                results[leaders[0][0]]["wins"] += 1

    for tally in results.values():
        tally["mean"] = statistics.mean(tally["scores"])
        tally["median"] = statistics.median(tally["scores"])
        tally["min"] = min(tally["scores"])
        tally["max"] = max(tally["scores"])
        del tally["scores"]

    logging.info(f"Simulation complete")
    return results

    User entry point for simulating games.

    Args:
        setup_callback (function): Hook called before a game starts simulating
            to give the user a chance to tweak the game settings and add AI
            players. Called with parameters setup_callback(simluation).
        number_games (int): Optional number of games to simulate.
        concurrency (int): Optional number of concurrent simulations.

    Returns:
        (dict) Tallied scores for each AI player

    
User entry point for simulating games.
User entry point for simulating games .
def simulate(
    setup_callback,
    number_games=DEFAULT_SIMULATION_COUNT,
    concurrency=DEFAULT_SIMULATION_CONCURRENCY,
):
    logging.info(f"Simulating {number_games} with concurrency of {concurrency}")
    simulations = []
    results = {}
    for i in range(number_games):  
        simulation = Simulation()
        setup_callback(simulation)
        simulations.append(simulation)
    with Pool(concurrency) as pool:
        for simulation in pool.map(run_simulation, simulations):
            scores = simulation.game.scores
            leaders = simulation.game.get_leaders()
            for color in range(len(scores)):  
                if color not in results.keys():
                    results[color] = {"wins": 0, "scores": []}
                results[color]["scores"].append(scores[color]["controlled"])
            if len(leaders) == 1:
                results[leaders[0][0]]["wins"] += 1
    for tally in results.values():
        tally["mean"] = statistics.mean(tally["scores"])
        tally["median"] = statistics.median(tally["scores"])
        tally["min"] = min(tally["scores"])
        tally["max"] = max(tally["scores"])
        del tally["scores"]
    logging.info(f"Simulation complete")
    return results
def simulate ( setup_callback , number_games = DEFAULT_SIMULATION_COUNT , concurrency = DEFAULT_SIMULATION_CONCURRENCY , ) : logging . info ( f"Simulating {number_games} with concurrency of {concurrency}" ) simulations = [ ] results = { } for i in range ( number_games ) : simulation = Simulation ( ) setup_callback ( simulation ) simulations . append ( simulation ) with Pool ( concurrency ) as pool : for simulation in pool . map ( run_simulation , simulations ) : scores = simulation . game . scores leaders = simulation . game . get_leaders ( ) for color in range ( len ( scores ) ) : if color not in results . keys ( ) : results [ color ] = { "wins" : 0 , "scores" : [ ] } results [ color ] [ "scores" ] . append ( scores [ color ] [ "controlled" ] ) if len ( leaders ) == 1 : results [ leaders [ 0 ] [ 0 ] ] [ "wins" ] += 1 for tally in results . values ( ) : tally [ "mean" ] = statistics . mean ( tally [ "scores" ] ) tally [ "median" ] = statistics . median ( tally [ "scores" ] ) tally [ "min" ] = min ( tally [ "scores" ] ) tally [ "max" ] = max ( tally [ "scores" ] ) del tally [ "scores" ] logging . info ( f"Simulation complete" ) return results
User entry point for simulating games.
User entry point for simulating games .
"""
    User entry point for simulating games.

    Args:
        setup_callback (function): Hook called before a game starts simulating
            to give the user a chance to tweak the game settings and add AI
            players. Called with parameters setup_callback(simluation).
        number_games (int): Optional number of games to simulate.
        concurrency (int): Optional number of concurrent simulations.

    Returns:
        (dict) Tallied scores for each AI player

    """ # Create all our simulations and call the user's hook for further setup # pylint: disable=unused-variable # Run through the simulations concurrently # Keep a record of every score for every game for each player # pylint: disable=consider-using-enumerate # Add a win to the player's tally for outright wins (ignore ties).
(dict) Tallied scores for each AI player
( dict ) Tallied scores for each AI player
setup_callback
Hook called before a game starts simulating
to give the user a chance to tweak the game settings and add AI
players. Called with parameters setup_callback(simluation).
Hook called before a game starts simulating to give the user a chance to tweak the game settings and add AI players . Called with parameters setup_callback ( simluation ) .
number_games
Optional number of games to simulate.
Optional number of games to simulate .
concurrency
Optional number of concurrent simulations.
Optional number of concurrent simulations .