from nxsim import NetworkSimulation from nxsim import BaseNetworkAgent from nxsim import BaseLoggingAgent from random import randint from matplotlib import pyplot as plt import random import numpy as np import networkx as nx import settings settings.init() 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) myList=[] networkStatus=[] for x in range(0, settings.number_of_nodes): networkStatus.append({'id':x}) # # Just like subclassing a process in SimPy # class MyAgent(BaseNetworkAgent): # def __init__(self, environment=None, agent_id=0, state=()): # Make sure to have these three keyword arguments # super().__init__(environment=environment, agent_id=agent_id, state=state) # # Add your own attributes here # def run(self): # # Add your behaviors here class SentimentCorrelationModel(BaseNetworkAgent): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) self.outside_effects_prob = settings.outside_effects_prob self.anger_prob = settings.anger_prob self.joy_prob = settings.joy_prob self.sadness_prob = settings.sadness_prob self.disgust_prob = settings.disgust_prob self.time_awareness=[] for i in range(4): self.time_awareness.append(0) #0-> Anger, 1-> joy, 2->sadness, 3 -> disgust networkStatus[self.id][self.env.now]=0 def run(self): while True: if self.env.now > 10: G.add_node(205) G.add_edge(205,0) angry_neighbors_1_time_step=[] joyful_neighbors_1_time_step=[] sad_neighbors_1_time_step=[] disgusted_neighbors_1_time_step=[] angry_neighbors = self.get_neighboring_agents(state_id=1) for x in angry_neighbors: if x.time_awareness[0] > (self.env.now-500): angry_neighbors_1_time_step.append(x) num_neighbors_angry = len(angry_neighbors_1_time_step) joyful_neighbors = self.get_neighboring_agents(state_id=2) for x in joyful_neighbors: if x.time_awareness[1] > (self.env.now-500): joyful_neighbors_1_time_step.append(x) num_neighbors_joyful = len(joyful_neighbors_1_time_step) sad_neighbors = self.get_neighboring_agents(state_id=3) for x in sad_neighbors: if x.time_awareness[2] > (self.env.now-500): sad_neighbors_1_time_step.append(x) num_neighbors_sad = len(sad_neighbors_1_time_step) disgusted_neighbors = self.get_neighboring_agents(state_id=4) for x in disgusted_neighbors: if x.time_awareness[3] > (self.env.now-500): disgusted_neighbors_1_time_step.append(x) num_neighbors_disgusted = len(disgusted_neighbors_1_time_step) # #Outside effects. Asignamos un estado aleatorio # if random.random() < settings.outside_effects_prob: # if self.state['id'] == 0: # self.state['id'] = random.randint(1,4) # myList.append(self.id) # networkStatus[self.id][self.env.now]=self.state['id'] #Almaceno cuando se ha infectado para la red dinamica # self.time_awareness = self.env.now #Para saber cuando se han contagiado # yield self.env.timeout(settings.timeout) # else: # yield self.env.timeout(settings.timeout) # #Imitation effects-Joy # if random.random() < (settings.joy_prob*(num_neighbors_joyful)/10): # myList.append(self.id) # self.state['id'] = 2 # networkStatus[self.id][self.env.now]=2 # yield self.env.timeout(settings.timeout) # #Imitation effects-Sadness # if random.random() < (settings.sadness_prob*(num_neighbors_sad)/10): # myList.append(self.id) # self.state['id'] = 3 # networkStatus[self.id][self.env.now]=3 # yield self.env.timeout(settings.timeout) # #Imitation effects-Disgust # if random.random() < (settings.disgust_prob*(num_neighbors_disgusted)/10): # myList.append(self.id) # self.state['id'] = 4 # networkStatus[self.id][self.env.now]=4 # yield self.env.timeout(settings.timeout) # #Imitation effects-Anger # if random.random() < (settings.anger_prob*(num_neighbors_angry)/10): # myList.append(self.id) # self.state['id'] = 1 # networkStatus[self.id][self.env.now]=1 # yield self.env.timeout(settings.timeout) # yield self.env.timeout(settings.timeout) ########################################### anger_prob= settings.anger_prob+(len(angry_neighbors_1_time_step)*settings.anger_prob) print("anger_prob " + str(anger_prob)) joy_prob= settings.joy_prob+(len(joyful_neighbors_1_time_step)*settings.joy_prob) print("joy_prob " + str(joy_prob)) sadness_prob = settings.sadness_prob+(len(sad_neighbors_1_time_step)*settings.sadness_prob) print("sadness_prob "+ str(sadness_prob)) disgust_prob = settings.disgust_prob+(len(disgusted_neighbors_1_time_step)*settings.disgust_prob) print("disgust_prob " + str(disgust_prob)) outside_effects_prob= settings.outside_effects_prob print("outside_effects_prob " + str(outside_effects_prob)) num = random.random() if(numanger_prob): myList.append(self.id) self.state['id'] = 2 networkStatus[self.id][self.env.now]=2 self.time_awareness[self.state['id']-1] = self.env.now elif (numjoy_prob+anger_prob): myList.append(self.id) self.state['id'] = 3 networkStatus[self.id][self.env.now]=3 self.time_awareness[self.state['id']-1] = self.env.now elif (numsadness_prob+anger_prob+joy_prob): myList.append(self.id) self.state['id'] = 4 networkStatus[self.id][self.env.now]=4 self.time_awareness[self.state['id']-1] = self.env.now yield self.env.timeout(settings.timeout) # anger_propagation = settings.anger_prob*num_neighbors_angry/10 # joy_propagation = anger_propagation + (settings.joy_prob*num_neighbors_joyful/10) # sadness_propagation = joy_propagation + (settings.sadness_prob*num_neighbors_sad/10) # disgust_propagation = sadness_propagation + (settings.disgust_prob*num_neighbors_disgusted/10) # outside_effects_propagation = disgust_propagation + settings.outside_effects_prob # if (num