-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
70 lines (49 loc) · 1.57 KB
/
simulation.py
File metadata and controls
70 lines (49 loc) · 1.57 KB
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
import networkx as nx
import numpy as np
import random
# PARAMETERS
N = 3000
M = 4
TIMESTEPS = 50
symbolic_flow = 0.6
threshold = 0.5
rewire_alpha = 0.3
# INITIAL NETWORK
G = nx.barabasi_albert_graph(N, M)
# INITIAL OPINIONS
for node in G.nodes():
G.nodes[node]['opinion'] = random.uniform(-1,1)
def compute_tension(G):
tensions = []
for i,j in G.edges():
oi = G.nodes[i]['opinion']
oj = G.nodes[j]['opinion']
tensions.append(abs(oi - oj))
return np.mean(tensions)
def polarization(G):
opinions = [G.nodes[i]['opinion'] for i in G.nodes()]
return np.var(opinions)
for t in range(TIMESTEPS):
T = compute_tension(G)
for node in list(G.nodes()):
neighbors = list(G.neighbors(node))
if not neighbors:
continue
if random.random() < symbolic_flow * T:
influence = np.mean([G.nodes[n]['opinion'] for n in neighbors])
G.nodes[node]['opinion'] += 0.1*(influence - G.nodes[node]['opinion'])
# EDGE REWIRING
for i,j in list(G.edges()):
oi = G.nodes[i]['opinion']
oj = G.nodes[j]['opinion']
tension = abs(oi - oj)
if tension > threshold and random.random() < rewire_alpha:
G.remove_edge(i,j)
candidates = [n for n in G.nodes()
if abs(G.nodes[n]['opinion'] - oi) < 0.2 and n != i]
if candidates:
new = random.choice(candidates)
G.add_edge(i,new)
print("Step:",t,
"Tension:",compute_tension(G),
"Polarization:",polarization(G))