-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrend_cycles_2.py
150 lines (104 loc) · 4.03 KB
/
trend_cycles_2.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
import random as rnd
import matplotlib.pyplot as plt
import numpy as np
import pickle
N = 1000 # number of agents
msc_all = 500 # number of Monte Carlo steps
q_a = 8 # number of agents we choose if chosen agent is anticonformist
q_c = 2 # number of agents we choose if chosen agent is conformist
p = 0.2 # propability that the agent is always anticonformist
c = 0.1 # initial concentration of agents with a positive opinion
beta = 0.3 #The probability of rewiring each edge
k = [30, 50, 80, 120] #k nearest neighbors
def initial_opinions(c, N):
opinions = list()
for i in range(N):
r_i = rnd.uniform(0, 1)
if r_i < c:
opinions.append(1)
else:
opinions.append(-1)
return opinions
def initial_behaviours(p, N):
behaviours = list()
for i in range(N):
h_i = rnd.uniform(0, 1)
if h_i < p:
behaviours.append(-1)
else:
behaviours.append(1)
return behaviours
def opinions (k, beta):
opinions = initial_opinions(c, N)
behaviours = initial_behaviours(p, N)
time = 0
ones_total = list()
minus_total = list()
G = pickle.load(open(f'graph{k}.pickle', 'rb'))
while time <= msc_all:
ones = list()
minus = list()
for t in range(N):
i = rnd.randint(0, N-1)
rnd_agent_bh = behaviours[i]
neighbours = list(G.neighbors(i))
if rnd_agent_bh == -1:
rnd_neigh = rnd.sample(neighbours, q_a)
neigh_ops = list()
for l in range(len(rnd_neigh)):
neigh_ops.append(opinions[rnd_neigh[l]])
if len(set(neigh_ops)) == 1:
if neigh_ops[0] == 1:
opinions[i] = -1
elif neigh_ops[0] == -1:
opinions[i] = 1
count_ones = opinions.count(1)
count_minus_ones = opinions.count(-1)
ones.append(count_ones)
minus.append(count_minus_ones)
else:
count_ones = opinions.count(1)
count_minus_ones = opinions.count(-1)
ones.append(count_ones)
minus.append(count_minus_ones)
if rnd_agent_bh == 1:
rnd_neigh = rnd.sample(neighbours, q_c)
neigh_ops = list()
for l in range(len(rnd_neigh)):
neigh_ops.append(opinions[rnd_neigh[l]])
if len(set(neigh_ops)) == 1:
if neigh_ops[0] == 1:
opinions[i] = 1
else:
opinions[i] = -1
count_ones = opinions.count(1)
count_minus_ones = opinions.count(-1)
ones.append(count_ones)
minus.append(count_minus_ones)
else:
count_ones = opinions.count(1)
count_minus_ones = opinions.count(-1)
ones.append(count_ones)
minus.append(count_minus_ones)
ones_total.append(np.mean(ones))
minus_total.append(np.mean(minus))
time = time +1
times = list(range(1, len(ones_total)+1))
positive_conc = list()
for i in ones_total:
positive_conc.append(i/N)
negative_conc = list()
for i in minus_total:
negative_conc.append(i/N)
plt.figure(figsize=(10, 7))
plt.plot(times, positive_conc, negative_conc)
plt.xlabel('MCS Steps')
plt.ylabel('c(t)')
plt.legend(["Positive", "Negative"])
plt.suptitle("Concentrations of positive and negative opinions")
plt.title(f'Watts-Strogatz - Graph parameters - K = {k}, beta = {beta}, N = {N}\n '
f'Algorithm parameters - Q_A = {q_a}, Q_C = {q_c}, p = {p}, c = {c}')
plt.show()
for amt in k:
for i in range(1):
opinions(50, beta)