-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrial.py
66 lines (53 loc) · 1.96 KB
/
trial.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
import random
import networkx as nx
import matplotlib.pyplot as plt
def initial_opinions(x, N):
amt_pos = int(N * x)
opinions = [1] * amt_pos + [0] * (N - amt_pos)
random.shuffle(opinions)
return opinions
def create_graph(N):
return nx.complete_graph(N)
def simulate_opinions(N, x, p, q, msc):
G = create_graph(N)
positive_counts = 0
for _ in range(msc):
opinions = initial_opinions(x, N)
for _ in range(100):
updated_opinions = opinions.copy()
for agent_a in range(N):
neighbours = list(G.neighbors(agent_a))
rnd_neigh = random.sample(neighbours, min(q, len(neighbours)))
neigh_ops = [opinions[ind] for ind in rnd_neigh]
if len(set(neigh_ops)) == 1:
updated_opinions[agent_a] = neigh_ops[0]
else:
rnd_val = random.uniform(0, 1)
if rnd_val <= p:
updated_opinions[agent_a] = 1
else:
updated_opinions[agent_a] = 0
opinions = updated_opinions
if all(opinion == 1 for opinion in opinions): # If all opinions are positive
positive_counts += 1
break
return positive_counts / msc
x = 0.5
msc_all = 50
q = 2
ps = [i * 0.01 for i in range(101)]
Ns = [50, 100, 150, 200]
for N in Ns:
fractions_p = []
for p in ps:
fraction_positive = simulate_opinions(N, x, p, q, msc_all)
fractions_p.append(fraction_positive)
plt.plot(ps, fractions_p, label=f'N = {N}')
'''d) Make a plot of exit
probability E as a function of p, where exit probability E = fraction of configurations for
which a positive consensus is reached.'''
plt.xlabel('Convincing Ability (p)')
plt.ylabel('Exit Propability')
plt.title('Monte Carlo Simulation for Positive Consensus')
plt.legend()
plt.show()