/
functions.py
256 lines (196 loc) · 8.98 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
"""
@author: Federico Malizia
"""
import numpy as np
import pandas as pd
import random
import networkx as nx
from db.objects import*
from agents import *
import matplotlib.pyplot as plt
from IPython.display import clear_output
def set_age():
agelist = [i for i in range(14,85)]
age = np.random.choice(agelist)
return age
def random_num():
randomlist = [i for i in range(1,100)]
random_num = np.random.choice(randomlist)
return random_num
def generate_agents():
agents = []
N = int(input("What is the network size (number of nodes)? "))
shyness = float(input("Do you want to set a global agent shyness rate? (type -1 for random values or choose a value between 0 and 1)"))
if shyness > 1:
return(print("Shyness rate must be between 0 and 1!"))
else:
unique_id_list = list(random.sample(range(1, 10000000000), N+1))
for i in range(N):
name = np.random.choice(names)
age = set_age()
city = np.random.choice(cities,p=cities_prob)
party = np.random.choice(parties,p=parties_probs)
unique_id = np.random.choice(unique_id_list)
if shyness == -1 :
a = User(name,age,city,party,unique_id,random.random())
else:
a = User(name,age,city,party,unique_id,shyness)
agents.append(a)
return(agents, N)
def network(agents):
G = nx.Graph()
for i in agents:
G.add_node(i)
return(G)
def attach_features(agents):
for i in agents:
features_vector = []
features_vector.append(cities_dict[i.district])
features_vector.append(parties_dict[i.party])
features_vector.append(i.age)
features_vector.append(random_num())
i.features(features_vector)
def update_mood(agents,G):
k_list =[]
for i in G.nodes():
k_list.append(G.degree(i))
degreeq2 = np.ceil (max(k_list)*0.25)
degreeq3 = np.ceil(max(k_list)*0.75)
average_k = np.ceil(sum(k_list)/len(k_list))
d =[]
for i in G.nodes():
d.append(G.degree(i))
N = len(G.nodes())
for node in agents:
node.mood(d,degreeq2,average_k,degreeq3)
for node in (list(G.nodes(data=True))):
node[1]['state'] = node[0].get_user_state()
def draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size):
segregated =[]
asocial = []
social = []
influencer = []
cool = []
node_color = []
fig, plot = plt.subplots(1, 2, figsize=(20,10),gridspec_kw={'width_ratios': [1, 2]})
update_mood(agents,G)
for node in list(G.nodes(data=True)):
if 'seg' in node[1]['state']:
node_color.append('black')
elif 'a' in node[1]['state']:
node_color.append('blue')
elif 's' in node[1]['state']:
node_color.append('green')
elif 'c' in node[1]['state']:
node_color.append('orange')
elif 'i' in node[1]['state']:
node_color.append('red')
for node in list(G.nodes()):
if node.state =='seg':
segregated.append(node)
if node.state=='a':
asocial.append(node)
if node.state=='s':
social.append(node)
if node.state=='c':
cool.append(node)
if node.state=='i':
influencer.append(node)
segregated_size.append(len(segregated)/(len(G.nodes())))
asocial_size.append(len(asocial)/(len(G.nodes())))
social_size.append(len(social)/(len(G.nodes())))
cool_size.append(len(cool)/(len(G.nodes())))
influencer_size.append(len(influencer)/(len(G.nodes())))
plot[0].plot(segregated_size, color = 'black', label="Fraction of segregated users")
plot[0].plot(asocial_size, color = 'blue', label="Fraction of asocial users")
plot[0].plot(social_size, color = 'green', label="Fraction or social users")
plot[0].plot(cool_size, color = 'orange', label="Fraction of cool users")
plot[0].plot(influencer_size, color = 'red', label="Fraction of influencers")
plot[0].title.set_text("Users distribution")
plot[0].legend()
my_pos = nx.spring_layout(G, seed = 100)
plot[1] = nx.draw(G, pos= my_pos, node_size=150, with_labels=0, alpha=1, node_color=node_color, edge_color='grey')
plt.pause(0.5)
plt.show()
def run_network(agents,G):
threshold = float(input("What is the tolerance degree between agents?? (-1 for random values or choose a value between 0 and 1) "))
t = int(input("How many interaction between agents do you want?"))
segregated_size =[]
asocial_size = []
social_size = []
influencer_size = []
cool_size = []
node_color = []
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
for i in range(t):
node = np.random.choice(agents)
N = len(G.nodes())
step = node.look_around(N)
node_r_index = (agents.index(node) + step)
if node_r_index < len(agents):
node_r = agents[node_r_index]
decision = node.make_your_decision()
if threshold == -1 :
success = node.approach(node_r, random.random(),decision)
else:
success = node.approach(node_r, threshold,decision)
if success == True:
node_r.got_approached(node,True)
G.add_edge(node, node_r)
if i == np.ceil(t*(0.01)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.03)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.05)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.08)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == t*(0.1):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.13)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.16)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == t*(0.2):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.23)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.26)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == t*(0.3):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i== t*(0.4):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == t*(0.5):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.6)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.7)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.8)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == np.ceil(t*(0.9)):
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
if i == t:
clear_output(wait=True)
draw_network(agents,G,segregated_size,asocial_size,social_size,cool_size,influencer_size)
else:
pass