1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 23:42:29 +00:00
soil/models/IndependentCascadeModel/IndependentCascadeModel.py

54 lines
1.8 KiB
Python
Raw Normal View History

2017-04-20 11:36:17 +00:00
import random
from ..BaseBehaviour import *
from .. import sentimentCorrelationNodeArray
class IndependentCascadeModel(BaseBehaviour):
"""
Settings:
innovation_prob
imitation_prob
"""
def __init__(self, environment=None, agent_id=0, state=()):
super().__init__(environment=environment, agent_id=agent_id, state=state)
2017-04-21 11:55:42 +00:00
self.innovation_prob = environment.innovation_prob
self.imitation_prob = environment.imitation_prob
2017-04-20 11:36:17 +00:00
self.time_awareness = 0
2017-04-21 11:55:42 +00:00
sentimentCorrelationNodeArray[self.id][self.env.now] = 0
2017-04-20 11:36:17 +00:00
2017-04-21 11:55:42 +00:00
def step(self, now):
2017-04-20 11:36:17 +00:00
self.behaviour()
super().step(now)
def behaviour(self):
2017-04-21 11:55:42 +00:00
aware_neighbors_1_time_step = []
2017-04-20 11:36:17 +00:00
# Outside effects
2017-04-21 11:55:42 +00:00
if random.random() < self.innovation_prob:
2017-04-20 11:36:17 +00:00
if self.state['id'] == 0:
self.state['id'] = 1
2017-04-21 11:55:42 +00:00
sentimentCorrelationNodeArray[self.id][self.env.now] = 1
self.time_awareness = self.env.now # To know when they have been infected
2017-04-20 11:36:17 +00:00
else:
pass
self.attrs['status'] = self.state['id']
return
# Imitation effects
if self.state['id'] == 0:
aware_neighbors = self.get_neighboring_agents(state_id=1)
for x in aware_neighbors:
if x.time_awareness == (self.env.now-1):
aware_neighbors_1_time_step.append(x)
num_neighbors_aware = len(aware_neighbors_1_time_step)
2017-04-21 11:55:42 +00:00
if random.random() < (self.imitation_prob*num_neighbors_aware):
2017-04-20 11:36:17 +00:00
self.state['id'] = 1
2017-04-21 11:55:42 +00:00
sentimentCorrelationNodeArray[self.id][self.env.now] = 1
2017-04-20 11:36:17 +00:00
else:
pass
self.attrs['status'] = self.state['id']
return