1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 07:22:29 +00:00
soil/soil.py

81 lines
2.3 KiB
Python
Raw Normal View History

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
import models
2015-12-18 12:03:58 +00:00
import math
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
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:
G = nx.barabasi_albert_graph(settings.number_of_nodes,3)
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-18 10:23:24 +00:00
sim = NetworkSimulation(topology=G, states=init_states, agent_type=SISaModel,
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 = []
y_values = []
for time in range(0, settings.max_time):
value = settings.sentiment_about[0]
real_time = time * settings.timeout
for x in range(0, settings.number_of_nodes):
if "sentiment_enterprise_BBVA" in models.networkStatus["agente_%s" % x]:
if real_time in models.networkStatus["agente_%s" % x]["sentiment_enterprise_BBVA"]:
value += models.networkStatus["agente_%s" % x]["sentiment_enterprise_BBVA"][real_time]
x_values.append(real_time)
y_values.append(value)
plt.plot(x_values,y_values)
#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
for x in range(0, settings.number_of_nodes):
for empresa in models.networkStatus["agente_%s"%x]:
emotionStatusAux=[]
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-11 11:21:04 +00:00
print("Done!")
2015-12-17 11:56:39 +00:00
with open('data.txt', 'w') as outfile:
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