-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockedMatrix.py
50 lines (46 loc) · 1.53 KB
/
BlockedMatrix.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
#####################################################
## Created on Nov 28, 2015 by Ailing Zhang ##
#####################################################
import numpy as np
from random import *
import pdb
from plotGraph import plotGraph
# BlockedMatrix function is used to generate the Q matrix of a Blocked Markov process.
# For every 2 blocks, we randomly pick 1 state in each block and connect them.
# Otherwise there is no steady state solution for blocked matrix.
# n is the number of states
# m is number of blocks
def BlockedMatrix(n,m):
res = np.zeros((n, n))
cluster = [[] for x in range(m)]
for i in range(n):
r = randint(0, m-1)
cluster[r].append(i)
print cluster
for j in range(m):
for k in range(len(cluster[j])):
local_sum = 0
row = cluster[j][k]
for l in range (len(cluster[j])):
col = cluster[j][l]
if(l!=k):
res[row][col]=randint(1, 3)
local_sum = local_sum + res[row][col]
res[row][row] = -local_sum
print res
plotGraph(res, "orignal")
for c1 in range(m):
for c2 in range(m):
if c1 != c2 and len(cluster[c1])>0 and len(cluster[c2]) > 0:
row = cluster[c1][randint(0, len(cluster[c1])-1)]
col = cluster[c2][randint(0, len(cluster[c2])-1)]
res[row][col] = 1
res[row][row] = res[row][row] - res[row][col]
print (row, col)
return res
'''__main()'''
if __name__ == "__main__":
n = 100
m = 4
Q = BlockedMatrix(n, m)
plotGraph(Q, "general")