1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
This commit is contained in:
Tasio Mendez 2017-04-27 11:35:44 +02:00
commit f1bb636ca8
4 changed files with 165 additions and 72 deletions

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
nxsim
simpy
networkx
numpy
matplotlib

View File

@ -1,5 +1,7 @@
# General configuration # General configuration
import json
# Network settings # Network settings
network_type = 1 network_type = 1
number_of_nodes = 1000 number_of_nodes = 1000
@ -7,6 +9,12 @@ max_time = 50
num_trials = 1 num_trials = 1
timeout = 2 timeout = 2
with open('simulation_settings.json', 'r') as f:
environment_params = json.load(f)
'''
environment_params = { environment_params = {
# Zombie model # Zombie model
'bite_prob': 0.01, # 0-1 'bite_prob': 0.01, # 0-1
@ -62,3 +70,4 @@ environment_params = {
'prob_vaccinated_vaccinate_neutral': 0.035, 'prob_vaccinated_vaccinate_neutral': 0.035,
'prob_generate_anti_rumor': 0.035 'prob_generate_anti_rumor': 0.035
} }
'''

53
simulation_settings.json Normal file
View File

@ -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
}

124
soil.py
View File

@ -8,42 +8,43 @@ import models
import math import math
import json import json
#################
# Visualization #
#################
#################### def visualization(graph_name):
# Network creation #
####################
if settings.network_type == 0: for x in range(0, settings.number_of_nodes):
G = nx.complete_graph(settings.number_of_nodes) for attribute in models.networkStatus["agent_%s" % x]:
if settings.network_type == 1: emotionStatusAux = []
G = nx.barabasi_albert_graph(settings.number_of_nodes, 10) for t_step in models.networkStatus["agent_%s" % x][attribute]:
if settings.network_type == 2: prec = 2
G = nx.margulis_gabber_galil_graph(settings.number_of_nodes, None) output = math.floor(models.networkStatus["agent_%s" % x][attribute][t_step] * (10 ** prec)) / (10 ** prec) # 2 decimals
# More types of networks can be added here emotionStatusAux.append((output, t_step,None))
attributes = {}
attributes[attribute] = emotionStatusAux
G.add_node(x, attributes)
############## print("Done!")
# Simulation #
##############
sim = NetworkSimulation(topology=G, states=init_states, agent_type=ControlModelM2, max_time=settings.max_time, with open('data.txt', 'w') as outfile:
num_trials=settings.num_trials, logging_interval=1.0, **settings.environment_params) json.dump(models.networkStatus, outfile, sort_keys=True, indent=4, separators=(',', ': '))
sim.run_simulation()
nx.write_gexf(G, graph_name+".gexf", version="1.2draft")
########### ###########
# Results # # Results #
########### ###########
def results(model_name):
x_values = []
infected_values = []
neutral_values = []
cured_values = []
vaccinated_values = []
x_values = [] attribute_plot = 'status'
infected_values = [] for time in range(0, settings.max_time):
neutral_values = []
cured_values = []
vaccinated_values = []
attribute_plot = 'status'
for time in range(0, settings.max_time):
value_infectados = 0 value_infectados = 0
value_neutral = 0 value_neutral = 0
value_cured = 0 value_cured = 0
@ -74,34 +75,59 @@ for time in range(0, settings.max_time):
vaccinated_values.append(value_vaccinated) vaccinated_values.append(value_vaccinated)
activity = False activity = False
infected_line = plt.plot(x_values, infected_values, label='Infected') fig1 = plt.figure()
neutral_line = plt.plot(x_values, neutral_values, label='Neutral') ax1 = fig1.add_subplot(111)
cured_line = plt.plot(x_values, cured_values, label='Cured')
vaccinated_line = plt.plot(x_values, vaccinated_values, label='Vaccinated') infected_line = ax1.plot(x_values, infected_values, label='Infected')
plt.legend() neutral_line = ax1.plot(x_values, neutral_values, label='Neutral')
plt.savefig('control_model.png') cured_line = ax1.plot(x_values, cured_values, label='Cured')
# plt.show() vaccinated_line = ax1.plot(x_values, vaccinated_values, label='Vaccinated')
ax1.legend()
fig1.savefig(model_name+'.png')
# plt.show()
################# ####################
# Visualization # # Network creation #
################# ####################
if settings.network_type == 0:
G = nx.complete_graph(settings.number_of_nodes)
if settings.network_type == 1:
G = nx.barabasi_albert_graph(settings.number_of_nodes, 10)
if settings.network_type == 2:
G = nx.margulis_gabber_galil_graph(settings.number_of_nodes, None)
# More types of networks can be added here
##############
# Simulation #
##############
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()
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))
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")