-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathother.py
173 lines (127 loc) · 4.68 KB
/
other.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
import random as rnd
import networkx as nx
import matplotlib.pyplot as plt
N = 10000 # 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.8 # initial concentration of of agents with a positive opinion
prop = 0.15
def create_graph(N):
#watts_strogatz = nx.watts_strogatz_graph(N, q, prop)
graph = nx.complete_graph(N)
return graph
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
opinions = initial_opinions(c, N)
behaviours = initial_behaviours(p, N)
t = 0 # time counter
conf_pos = list()
conf_neg = list()
ant_pos = list()
ant_neg = list()
G = create_graph(N)
while t <= msc_all:
i = rnd.randint(0, N-1)
rnd_agent_op = opinions[i]
rnd_agent_bh = behaviours[i]
if rnd_agent_bh == -1:
neighbours = list(G.neighbors(i))
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
else:
opinions[i] = opinions[i]
anty_plus_amt = 0
conf_plus_amt = 0
for i in range(len(opinions)):
if behaviours[i]==1:
if opinions[i]==1:
conf_plus_amt = conf_plus_amt+1
if behaviours[i] == -1:
if opinions[i] == 1:
anty_plus_amt = anty_plus_amt + 1
conf_pos.append(conf_plus_amt)
ant_pos.append(anty_plus_amt)
t = t + 1
else:
anty_plus_amt = 0
conf_plus_amt = 0
for i in range(len(opinions)):
if behaviours[i] == 1:
if opinions[i] == 1:
conf_plus_amt = conf_plus_amt + 1
if behaviours[i] == -1:
if opinions[i] == 1:
anty_plus_amt = anty_plus_amt + 1
conf_pos.append(conf_plus_amt)
ant_pos.append(anty_plus_amt)
t = t + 1
if rnd_agent_bh == 1:
neighbours = list(G.neighbors(i))
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] = opinions[i]
anty_plus_amt = 0
conf_plus_amt = 0
for i in range(len(opinions)):
if behaviours[i] == 1:
if opinions[i] == 1:
conf_plus_amt = conf_plus_amt + 1
if behaviours[i] == -1:
if opinions[i] == 1:
anty_plus_amt = anty_plus_amt + 1
conf_pos.append(conf_plus_amt)
ant_pos.append(anty_plus_amt)
t = t + 1
else:
anty_plus_amt = 0
conf_plus_amt = 0
for i in range(len(opinions)):
if behaviours[i] == 1:
if opinions[i] == 1:
conf_plus_amt = conf_plus_amt + 1
if behaviours[i] == -1:
if opinions[i] == 1:
anty_plus_amt = anty_plus_amt + 1
conf_pos.append(conf_plus_amt)
ant_pos.append(anty_plus_amt)
t = t + 1
times = list(range(1, len(conf_pos)+1))
print(conf_pos)
positive_conc_conf = list()
positive_conc_anty = list()
for i in conf_pos:
count = behaviours.count(1)
positive_conc_conf.append(i/count)
for i in ant_pos:
positive_conc_anty.append(i/behaviours.count(-1))
plt.plot(times, positive_conc_conf, positive_conc_anty)
plt.show()