-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
175 lines (150 loc) · 3.54 KB
/
code.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
import networkx as nx
import matplotlib.pyplot as plt
import random
import math
import time
def create_graph():
G=nx.Graph()
#G.add_nodes_from(range(1,101))
for i in range(1,21):
G.add_node(i)
return G
def vis(G,t):
time.sleep(1)
labeldict=get_label(G)
nodesize=get_size(G)
color=get_color(G)
nx.draw(G,labels=labeldict,node_size=nodesize,node_color=color)
plt.savefig('evolution_'+str(t)+'.jpg')
plt.clf()
plt.cla()
nx.write_gml(G,'evolution_'+str(t)+'.gml')
def assign_health(G):
for each in G.nodes():
G.node[each]['name']=random.randint(1,30)
G.node[each]['type']='Person'
def get_label(G):
dict1={}
for each in G.nodes():
dict1[each]=G.node[each]['name']
return dict1
def get_size(G):
array1=[]
for each in G.nodes():
if G.node[each]['type']=='Person':
array1.append(G.node[each]['name']*40)
else:
array1.append(1000)
return array1
def add_foci(G):
n=G.number_of_nodes()
i=n+1
foci_nodes=['smoking','drinking','exercise','playing_sports']
for j in range(0,4):
G.add_node(i)
G.node[i]['name']=foci_nodes[j]
G.node[i]['type']='foci'
i=i+1
def get_color(G):
c=[]
for each in G.nodes():
if G.node[each]['type']=='Person':
if G.node[each]['name']==1:
c.append('green')
elif G.node[each]['name']==30:
c.append('yellow')
else:
c.append('blue')
else:
c.append('red')
return c
def get_foci_nodes():
f=[]
for each in G.nodes():
if G.node[each]['type']=='foci':
f.append(each)
return f
def get_person_nodes():
f=[]
for each in G.nodes():
if G.node[each]['type']=='Person':
f.append(each)
return f
def add_foci_edges():
foci_nodes=get_foci_nodes()
people_node=get_person_nodes()
for each in people_node:
r=random.choice(foci_nodes)
G.add_edge(each,r)
def homophily(G):
pnodes=get_person_nodes()
for u in pnodes:
for v in pnodes:
if u!=v:
diff=abs(G.node[u]['name']-G.node[v]['name'])
prob=float(1)/(diff+1000)
r=random.uniform(0,1)
if r<prob:
G.add_edge(u,v)
def cmn(u,v,G):
nu=set(G.neighbors(u))
nv=set(G.neighbors(v))
return len(nu & nv)
def closure(G):
array1=[]
for u in G.nodes():
for v in G.nodes():
if u!=v and (G.node[u]['type']=='Person' or G.node[v]['type']=='Person'):
k=cmn(u,v,G)
p=1-math.pow((1-0.01),k)
tmp=[]
tmp.append(u)
tmp.append(v)
tmp.append(p)
array1.append(tmp)
for each in array1:
u=each[0]
v=each[1]
p=each[2]
r=random.uniform(0,1)
if r<p:
G.add_edge(u,v)
def change_health(G):
fnodes=get_foci_nodes()
for each in fnodes:
if G.node[each]['name']=='smoking':
for each1 in G.neighbors(each):
if G.node[each1]['name']!=30:
G.node[each1]['name']=G.node[each1]['name']+3
if G.node[each1]['name']>30:
G.node[each1]['name']=30
if G.node[each]['name']=='drinking':
for each1 in G.neighbors(each):
if G.node[each1]['name']!=30:
G.node[each1]['name']=G.node[each1]['name']+2
if G.node[each1]['name']>30:
G.node[each1]['name']=30
if G.node[each]['name']=='exercise':
for each1 in G.neighbors(each):
if G.node[each1]['name']!=1:
G.node[each1]['name']=G.node[each1]['name']-2
if G.node[each1]['name']<1:
G.node[each1]['name']=1
if G.node[each]['name']=='playing_sports':
for each1 in G.neighbors(each):
if G.node[each1]['name']!=1:
G.node[each1]['name']=G.node[each1]['name']-1
if G.node[each1]['name']<1:
G.node[each1]['name']=1
G=create_graph()
assign_health(G)
add_foci(G)
add_foci_edges()
time.sleep(10)
t=0
vis(G,t)
for t in range(0,5):
homophily(G)
closure(G)
change_health(G)
vis(G,t+1)