2016-04-13 16:08:41 +00:00
|
|
|
from models import *
|
2015-12-04 10:41:42 +00:00
|
|
|
from nxsim import NetworkSimulation
|
2016-04-18 10:23:24 +00:00
|
|
|
import numpy
|
|
|
|
from matplotlib import pyplot as plt
|
2015-12-04 10:41:42 +00:00
|
|
|
import networkx as nx
|
|
|
|
import settings
|
2016-04-13 16:08:41 +00:00
|
|
|
import models
|
2015-12-18 12:03:58 +00:00
|
|
|
import math
|
2016-03-04 12:02:11 +00:00
|
|
|
import json
|
2015-12-04 10:41:42 +00:00
|
|
|
|
2015-12-04 11:32:24 +00:00
|
|
|
settings.init() # Loads all the data from settings
|
2016-04-13 16:08:41 +00:00
|
|
|
models.init() # Loads the models and network variables
|
2015-12-04 10:41:42 +00:00
|
|
|
|
2015-12-04 11:32:24 +00:00
|
|
|
####################
|
|
|
|
# Network creation #
|
|
|
|
####################
|
2015-12-04 10:41:42 +00:00
|
|
|
|
|
|
|
if settings.network_type == 0:
|
|
|
|
G = nx.complete_graph(settings.number_of_nodes)
|
|
|
|
if settings.network_type == 1:
|
2016-04-21 10:56:06 +00:00
|
|
|
G = nx.barabasi_albert_graph(settings.number_of_nodes,10)
|
2015-12-04 10:41:42 +00:00
|
|
|
if settings.network_type == 2:
|
|
|
|
G = nx.margulis_gabber_galil_graph(settings.number_of_nodes, None)
|
2015-12-04 11:32:24 +00:00
|
|
|
# More types of networks can be added here
|
2015-12-04 10:41:42 +00:00
|
|
|
|
2015-12-04 11:32:24 +00:00
|
|
|
##############
|
|
|
|
# Simulation #
|
|
|
|
##############
|
2015-12-04 10:41:42 +00:00
|
|
|
|
2016-04-21 10:56:06 +00:00
|
|
|
sim = NetworkSimulation(topology=G, states=init_states, agent_type=SpreadModelM2,
|
2015-12-04 10:41:42 +00:00
|
|
|
max_time=settings.max_time, num_trials=settings.num_trials, logging_interval=1.0)
|
|
|
|
|
|
|
|
|
|
|
|
sim.run_simulation()
|
|
|
|
|
2015-12-04 11:32:24 +00:00
|
|
|
###########
|
|
|
|
# Results #
|
|
|
|
###########
|
2016-04-18 10:23:24 +00:00
|
|
|
x_values = []
|
2016-04-27 11:30:31 +00:00
|
|
|
infected_values = []
|
|
|
|
neutral_values = []
|
|
|
|
cured_values = []
|
|
|
|
vaccinated_values = []
|
2016-04-18 10:23:24 +00:00
|
|
|
|
2016-04-21 10:56:06 +00:00
|
|
|
attribute_plot = 'status'
|
2016-04-18 10:23:24 +00:00
|
|
|
for time in range(0, settings.max_time):
|
2016-04-27 11:30:31 +00:00
|
|
|
value_infectados = 0
|
|
|
|
value_neutral = 0
|
|
|
|
value_cured = 0
|
|
|
|
value_vaccinated = 0
|
2016-04-18 10:23:24 +00:00
|
|
|
real_time = time * settings.timeout
|
2016-04-21 10:56:06 +00:00
|
|
|
activity= False
|
2016-04-18 10:23:24 +00:00
|
|
|
for x in range(0, settings.number_of_nodes):
|
2016-04-21 10:56:06 +00:00
|
|
|
if attribute_plot in models.networkStatus["agente_%s" % x]:
|
|
|
|
if real_time in models.networkStatus["agente_%s" % x][attribute_plot]:
|
|
|
|
if models.networkStatus["agente_%s" % x][attribute_plot][real_time] == 1: ##Representar infectados
|
2016-04-27 11:30:31 +00:00
|
|
|
value_infectados += 1
|
|
|
|
activity = True
|
|
|
|
if models.networkStatus["agente_%s" % x][attribute_plot][real_time] == 0: ##Representar neutrales
|
|
|
|
value_neutral += 1
|
|
|
|
activity = True
|
|
|
|
if models.networkStatus["agente_%s" % x][attribute_plot][real_time] == 2: ##Representar cured
|
|
|
|
value_cured += 1
|
|
|
|
activity = True
|
|
|
|
if models.networkStatus["agente_%s" % x][attribute_plot][real_time] == 3: ##Representar vaccinated
|
|
|
|
value_vaccinated += 1
|
2016-04-21 10:56:06 +00:00
|
|
|
activity = True
|
2016-04-18 10:23:24 +00:00
|
|
|
|
2016-04-21 10:56:06 +00:00
|
|
|
if activity:
|
|
|
|
x_values.append(real_time)
|
2016-04-27 11:30:31 +00:00
|
|
|
infected_values.append(value_infectados)
|
|
|
|
neutral_values.append(value_neutral)
|
|
|
|
cured_values.append(value_cured)
|
|
|
|
vaccinated_values.append(value_vaccinated)
|
2016-04-21 10:56:06 +00:00
|
|
|
activity=False
|
2016-04-18 10:23:24 +00:00
|
|
|
|
2016-04-27 11:30:31 +00:00
|
|
|
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()
|
2016-04-21 10:56:06 +00:00
|
|
|
plt.show()
|
2015-12-04 10:41:42 +00:00
|
|
|
|
|
|
|
|
2015-12-04 11:32:24 +00:00
|
|
|
#################
|
|
|
|
# Visualization #
|
|
|
|
#################
|
2015-12-04 10:41:42 +00:00
|
|
|
|
2016-04-13 16:08:41 +00:00
|
|
|
|
|
|
|
for x in range(0, settings.number_of_nodes):
|
|
|
|
for empresa in models.networkStatus["agente_%s"%x]:
|
2016-03-04 12:02:11 +00:00
|
|
|
emotionStatusAux=[]
|
2016-04-13 16:08:41 +00:00
|
|
|
for tiempo in models.networkStatus["agente_%s"%x][empresa]:
|
|
|
|
prec = 2
|
|
|
|
output = math.floor(models.networkStatus["agente_%s"%x][empresa][tiempo] * (10 ** prec)) / (10 ** prec) #Para tener 2 decimales solo
|
|
|
|
emotionStatusAux.append((output,tiempo,None))
|
|
|
|
attributes = {}
|
|
|
|
attributes[empresa] = emotionStatusAux
|
|
|
|
G.add_node(x, attributes)
|
2016-01-08 11:49:46 +00:00
|
|
|
|
|
|
|
|
2016-01-11 11:21:04 +00:00
|
|
|
print("Done!")
|
2015-12-17 11:56:39 +00:00
|
|
|
|
2016-03-04 12:02:11 +00:00
|
|
|
with open('data.txt', 'w') as outfile:
|
2016-04-13 16:08:41 +00:00
|
|
|
json.dump(models.networkStatus, outfile, sort_keys=True, indent=4, separators=(',', ': '))
|
2015-12-04 10:41:42 +00:00
|
|
|
|
|
|
|
nx.write_gexf(G,"test.gexf", version="1.2draft")
|
2015-12-04 11:32:24 +00:00
|
|
|
|