-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex4.py
167 lines (130 loc) · 4.62 KB
/
ex4.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sys
import queue
import random
GOAL = 'G'
START = 'S'
GOST = 'B' # Both goal and start
WALL = '#'
class ComaState:
def __init__(self, states, dir="B", prev=None):
"""
`state` is a sorted tuple of states of possible commando positions
"""
self.state = states
self.depth = 0 if prev is None else prev.depth + 1
self.prev = prev
self.dir = dir
self.len = len(self.state)
self.F = self.depth
self.HASH = hash(tuple(sorted(self.state)))
def __hash__(self):
return self.HASH
def __eq__(self, other):
return self.state == other.state
def __str__(self):
return ', '.join(["({} {})".format(x, y) for x, y in self.state])
def __repr__(self):
return self.__str__()
def __lt__(self, other):
return self.F < other.F
class Commando:
DIR = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1), "B": (0, 0)}
MOVES = ["U", "D", "L", "R"]
def __init__(self, board, uncert=False, non_admissible=False):
self.board, self.goals, self.starts = self.stripBoard(board)
self.initState = ComaState(self.starts)
if uncert:
self.initState = self.uncertainty(self.initState)
self.non_admissible = non_admissible
def stripBoard(self, board):
"""
Return values:
board : [[]] a board with '.' and '#' only
goals : set() a set of tuples of goals
starts : set() a set of tuples of start fields
"""
starts = []
goals = []
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == GOAL:
goals.append((i, j))
board[i][j] = ' '
if board[i][j] == START:
starts.append((i, j))
board[i][j] = ' '
if board[i][j] == GOST:
starts.append((i, j))
goals.append((i, j))
board[i][j] = ' '
return board, set(goals), set(starts)
def comaToStr(self):
b = [b[:] for b in self.board]
for i, j in self.initState.state:
b[i][j] = START
for i, j in self.goals:
b[i][j] = GOAL
for i, j in self.goals & self.initState.state:
b[i][j] = GOST
return b
def __str__(self):
return '\n'.join([''.join(row) for row in self.comaToStr()])
def __repr__(self):
return self.__str__()
def move(self, state, move):
x, y = state[0] + self.DIR[move][0], state[1] + self.DIR[move][1]
return (x, y) if self.board[x][y] != WALL else state
def getNeighbour(self, comaState, move):
return ComaState({self.move(s, move) for s in comaState.state},
move, comaState)
def uncertainty(self, state):
prev = None
while True:
ans = dict((m, len(self.getNeighbour(state, m).state))
for m in random.sample(self.MOVES, 4))
m = min(ans, key=ans.get)
prev = state
state = self.getNeighbour(state, m)
if ans[m] < 3 or prev == state:
return state
return state
def isSolved(self, state):
return state.state.issubset(self.goals)
def traceback(self, state):
ans = []
while state.depth > 0:
ans.append(state.dir)
state = state.prev
return ''.join(reversed(ans))
def playBfs(self):
state = self.initState
q = queue.Queue()
q.put(state)
visited = set([state])
stLen = state.len
while q.empty() is False and self.isSolved(state) is False:
state = q.get()
# Let the Q with pending states be descending in lengths
if state.len <= stLen:
for move in random.sample(self.MOVES, 4):
neigh = self.getNeighbour(state, move)
if neigh not in visited:
q.put(neigh)
visited = visited | set([neigh])
stLen = neigh.len
return self.traceback(state)
def readBoard(finput):
board = []
with open(finput) as f:
board = [list(line.strip('\n')) for line in f]
return board
if __name__ == '__main__':
finput = 'zad_input.txt'
foutput = 'zad_output.txt'
if len(sys.argv) == 2:
finput = sys.argv[1]
coma = Commando(readBoard(finput), uncert=True)
ans = coma.playBfs()
print(ans)
fout = open(foutput, "w")
print(ans, file=fout)