From f8d8345d5a7573a3f5f73599c666fd5419348415 Mon Sep 17 00:00:00 2001 From: Alberto Pascual Date: Wed, 26 Apr 2017 11:17:50 +0200 Subject: [PATCH] requirements added for faster install, settings separated in JSON file, now is possible to run different agent simulations at a time --- requirements.txt | 5 ++ settings.py | 9 +++ simulation_settings.json | 53 ++++++++++++ soil.py | 170 ++++++++++++++++++++++----------------- 4 files changed, 165 insertions(+), 72 deletions(-) create mode 100644 requirements.txt create mode 100644 simulation_settings.json diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0b541b4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +nxsim +simpy +networkx +numpy +matplotlib \ No newline at end of file diff --git a/settings.py b/settings.py index 8a93b98..e4d1806 100644 --- a/settings.py +++ b/settings.py @@ -1,5 +1,7 @@ # General configuration +import json + # Network settings network_type = 1 number_of_nodes = 1000 @@ -7,6 +9,12 @@ max_time = 50 num_trials = 1 timeout = 2 + +with open('simulation_settings.json', 'r') as f: + environment_params = json.load(f) + +''' + environment_params = { # Zombie model 'bite_prob': 0.01, # 0-1 @@ -62,3 +70,4 @@ environment_params = { 'prob_vaccinated_vaccinate_neutral': 0.035, 'prob_generate_anti_rumor': 0.035 } +''' \ No newline at end of file diff --git a/simulation_settings.json b/simulation_settings.json new file mode 100644 index 0000000..e162f12 --- /dev/null +++ b/simulation_settings.json @@ -0,0 +1,53 @@ +{ + + "agent": ["BaseBehaviour","SISaModel","ControlModelM2"], + + + "bite_prob": 0.01, + "heal_prob": 0.01, + + "innovation_prob": 0.001, + "imitation_prob": 0.005, + + "outside_effects_prob": 0.2, + "anger_prob": 0.06, + "joy_prob": 0.05, + "sadness_prob": 0.02, + "disgust_prob": 0.02, + + "enterprises": ["BBVA", "Santander", "Bankia"], + + "tweet_probability_users": 0.44, + "tweet_relevant_probability": 0.25, + "tweet_probability_about": [0.15, 0.15, 0.15], + "sentiment_about": [0, 0, 0], + + "tweet_probability_enterprises": [0.3, 0.3, 0.3], + + "neutral_discontent_spon_prob": 0.04, + "neutral_discontent_infected_prob": 0.04, + "neutral_content_spon_prob": 0.18, + "neutral_content_infected_prob": 0.02, + + "discontent_neutral": 0.13, + "discontent_content": 0.07, + "variance_d_c": 0.02, + + "content_discontent": 0.009, + "variance_c_d": 0.003, + "content_neutral": 0.088, + + "standard_variance": 0.055, + + + "prob_neutral_making_denier": 0.035, + + "prob_infect": 0.075, + + "prob_cured_healing_infected": 0.035, + "prob_cured_vaccinate_neutral": 0.035, + + "prob_vaccinated_healing_infected": 0.035, + "prob_vaccinated_vaccinate_neutral": 0.035, + "prob_generate_anti_rumor": 0.035 +} \ No newline at end of file diff --git a/soil.py b/soil.py index fd1494f..ae4c537 100644 --- a/soil.py +++ b/soil.py @@ -8,6 +8,84 @@ import models import math import json +################# +# Visualization # +################# + +def visualization(graph_name): + + for x in range(0, settings.number_of_nodes): + for attribute in models.networkStatus["agent_%s" % x]: + emotionStatusAux = [] + for t_step in models.networkStatus["agent_%s" % x][attribute]: + prec = 2 + output = math.floor(models.networkStatus["agent_%s" % x][attribute][t_step] * (10 ** prec)) / (10 ** prec) # 2 decimals + emotionStatusAux.append((output, t_step,None)) + attributes = {} + attributes[attribute] = emotionStatusAux + G.add_node(x, attributes) + + + print("Done!") + + with open('data.txt', 'w') as outfile: + json.dump(models.networkStatus, outfile, sort_keys=True, indent=4, separators=(',', ': ')) + + nx.write_gexf(G, graph_name+".gexf", version="1.2draft") + +########### +# Results # +########### +def results(model_name): + x_values = [] + infected_values = [] + neutral_values = [] + cured_values = [] + vaccinated_values = [] + + attribute_plot = 'status' + for time in range(0, settings.max_time): + value_infectados = 0 + value_neutral = 0 + value_cured = 0 + value_vaccinated = 0 + real_time = time * settings.timeout + activity = False + for x in range(0, settings.number_of_nodes): + if attribute_plot in models.networkStatus["agent_%s" % x]: + if real_time in models.networkStatus["agent_%s" % x][attribute_plot]: + if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 1: ## Infected + value_infectados += 1 + activity = True + if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 0: ## Neutral + value_neutral += 1 + activity = True + if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 2: ## Cured + value_cured += 1 + activity = True + if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 3: ## Vaccinated + value_vaccinated += 1 + activity = True + + if activity: + x_values.append(real_time) + infected_values.append(value_infectados) + neutral_values.append(value_neutral) + cured_values.append(value_cured) + vaccinated_values.append(value_vaccinated) + activity = False + + fig1 = plt.figure() + ax1 = fig1.add_subplot(111) + + infected_line = ax1.plot(x_values, infected_values, label='Infected') + neutral_line = ax1.plot(x_values, neutral_values, label='Neutral') + cured_line = ax1.plot(x_values, cured_values, label='Cured') + vaccinated_line = ax1.plot(x_values, vaccinated_values, label='Vaccinated') + ax1.legend() + fig1.savefig(model_name+'.png') + # plt.show() + #################### # Network creation # @@ -26,82 +104,30 @@ if settings.network_type == 2: # Simulation # ############## -sim = NetworkSimulation(topology=G, states=init_states, agent_type=ControlModelM2, max_time=settings.max_time, +agents = settings.environment_params['agent'] + + + +print("Using Agent(s): {agents}".format(agents=agents)) + +if len(agents) > 1: + for agent in agents: + sim = NetworkSimulation(topology=G, states=init_states, agent_type=locals()[agent], max_time=settings.max_time, num_trials=settings.num_trials, logging_interval=1.0, **settings.environment_params) - -sim.run_simulation() + sim.run_simulation() + print(str(agent)) + results(str(agent)) + visualization(str(agent)) +else: + agent = agents[0] + sim = NetworkSimulation(topology=G, states=init_states, agent_type=locals()[agent], max_time=settings.max_time, + num_trials=settings.num_trials, logging_interval=1.0, **settings.environment_params) + sim.run_simulation() + results(str(agent)) + visualization(str(agent)) -########### -# Results # -########### - -x_values = [] -infected_values = [] -neutral_values = [] -cured_values = [] -vaccinated_values = [] - -attribute_plot = 'status' -for time in range(0, settings.max_time): - value_infectados = 0 - value_neutral = 0 - value_cured = 0 - value_vaccinated = 0 - real_time = time * settings.timeout - activity = False - for x in range(0, settings.number_of_nodes): - if attribute_plot in models.networkStatus["agent_%s" % x]: - if real_time in models.networkStatus["agent_%s" % x][attribute_plot]: - if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 1: ## Infected - value_infectados += 1 - activity = True - if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 0: ## Neutral - value_neutral += 1 - activity = True - if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 2: ## Cured - value_cured += 1 - activity = True - if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 3: ## Vaccinated - value_vaccinated += 1 - activity = True - - if activity: - x_values.append(real_time) - infected_values.append(value_infectados) - neutral_values.append(value_neutral) - cured_values.append(value_cured) - vaccinated_values.append(value_vaccinated) - activity = False - -infected_line = plt.plot(x_values, infected_values, label='Infected') -neutral_line = plt.plot(x_values, neutral_values, label='Neutral') -cured_line = plt.plot(x_values, cured_values, label='Cured') -vaccinated_line = plt.plot(x_values, vaccinated_values, label='Vaccinated') -plt.legend() -plt.savefig('control_model.png') -# plt.show() -################# -# Visualization # -################# - -for x in range(0, settings.number_of_nodes): - for attribute in models.networkStatus["agent_%s" % x]: - emotionStatusAux = [] - for t_step in models.networkStatus["agent_%s" % x][attribute]: - prec = 2 - output = math.floor(models.networkStatus["agent_%s" % x][attribute][t_step] * (10 ** prec)) / (10 ** prec) # 2 decimals - emotionStatusAux.append((output, t_step,None)) - attributes = {} - attributes[attribute] = emotionStatusAux - G.add_node(x, attributes) -print("Done!") - -with open('data.txt', 'w') as outfile: - json.dump(models.networkStatus, outfile, sort_keys=True, indent=4, separators=(',', ': ')) - -nx.write_gexf(G, "test.gexf", version="1.2draft")