-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
75 lines (55 loc) · 1.6 KB
/
python.py
File metadata and controls
75 lines (55 loc) · 1.6 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
colors = [['red', 'green', 'green', 'red' , 'red'],
['red', 'red', 'green', 'red', 'red'],
['red', 'red', 'green', 'green', 'red'],
['red', 'red', 'red', 'red', 'red']]
measurements = ['green', 'red']
motions = [[0,0], [1,0]]
sensor_right = 0.8
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
total = len(colors) * len(colors[0])
n = 1.0 / total
p = [[n, n, n, n, n],
[n, n, n, n, n],
[n, n, n, n, n],
[n, n, n, n, n]]
def makeMove(motion):
temp = []
for i in range(len(p)):
temp.append([])
for j in range(len(p[0])):
y = ((i - motion[0]) % len(p))
x = ((j - motion[1]) % len(p[0]))
#print "p[" + str(y) + "][" + str(x) + "] = " + str(p[y][x])
temp[i].append((1 - p_move) * p[i][j] + p_move * p[y][x])
return temp
def sense(measurement):
q = [];
for i in range(len(p)):
q.append([]);
for j in range(len(p[0])):
hit = (colors[i][j] == measurement)
n = p[i][j] * (hit * sensor_right + (1 - hit) * (1 - sensor_right))
q[i].append(n)
qsum = []
for i in range(len(q)):
qsum.append(sum(q[i]))
s = sum(qsum)
for i in range(len(q)):
for j in range(len(q[0])):
q[i][j] = q[i][j] / s
return q
for i in range(len(motions)):
p = makeMove(motions[i])
p = sense(measurements[i])
show(p)
#Your probability array must be printed
#with the following code.
show(p)