1
0
mirror of https://github.com/gsi-upm/soil synced 2024-11-14 15:32:29 +00:00
soil/models/BigMarketModel/BigMarketModel.py

105 lines
4.4 KiB
Python
Raw Normal View History

2017-04-20 11:36:17 +00:00
import random
from ..BaseBehaviour import *
class BigMarketModel(BaseBehaviour):
"""
Settings:
Names:
enterprises [Array]
tweet_probability_enterprises [Array]
Users:
tweet_probability_users
tweet_relevant_probability
tweet_probability_about [Array]
sentiment_about [Array]
"""
def __init__(self, environment=None, agent_id=0, state=()):
super().__init__(environment=environment, agent_id=agent_id, state=state)
2017-04-24 10:55:00 +00:00
self.enterprises = environment.environment_params['enterprises']
2017-04-20 11:36:17 +00:00
self.type = ""
2017-04-24 10:55:00 +00:00
self.number_of_enterprises = len(environment.environment_params['enterprises'])
2017-04-20 11:36:17 +00:00
2017-04-21 11:55:42 +00:00
if self.id < self.number_of_enterprises: # Enterprises
2017-04-24 10:55:00 +00:00
self.state['id'] = self.id
self.type = "Enterprise"
self.tweet_probability = environment.environment_params['tweet_probability_enterprises'][self.id]
2017-04-21 11:55:42 +00:00
else: # normal users
2017-04-24 10:55:00 +00:00
self.state['id'] = self.number_of_enterprises
self.type = "User"
self.tweet_probability = environment.environment_params['tweet_probability_users']
self.tweet_relevant_probability = environment.environment_params['tweet_relevant_probability']
self.tweet_probability_about = environment.environment_params['tweet_probability_about'] # List
self.sentiment_about = environment.environment_params['sentiment_about'] # List
2017-04-20 11:36:17 +00:00
def step(self, now):
2017-04-21 11:55:42 +00:00
if self.id < self.number_of_enterprises: # Enterprise
2017-04-20 11:36:17 +00:00
self.enterpriseBehaviour()
else: # Usuario
self.userBehaviour()
2017-04-21 11:55:42 +00:00
for i in range(self.number_of_enterprises): # So that it never is set to 0 if there are not changes (logs)
2017-04-20 11:36:17 +00:00
self.attrs['sentiment_enterprise_%s'% self.enterprises[i]] = self.sentiment_about[i]
super().step(now)
def enterpriseBehaviour(self):
2017-04-21 11:55:42 +00:00
if random.random()< self.tweet_probability: # Tweets
aware_neighbors = self.get_neighboring_agents(state_id=self.number_of_enterprises) # Nodes neighbour users
2017-04-20 11:36:17 +00:00
for x in aware_neighbors:
if random.uniform(0,10) < 5:
2017-04-21 11:55:42 +00:00
x.sentiment_about[self.id] += 0.1 # Increments for enterprise
2017-04-20 11:36:17 +00:00
else:
2017-04-21 11:55:42 +00:00
x.sentiment_about[self.id] -= 0.1 # Decrements for enterprise
2017-04-20 11:36:17 +00:00
# Establecemos limites
if x.sentiment_about[self.id] > 1:
x.sentiment_about[self.id] = 1
if x.sentiment_about[self.id]< -1:
x.sentiment_about[self.id] = -1
x.attrs['sentiment_enterprise_%s'% self.enterprises[self.id]] = x.sentiment_about[self.id]
def userBehaviour(self):
2017-04-21 11:55:42 +00:00
if random.random() < self.tweet_probability: # Tweets
if random.random() < self.tweet_relevant_probability: # Tweets something relevant
2017-04-20 11:36:17 +00:00
# Tweet probability per enterprise
for i in range(self.number_of_enterprises):
random_num = random.random()
if random_num < self.tweet_probability_about[i]:
# The condition is fulfilled, sentiments are evaluated towards that enterprise
if self.sentiment_about[i] < 0:
# NEGATIVO
self.userTweets("negative",i)
elif self.sentiment_about[i] == 0:
# NEUTRO
pass
else:
# POSITIVO
self.userTweets("positive",i)
def userTweets(self,sentiment,enterprise):
2017-04-21 11:55:42 +00:00
aware_neighbors = self.get_neighboring_agents(state_id=self.number_of_enterprises) # Nodes neighbours users
2017-04-20 11:36:17 +00:00
for x in aware_neighbors:
if sentiment == "positive":
x.sentiment_about[enterprise] +=0.003
elif sentiment == "negative":
x.sentiment_about[enterprise] -=0.003
else:
pass
# Establecemos limites
if x.sentiment_about[enterprise] > 1:
x.sentiment_about[enterprise] = 1
if x.sentiment_about[enterprise] < -1:
x.sentiment_about[enterprise] = -1
x.attrs['sentiment_enterprise_%s'% self.enterprises[enterprise]] = x.sentiment_about[enterprise]