-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle.py
More file actions
283 lines (208 loc) · 9.28 KB
/
puzzle.py
File metadata and controls
283 lines (208 loc) · 9.28 KB
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
from abc import ABC, abstractmethod
from math import prod
import numpy as np
from rotcomp import RotComp
class Puzzle(ABC):
"""Abstract, non-public parent class of LoopoverPuzzle and LinearPuzzle. """
def __init__(self, board, *, ids=None):
"""Construct a Puzzle object representing its board and its solved permutation.
Args:
board: Array-like object describing the pieces in their starting permutation of the puzzle board.
ids: (optional, keyword-only) Array-like object used to uniquely identify the pieces of the board, also
representing their order in the solved permutation.
"""
self._board: np.ndarray
self._ids: np.ndarray
if isinstance(board, Puzzle):
self._board = board._board.copy()
self._ids = board._ids.copy()
return
self._board = np.array(board, dtype=str)
self._ids = self._get_range_array()
if ids is not None:
self._ids = np.array(ids)
@classmethod
@abstractmethod
def from_shape(cls, shape, *, randomize=False):
"""Alternate constructor for Puzzle objects.
Constructs one, of the requested shape, and fills it with counting numbers from 0. Its solved state will be
the permutation where all numbers are in order from left to right, from top to bottom. Option to randomize
its starting permutation.
Args:
shape: Tuple of dimensions for the board.
randomize: (optional) Randomize the starting permutation of the board. False by default.
"""
board = cls._get_range_array(shape=shape)
loopover_puzzle = cls(board)
if randomize:
loopover_puzzle.randomize_perm()
return loopover_puzzle
def define_solved_perm(self, solved_board):
"""Define the permutation in which the board is considered solved, as apparent through its is_solved attribute.
Args:
solved_board: A Puzzle or array-like object representing the solved permutation of self.
Raises:
PuzzlePermError: if the given solved_board is not a permutation of self.
"""
solved_puzzle = type(self)(solved_board)
if not self.is_perm_of(solved_puzzle):
raise PuzzlePermError
src_ordering = self._board.ravel().argsort()
dst_ordering = solved_puzzle._board.ravel().argsort()
self._ids.ravel()[src_ordering] = dst_ordering
@abstractmethod
def get_solution(self):
"""Return a sequence of actions taking self from its current permutation to the solved one.
These actions can either be Move or Rot objects depending on what the type of puzzle considers legal.
Returns:
solution: The solution to the Puzzle.
"""
pass
@abstractmethod
def apply_action(self, action):
"""Apply the given action to self.
This either can be moving or rotating depending on the type of puzzle.
Args:
action: Sequence of actions, usually a MoveComp or a RotComp, to apply to self.
"""
pass
def get_rotcomp_solution(self):
"""Return the more or less abstract solution to the Puzzle, in the form of a RotComp.
For LoopoverPuzzles, a RotComp is a higher-level transformation that is then implemented in terms of a MoveComp.
For LinearPuzzles, a RotComp is a legal transformation that can be applied directly.
Returns:
rotcomp: A RotComp taking self to its solved permutation.
"""
rotcomp = RotComp()
visited_indices = []
for id_ in self._ids.ravel():
if id_ in visited_indices:
continue
rotcomp.append([])
while id_ not in visited_indices:
visited_indices.append(id_)
rotcomp[-1].append(int(id_))
id_ = self._ids[self._unravel_index(id_)]
if len(rotcomp[-1]) < 2:
del rotcomp[-1]
return rotcomp
@abstractmethod
def randomize_perm(self):
"""Generate and apply a random action to self. """
pass
@abstractmethod
def rot(self, rotcomp):
"""Apply a RotComp or alike transformation to self.
Args:
rotcomp: A RotComp or RotComp like sequence.
"""
pass
def copy(self):
"""Return a deep copy of self. """
return type(self)(self)
def draw(self):
"""Draw the board. """
print(self._get_pretty_repr())
def draw_ids(self):
"""Draw the piece ids in the form of a board, representing the expected order to solve the Puzzle. """
print(self._get_pretty_repr(use_ids=True))
def is_perm_of(self, other):
"""Check if self and other are permutations of the same board (ie they contain the same pieces).
Args:
other: Puzzle object to compare self to.
Raises:
TypeError: it other is not a Puzzle.
"""
if not isinstance(other, Puzzle):
raise TypeError("other has to be a Puzzle. ")
if self._board.shape != other._board.shape:
return False
return np.array_equal(np.sort(self._board, axis=None), np.sort(other._board, axis=None))
def has_equal_board(self, other):
"""Check if self and other are the same permutation of the same board.
Args:
other: Puzzle object to compare self to.
Raises:
TypeError: it other is not a Puzzle.
"""
if not isinstance(other, Puzzle):
raise TypeError("other has to be a Puzzle. ")
return np.array_equal(self._board, other._board)
@property
def is_solved(self):
"""Property that is True if self is in the conceptually solved permutation, False otherwise. """
ids_ravelled = self._ids.ravel()
return np.array_equal(ids_ravelled, np.sort(ids_ravelled))
@property
def shape(self):
"""Tuple describing the dimensions of self's board. """
return self._board.shape
@property
def n_pieces(self):
"""Number of pieces in the board. """
return prod(self.shape)
def __str__(self):
return self.__repr__(with_meta=False)
def __repr__(self, *, with_meta=True):
str_meta = f", ids={self._ids.tolist()}" if with_meta else ""
return f"{type(self).__name__}({self._board.tolist()}{str_meta})"
def _rot_directly(self, rotcomp):
"""Directly apply a RotComp to self.
Non-public method. This might not be a legal action depending on the type of puzzle.
Args:
rotcomp: RotComp or alike transformation.
"""
rotcomp = RotComp(rotcomp)
ted_perm = self.copy()
for rot in rotcomp:
previous_perm = ted_perm.copy()
for src_id, dst_id in zip(rot.rolled(), rot):
dst_multi_index = previous_perm._get_multi_index_from_id(dst_id)
ted_perm._ids[dst_multi_index] = src_id
ted_perm._board[dst_multi_index] = previous_perm._board[previous_perm._get_multi_index_from_id(src_id)]
self._ids = ted_perm._ids
self._board = ted_perm._board
@abstractmethod
def _get_pretty_repr(self, *, use_ids=False):
"""Return a nice representation of the requested board. Non-public method.
Args:
use_ids: (optional) If True, a representation of the ids is requested, otherwise and by default, one of the
board is.
Returns:
pretty_repr: A str that is intended as a drawing of the requested board.
"""
pass
def _get_multi_index_from_id(self, id_):
"""Return the multi_index corresponding to the identified piece. Non-public method.
Args:
id_: Piece id.
Returns:
multi_index: A multi_index letting you index inside the internal representations of the board and ids.
"""
return tuple(int(axis_index) for axis_index in np.where(self._ids == id_))
def _unravel_index(self, index):
"""Return the multi_index corresponding to this flat index. Non-public method. """
return np.unravel_index(index, self.shape)
def _ravel_multi_index(self, multi_index):
"""Return the flat index corresponding to this multi_index. Non-public method. """
return np.ravel_multi_index(multi_index, self.shape)
def _get_range_array(self=None, *, shape=None) -> np.ndarray:
"""Return an array with numbers counting from 0. Non-public method.
Args:
self: (optional) If not passed, i.e. the method is called on the class, shape needs to be passed. Otherwise,
self's shape is used.
shape: The shape requested for the returned array.
Returns:
range_array: An array with numbers counting from 0.
"""
if shape is None:
shape = self.shape
return np.arange(prod(shape), dtype=int).reshape(shape)
class PuzzleError(Exception):
pass
class PuzzlePermError(PuzzleError):
def __init__(self, message="self and other are not permutations of one another. "):
super().__init__(message)
class PuzzleDimError(PuzzleError):
def __init__(self, message="Wrong dimension for this type of puzzle. "):
super().__init__(message)