-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateenvironment.py
353 lines (270 loc) · 13.9 KB
/
stateenvironment.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import copy
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
from environment import Environment
class State:
EMPTY = 0
PLAYER = 1
BOX = 2
WALL = 3
def __init__(self, walls, boxes, player, storage, xlim, ylim):
self.map = np.zeros((xlim + 1, ylim + 1))
for wall in walls:
self.map[wall] = self.WALL
for box in boxes:
self.map[box] = self.BOX
self.map[tuple(player)] = self.PLAYER
self.player = player
self.storage = set(storage)
self.boxes = np.array(boxes)
# self.max_score = 0
def __hash__(self):
return self.map.tobytes()
def copy(self):
"""
Return a value-copied instance.
"""
s = State(walls=[], boxes=[], player=(0, 0), storage=self.storage, xlim=0, ylim=0)
s.map = np.copy(self.map)
s.player = np.copy(self.player)
s.boxes = np.copy(self.boxes)
s.storage = self.storage.copy()
# s.max_score = self.max_score
return s
class StateEnvironment(Environment):
def __init__(self, filename, walls, boxes, player, storage, xlim, ylim, pause=0.05):
super().__init__(filename, xlim, ylim)
self.state = State(walls, boxes, player, storage, xlim, ylim)
self.pause = pause
self.deadlock_table = {}
self.original_state = copy.deepcopy(self.state)
self.state_hash = None
self.cache_miss = 0
self.cache_hit = 0
def reset(self):
# print("reset!")
# print(f"player:{self.state[0]}")
# print(f"reset_player:{self.original_player}")
self.state = copy.deepcopy(self.original_state)
def is_valid(self, location):
x, y = location
return (x >= 0 and x <= self.xlim and y >= 0 and y <= self.ylim)
def count_goals(self, state):
return sum([1 if state.map[place] == State.BOX else 0 for place in state.storage])
# count = 0
# for place in state.storage:
# if state.map[place] == State.BOX:
# count += 1
# return count
def is_goal_state(self, state):
for place in state.storage:
if state.map[place] != State.BOX:
return False
return True
def get_player(self, state):
return state.player
def get_neighbors(self, location):
return [location + direction for direction in Environment.DIRECTIONS]
def is_frozen(self, state, location, previous=None):
''''
Detects if a given box in a state in frozen deadlock.
previous: the set of edges for dependencies. (a depends b)
'''
if location.tobytes() in self.deadlock_table[self.state_hash]:
return self.deadlock_table[self.state_hash][location.tobytes()]
location_tuple = tuple(location)
neighbors = self.get_neighbors(location)
state_map = state.map
for i in range(len(neighbors)):
neighbor = tuple(neighbors[i])
next_neighbor = tuple(neighbors[(i + 1) % len(neighbors)])
# if location_tuple == (9,3) or location_tuple == (9, 4) or location_tuple == (8, 4):
# print(f"neighbor:{neighbor}")
# print(f"next_neighbor:{next_neighbor}")
# print(f"frozen_neighbor:{(neighbor, location_tuple) in previous}")
# print(f"frozen_next_neighbor:{(next_neighbor, location_tuple) in previous}")
if state_map[neighbor] == State.WALL and state_map[next_neighbor] == State.WALL:
self.deadlock_table[self.state_hash][location.tobytes()] = True
return True
elif state_map[neighbor] == State.WALL and state_map[next_neighbor] == State.BOX:
previous.add((location_tuple, next_neighbor))
if (next_neighbor, location_tuple) in previous:
# depndency cycle!
return True
if self.is_frozen(state, np.array(next_neighbor), previous):
return True
previous.remove((location_tuple, next_neighbor))
elif state_map[neighbor] == State.BOX and state_map[next_neighbor] == State.WALL:
previous.add((location_tuple, neighbor))
# print("case 3")
if (neighbor, location_tuple) in previous:
# dependency cycle!
return True
if self.is_frozen(state, np.array(neighbor), previous):
return True
previous.remove((location_tuple, neighbor))
elif state_map[neighbor] == State.BOX and state_map[next_neighbor] == State.BOX:
previous.add((location_tuple, neighbor))
previous.add((location_tuple, next_neighbor))
frozen_neighbor = True if (neighbor, location_tuple) in previous else self.is_frozen(state,
np.array(neighbor),
previous)
frozen_next_neighbor = True if (next_neighbor, location_tuple) in previous else self.is_frozen(state,
np.array(
next_neighbor),
previous)
# ax = plt.gca()
# rect = patches.Rectangle((neighbor[0]+0.5, neighbor[1]+0.5), -1, -1, linewidth=0.5, edgecolor='deepskyblue')
# ax.add_patch(rect)
# rect = patches.Rectangle((next_neighbor[0]+0.5, next_neighbor[1]+0.5), -1, -1, linewidth=0.5, edgecolor='deepskyblue')
# ax.add_patch(rect)
# plt.show(block=True)
if frozen_neighbor and frozen_next_neighbor:
return True
if (location_tuple, neighbor) in previous:
previous.remove((location_tuple, neighbor))
if (location_tuple, next_neighbor) in previous:
previous.remove((location_tuple, next_neighbor))
return False
def is_dead_diagonal(self, state, location):
state_map = state.map
directions = Environment.DIRECTIONS
# neighbors = self.get_neighbors(location)
# 0 top right
# 1 bottom right
# 2 bottom left
# 3 top
if tuple(location) in state.storage:
return False
diagonals = [location + directions[i] + directions[(i + 1) % len(directions)] for i in range(len(directions))]
for i in range(len(diagonals)):
diagonal_neighbor = tuple(diagonals[i])
diagonal_next_neighbor = tuple(diagonals[(i + 1) % 4])
orientation = directions[(i + 1) % len(directions)]
across = tuple(2 * orientation + location)
center = location + orientation
if tuple(center) in state.storage:
continue
if self.is_valid(across) and state_map[tuple(center)] == State.EMPTY:
if state_map[diagonal_neighbor] > State.PLAYER and state_map[diagonal_next_neighbor] > State.PLAYER and state_map[across] > State.PLAYER: # hack for checking if its box or wall
possible_corner1 = location + directions[i]
possible_corner2 = location + directions[(i + 2) % len(directions)]
opposite_corner1 = possible_corner2 + 2 * orientation
opposite_corner2 = possible_corner1 + 2 * orientation
if ((state_map[tuple(possible_corner1)] > State.PLAYER and state_map[
tuple(opposite_corner1)] > State.PLAYER) or (
state_map[tuple(possible_corner2)] > State.PLAYER and state_map[
tuple(opposite_corner2)] > State.PLAYER)):
# dead diagonal! hash all valid locations and update...
surrounding = [diagonal_neighbor, diagonal_next_neighbor, across, possible_corner1,
possible_corner2, opposite_corner1, opposite_corner2, location]
for place in np.array(surrounding):
if state_map[tuple(place)] == State.BOX and tuple(place) not in state.storage:
self.deadlock_table[self.state_hash][place.tobytes()] = True
return True
# print(f"return false for {location}")
return False
def is_deadlock(self, state):
# if self.cache_miss != 0 and self.cache_hit != 0:
# print(f"deadlock_table_rate:{self.cache_hit/(self.cache_hit + self.cache_miss)}")
# if not self.frozen_nodes:
# self.frozen_nodes = set([])
self.state_hash = state.map.tobytes()
if self.state_hash not in self.deadlock_table:
self.cache_miss += 1
self.deadlock_table[self.state_hash] = {}
else:
self.cache_hit += 1
frozen_count = 0
for box in state.boxes:
#print(box)
box_hash = box.tobytes()
if tuple(box) not in state.storage:
if box_hash in self.deadlock_table[self.state_hash]:
if self.deadlock_table[self.state_hash][box_hash]:
frozen_count += 1
else:
if self.is_frozen(state, box, previous=set([])):
self.deadlock_table[self.state_hash][box.tobytes()] = True
frozen_count += 1
elif self.is_dead_diagonal(state, box):
frozen_count += 1
return (len(state.boxes) - frozen_count < len(state.storage))
# if len(state.boxes) - frozen_count < len(state.storage):
# return True
# return False
def next_state(self, state, action):
'''
Returns a copy with the next state.
'''
player = self.get_player(state)
next_position = np.array(player) + action
next_state = state.copy()
if state.map[tuple(next_position)] == State.BOX:
next_box_position = next_position + action
if state.map[tuple(next_box_position)] == State.EMPTY:
next_state.map[tuple(player)] = State.EMPTY
next_state.player = next_position
next_state.map[tuple(next_position)] = State.PLAYER
next_state.map[tuple(next_box_position)] = State.BOX
for index in range(len(next_state.boxes)):
if (next_state.boxes[index] == next_position).all():
next_state.boxes[index] = next_box_position
elif state.map[tuple(next_position)] == State.WALL:
pass
elif state.map[tuple(next_position)] == State.EMPTY:
# print("EMPTY")
next_state.map[tuple(player)] = State.EMPTY
next_state.map[tuple(next_position)] = State.PLAYER
next_state.player = next_position
# return next_position
#score_count = self.count_goals(next_state)
# if next_state.max_score < score_count:
# next_state.max_score = score_count
return next_state
def draw(self, state, save_figure=False):
# print(f"num_score:{self.state[1,0]}")
ax = plt.gca()
ax.clear()
# create square boundary
lim = max(self.xlim, self.ylim)
plt.xlim(0, lim + 1)
plt.ylim(0, lim + 1)
ax.set_xticks(np.arange(0, lim + 1))
ax.set_yticks(np.arange(0, lim + 1))
plt.grid(alpha=0.2)
deadlock_flag = self.is_deadlock(state)
for i in range(self.xlim + 1):
for j in range(self.ylim + 1):
# print((i,j))
if state.map[i, j] == State.WALL:
rect = patches.Rectangle((i + 0.5, j + 0.5), -1, -1, linewidth=0.5, edgecolor='slategray',
facecolor='slategray')
ax.add_patch(rect)
elif state.map[i, j] == State.PLAYER:
plt.plot(i, j, 'o', color='orange')
elif state.map[i, j] == State.BOX:
nphash = np.array([i, j]).tobytes()
if deadlock_flag and nphash in self.deadlock_table[self.state_hash] and \
self.deadlock_table[self.state_hash][nphash]:
rect = patches.Rectangle((i + 0.5, j + 0.5), -1, -1, linewidth=0.5, edgecolor='red',
facecolor='red')
ax.add_patch(rect)
else:
rect = patches.Rectangle((i + 0.5, j + 0.5), -1, -1, linewidth=0.5, edgecolor='tan',
facecolor='tan')
ax.add_patch(rect)
for place in state.storage:
circle = patches.Circle(place, 0.05, edgecolor='limegreen', facecolor='limegreen')
ax.add_patch(circle)
# plt.draw()
# plt.show()
if save_figure:
plt.savefig('sokoban.png')
else:
plt.show(block=False)
# background = fig.canvas.copy_from_bbox(ax.bbox)
# fig.canvas.restore_region(background)
# fig.canvas.draw()
plt.pause(self.pause)