commit fe6942ce0dd1e38d092defdcc4117918aa369bba Author: JesusMSM Date: Fri Dec 4 11:41:42 2015 +0100 First commit diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..86acbd5 --- /dev/null +++ b/settings.py @@ -0,0 +1,37 @@ +# settings.py +def init(): + global number_of_nodes + global max_time + global num_trials + global bite_prob + global network_type + global heal_prob + global innovation_prob + global imitation_prob + global timeout + global outside_effects_prob + global anger_prob + global joy_prob + global sadness_prob + global disgust_prob + + network_type=1 + number_of_nodes=200 + max_time=1000 + num_trials=1 + timeout=10 + + #Zombie model + bite_prob=0.01 # 0-1 + heal_prob=0.01 # 0-1 + + #Bass model + innovation_prob=0.01 + imitation_prob=0.01 + + #SentimentCorrelation model + outside_effects_prob = 0.2 + anger_prob = 0.08 + joy_prob = 0.05 + sadness_prob = 0.02 + disgust_prob = 0.02 diff --git a/soil.py b/soil.py new file mode 100644 index 0000000..d6d6204 --- /dev/null +++ b/soil.py @@ -0,0 +1,394 @@ +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