def batch_run_classroom(j):
    """
    Helper function.
    The function containing the batchrunner for the classroom simulations.
    :param j: j is the setup type. eg j=2 => setup type [2,2,2]
    :return: Returns lists of average number of infected for every timestep.
    Saves the raw data from the datacollector into a csv file.
    """
    batch_run = BatchRunner(covid_Model,
        variable_parameters={"N": range(24,25,1)},
        fixed_parameters={"width": 11, "height": 11, "setUpType": [j]},
        iterations=antal_iterationer,
        max_steps=antal_tidsskridt_per_simulation)
    batch_run.run_all() #run batchrunner
    ordered_df = batch_run.get_collector_model()
    data_list = list(ordered_df.values()) #saves batchrunner data in list
    for i in range(len(data_list)):
        data_list[i]['Iteration'] = i+1
    pd.concat(data_list).to_csv('csvdata/classroom_test'+str(j)+'.csv')


    num_of_infected = [0]*(antal_tidsskridt_per_simulation+1)
    for i in range(len(data_list)):
        temp_list = []
        for k in range(len(data_list[i]["infected"])):
            num_of_infected[k]+=data_list[i]["infected"][k]
            temp_list.append(data_list[i]["infected"][k])
    num_of_infected = [number / antal_iterationer for number in num_of_infected] #avg number of infected
    return num_of_infected
Esempio n. 2
0
def plot_busy(fix_par, var_par, model, iter, steps):
    batch_run = BatchRunner(
        model,
        variable_parameters=var_par,
        fixed_parameters=fix_par,
        iterations=iter,
        max_steps=steps,
        model_reporters={"busy": lambda m: busy_employees(m)},
    )
    batch_run.run_all()  #run batchrunner

    data_list = list(batch_run.get_collector_model().values()
                     )  # saves batchrunner data in a list

    sum_of_busy = [0] * (steps + 1)  #makes list for y-values
    for i in range(len(data_list)):
        for j in range(len(data_list[i]["busy"])):
            sum_of_busy[j] += data_list[i]["busy"][
                j]  #at the right index add number of infected

    sum_of_infected = [number / iter for number in sum_of_busy
                       ]  #divide list with number of iterations to get avg
    time = [i
            for i in range(0, steps + 1)]  #makes list of x-values for plotting
    plt.plot(time, sum_of_busy, label='# busy employees', color='Green')
    #  plt.plot(time, num_of_susceptible, label= 'Number of Susceptible', color = 'Green', linestyle='dashed')
    plt.xlabel('Tidsskridt')
    plt.ylabel('Mean busy employees')
    plt.title('ved %s simulationer' % iter)
    plt.legend()
    return
def batch_run(j):
    """
     Helper function.
    The function containing the batchrunner for the full simulations.
    :param j: j is the setup type. eg j=2 => setup type [2,2,2]
    :return: Returns lists of average number of Infected, Susceptible, Recovered for every timestep.
    Saves the raw data from the datacollector into a csv file.
    """
    batch_run = BatchRunner(covid_Model,
                            variable_parameters={"N": range(24,25,1)},
                            fixed_parameters={"width": 26, "height": 38, "setUpType": [j,j,j]},
                            iterations=antal_iterationer,
                            max_steps=antal_tidsskridt_per_simulation,
                            model_reporters={"infected": lambda m: get_infected_count(m)})
    batch_run.run_all() #run batchrunner

    ordered_df = batch_run.get_collector_model()
    data_list = list(ordered_df.values()) #saves batchrunner data in list
    for i in range(len(data_list)):
        data_list[i]['Iteration'] = i+1
    pd.concat(data_list).to_csv('csvdata/rawdata'+str(j)+'.csv')
    num_of_infected = [0]*(antal_tidsskridt_per_simulation+1)
    num_of_susceptible = [0]*(antal_tidsskridt_per_simulation+1)
    num_of_recovered = [0]*(antal_tidsskridt_per_simulation+1)

    for i in range(len(data_list)):
        for j in range(len(data_list[i]["infected"])):
            num_of_infected[j]+=data_list[i]["infected"][j]
            num_of_susceptible[j] += data_list[i]["Agent_count"][j]-(data_list[i]["infected"][j]+data_list[i]["recovered"][j]+number_of_vaccinated) #number of susceptible at each time step
            num_of_recovered[j] += data_list[i]["recovered"][j]
    num_of_infected =[number / antal_iterationer for number in num_of_infected] #avg number of infected
    num_of_susceptible = [number / antal_iterationer for number in num_of_susceptible]
    num_of_recovered = [number / antal_iterationer for number in num_of_recovered]

    return num_of_infected, num_of_susceptible, num_of_recovered
def experiment_1(replicates=40, max_steps=200, graph_type="None"):
    """
    Experiment 1 - Run simulations of civil violence with network model.
    Function to generates data which are used for comparison of network topology influence on civil violence model.
    """
    path = 'archives/saved_data_experiment_1_{0}_{1}'.format(
        int(time.time()), graph_type)

    configuration = read_configuration()
    model_params = {}
    model_params.update(
        configuration
    )  # Overwritten user parameters don't appear in the graphic interface
    model_params.update({'seed': None})
    model_params['graph_type'] = graph_type
    model_params['max_iter'] = max_steps

    batch = BatchRunner(
        CivilViolenceModel,
        max_steps=max_steps,
        iterations=replicates,
        fixed_parameters=model_params,
        model_reporters={
            'All_Data': lambda m: m.datacollector,
            "QUIESCENT": lambda m: m.count_type_citizens("QUIESCENT"),
            "ACTIVE": lambda m: m.count_type_citizens("ACTIVE"),
            "JAILED": lambda m: m.count_type_citizens("JAILED"),
            "OUTBREAKS": lambda m: m.outbreaks
        },  # attempt all
        display_progress=True)

    batch.run_all()

    batch_df = batch.get_model_vars_dataframe()
    batch_df = batch_df.drop('All_Data', axis=1)

    data = batch_df
    run_data = batch.get_collector_model()

    with open(path, 'ab') as f:
        np.save(f, data)

    run_path = path + '_run'
    with open(run_path, 'ab') as f:
        np.save(f, run_data)
Esempio n. 5
0
# one_agent_wealth = agent_wealth.xs(14, level="AgentID")
# one_agent_wealth.Wealth.plot()
# plt.show()

fixed_params = {"width": 10, "height": 10}
variable_params = {"N": range(10, 500, 10)}

batch_run = BatchRunner(ParkingModel,
                        variable_params,
                        fixed_params,
                        iterations=5,
                        max_steps=100,
                        model_reporters={"Gini": compute_gini})
batch_run.run_all()

run_data = batch_run.get_model_vars_dataframe()
run_data.head()
plt.scatter(run_data.N, run_data.Gini)
plt.show()

#Get the Agent DataCollection
data_collector_agents = batch_run.get_collector_agents()

print(data_collector_agents[(10, 2)])

#Get the Model DataCollection.

data_collector_model = batch_run.get_collector_model()

print(data_collector_model[(10, 1)])