From 52a4a93688657bf57c4ee8beab53c230a1442dda Mon Sep 17 00:00:00 2001 From: Roman Romanchuk Date: Sat, 3 Mar 2012 19:37:31 +0200 Subject: [PATCH 1/2] Answer for homework 1.4 added --- homework1.4/task.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/homework1.4/task.py b/homework1.4/task.py index 5da90d9..814aa7f 100644 --- a/homework1.4/task.py +++ b/homework1.4/task.py @@ -27,9 +27,47 @@ def calculate(): p = [] + l = len(colors[1]) + h = len(colors) + for i in range(h): + p_row = [] + for j in range(l): + p_row.append(1.0/h*l) + p.append(p_row) + + def move(p, U): + q = [] + for i in range(h): + q_row = [] + for j in range(l): + q_row.append(p_move*p[(i-U[0])%h][(j-U[1])%l]+(1-p_move)*p[i%h][j%l]) + q.append(q_row) + return q + + def sense(p, Z): + q=[] + for i in range(h): + q_row = [] + for j in range(l): + hit = (Z == colors[i][j]) + q_row.append(p[i][j] * (hit * sensor_right + (1-hit) * (1-sensor_right))) + q.append(q_row) + s=0 + for i in range(len(q)): + s+=sum(q[i]) + + for i in range(h): + for j in range(l): + q[i][j] = q[i][j] / s + return q + + + for i in range(len(motions)): + p = move(p, motions[i]) + p = sense(p, measurements[i]) #Your probability array must be printed #with the following code. - show(p) + #show(p) return p From a7d835fb3849c0dbcd2d8a47451b43bd25e70888 Mon Sep 17 00:00:00 2001 From: blertap Date: Thu, 24 Jan 2013 15:27:26 +0100 Subject: [PATCH 2/2] Update homework1.4/task.py --- homework1.4/task.py | 95 ++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 58 deletions(-) diff --git a/homework1.4/task.py b/homework1.4/task.py index 814aa7f..74e9b22 100644 --- a/homework1.4/task.py +++ b/homework1.4/task.py @@ -3,71 +3,50 @@ ['red', 'red', 'green', 'green', 'red'], ['red', 'red', 'red', 'red', 'red']] -measurements = ['green', 'green', 'green', 'green', 'green'] +measurements = ['green', 'green', 'green' ,'green', 'green'] +p=[] - -motions = [[0, 0], [0, 1], [1, 0], [1, 0], [0, 1]] +motions = [[0,0],[0,1],[1,0],[1,0],[0,1]] sensor_right = 0.7 - p_move = 0.8 +sensor_wrong=1.0-sensor_right +p_stay=1.0-p_move + +def sense(p,colors,measurement): + aux=[[0.0 for row in range (len(p[0]))] for col in range(len(p))] + s=0.0 + + for i in range(len(p)): + for j in range (len(p[i])): + hit=(measurement==colors[i][j]) + aux[i][j]=p[i][j]*(hit*sensor_right+(1-hit)*(sensor_wrong)) + s+=aux[i][j] + + for i in range (len(aux)): + for j in range (len(p[i])): + aux[i][j]/=s + return aux + +def move(p, motion): + aux=[[0.0 for row in range(len(p[0]))] for col in range (len(p))] + + for i in range(len(p)): + for j in range (len(p[i])): + aux[i][j]=(p_move*p[(i-motion[0])%len(p)][(j-motion[1])%len(p[i])])+(p_stay*p[i][j]) + return aux + def show(p): for i in range(len(p)): print p[i] + +pinit=1.0/float(len(colors))/float(len(colors[0])) +p=[[pinit for row in range(len(colors[0]))] for col in range(len(colors))] + +for k in range(len(measurements)): + p=move(p, motions[k]) + p=sense(p,colors,measurements[k]) -def calculate(): - - #DO NOT USE IMPORT - #ENTER CODE BELOW HERE - #ANY CODE ABOVE WILL CAUSE - #HOMEWORK TO BE GRADED - #INCORRECT - - p = [] - - l = len(colors[1]) - h = len(colors) - for i in range(h): - p_row = [] - for j in range(l): - p_row.append(1.0/h*l) - p.append(p_row) - - def move(p, U): - q = [] - for i in range(h): - q_row = [] - for j in range(l): - q_row.append(p_move*p[(i-U[0])%h][(j-U[1])%l]+(1-p_move)*p[i%h][j%l]) - q.append(q_row) - return q - - def sense(p, Z): - q=[] - for i in range(h): - q_row = [] - for j in range(l): - hit = (Z == colors[i][j]) - q_row.append(p[i][j] * (hit * sensor_right + (1-hit) * (1-sensor_right))) - q.append(q_row) - s=0 - for i in range(len(q)): - s+=sum(q[i]) - - for i in range(h): - for j in range(l): - q[i][j] = q[i][j] / s - return q - - - for i in range(len(motions)): - p = move(p, motions[i]) - p = sense(p, measurements[i]) - #Your probability array must be printed - #with the following code. - - #show(p) - return p - +show(p)