mirror of
https://github.com/gsi-upm/soil
synced 2024-11-25 12:22:28 +00:00
Merge branch 'models' of https://lab.cluster.gsi.dit.upm.es/soil/soil into models
This commit is contained in:
commit
f1bb636ca8
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@ -0,0 +1,5 @@
|
||||
nxsim
|
||||
simpy
|
||||
networkx
|
||||
numpy
|
||||
matplotlib
|
@ -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
|
||||
}
|
||||
'''
|
53
simulation_settings.json
Normal file
53
simulation_settings.json
Normal 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
|
||||
}
|
108
soil.py
108
soil.py
@ -8,34 +8,35 @@ import models
|
||||
import math
|
||||
import json
|
||||
|
||||
#################
|
||||
# Visualization #
|
||||
#################
|
||||
|
||||
####################
|
||||
# Network creation #
|
||||
####################
|
||||
def visualization(graph_name):
|
||||
|
||||
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
|
||||
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)
|
||||
|
||||
|
||||
##############
|
||||
# Simulation #
|
||||
##############
|
||||
print("Done!")
|
||||
|
||||
sim = NetworkSimulation(topology=G, states=init_states, agent_type=ControlModelM2, max_time=settings.max_time,
|
||||
num_trials=settings.num_trials, logging_interval=1.0, **settings.environment_params)
|
||||
|
||||
sim.run_simulation()
|
||||
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 = []
|
||||
@ -74,34 +75,59 @@ for time in range(0, settings.max_time):
|
||||
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')
|
||||
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()
|
||||
|
||||
|
||||
#################
|
||||
# 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")
|
||||
|
Loading…
Reference in New Issue
Block a user