fork download
  1. colors = [['red', 'green', 'green', 'red' , 'red'],
  2. ['red', 'red', 'green', 'red', 'red'],
  3. ['red', 'red', 'green', 'green', 'red'],
  4. ['red', 'red', 'red', 'red', 'red']]
  5.  
  6. measurements = ['green', 'green', 'green' ,'green', 'green']
  7.  
  8.  
  9. motions = [[0,0],[0,1],[1,0],[1,0],[0,1]]
  10.  
  11. sensor_right = 0.7
  12.  
  13. p_move = 0.8
  14.  
  15. def show(p):
  16. for i in range(len(p)):
  17. print p[i]
  18.  
  19. #DO NOT USE IMPORT
  20. #ENTER CODE BELOW HERE
  21. #ANY CODE ABOVE WILL CAUSE
  22. #HOMEWORK TO BE GRADED
  23. #INCORRECT
  24.  
  25. #make same structure list as colors list
  26. def duplicateMatrix(l):
  27. q = []
  28. for i in range(len(l)):
  29. q.append([])
  30. for j in range(len(l[i])):
  31. q[i].append(0)
  32. return q
  33.  
  34. # update probabilities on move
  35. def move(m, p):
  36. q = duplicateMatrix(p)
  37. for i in range(x):
  38. for j in range(y):
  39. s = p[(i - m[0]) % x][(j - m[1]) % y] * p_move
  40. s += p[i][j] * (1 - p_move)
  41. q[i][j] = s
  42. return q
  43.  
  44. # update probabilities on sense
  45. def sense(m, p):
  46. q = duplicateMatrix(p)
  47. total = 0
  48. for i in range(x):
  49. for j in range(y):
  50. hit = (m == colors[i][j])
  51. q[i][j] = p[i][j] * (hit * sensor_right + (1-hit) * (1-sensor_right))
  52. total += q[i][j]
  53. # normalize
  54. for i in range(x):
  55. for j in range(y):
  56. q[i][j] /= total
  57.  
  58. return q
  59.  
  60. # initialize probabilities list
  61. p = duplicateMatrix(colors)
  62.  
  63. # get size of list as x and y
  64. (x,y)=(len(colors), len(colors[0]))
  65.  
  66. # total number of grids
  67. n = x * y
  68.  
  69. # fill p list with uniform distribution
  70. for i in range(x):
  71. for j in range(y):
  72. p[i][j] = 1./n
  73.  
  74. # execute actions
  75. for i in range(len(motions)):
  76. p = move(motions[i], p)
  77. p = sense(measurements[i], p)
  78.  
  79. #Your probability array must be printed
  80. #with the following code.
  81.  
  82. show(p)
Success #stdin #stdout 0.03s 6660KB
stdin
Standard input is empty
stdout
[0.011059807427972012, 0.02464041578496803, 0.067996628067859152, 0.044724870458121582, 0.02465153121665372]
[0.0071532041833209815, 0.010171326481705892, 0.086965960026646888, 0.07988429965998084, 0.0093506685084371859]
[0.0073973668861116709, 0.0089437306704527025, 0.11272964670259776, 0.35350722955212721, 0.040655492078276775]
[0.0091065058056464965, 0.0071532041833209815, 0.014349221618346574, 0.043133291358448948, 0.036425599329004736]