-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_graph.py
171 lines (119 loc) · 4.65 KB
/
full_graph.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
import random as rnd
import matplotlib.pyplot as plt
import numpy as np
import pickle
import pandas as pd
import networkx as nx
# 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 create_real_graph(file_name):
graph = nx.Graph()
edges = pd.read_csv(file_name, names= ["Start", "End"])
start = edges["Start"].tolist()
end = edges["End"].tolist()
for i in range(len(start)):
graph.add_edge(start[i], end[i])
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
def opinions(N):
#G = pickle.load(open(f'graph{file}.pickle', 'rb'))
G = nx.complete_graph(N)
N = G.number_of_nodes()
opinions = initial_opinions(c, N)
behaviours = initial_behaviours(p, N)
time = 0
ones_total = list()
minus_total = list()
while time <= msc_all:
ones = list()
minus = list()
for t in range(N):
i = rnd.randint(1, N-1)
rnd_agent_bh = behaviours[i]
neighbours = list(G.neighbors(i))
if rnd_agent_bh == -1:
rnd_neigh = rnd.sample(neighbours, min(q_a, len(neighbours)))
neigh_ops = list()
for l in range(len(rnd_neigh)):
if rnd_neigh[l] < len(opinions):
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, min(q_c, len(neighbours)))
neigh_ops = list()
for l in range(len(rnd_neigh)):
if rnd_neigh[l] < len(opinions):
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, 8))
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'Full Graph, N = {N}\n '
f'Algorithm parameters - Q_A = {q_a}, Q_C = {q_c}, p = {p}, c = {c}\n'
f'Clustering coefficient = {nx.average_clustering(G)}, Average degree = {np.mean([d for _, d in G.degree()])}')
plt.show()
return times, positive_conc, negative_conc
opinions(1000)