-
Notifications
You must be signed in to change notification settings - Fork 0
/
rushhour.py
388 lines (299 loc) · 11.4 KB
/
rushhour.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import numpy as np
from enum import Enum
from copy import deepcopy
from dataclasses import dataclass
from bisect import insort
BOARD_SIZE = 6
RED_CAR_ID = 1
RED_CAR_LENGTH = 2
class Direction(Enum):
HORIZONTAL = 1
VERTICAL = 2
@dataclass
class Move:
"""Store a move of car with 'car_id" to position 'pos'"""
car_id: int
delta: int
pos: int
class Car:
def __init__(
self,
car_id: int,
lane: int,
length: int,
direction: Direction,
redcar=False,
):
if not (car_id > 0):
raise ValueError("Car must have car_id > 0")
self.car_id = car_id
self.direction = direction
self.redcar = redcar
if direction == Direction["HORIZONTAL"]:
def get_blocks(p):
return lane, slice(p, p + length)
elif direction == Direction["VERTICAL"]:
def get_blocks(p):
return slice(p, p + length), lane
else:
raise ValueError("Unsupported direction")
self.blocks = dict()
startpos = 0
while True:
self.blocks[startpos] = get_blocks(startpos)
if (startpos + length) >= BOARD_SIZE:
break
startpos += 1
self.possible_positions = sorted(list(self.blocks.keys()))
self.last_position = self.possible_positions[-1]
def __repr__(self) -> str:
rc = "RED " if self.redcar else ""
return f"{rc}Car ID {self.car_id}, pos {self.pos}, direction {self.direction}, len {self.length}"
class Cars:
def __init__(self):
self.cars = dict()
def add_car(self, car):
if car.car_id in self.cars:
raise ValueError(f"Car {car.car_id} already added")
self.cars[car.car_id] = car
def __len__(self) -> int:
return len(self.cars)
def __getitem__(self, car_id) -> Car:
return self.cars[car_id]
class CarPositions:
def __init__(self):
self.positions = dict()
def add_position(self, car_id, pos):
if car_id in self.positions:
raise ValueError(f"Id {car_id} is already in CarPositions")
self.positions[car_id] = pos
def get_positions(self):
return self.positions
def move_car(self, car_id, pos):
self.positions[car_id] = pos
def __iter__(self):
for k, v in self.positions.items():
yield k, v
def __len__(self):
return len(self.positions)
def __getitem__(self, car_id) -> int:
return self.positions[car_id]
def __eq__(self, other):
return self.positions == other.positions
class CarCollision(Exception):
pass
def board_string_to_array(board_string) -> np.ndarray:
rows = []
for line in board_string.splitlines():
row = np.array(line.split()).astype(int)
if len(row) > 0:
rows.append(row)
board_array = np.array(rows)
return board_array
class Board:
def __init__(self, minimize=None, verbose=False):
self.board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=np.uint8)
self.car_positions = CarPositions()
self.cars = Cars()
self.previous_hash = None
self.car_swaps = 0
self.move_count = 0
self.move_to_get_here = None
if minimize is None:
minimize = "car_swaps"
if minimize == "move_count":
self.max_move_distance = 1
elif minimize == "car_swaps":
self.max_move_distance = np.inf
else:
raise ValueError(
"minimize parameter must be either move_count or car_swaps"
)
@classmethod
def board_from_numpy(cls, board_array: np.ndarray, **kwargs):
board = cls(**kwargs)
for car, pos in get_cars(board_array):
board.place_car(car, pos)
if car.car_id == RED_CAR_ID:
if car.direction != Direction.HORIZONTAL:
raise RuntimeError(f"Red car (ID {RED_CAR_ID}) must be horizontal)")
return board
@classmethod
def board_from_str(cls, board_string: str, **kwargs):
return cls.board_from_numpy(board_string_to_array(board_string), **kwargs)
def place_car(self, car, pos):
self.car_positions.add_position(car.car_id, pos)
blocks = car.blocks[pos]
if np.sum(self.board[blocks]) > 0:
# Another car was already placed here
raise CarCollision()
self.board[blocks] = car.car_id
self.cars.add_car(car)
def move_car(self, car_id, pos):
car = self.cars[car_id]
old_pos = self.car_positions[car_id]
old_blocks = car.blocks[old_pos]
self.board[old_blocks] = 0
new_blocks = car.blocks[pos]
self.board[new_blocks] = car_id
self.car_positions.move_car(car_id, pos)
if (self.move_to_get_here is None) or (car_id != self.move_to_get_here.car_id):
self.car_swaps += 1
delta = pos - old_pos
self.move_count += 1
self.move_to_get_here = Move(car_id=car_id, delta=delta, pos=pos)
def is_solved(self):
return self.car_positions[RED_CAR_ID] == (BOARD_SIZE - RED_CAR_LENGTH)
def get_next_boards(self):
next_boards = list()
for car_id, pos in self.car_positions:
car = self.cars[car_id]
# How far we can move in either direction depends on
min_pos = max(pos - self.max_move_distance, 0)
max_pos = min(pos + self.max_move_distance, car.last_position)
for move_pos in [
range(pos - 1, min_pos - 1, -1),
range(pos + 1, max_pos + 1),
]:
for next_pos in move_pos:
new_blocks = car.blocks[next_pos]
if len(set(self.board[new_blocks]) - {0, car.car_id}) > 0:
# If we would move to a position that has other blocks
# than the empty block or the existing car car_id, we couldn't
# Try moving in the other direction instead
break
# Avoid making copy of the .cars object, as
# these are constant for all boards
memo = {}
memo[id(self.cars)] = self.cars
b = deepcopy(self, memo)
b.previous_hash = hash(self)
b.move_car(car_id, next_pos)
next_boards.append(b)
return next_boards
def __lt__(self, other) -> bool:
"""Compare two Boards, based on move-count first, car-swaps secondly"""
if self.move_count == other.move_count:
return self.car_swaps < other.car_swaps
else:
return self.move_count < other.move_count
def __repr__(self) -> str:
r = "\n".join(
[" ".join([f"{car_id:2d}" for car_id in row]) for row in self.board]
)
return r.replace(" 0", " ")
def __hash__(self):
return hash(self.board.tobytes())
class SortedBoards:
def __init__(self):
self.boards = []
def insert(self, board: Board):
"""Insert into list, keeping self.boards sorted in decreasing move count order"""
insort(self.boards, board)
def pop(self) -> Board:
"""Pop the board with the lowest move count, i.e. the first element"""
# TODO : Refactor, to avoid costly pop(0)
return self.boards.pop(0)
def __len__(self) -> int:
return len(self.boards)
def get_cars(board: np.ndarray):
ysize, xsize = board.shape
assert xsize == BOARD_SIZE, "X-size must match BOARD_SIZE"
assert ysize == BOARD_SIZE, "Y-size must match BOARD_SIZE"
for direction, b in [(Direction.HORIZONTAL, board), (Direction.VERTICAL, board.T)]:
for rowpos, row in enumerate(b):
car_id = 0
length = 0
startpos = 0
for colpos in range(xsize + 1):
if colpos < xsize:
val = row[colpos]
else:
val = 0
if val == 0:
# Empty spot
if (car_id > 0) and (length > 1):
yield Car(
car_id, rowpos, length, direction, car_id == 1
), startpos
length = 0
elif val == car_id:
# Car continues
length += 1
else:
# New car ?
if (car_id > 0) and (length > 1):
yield Car(
car_id, rowpos, length, direction, car_id == 1
), startpos
length = 1
startpos = colpos
car_id = val
class Solver:
def __init__(self, initial_board: Board):
self.initial_board = initial_board
self.boards = dict()
self.solution_car_swap_count = np.inf
self.solved_board = None
self.check_boards = SortedBoards()
if self.initial_board.is_solved():
raise RuntimeError("Initial board is already solved")
self.add_board(self.initial_board)
def add_board(self, board: Board):
board_hash = hash(board)
if board_hash in self.boards.keys():
existing_board = self.boards[board_hash]
if board < existing_board:
# Found shorter path
self.boards[board_hash] = board
else:
# Found new board position
self.boards[board_hash] = board
# Check out its neighbors later
self.check_boards.insert(board)
if board.is_solved():
if (self.solved_board is None) or (board < self.solved_board):
self.solved_board = board
self.solution_car_swap_count = board.car_swaps
print(
f"Found solution of with {board.move_count} moves, {board.car_swaps} car swaps"
)
print(f"Have looked at {len(self.boards)} board positions so far")
def solve(self):
self.check_boards.insert(self.initial_board)
while len(self.check_boards) > 0:
# Pick a board to check
check_board = self.check_boards.pop()
for new_board in check_board.get_next_boards():
self.add_board(new_board)
board = self.solved_board
if board is not None:
solution_boards = [board]
while True:
previous_hash = board.previous_hash
if previous_hash is None:
break
else:
board = self.boards[previous_hash]
solution_boards.append(board)
return solution_boards[::-1]
if __name__ == "__main__":
# fmt: off
board_array = np.array([
[ 2, 3, 4, 4, 5, 0],
[ 2, 3, 6, 0, 5, 7],
[ 2, 0, 6, 1, 1, 7],
[ 8, 8, 8, 9, 0, 7],
[ 0, 0, 10, 9, 11, 11],
[ 12, 12, 10, 13, 13, 0],
])
# fmt: on
s = Solver(Board.board_from_numpy(board_array))
solution = s.solve()
if len(solution):
print(f"Solution length {len(solution)}")
print(f"Studied {len(s.boards)} board positions")
for i, board in enumerate(solution):
print(f"****** Board position {i} : {board.move_to_get_here}")
print(f"****** Board hash {hash(board)}")
print(board)