From aaf5f709f1fdd70118505b4c83bca7ccc5df2ee9 Mon Sep 17 00:00:00 2001 From: Tasio Mendez Date: Mon, 24 Apr 2017 12:55:00 +0200 Subject: [PATCH] Settings models --- models/BassModel/BassModel.py | 4 +- models/BigMarketModel/BigMarketModel.py | 22 ++--- .../IndependentCascadeModel.py | 4 +- models/ModelM2/ControlModelM2.py | 31 +++---- models/ModelM2/SpreadModelM2.py | 27 +++--- models/SISaModel/SISaModel.py | 28 +++--- .../SentimentCorrelationModel.py | 10 +-- settings.py | 86 ++++++++++--------- soil.py | 31 ++++--- 9 files changed, 125 insertions(+), 118 deletions(-) diff --git a/models/BassModel/BassModel.py b/models/BassModel/BassModel.py index c7467e6..6de5d91 100644 --- a/models/BassModel/BassModel.py +++ b/models/BassModel/BassModel.py @@ -13,8 +13,8 @@ class BassModel(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.innovation_prob = environment.innovation_prob - self.imitation_prob = environment.imitation_prob + self.innovation_prob = environment.environment_params['innovation_prob'] + self.imitation_prob = environment.environment_params['imitation_prob'] sentimentCorrelationNodeArray[self.id][self.env.now] = 0 def step(self, now): diff --git a/models/BigMarketModel/BigMarketModel.py b/models/BigMarketModel/BigMarketModel.py index 0e46a59..e4f8f81 100644 --- a/models/BigMarketModel/BigMarketModel.py +++ b/models/BigMarketModel/BigMarketModel.py @@ -21,21 +21,21 @@ class BigMarketModel(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.enterprises = environment.enterprises + self.enterprises = environment.environment_params['enterprises'] self.type = "" - self.number_of_enterprises = len(environment.enterprises) + self.number_of_enterprises = len(environment.environment_params['enterprises']) if self.id < self.number_of_enterprises: # Enterprises - self.state['id']=self.id - self.type="Enterprise" - self.tweet_probability = environment.tweet_probability_enterprises[self.id] + self.state['id'] = self.id + self.type = "Enterprise" + self.tweet_probability = environment.environment_params['tweet_probability_enterprises'][self.id] else: # normal users - self.state['id']=self.number_of_enterprises - self.type="User" - self.tweet_probability = environment.tweet_probability_users - self.tweet_relevant_probability = environment.tweet_relevant_probability - self.tweet_probability_about = environment.tweet_probability_about # List - self.sentiment_about = environment.sentiment_about # List + 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 def step(self, now): diff --git a/models/IndependentCascadeModel/IndependentCascadeModel.py b/models/IndependentCascadeModel/IndependentCascadeModel.py index f8dd87d..06a5b3a 100644 --- a/models/IndependentCascadeModel/IndependentCascadeModel.py +++ b/models/IndependentCascadeModel/IndependentCascadeModel.py @@ -13,8 +13,8 @@ class IndependentCascadeModel(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.innovation_prob = environment.innovation_prob - self.imitation_prob = environment.imitation_prob + self.innovation_prob = environment.environment_params['innovation_prob'] + self.imitation_prob = environment.environment_params['imitation_prob'] self.time_awareness = 0 sentimentCorrelationNodeArray[self.id][self.env.now] = 0 diff --git a/models/ModelM2/ControlModelM2.py b/models/ModelM2/ControlModelM2.py index cdf9b49..b8f818b 100644 --- a/models/ModelM2/ControlModelM2.py +++ b/models/ModelM2/ControlModelM2.py @@ -24,8 +24,8 @@ class ControlModelM2(BaseBehaviour): """ # Init infected - init_states[random.randint(0, settings.number_of_nodes-1)] = {'id':1} - init_states[random.randint(0, settings.number_of_nodes-1)] = {'id':1} + init_states[random.randint(0, settings.number_of_nodes-1)] = {'id': 1} + init_states[random.randint(0, settings.number_of_nodes-1)] = {'id': 1} # Init beacons init_states[random.randint(0, settings.number_of_nodes-1)] = {'id': 4} @@ -34,22 +34,23 @@ class ControlModelM2(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.prob_neutral_making_denier = np.random.normal(environment.prob_neutral_making_denier, - environment.standard_variance) + self.prob_neutral_making_denier = np.random.normal(environment.environment_params['prob_neutral_making_denier'], + environment.environment_params['standard_variance']) - self.prob_infect = np.random.normal(environment.prob_infect, environment.standard_variance) + self.prob_infect = np.random.normal(environment.environment_params['prob_infect'], + environment.environment_params['standard_variance']) - self.prob_cured_healing_infected = np.random.normal(environment.prob_cured_healing_infected, - environment.standard_variance) - self.prob_cured_vaccinate_neutral = np.random.normal(environment.prob_cured_vaccinate_neutral, - environment.standard_variance) + self.prob_cured_healing_infected = np.random.normal(environment.environment_params['prob_cured_healing_infected'], + environment.environment_params['standard_variance']) + self.prob_cured_vaccinate_neutral = np.random.normal(environment.environment_params['prob_cured_vaccinate_neutral'], + environment.environment_params['standard_variance']) - self.prob_vaccinated_healing_infected = np.random.normal(environment.prob_vaccinated_healing_infected, - environment.standard_variance) - self.prob_vaccinated_vaccinate_neutral = np.random.normal(environment.prob_vaccinated_vaccinate_neutral, - environment.standard_variance) - self.prob_generate_anti_rumor = np.random.normal(environment.prob_generate_anti_rumor, - environment.standard_variance) + self.prob_vaccinated_healing_infected = np.random.normal(environment.environment_params['prob_vaccinated_healing_infected'], + environment.environment_params['standard_variance']) + self.prob_vaccinated_vaccinate_neutral = np.random.normal(environment.environment_params['prob_vaccinated_vaccinate_neutral'], + environment.environment_params['standard_variance']) + self.prob_generate_anti_rumor = np.random.normal(environment.environment_params['prob_generate_anti_rumor'], + environment.environment_params['standard_variance']) def step(self, now): diff --git a/models/ModelM2/SpreadModelM2.py b/models/ModelM2/SpreadModelM2.py index ed4f16e..ee3dc09 100644 --- a/models/ModelM2/SpreadModelM2.py +++ b/models/ModelM2/SpreadModelM2.py @@ -29,22 +29,23 @@ class SpreadModelM2(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.prob_neutral_making_denier = np.random.normal(environment.prob_neutral_making_denier, - environment.standard_variance) + self.prob_neutral_making_denier = np.random.normal(environment.environment_params['prob_neutral_making_denier'], + environment.environment_params['standard_variance']) - self.prob_infect = np.random.normal(environment.prob_infect, environment.standard_variance) + self.prob_infect = np.random.normal(environment.environment_params['prob_infect'], + environment.environment_params['standard_variance']) - self.prob_cured_healing_infected = np.random.normal(environment.prob_cured_healing_infected, - environment.standard_variance) - self.prob_cured_vaccinate_neutral = np.random.normal(environment.prob_cured_vaccinate_neutral, - environment.standard_variance) + self.prob_cured_healing_infected = np.random.normal(environment.environment_params['prob_cured_healing_infected'], + environment.environment_params['standard_variance']) + self.prob_cured_vaccinate_neutral = np.random.normal(environment.environment_params['prob_cured_vaccinate_neutral'], + environment.environment_params['standard_variance']) - self.prob_vaccinated_healing_infected = np.random.normal(environment.prob_vaccinated_healing_infected, - environment.standard_variance) - self.prob_vaccinated_vaccinate_neutral = np.random.normal(environment.prob_vaccinated_vaccinate_neutral, - environment.standard_variance) - self.prob_generate_anti_rumor = np.random.normal(environment.prob_generate_anti_rumor, - environment.standard_variance) + self.prob_vaccinated_healing_infected = np.random.normal(environment.environment_params['prob_vaccinated_healing_infected'], + environment.environment_params['standard_variance']) + self.prob_vaccinated_vaccinate_neutral = np.random.normal(environment.environment_params['prob_vaccinated_vaccinate_neutral'], + environment.environment_params['standard_variance']) + self.prob_generate_anti_rumor = np.random.normal(environment.environment_params['prob_generate_anti_rumor'], + environment.environment_params['standard_variance']) def step(self, now): diff --git a/models/SISaModel/SISaModel.py b/models/SISaModel/SISaModel.py index 6fd78a0..b89ac5a 100644 --- a/models/SISaModel/SISaModel.py +++ b/models/SISaModel/SISaModel.py @@ -32,20 +32,24 @@ class SISaModel(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.neutral_discontent_spon_prob = np.random.normal(environment.neutral_discontent_spon_prob, - environment.standard_variance) - self.neutral_discontent_infected_prob = np.random.normal(environment.neutral_discontent_infected_prob, - environment.standard_variance) - self.neutral_content_spon_prob = np.random.normal(environment.neutral_content_spon_prob, - environment.standard_variance) - self.neutral_content_infected_prob = np.random.normal(environment.neutral_content_infected_prob, - environment.standard_variance) + self.neutral_discontent_spon_prob = np.random.normal(environment.environment_params['neutral_discontent_spon_prob'], + environment.environment_params['standard_variance']) + self.neutral_discontent_infected_prob = np.random.normal(environment.environment_params['neutral_discontent_infected_prob'], + environment.environment_params['standard_variance']) + self.neutral_content_spon_prob = np.random.normal(environment.environment_params['neutral_content_spon_prob'], + environment.environment_params['standard_variance']) + self.neutral_content_infected_prob = np.random.normal(environment.environment_params['neutral_content_infected_prob'], + environment.environment_params['standard_variance']) - self.discontent_neutral = np.random.normal(environment.discontent_neutral, environment.standard_variance) - self.discontent_content = np.random.normal(environment.discontent_content, environment.variance_d_c) + self.discontent_neutral = np.random.normal(environment.environment_params['discontent_neutral'], + environment.environment_params['standard_variance']) + self.discontent_content = np.random.normal(environment.environment_params['discontent_content'], + environment.environment_params['variance_d_c']) - self.content_discontent = np.random.normal(environment.content_discontent, environment.variance_c_d) - self.content_neutral = np.random.normal(environment.content_neutral, environment.standard_variance) + self.content_discontent = np.random.normal(environment.environment_params['content_discontent'], + environment.environment_params['variance_c_d']) + self.content_neutral = np.random.normal(environment.environment_params['content_neutral'], + environment.environment_params['standard_variance']) def step(self, now): if self.state['id'] == 0: diff --git a/models/SentimentCorrelationModel/SentimentCorrelationModel.py b/models/SentimentCorrelationModel/SentimentCorrelationModel.py index 8eae6a4..da520b2 100644 --- a/models/SentimentCorrelationModel/SentimentCorrelationModel.py +++ b/models/SentimentCorrelationModel/SentimentCorrelationModel.py @@ -19,11 +19,11 @@ class SentimentCorrelationModel(BaseBehaviour): def __init__(self, environment=None, agent_id=0, state=()): super().__init__(environment=environment, agent_id=agent_id, state=state) - self.outside_effects_prob = environment.outside_effects_prob - self.anger_prob = environment.anger_prob - self.joy_prob = environment.joy_prob - self.sadness_prob = environment.sadness_prob - self.disgust_prob = environment.disgust_prob + self.outside_effects_prob = environment.environment_params['outside_effects_prob'] + self.anger_prob = environment.environment_params['anger_prob'] + self.joy_prob = environment.environment_params['joy_prob'] + self.sadness_prob = environment.environment_params['sadness_prob'] + self.disgust_prob = environment.environment_params['disgust_prob'] self.time_awareness = [] for i in range(4): # In this model we have 4 sentiments self.time_awareness.append(0) # 0-> Anger, 1-> joy, 2->sadness, 3 -> disgust diff --git a/settings.py b/settings.py index f36cc40..8a93b98 100644 --- a/settings.py +++ b/settings.py @@ -7,56 +7,58 @@ max_time = 50 num_trials = 1 timeout = 2 -# Zombie model -bite_prob = 0.01 # 0-1 -heal_prob = 0.01 # 0-1 +environment_params = { + # Zombie model + 'bite_prob': 0.01, # 0-1 + 'heal_prob': 0.01, # 0-1 -# Bass model -innovation_prob = 0.001 -imitation_prob = 0.005 + # Bass model + 'innovation_prob': 0.001, + 'imitation_prob': 0.005, -# Sentiment Correlation model -outside_effects_prob = 0.2 -anger_prob = 0.06 -joy_prob = 0.05 -sadness_prob = 0.02 -disgust_prob = 0.02 + # Sentiment Correlation model + 'outside_effects_prob': 0.2, + 'anger_prob': 0.06, + 'joy_prob': 0.05, + 'sadness_prob': 0.02, + 'disgust_prob': 0.02, -# Big Market model -## Names -enterprises = ["BBVA", "Santander", "Bankia"] -## Users -tweet_probability_users = 0.44 -tweet_relevant_probability = 0.25 -tweet_probability_about = [0.15, 0.15, 0.15] -sentiment_about = [0, 0, 0] # Default values -## Enterprises -tweet_probability_enterprises = [0.3, 0.3, 0.3] + # Big Market model + ## Names + 'enterprises': ["BBVA", "Santander", "Bankia"], + ## Users + 'tweet_probability_users': 0.44, + 'tweet_relevant_probability': 0.25, + 'tweet_probability_about': [0.15, 0.15, 0.15], + 'sentiment_about': [0, 0, 0], # Default values + ## Enterprises + 'tweet_probability_enterprises': [0.3, 0.3, 0.3], -# SISa -neutral_discontent_spon_prob = 0.04 -neutral_discontent_infected_prob = 0.04 -neutral_content_spon_prob = 0.18 -neutral_content_infected_prob = 0.02 + # SISa + 'neutral_discontent_spon_prob': 0.04, + 'neutral_discontent_infected_prob': 0.04, + 'neutral_content_spon_prob': 0.18, + 'neutral_content_infected_prob': 0.02, -discontent_neutral = 0.13 -discontent_content = 0.07 -variance_d_c = 0.02 + 'discontent_neutral': 0.13, + 'discontent_content': 0.07, + 'variance_d_c': 0.02, -content_discontent = 0.009 -variance_c_d = 0.003 -content_neutral = 0.088 + 'content_discontent': 0.009, + 'variance_c_d': 0.003, + 'content_neutral': 0.088, -standard_variance = 0.055 + 'standard_variance': 0.055, -# Spread Model M2 and Control Model M2 -prob_neutral_making_denier = 0.035 + # Spread Model M2 and Control Model M2 + 'prob_neutral_making_denier': 0.035, -prob_infect = 0.075 + 'prob_infect': 0.075, -prob_cured_healing_infected = 0.035 -prob_cured_vaccinate_neutral = 0.035 + 'prob_cured_healing_infected': 0.035, + 'prob_cured_vaccinate_neutral': 0.035, -prob_vaccinated_healing_infected = 0.035 -prob_vaccinated_vaccinate_neutral = 0.035 -prob_generate_anti_rumor = 0.035 + 'prob_vaccinated_healing_infected': 0.035, + 'prob_vaccinated_vaccinate_neutral': 0.035, + 'prob_generate_anti_rumor': 0.035 +} diff --git a/soil.py b/soil.py index 5a8ec4c..fd1494f 100644 --- a/soil.py +++ b/soil.py @@ -26,8 +26,8 @@ if settings.network_type == 2: # Simulation # ############## -sim = NetworkSimulation(topology=G, states=init_states, agent_type=ControlModelM2, - max_time=settings.max_time, num_trials=settings.num_trials, logging_interval=1.0) +sim = NetworkSimulation(topology=G, states=init_states, agent_type=ControlModelM2, max_time=settings.max_time, + num_trials=settings.num_trials, logging_interval=1.0, **settings.environment_params) sim.run_simulation() @@ -49,14 +49,14 @@ for time in range(0, settings.max_time): value_cured = 0 value_vaccinated = 0 real_time = time * settings.timeout - activity= False + activity = False for x in range(0, settings.number_of_nodes): if attribute_plot in models.networkStatus["agent_%s" % x]: if real_time in models.networkStatus["agent_%s" % x][attribute_plot]: if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 1: ## Infected value_infectados += 1 activity = True - if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 0: ## Neutral + if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 0: ## Neutral value_neutral += 1 activity = True if models.networkStatus["agent_%s" % x][attribute_plot][real_time] == 2: ## Cured @@ -72,12 +72,12 @@ for time in range(0, settings.max_time): neutral_values.append(value_neutral) cured_values.append(value_cured) vaccinated_values.append(value_vaccinated) - activity=False + activity = False -infected_line = plt.plot(x_values,infected_values,label='Infected') -neutral_line = plt.plot(x_values,neutral_values, label='Neutral') -cured_line = plt.plot(x_values,cured_values, label='Cured') -vaccinated_line = plt.plot(x_values,vaccinated_values, label='Vaccinated') +infected_line = plt.plot(x_values, infected_values, label='Infected') +neutral_line = plt.plot(x_values, neutral_values, label='Neutral') +cured_line = plt.plot(x_values, cured_values, label='Cured') +vaccinated_line = plt.plot(x_values, vaccinated_values, label='Vaccinated') plt.legend() plt.savefig('control_model.png') # plt.show() @@ -88,12 +88,12 @@ plt.savefig('control_model.png') ################# for x in range(0, settings.number_of_nodes): - for attribute in models.networkStatus["agent_%s"%x]: - emotionStatusAux=[] - for t_step in models.networkStatus["agent_%s"%x][attribute]: + for attribute in models.networkStatus["agent_%s" % x]: + emotionStatusAux = [] + for t_step in models.networkStatus["agent_%s" % x][attribute]: prec = 2 - output = math.floor(models.networkStatus["agent_%s"%x][attribute][t_step] * (10 ** prec)) / (10 ** prec) # 2 decimals - emotionStatusAux.append((output,t_step,None)) + output = math.floor(models.networkStatus["agent_%s" % x][attribute][t_step] * (10 ** prec)) / (10 ** prec) # 2 decimals + emotionStatusAux.append((output, t_step,None)) attributes = {} attributes[attribute] = emotionStatusAux G.add_node(x, attributes) @@ -104,5 +104,4 @@ print("Done!") with open('data.txt', 'w') as outfile: json.dump(models.networkStatus, outfile, sort_keys=True, indent=4, separators=(',', ': ')) -nx.write_gexf(G,"test.gexf", version="1.2draft") - +nx.write_gexf(G, "test.gexf", version="1.2draft")