-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.py
More file actions
130 lines (94 loc) · 2.72 KB
/
net.py
File metadata and controls
130 lines (94 loc) · 2.72 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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation # animation plot
import pandas as pd
import networkx as nx
#iterations
frames = 100
#particles
n = 6
#time step
dt = 0.1
###################################
fig, ax = plt.subplots(2,2)
#matrix = np.random.randint(2, size=(n,n))
matrix = np.array([[0,1,0,0,0,0],
[0,0,1,0,0,0],
[0,0,0,1,0,0],
[0,0,0,0,1,0],
[0,0,1,0,0,0],
[0,0,0,0,0,0]])
matrix = matrix.T
nodes = np.zeros((n,1))
up = np.zeros((n,1))
#initial state
# nodes[0] = 1
nodes[0] = 1
#nodes = np.random.randint(2,size= (n,1))
labels = {}
g = nx.Graph()
pos = nx.layout.spring_layout(g)
time = []
def decimal(u):
b = '\n'.join(''.join('%d' %x for x in y) for y in u)
#return int(b,2)
return b
def random_adjancency_matrix(n):
matrix = np.zeros((n,n))
for i in range(n):
matrix[i][np.random.randint(n)] = 1
return matrix
# matrix = random_adjancency_matrix(n).T
# print(matrix)
#g1 = nx.from_numpy_matrix(matrix)
def init():
ax[0,0].imshow(nodes.T)
#ax[1,0] = nx.draw(g)
ax[0,1].imshow(matrix.T)
ax[1,0] = nx.draw(g)
state = decimal(nodes.T)
labels[state] = state
ax[1,0] = nx.draw_networkx_labels(g,labels,font_size=5)
return ax[0,0],
def change():
nodes = np.zeros((n,1))
up = np.zeros((n,1))
def evo(frames):
plt.ion()
plt.cla()
up = np.zeros((n,1))
#up = matrix.dot(nodes)
previous_state = decimal(nodes.T)
for i in range(n):
somma = 0
for j in range(n):
somma = somma + matrix[i][j]*nodes[j]
up[i] = somma
for i in range(n):
if up[i] != 0:
nodes[i] = 1
else:
nodes[i] = 0
time.append(1)
### changin initial conditions after a time t
if len(time)>20:
nodes[-1] = 1
matrix[4][5] = 1
state = decimal(nodes.T)
g.add_edge(previous_state,state)
ax[0,0].imshow(nodes.T)
npos = nx.layout.shell_layout(g)
ax[1,0] = nx.draw(g,pos = npos)
ax[1,0] = nx.draw_networkx_nodes(g,npos,
nodelist=[state],
node_color='g')
ax[1,0] = nx.draw_networkx_nodes(g,npos,
nodelist=[previous_state],
node_color='y')
labels[state] = state
ax[1,0] = nx.draw_networkx_labels(g,npos,labels,font_size=5)
ax[0,1].imshow(matrix)
return ax[0,0], ax[1,0] ,ax[1,1], ax[0,1]
ani = FuncAnimation(fig, evo, frames = np.arange(0,100), interval = 200,init_func = init, blit = False)
#ani.save('network.gif',dpi = 100,writer = "imagemagick")