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
|
colors = [['red', 'green', 'green', 'red' , 'red'],
['red', 'red', 'green', 'red', 'red'],
['red', 'red', 'green', 'green', 'red'],
['red', 'red', 'red', 'red', 'red']]
measurements = ['green', 'green', 'green' ,'green', 'green']
motions = [[0,0],[0,1],[1,0],[1,0],[0,1]]
sensor_right = 0.7
p_move = 0.8
def show(p):
for i in range(len(p)):
print p[i]
#DO NOT USE IMPORT
#ENTER CODE BELOW HERE
#ANY CODE ABOVE WILL CAUSE
#HOMEWORK TO BE GRADED
#INCORRECT
equi_prob = 1./ (len(colors) * len(colors[0]))
mat_x = len(colors)
mat_y = len(colors[0])
p = [[equi_prob for x in range(mat_y)] for y in range(mat_x)]
def sense(p, Z):
q = p
s = 0
for i in range(mat_x):
for j in range(mat_y):
hit = (Z == colors[i][j])
q[i][j] = p[i][j] * (hit * sensor_right + (1-hit) * (1-sensor_right))
s += q[i][j]
for i in range(mat_x):
for j in range(mat_y):
q[i][j] = q[i][j] / s
return q
def moveRight(p):
q = [[equi_prob for x in range(mat_y)] for y in range(mat_x)]
for i in range(mat_x):
for j in range(mat_y):
k = i+1
if k >= mat_x:
k=0
q[k][j] = p[i][j]*(p_move) + p[k][j]*(1-p_move)
return q
def moveLeft(p):
q = [[equi_prob for x in range(mat_y)] for y in range(mat_x)]
for i in range(mat_x):
for j in range(mat_y):
k = i-1
if k < 0:
k=mat_x-1
q[k][j] = p[i][j]*(p_move) + p[k][j]*(1-p_move)
return q
def moveUp(p):
q = [[equi_prob for x in range(mat_y)] for y in range(mat_x)]
for i in range(mat_x):
for j in range(mat_y):
k = j-1
if k < 0:
k=mat_y-1
q[i][k] = p[i][j]*(p_move) + p[i][k]*(1-p_move)
return q
def moveDown(p):
q = [[equi_prob for x in range(mat_y)] for y in range(mat_x)]
for i in range(mat_x):
for j in range(mat_y):
k = j+1
if k >= mat_y:
k=0
q[i][k] = p[i][j]*(p_move) + p[i][k]*(1-p_move)
return q
def process(p):
for i in range(len(measurements)):
if motions[i][0] == 1:
p = moveRight(p)
if motions[i][0] == -1:
p = moveLeft(p)
if motions[i][1] == -1:
p = moveUp(p)
if motions[i][1] == 1:
p = moveDown(p)
p = sense(p, measurements[i])
return p
p = process(p)
#Your probability array must be printed
#with the following code.
#
show(p) |
Partager