Пример #1
0
def run_simulation(setup, savefile):
    """
    A helper function that will run the simulation.
    :param setup: An object containing all variables that will be used in the simulation.
    :param savefile: The file where we want to save the results to.
    :return: averaged payoffs per generation, number of times the target was reached per generation 
        and the averaged contributions per round per generation (GxR matrix)
    """
    generations_avg_payoffs = []
    generations_targets_reached = []
    generations_rounds_contributions_counts = []
    generations_behaviors_counts = []

    generation = Generation(setup)
    for i in range(setup.num_generations):
        setup.risk += setup.risk_cont
        print("Generation " + str(i))
        targets_reached, avg_payoff, rounds_contributions_counts, behaviors_counts = generation.play(
        )
        generations_avg_payoffs.append(avg_payoff)
        generations_targets_reached.append(np.sum(targets_reached))
        generations_rounds_contributions_counts.append(
            rounds_contributions_counts.tolist())
        generations_behaviors_counts.append(behaviors_counts.tolist())
        print("Targets reached: ", np.sum(targets_reached))
        print("With an average payoff of " + str(avg_payoff))
        print("Behavior counts: ")
        print(behaviors_counts)
        print("Rounds contributions counts: ")
        print(rounds_contributions_counts)
        generation.evolve()
        # If we are investigating the robustness of strategies we return the number of generations it has survived.
        if setup.strategy is not None:
            if generation.frequency < setup.population_size / 2:
                return i

    # If we are investigating the robustness of strategies we return the number of generations it has survived.
    if setup.strategy is not None:
        return setup.num_generations

    print(generations_targets_reached)

    # We place everything inside a dictionary and then in a data frame.
    results = {
        "avg_payoffs": generations_avg_payoffs,
        "targets_reached": generations_targets_reached,
        "rounds_contributions_counts": generations_rounds_contributions_counts,
        "behaviors_counts": generations_behaviors_counts
    }
    results_df = pd.DataFrame(results)

    # We write the results to a file.
    results_df.to_csv(savefile,
                      sep=',',
                      mode='w',
                      header=True,
                      index=False,
                      encoding="ascii")