Skip to content

Instantly share code, notes, and snippets.

@ptasheq
Created March 26, 2015 09:13
Show Gist options
  • Save ptasheq/866f1d364c7b606f4e5b to your computer and use it in GitHub Desktop.
Save ptasheq/866f1d364c7b606f4e5b to your computer and use it in GitHub Desktop.
Uncertainty in Wumpus world
from sys import argv
width = 0
height = 0
def loadMap(filename):
with open(filename, 'r') as f:
global width
global height
height, width = [int(x) for x in f.readline().split()]
p = float(f.readline())
loadedMap = []
for lineNumber in range(0, height):
loadedMap.append(f.readline().split()[0])
return p, loadedMap
def clearImproperIndices(x):
return not((x[1] == width) or (x[0] == height) or (-1 in x))
def getFrontIndices(fieldMap, probTable):
# if unknown field is next to visited, it means it can't go to front
for i in range(0, height):
for j in range(0, width):
if (fieldMap[i][j] == 'O'):
neighbours = ((i, j - 1), (i, j + 1), (i - 1, j), (i + 1, j))
for coords in filter(clearImproperIndices, neighbours):
if fieldMap[coords[0]][coords[1]] == '?':
fieldMap[coords[0]] = fieldMap[coords[0]][:coords[1]] + '|' + fieldMap[coords[0]][coords[1] + 1:]
probTable[coords[0]][coords[1]][0] = 0.0
# connections is a list of fields where at least one has to be a hole
connections = []
front = set()
for i in range(0, height):
for j in range(0, width):
if (fieldMap[i][j] == 'B'):
neighbours = ((i, j - 1), (i, j + 1), (i - 1, j), (i + 1, j))
fieldsAroundSameBreeze = []
for coords in filter(clearImproperIndices, neighbours):
if fieldMap[coords[0]][coords[1]] == '?':
fieldsAroundSameBreeze.append(probTable[coords[0]][coords[1]])
front.add((coords[0], coords[1]))
if len(fieldsAroundSameBreeze):
connections.append(tuple(fieldsAroundSameBreeze))
# iterate over set to get references
front = tuple([probTable[coords[0]][coords[1]], [0.0], [0.0]] for coords in front)
for i in range(0, height):
fieldMap[i] = fieldMap[i].replace('|', '?')
fieldMap = tuple(fieldMap)
return front, connections
if (len(argv) != 3):
raise Exception('Too few arguments!')
prob, fieldMap = loadMap(argv[1])
probTable = tuple([[prob] for j in i] for i in fieldMap)
front, connections = getFrontIndices(fieldMap, probTable)
def checkNextField(index):
for pr in [prob, 1 - prob]:
front[index][0][0] = pr
if front[index][0][0] == 1 - prob:
for con in connections:
if not([0.0] in con) and not([prob] in con):
front[index][0][0] = 0.0
return
# our path in tree is valid - we are in last node
if (index == len(front) - 1):
sum = front[0][0][0]
for field in front[1:]:
sum *= field[0][0]
for field in front:
i = 1 if field[0][0] == prob else 2
field[i][0] += sum
else:
checkNextField(index + 1)
front[index][0][0] = 0.0
if len(front) > 0:
checkNextField(0)
for field in front:
field[0][0] = field[1][0] / (field[1][0] + field[2][0])
line = ''
for i in range(0, height):
for j in range(0, width):
if (fieldMap[i][j] in ('B', 'O')):
probTable[i][j][0] = 0.0
line += format(probTable[i][j][0], '.2f') + ' '
line = line[:-1] + '\n'
with open(argv[2], 'w') as f:
f.write(line[:-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment