-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp9.py
More file actions
101 lines (93 loc) · 2.58 KB
/
Copy pathp9.py
File metadata and controls
101 lines (93 loc) · 2.58 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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def getPositon(a):
raw, column = a.shape # get the matrix of a raw and column
positon = np.argmax(a) # get the index of max in the a
# print(positon)
r, c = divmod(positon , column)
return r,c
def move(p,u,pMoveCorrect):
nRow=int(len(p))
nCol=int(len(p[0]))
q=np.zeros(ones_size)
for r in range(1,nRow+1):
for c in range(1,nCol+1):
q[r-1,c-1]=pMoveCorrect * p[np.mod(r-1-u[0],nRow),np.mod(c-1-u[1],nCol)]+ (1-pMoveCorrect)* p[r-1,c-1]
# return q
return q
world = (('red', 'green', 'green', 'red', 'red'),
('red', 'red', 'green', 'red', 'red'),
('red', 'red', 'green', 'green', 'red'),
('red', 'red', 'red', 'red', 'red'))
# print(world)
# print(len(world))
# print(len(world[1]))
nRow = len(world)
nCol = len(world[1])
ones_size=(nRow,nCol)
ones=np.ones(ones_size)
step={
'stop':[0,0],
'right':[0,1],
'left':[0,-1],
'down':[1,0],
'up':[-1,0]
}
pMoveCorrect = 0.8
pStart = 0.7
p = (1 - pStart) / (nRow * nCol - 1) * ones
# Fisrt configuration
# p[2,1]=pStart
# motions=step['right']
# Second configuration
p[0,2]=pStart
motions=step['up']
q=move(p,motions,pMoveCorrect)
print('The Prior:\n',p)
print('The probability after moving:\n',q)
r,c=getPositon(q)
print('The largest probability %s occurs at cell(%s,%s)\n'%(q[r,c],r,c))
plt.ion()
h=plt.figure(1)
ax=Axes3D(h)
# x=np.arange(1,nRow+1,1)
# y=np.arange(1,nCol+1,1)
# z=p.flatten()
# x=x.flatten()
# y=y.flatten()
# dx_size=(nRow,nCol)
# dx=np.ones(dx_size)
# dy=dx.copy()
# dz=p.flatten()
# ax.bar3d(x,y,z,dx,dy,dz)
# plt.show()
ly= len(p[0]) #5 # Work out matrix dimensions
lx= len(p[:,0]) #4
xpos = np.arange(0,lx,1) # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)
dx = np.ones_like(zpos)
dy = dx.copy()
dz = p.T.flatten()
ax.bar3d(xpos,ypos,zpos, dx, dy, dz)
h1=plt.figure(2)
ax=Axes3D(h1)
ly= len(q[0]) #5 # Work out matrix dimensions
lx= len(q[:,0]) #4
xpos = np.arange(0,lx,1) # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)
dx = np.ones_like(zpos)
dy = dx.copy()
dz = q.T.flatten()
# color=plt.cm.
ax.bar3d(xpos,ypos,zpos, dx, dy, dz)
plt.ioff()
plt.show()