-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
207 lines (163 loc) · 6.21 KB
/
model.py
File metadata and controls
207 lines (163 loc) · 6.21 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from random import choice
from functools import reduce
import matplotlib.animation as animation # animation plot
import pandas as pd
import networkx as nx
import random_network as rn
fig, ax = plt.subplots(1,1)
#iterations
frames = 500
N = 5
K = 2
number_of_clusters = 2
#################################################################
#creation of the subnetworks
gr = [rn.Random_Network(N,K) for i in range(number_of_clusters)]
control_nodes = [gr[i].control_nodes[0]+i*N for i in range(number_of_clusters)]
env_control_nodes = [gr[i].control_nodes[1]+i*N for i in range(number_of_clusters)]
tot = rn.create_net(gr, control_nodes,env_control_nodes, N,N)
negedges = list(zip(list(np.where(tot.T<0)[0]),list(np.where(tot.T<0)[1])))
#print(negedges)
Net = rn.Network(tot,number_of_clusters)
graph = nx.from_numpy_matrix(tot.T, create_using=nx.DiGraph)
npos = nx.spring_layout(graph)
#cycles = nx.cycle_basis(graph.to_undirected())
################## ONLY FOR VISUALIZATION #######################
tot1 = rn.create_clusters(gr, control_nodes,env_control_nodes, N,number_of_clusters,visual=True)
abs_tot = abs(tot1)
graph1 = nx.from_numpy_matrix(abs_tot.T, create_using=nx.DiGraph)
npos = nx.kamada_kawai_layout(graph1)
#################################################################
def init():
ax = nx.draw(graph,pos = npos)
active_nodes = []
non_active_nodes = []
for i in range(number_of_clusters):
Net.nodes[np.random.randint(N*i, N*(i+1))] = 1
# for i in range(N):
# Net.nodes[i] = 1
for i in range(len(Net.nodes)):
if Net.nodes[i] == 1 :
active_nodes.append(i)
else:
non_active_nodes.append(i)
ax = nx.draw_networkx(graph,npos, with_labels= False)
ax = nx.draw_networkx_nodes(graph,npos,
nodelist=active_nodes,
node_color='y')
return ax,
def evo(frames):
plt.ion()
plt.cla()
up = Net.adj_matrix.dot(Net.nodes)
Net.nodes = (up >0).astype(int)
rn.env(Net,env_control_nodes,p=0.1)
#rn.env(Net,control_nodes,p=0.1)
rn.noise(Net,p=0.2)
active_nodes = []
non_active_nodes = []
for i in range(len(Net.nodes)):
if Net.nodes[i] == 1 :
active_nodes.append(i)
else:
non_active_nodes.append(i)
ax = nx.draw_networkx(graph,npos, with_labels= True)
ax = nx.draw_networkx_nodes(graph,npos,
nodelist=control_nodes,
node_size=800)
ax = nx.draw_networkx_nodes(graph,npos,
nodelist=active_nodes,
node_color='y')
ax = nx.draw_networkx_edges(graph, npos,
edgelist=negedges,
width=3, alpha=0.4, edge_color='r')
plt.title("frame " +str(frames))
return ax
#print(control_nodes)
ani = FuncAnimation(fig, evo, frames = np.arange(0,500), interval = 200,init_func = init, blit = False)
#ani.save('network.gif',dpi = 100,writer = "imagemagick")
plt.savefig("network.png")
# #%%
# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib.animation import FuncAnimation
# from random import choice
# from functools import reduce
# import matplotlib.animation as animation # animation plot
# import pandas as pd
# import networkx as nx
# import random_network as rn
# fig, ax = plt.subplots(1,1)
# #iterations
# frames = 500
# N = 10
# K = 2
# number_of_clusters = 2
# ######################################
# #creation of the subnetworks
# gr = [rn.Random_Network(N,K)]
# gr.append(rn.Random_Network(1, 1))
# gr[1].adj_matrix
# single_cluster_control_nodes = [rn.outgoing_links(gr[i],N) for i in range(number_of_clusters)]
# control_nodes = [single_cluster_control_nodes[i] + i*N for i in range(number_of_clusters)]
# tot = rn.create_clusters(gr, control_nodes, N,number_of_clusters)
# negedges = list(zip(list(np.where(tot.T<0)[0]),list(np.where(tot.T<0)[1])))
# Net = rn.Network(tot)
# graph = nx.from_numpy_matrix(tot.T, create_using=nx.DiGraph)
# npos = nx.spring_layout(graph)
# cycles = nx.cycle_basis(graph.to_undirected())
# ################## ONLY FOR VISUALIZATION #######################
# abs_tot = abs(tot)
# graph1 = nx.from_numpy_matrix(abs_tot.T, create_using=nx.DiGraph)
# npos = nx.kamada_kawai_layout(graph1)
# #################################################################
# def init():
# ax = nx.draw(graph,pos = npos)
# active_nodes = []
# non_active_nodes = []
# for i in range(number_of_clusters):
# Net.nodes[control_nodes[i]] = 1
# for i in range(N):
# Net.nodes[i] = 1
# for i in range(len(Net.nodes)):
# if Net.nodes[i] == 1 :
# active_nodes.append(i)
# else:
# non_active_nodes.append(i)
# ax = nx.draw_networkx(graph,npos, with_labels= False)
# ax = nx.draw_networkx_nodes(graph,npos,
# nodelist=active_nodes,
# node_color='y')
# return ax,
# def evo(frames):
# plt.ion()
# plt.cla()
# up = Net.adj_matrix.dot(Net.nodes)
# Net.nodes = (up >0).astype(int)
# rn.noise(Net,p=0.2)
# active_nodes = []
# non_active_nodes = []
# for i in range(len(Net.nodes)):
# if Net.nodes[i] == 1 :
# active_nodes.append(i)
# else:
# non_active_nodes.append(i)
# ax = nx.draw_networkx(graph,npos, with_labels= True)
# ax = nx.draw_networkx_nodes(graph,npos,
# nodelist=control_nodes,
# node_size=800)
# ax = nx.draw_networkx_nodes(graph,npos,
# nodelist=active_nodes,
# node_color='y')
# ax = nx.draw_networkx_edges(graph, npos,
# edgelist=negedges,
# width=3, alpha=0.4, edge_color='r')
# plt.title("frame " +str(frames))
# return ax
# print(rn.outgoing_links(gr[0], N))
# print(rn.outgoing_links(Net, N))
# ani = FuncAnimation(fig, evo, frames = np.arange(0,500), interval = 200,init_func = init, blit = False)
# #ani.save('network.gif',dpi = 100,writer = "imagemagick")