-
Notifications
You must be signed in to change notification settings - Fork 0
/
Steady State Network FLow.py
260 lines (218 loc) · 7.86 KB
/
Steady State Network FLow.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from math import sqrt
import numpy as np
import sys
##########################################################################################################
def Jacobi(A,tol = 1.0e-9):
# Jacobi method
# 'A' Matrix is numpy Matrix formatted in Jacobi function
# To find the largest element of off-diagonal A[d,e]
def Max_ELement(A):
n = len(A)
A_Maximum = 0.0
for i in range(n-1):
for j in range(i+1,n):
if abs(A[i,j]) >= A_Maximum:
A_Maximum = abs(A[i,j])
k = i; l = j
return A_Maximum,k,l
# Function Rotate_Matrix Matrix makes A[d,e]=0 and defines the Rotation Matrix
def Rotate_Matrix(A,r,d,e):
n = len(A)
A_Difference = A[e,e] - A[d,d]
if abs(A[d,e]) < abs(A_Difference)*1.0e-36: t = A[d,e]/A_Difference
else:
phi = A_Difference/(2.0*A[d,e])
t = 1.0/(abs(phi) + sqrt(phi**2 + 1.0))
if phi < 0.0: t = -t
c = 1.0/sqrt(t**2 + 1.0); s = t*c
Tau = s/(1.0 + c)
Temp = A[d,e]
A[d,e] = 0.0
A[d,d] = A[d,d] - t*Temp
A[e,e] = A[e,e] + t*Temp
for i in range(d): # This is the case of i < d
Temp = A[i,d]
A[i,d] = Temp - s*(A[i,e] + Tau*Temp)
A[i,e] = A[i,e] + s*(Temp - Tau*A[i,e])
for i in range(d+1,e): # This is the case of d < i < e
Temp = A[d,i]
A[d,i] = Temp - s*(A[i,e] + Tau*A[d,i])
A[i,e] = A[i,e] + s*(Temp - Tau*A[i,e])
for i in range(e+1,n): # This is the case of i > e
Temp = A[d,i]
A[d,i] = Temp - s*(A[e,i] + Tau*Temp)
A[e,i] = A[e,i] + s*(Temp - Tau*A[e,i])
for i in range(n): # This 'for' loop updates transformation matrix
Temp = r[i,d]
r[i,d] = Temp - s*(r[i,e] + Tau*r[i,d])
r[i,e] = r[i,e] + s*(Temp - Tau*r[i,e])
Max_Rot = 5*(n**2) # Setting the limit on number of rotations
p = np.identity(n)*1.0 # Initializing the transformation matrix
for i in range(Max_Rot): # Jacobi Rotation 'for' loop
A_Maximum,k,l = Max_ELement(A)
if A_Maximum < tol:
eigvec = p.tolist()
return eigvec,np.diagonal(A).tolist()
Rotate_Matrix(A,p,k,l)
################################################################################################################################################
def Inverse(A):
# Inverse function contains a list 'A'
def Zero_Matrix(rows, cols):
A = []
for i in range(rows):
A.append([])
for j in range(cols):
A[-1].append(0.0)
return A
def Copy_Matrix(M):
rows = len(M)
cols = len(M[0])
MC = Zero_Matrix(rows, cols)
for i in range(rows):
for j in range(rows):
MC[i][j] = M[i][j]
return MC
def matrix_multiply(A, B):
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if colsA != rowsB:
print('Number of A columns must equal number of B rows.')
sys.exit()
C = Zero_Matrix(rowsA, colsB)
for i in range(rowsA):
for j in range(colsB):
total = 0
for ii in range(colsA):
total += A[i][ii] * B[ii][j]
C[i][j] = total
return C
def Identity_Matrix(x):
matrix = [[0 for j in range(x)] for i in range(x)]
for i in range(x):
matrix[i][i] = 1
return matrix
I=Identity_Matrix(len(A))
AM = Copy_Matrix(A)
IM = Copy_Matrix(I)
n = len(AM)
# A and I matrix are not original matrix as we start the row operations.
# So, the matrices will be called AM for 'A Morphing' and 'IM for I Morphing'
fd = 0
fdScaler = 1. / AM[fd][fd]
for j in range(n):
AM[fd][j] = fdScaler * AM[fd][j]
IM[fd][j] = fdScaler * IM[fd][j]
n = len(A)
Indices = list(range(n))
for i in Indices[0:fd] + Indices[fd+1:]:
crScaler = AM[i][fd]
for j in range(n):
AM[i][j] = AM[i][j] - crScaler * AM[fd][j]
IM[i][j] = IM[i][j] - crScaler * IM[fd][j]
Indices = list(range(n))
for fd in range(1,n):
fdScaler = 1.0 / AM[fd][fd]
for j in range(n):
AM[fd][j] *= fdScaler
IM[fd][j] *= fdScaler
for i in Indices[:fd] + Indices[fd+1:]:
crScaler = AM[i][fd]
for j in range(n):
AM[i][j] = AM[i][j] - crScaler * AM[fd][j]
IM[i][j] = IM[i][j] - crScaler * IM[fd][j]
x=matrix_multiply(IM,I)
return x
###############################################################################################################################
def diagonilization(eigen_val,eigen_vec,iter):
eigvec_Inverse=Inverse(eigen_vec)
def matrix2(y):
arr = [[0 for j in range(1)] for i in range(y)]
x=0
for i in range(y):
arr[i][0]=1/y
x+=1
if x>y:
break
return arr
def diagonal_eigval(eigen_val):
arr = [[0 for j in range(len(eigen_val))] for i in range(len(eigen_val))]
x=0
for i in eigen_val:
arr[x][x]=pow(i,iter)
x+=1
if x>len(eigen_val):
break
return arr
def Zero_Matrix(rows, cols):
A = []
for i in range(rows):
A.append([])
for j in range(cols):
A[-1].append(0.0)
return A
def matrix_multiply(A, B):
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if colsA != rowsB:
print('Number of A columns must equal number of B rows.')
sys.exit()
C = Zero_Matrix(rowsA, colsB)
for i in range(rowsA):
for j in range(colsB):
total = 0
for ii in range(colsA):
total += A[i][ii] * B[ii][j]
C[i][j] = total
return C
dia_eigval=diagonal_eigval(eigen_val)
x=matrix_multiply(eigen_vec,dia_eigval)
z=matrix_multiply(eigvec_Inverse,matrix2(len(eigvec_Inverse)))
y=matrix_multiply(x,z)
return y
#################################################################################################################################################
# Input Begins Here:
print("Instruction: ")
print("1) The Input matrix of the network should be 'Symmetric Matrix'.")
print("2) The sum of Column elments of the Matrix should be 'Unity'.")
print("3) Input Non-Negative values.")
n = int(input("Enter the number of rows:"))
m = int(input("Enter the number of columns:"))
print("Enter the entries rowise(one element at a time): ")
matrix=[]
for i in range(n):
a=[]
for j in range(m):
a.append(float(input()))
matrix.append(a)
A_Matrix=np.array(matrix)
A_list=matrix
print("Input Matrix: ")
for i in A_list:
print(i)
print("")
eigen_vec,eigen_val=Jacobi(A_Matrix,tol = 1.0e-4)
print("Eigen Values of the Matrix: ")
print(eigen_val)
print("")
print("Eigen Vectors of the Matrix(Column wise): ")
for i in eigen_vec:
print(i)
print("")
eigvec_Inverse=Inverse(eigen_vec)
print("Inverse of Eigen Vector Matrix(Column wise):")
for i in eigvec_Inverse:
print(i)
print("")
dia1=diagonilization(eigen_val,eigen_vec,100)
print("Steady State of Network Flow (after 100 iterations): ")
for i in dia1:
print(i)
print("")
dia2=diagonilization(eigen_val,eigen_vec,200)
print("Steady State of Network Flow (after 200 iterations): ")
for i in dia2:
print(i)