Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created July 19, 2013 21:16
Show Gist options
  • Select an option

  • Save vicapow/6042414 to your computer and use it in GitHub Desktop.

Select an option

Save vicapow/6042414 to your computer and use it in GitHub Desktop.
colors = [['red', 'green', 'green', 'red' , 'red'],
['red', 'red', 'green', 'red', 'red'],
['red', 'red', 'green', 'green', 'red'],
['red', 'red', 'red', 'red', 'red']]
# for testing
colors = [['red', 'green']
, ['green', 'green']]
measurements = ['red', 'green', 'green' ,'green', 'green']
motions = [[0,0],[0,1],[1,0],[1,0],[0,1]]
sensor_right = 0.7
# for testing
p_move = 0.8
p_undershoot = 0.1
p_overshoot = 0.1
# for testing
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
rows = len(colors)
cols = len(colors[0])
# normalized, uniform distribution
p = [0] * rows
for i in range(rows):
p[i] = [1. / ( rows * cols )] * cols
def sum2d(p):
s = 0
for row in range(len(p)):
for col in range(len(p[0])):
s = s + p[row][col]
return s
def normalize(p):
s = sum2d(p)
q = []
s = s if s != 0 else 1
for i in range(len(p)):
row = []
for j in range(len(p[0])):
row.append(p[i][j] / s)
q.append(row)
return q
def move(p, m):
# move the robot
for row in range(rows):
for col in range(cols):
pass
# todo: write move code
return p
# p is the world position probability distribution
# Z is the senser reading
# does not normalize
def sense(p, Z):
q = []
for i in range(rows):
row = []
for j in range(cols):
row.append( p[i][j] * ( p_move if colors[i][j] is Z else 1 - p_move ) )
q.append(row)
return q
# p = move(p, motions[0])
print "show p"
show(p)
p = normalize(sense(p, measurements[0]))
print "show p"
show(p)
#Your probability array must be printed
#with the following code.
print "show p"
show(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment