-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlattice_maze.py
1358 lines (1160 loc) · 48.3 KB
/
lattice_maze.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import typing
import warnings
from dataclasses import dataclass
from itertools import chain
import numpy as np
from jaxtyping import Bool, Int, Int8, Shaped
from muutils.json_serialize.serializable_dataclass import (
SerializableDataclass,
serializable_dataclass,
serializable_field,
)
from muutils.misc import isinstance_by_type_name, list_split
from maze_dataset.constants import (
NEIGHBORS_MASK,
SPECIAL_TOKENS,
ConnectionList,
Coord,
CoordArray,
CoordList,
CoordTup,
)
from maze_dataset.token_utils import (
TokenizerDeprecationWarning,
connection_list_to_adj_list,
get_adj_list_tokens,
get_origin_tokens,
get_path_tokens,
get_target_tokens,
)
if typing.TYPE_CHECKING:
from maze_dataset.tokenization import (
MazeTokenizer,
MazeTokenizerModular,
TokenizationMode,
)
RGB = tuple[int, int, int]
"rgb tuple of values 0-255"
PixelGrid = Int[np.ndarray, "x y rgb"]
"rgb grid of pixels"
BinaryPixelGrid = Bool[np.ndarray, "x y"]
"boolean grid of pixels"
def _fill_edges_with_walls(connection_list: ConnectionList) -> ConnectionList:
"""fill the last elements of the connections lists as false for each dim"""
for dim in range(connection_list.shape[0]):
# last row for down
if dim == 0:
connection_list[dim, -1, :] = False
# last column for right
elif dim == 1:
connection_list[dim, :, -1] = False
else:
raise NotImplementedError(f"only 2d lattices supported. got {dim=}")
return connection_list
def color_in_pixel_grid(pixel_grid: PixelGrid, color: RGB) -> bool:
for row in pixel_grid:
for pixel in row:
if np.all(pixel == color):
return True
return False
@dataclass(frozen=True)
class PixelColors:
"standard colors for pixel grids"
WALL: RGB = (0, 0, 0)
OPEN: RGB = (255, 255, 255)
START: RGB = (0, 255, 0)
END: RGB = (255, 0, 0)
PATH: RGB = (0, 0, 255)
@dataclass(frozen=True)
class AsciiChars:
"standard ascii characters for mazes"
WALL: str = "#"
OPEN: str = " "
START: str = "S"
END: str = "E"
PATH: str = "X"
ASCII_PIXEL_PAIRINGS: dict[str, RGB] = {
AsciiChars.WALL: PixelColors.WALL,
AsciiChars.OPEN: PixelColors.OPEN,
AsciiChars.START: PixelColors.START,
AsciiChars.END: PixelColors.END,
AsciiChars.PATH: PixelColors.PATH,
}
"map ascii characters to pixel colors"
@serializable_dataclass(
frozen=True,
kw_only=True,
properties_to_serialize=["lattice_dim", "generation_meta"],
)
class LatticeMaze(SerializableDataclass):
"""lattice maze (nodes on a lattice, connections only to neighboring nodes)
Connection List represents which nodes (N) are connected in each direction.
First and second elements represent rightward and downward connections,
respectively.
Example:
Connection list:
[
[ # down
[F T],
[F F]
],
[ # right
[T F],
[T F]
]
]
Nodes with connections
N T N F
F T
N T N F
F F
Graph:
N - N
|
N - N
Note: the bottom row connections going down, and the
right-hand connections going right, will always be False.
"""
connection_list: ConnectionList
generation_meta: dict | None = serializable_field(default=None, compare=False)
lattice_dim = property(lambda self: self.connection_list.shape[0])
grid_shape = property(lambda self: self.connection_list.shape[1:])
n_connections = property(lambda self: self.connection_list.sum())
@property
def grid_n(self) -> int:
assert self.grid_shape[0] == self.grid_shape[1], "only square mazes supported"
return self.grid_shape[0]
# ============================================================
# basic methods
# ============================================================
@staticmethod
def heuristic(a: CoordTup, b: CoordTup) -> float:
"""return manhattan distance between two points"""
return np.abs(a[0] - b[0]) + np.abs(a[1] - b[1])
def __hash__(self) -> int:
return hash(self.connection_list.tobytes())
def nodes_connected(self, a: Coord, b: Coord, /) -> bool:
"""returns whether two nodes are connected"""
delta: Coord = b - a
if np.abs(delta).sum() != 1:
# return false if not even adjacent
return False
else:
# test for wall
dim: int = int(np.argmax(np.abs(delta)))
clist_node: Coord = a if (delta.sum() > 0) else b
return self.connection_list[dim, clist_node[0], clist_node[1]]
def is_valid_path(self, path: CoordArray, empty_is_valid: bool = False) -> bool:
"""check if a path is valid"""
# check path is not empty
if len(path) == 0:
if not empty_is_valid:
return False
else:
return True
# check all coords in bounds of maze
if not np.all((0 <= path) & (path < self.grid_shape)):
return False
# check all nodes connected
for i in range(len(path) - 1):
if not self.nodes_connected(path[i], path[i + 1]):
return False
return True
def coord_degrees(self) -> Int8[np.ndarray, "row col"]:
"""
Returns an array with the connectivity degree of each coord.
I.e., how many neighbors each coord has.
"""
int_conn: Int8[np.ndarray, "lattice_dim=2 row col"] = (
self.connection_list.astype(np.int8)
)
degrees: Int8[np.ndarray, "row col"] = np.sum(
int_conn, axis=0
) # Connections to east and south
degrees[:, 1:] += int_conn[1, :, :-1] # Connections to west
degrees[1:, :] += int_conn[0, :-1, :] # Connections to north
return degrees
def get_coord_neighbors(self, c: Coord) -> CoordArray:
"""
Returns an array of the neighboring, connected coords of `c`.
"""
neighbors: list[Coord] = [
neighbor
for neighbor in (c + NEIGHBORS_MASK)
if (
(0 <= neighbor[0] < self.grid_shape[0]) # in x bounds
and (0 <= neighbor[1] < self.grid_shape[1]) # in y bounds
and self.nodes_connected(c, neighbor) # connected
)
]
output: CoordArray = np.array(neighbors)
if len(neighbors) > 0:
assert output.shape == (
len(neighbors),
2,
), f"invalid shape: {output.shape}, expected ({len(neighbors)}, 2))\n{c = }\n{neighbors = }\n{self.as_ascii()}"
return output
def gen_connected_component_from(self, c: Coord) -> CoordArray:
"""return the connected component from a given coordinate"""
# Stack for DFS
stack: list[Coord] = [c]
# Set to store visited nodes
visited: set[CoordTup] = set()
while stack:
current_node: Coord = stack.pop()
# this is fine since we know current_node is a coord and thus of length 2
visited.add(tuple(current_node)) # type: ignore[arg-type]
# Get the neighbors of the current node
neighbors = self.get_coord_neighbors(current_node)
# Iterate over neighbors
for neighbor in neighbors:
if tuple(neighbor) not in visited:
stack.append(neighbor)
return np.array(list(visited))
def find_shortest_path(
self,
c_start: CoordTup,
c_end: CoordTup,
) -> CoordArray:
"""find the shortest path between two coordinates, using A*"""
c_start = tuple(c_start)
c_end = tuple(c_end)
g_score: dict[CoordTup, float] = (
dict()
) # cost of cheapest path to node from start currently known
f_score: dict[CoordTup, float] = {
c_start: 0.0
} # estimated total cost of path thru a node: f_score[c] := g_score[c] + heuristic(c, c_end)
# init
g_score[c_start] = 0.0
g_score[c_start] = self.heuristic(c_start, c_end)
closed_vtx: set[CoordTup] = set() # nodes already evaluated
open_vtx: set[CoordTup] = set([c_start]) # nodes to be evaluated
source: dict[CoordTup, CoordTup] = (
dict()
) # node immediately preceding each node in the path (currently known shortest path)
while open_vtx:
# get lowest f_score node
c_current: CoordTup = min(open_vtx, key=lambda c: f_score[c])
# f_current: float = f_score[c_current]
# check if goal is reached
if c_end == c_current:
path: list[CoordTup] = [c_current]
p_current: CoordTup = c_current
while p_current in source:
p_current = source[p_current]
path.append(p_current)
# ----------------------------------------------------------------------
# this is the only return statement
return np.array(path[::-1])
# ----------------------------------------------------------------------
# close current node
closed_vtx.add(c_current)
open_vtx.remove(c_current)
# update g_score of neighbors
_np_neighbor: Coord
for _np_neighbor in self.get_coord_neighbors(c_current):
neighbor: CoordTup = tuple(_np_neighbor)
if neighbor in closed_vtx:
# already checked
continue
g_temp: float = g_score[c_current] + 1 # always 1 for maze neighbors
if neighbor not in open_vtx:
# found new vtx, so add
open_vtx.add(neighbor)
elif g_temp >= g_score[neighbor]:
# if already knew about this one, but current g_score is worse, skip
continue
# store g_score and source
source[neighbor] = c_current
g_score[neighbor] = g_temp
f_score[neighbor] = g_score[neighbor] + self.heuristic(neighbor, c_end)
raise ValueError(
"A solution could not be found!",
f"{c_start = }, {c_end = }",
self.as_ascii(),
)
def get_nodes(self) -> CoordArray:
"""return a list of all nodes in the maze"""
rows: Int[np.ndarray, "x y"]
cols: Int[np.ndarray, "x y"]
rows, cols = np.meshgrid(
range(self.grid_shape[0]),
range(self.grid_shape[1]),
indexing="ij",
)
nodes: CoordArray = np.vstack((rows.ravel(), cols.ravel())).T
return nodes
def get_connected_component(self) -> CoordArray:
"""get the largest (and assumed only nonsingular) connected component of the maze
TODO: other connected components?
"""
if (self.generation_meta is None) or (
self.generation_meta.get("fully_connected", False)
):
# for fully connected case, pick any two positions
return self.get_nodes()
else:
# if metadata provided, use visited cells
visited_cells: set[CoordTup] | None = self.generation_meta.get(
"visited_cells", None
)
if visited_cells is None:
# TODO: dynamically generate visited_cells?
raise ValueError(
f"a maze which is not marked as fully connected must have a visited_cells field in its generation_meta: {self.generation_meta}\n{self}\n{self.as_ascii()}"
)
else:
visited_cells_np: Int[np.ndarray, "N 2"] = np.array(list(visited_cells))
return visited_cells_np
def generate_random_path(
self,
except_when_invalid: bool = True,
allowed_start: CoordList | None = None,
allowed_end: CoordList | None = None,
deadend_start: bool = False,
deadend_end: bool = False,
endpoints_not_equal: bool = False,
) -> CoordArray:
"""return a path between randomly chosen start and end nodes within the connected component
Note that setting special conditions on start and end positions might cause the same position to be selected as both start and end.
# Parameters:
- `except_when_invalid : bool`
deprecated. setting this to `False` will cause an error.
(defaults to `True`)
- `allowed_start : CoordList | None`
a list of allowed start positions. If `None`, any position in the connected component is allowed
(defaults to `None`)
- `allowed_end : CoordList | None`
a list of allowed end positions. If `None`, any position in the connected component is allowed
(defaults to `None`)
- `deadend_start : bool`
whether to ***force*** the start position to be a deadend (defaults to `False`)
(defaults to `False`)
- `deadend_end : bool`
whether to ***force*** the end position to be a deadend (defaults to `False`)
(defaults to `False`)
- `endpoints_not_equal : bool`
whether to ensure tha the start and end point are not the same
(defaults to `False`)
# Returns:
- `CoordArray`
a path between the selected start and end positions
# Raises:
- `ValueError` : if the connected component has less than 2 nodes and `except_when_invalid` is `True`
"""
# we can't create a "path" in a single-node maze
assert (
self.grid_shape[0] > 1 and self.grid_shape[1] > 1
), f"can't create path in single-node maze: {self.as_ascii()}"
# get connected component
connected_component: CoordArray = self.get_connected_component()
# initialize start and end positions
positions: Int[np.int8, "2 2"]
# handle deprecated parameter
if not except_when_invalid:
raise DeprecationWarning(
"except_when_invalid is deprecated. Set it to the default `True` to avoid this warning."
)
# if no special conditions on start and end positions
if (allowed_start, allowed_end, deadend_start, deadend_end) == (
None,
None,
False,
False,
):
positions = connected_component[
np.random.choice(
len(connected_component),
size=2,
replace=False,
)
]
return self.find_shortest_path(positions[0], positions[1])
# handle special conditions
connected_component_set: set[CoordTup] = set(map(tuple, connected_component))
# copy connected component set
allowed_start_set: set[CoordTup] = connected_component_set.copy()
allowed_end_set: set[CoordTup] = connected_component_set.copy()
# filter by explicitly allowed start and end positions
if allowed_start is not None:
allowed_start_set = set(map(tuple, allowed_start)) & connected_component_set
if allowed_end is not None:
allowed_end_set = set(map(tuple, allowed_end)) & connected_component_set
# filter by forcing deadends
if deadend_start:
allowed_start_set = set(
filter(
lambda x: len(self.get_coord_neighbors(x)) == 1, allowed_start_set
)
)
if deadend_end:
allowed_end_set = set(
filter(lambda x: len(self.get_coord_neighbors(x)) == 1, allowed_end_set)
)
# check we have valid positions
if len(allowed_start_set) == 0 or len(allowed_end_set) == 0:
raise ValueError("no valid start or end positions found")
# randomly select start and end positions
start_pos: CoordTup = tuple(
list(allowed_start_set)[np.random.randint(0, len(allowed_start_set))]
)
if endpoints_not_equal:
# remove start position from end positions
allowed_end_set.discard(start_pos)
end_pos: CoordTup = tuple(
list(allowed_end_set)[np.random.randint(0, len(allowed_end_set))]
)
return self.find_shortest_path(start_pos, end_pos)
# ============================================================
# to and from adjacency list
# ============================================================
def as_adj_list(
self, shuffle_d0: bool = True, shuffle_d1: bool = True
) -> Int8[np.ndarray, "conn start_end coord"]:
return connection_list_to_adj_list(self.connection_list, shuffle_d0, shuffle_d1)
@classmethod
def from_adj_list(
cls,
adj_list: Int8[np.ndarray, "conn start_end coord"],
) -> "LatticeMaze":
"""create a LatticeMaze from a list of connections
> [!NOTE]
> This has only been tested for square mazes. Might need to change some things if rectangular mazes are needed.
"""
# this is where it would probably break for rectangular mazes
grid_n: int = adj_list.max() + 1
connection_list: ConnectionList = np.zeros(
(2, grid_n, grid_n),
dtype=np.bool_,
)
for c_start, c_end in adj_list:
# check that exactly 1 coordinate matches
if (c_start == c_end).sum() != 1:
raise ValueError("invalid connection")
# get the direction
d: int = (c_start != c_end).argmax()
x: int
y: int
# pick whichever has the lesser value in the direction `d`
if c_start[d] < c_end[d]:
x, y = c_start
else:
x, y = c_end
connection_list[d, x, y] = True
return LatticeMaze(
connection_list=connection_list,
)
def as_adj_list_tokens(self) -> list[str | CoordTup]:
warnings.warn(
"`LatticeMaze.as_adj_list_tokens` will be removed from the public API in a future release.",
TokenizerDeprecationWarning,
)
return [
SPECIAL_TOKENS.ADJLIST_START,
*chain.from_iterable(
[
[
tuple(c_s),
SPECIAL_TOKENS.CONNECTOR,
tuple(c_e),
SPECIAL_TOKENS.ADJACENCY_ENDLINE,
]
for c_s, c_e in self.as_adj_list()
]
),
SPECIAL_TOKENS.ADJLIST_END,
]
def _as_adj_list_tokens(self) -> list[str | CoordTup]:
return [
SPECIAL_TOKENS.ADJLIST_START,
*chain.from_iterable(
[
[
tuple(c_s),
SPECIAL_TOKENS.CONNECTOR,
tuple(c_e),
SPECIAL_TOKENS.ADJACENCY_ENDLINE,
]
for c_s, c_e in self.as_adj_list()
]
),
SPECIAL_TOKENS.ADJLIST_END,
]
def _as_coords_and_special_AOTP(self) -> list[CoordTup | str]:
"""turn the maze into adjacency list, origin, target, and solution -- keep coords as tuples"""
output: list[str] = self._as_adj_list_tokens()
# if getattr(self, "start_pos", None) is not None:
if isinstance(self, TargetedLatticeMaze):
output += self._get_start_pos_tokens()
if isinstance(self, TargetedLatticeMaze):
output += self._get_end_pos_tokens()
if isinstance(self, SolvedMaze):
output += self._get_solution_tokens()
return output
def _as_tokens(
self, maze_tokenizer: "MazeTokenizer | TokenizationMode"
) -> list[str]:
if isinstance_by_type_name(maze_tokenizer, "TokenizationMode"):
maze_tokenizer = maze_tokenizer.to_legacy_tokenizer()
if (
isinstance_by_type_name(maze_tokenizer, "MazeTokenizer")
and maze_tokenizer.is_AOTP()
):
coords_raw: list[CoordTup | str] = self._as_coords_and_special_AOTP()
coords_processed: list[str] = maze_tokenizer.coords_to_strings(
coords=coords_raw, when_noncoord="include"
)
return coords_processed
else:
raise NotImplementedError(f"Unsupported tokenizer type: {maze_tokenizer}")
def as_tokens(
self,
maze_tokenizer: "MazeTokenizer | TokenizationMode | MazeTokenizerModular",
) -> list[str]:
"""serialize maze and solution to tokens"""
if isinstance_by_type_name(maze_tokenizer, "MazeTokenizerModular"):
return maze_tokenizer.to_tokens(self)
else:
return self._as_tokens(maze_tokenizer)
@classmethod
def _from_tokens_AOTP(
cls, tokens: list[str], maze_tokenizer: "MazeTokenizer | MazeTokenizerModular"
) -> "LatticeMaze":
"""create a LatticeMaze from a list of tokens"""
# figure out what input format
# ========================================
if tokens[0] == SPECIAL_TOKENS.ADJLIST_START:
adj_list_tokens = get_adj_list_tokens(tokens)
else:
# If we're not getting a "complete" tokenized maze, assume it's just a the adjacency list tokens
adj_list_tokens = tokens
warnings.warn(
"Assuming input is just adjacency list tokens, no special tokens found"
)
# process edges for adjacency list
# ========================================
edges: list[list[str]] = list_split(
adj_list_tokens,
SPECIAL_TOKENS.ADJACENCY_ENDLINE,
)
coordinates: list[tuple[CoordTup, CoordTup]] = list()
for e in edges:
# skip last endline
if len(e) != 0:
# convert to coords, split start and end
e_coords: list[str | CoordTup] = maze_tokenizer.strings_to_coords(
e, when_noncoord="include"
)
assert len(e_coords) == 3, f"invalid edge: {e = } {e_coords = }"
assert (
e_coords[1] == SPECIAL_TOKENS.CONNECTOR
), f"invalid edge: {e = } {e_coords = }"
coordinates.append((e_coords[0], e_coords[-1]))
assert all(
len(c) == 2 for c in coordinates
), f"invalid coordinates: {coordinates = }"
adj_list: Int8[np.ndarray, "conn start_end coord"] = np.array(coordinates)
assert tuple(adj_list.shape) == (
len(coordinates),
2,
2,
), f"invalid adj_list: {adj_list.shape = } {coordinates = }"
output_maze: LatticeMaze = cls.from_adj_list(adj_list)
# add start and end positions
# ========================================
is_targeted: bool = False
if all(
x in tokens
for x in (
SPECIAL_TOKENS.ORIGIN_START,
SPECIAL_TOKENS.ORIGIN_END,
SPECIAL_TOKENS.TARGET_START,
SPECIAL_TOKENS.TARGET_END,
)
):
start_pos_list: list[CoordTup] = maze_tokenizer.strings_to_coords(
get_origin_tokens(tokens), when_noncoord="error"
)
end_pos_list: list[CoordTup] = maze_tokenizer.strings_to_coords(
get_target_tokens(tokens), when_noncoord="error"
)
assert (
len(start_pos_list) == 1
), f"invalid start_pos_list: {start_pos_list = }"
assert len(end_pos_list) == 1, f"invalid end_pos_list: {end_pos_list = }"
start_pos: CoordTup = start_pos_list[0]
end_pos: CoordTup = end_pos_list[0]
output_maze = TargetedLatticeMaze.from_lattice_maze(
lattice_maze=output_maze,
start_pos=start_pos,
end_pos=end_pos,
)
is_targeted = True
if all(
x in tokens for x in (SPECIAL_TOKENS.PATH_START, SPECIAL_TOKENS.PATH_END)
):
assert is_targeted, "maze must be targeted to have a solution"
solution: list[CoordTup] = maze_tokenizer.strings_to_coords(
get_path_tokens(tokens, trim_end=True),
when_noncoord="error",
)
output_maze = SolvedMaze.from_targeted_lattice_maze(
targeted_lattice_maze=output_maze,
solution=solution,
)
return output_maze
@classmethod
def from_tokens(
cls,
tokens: list[str],
maze_tokenizer: "MazeTokenizer | TokenizationMode | MazeTokenizerModular",
) -> "LatticeMaze":
"""
Constructs a maze from a tokenization.
Only legacy tokenizers and their `MazeTokenizerModular` analogs are supported.
"""
if isinstance_by_type_name(maze_tokenizer, "TokenizationMode"):
maze_tokenizer = maze_tokenizer.to_legacy_tokenizer()
if (
isinstance_by_type_name(maze_tokenizer, "MazeTokenizerModular")
and not maze_tokenizer.is_legacy_equivalent()
):
raise NotImplementedError(
f"Only legacy tokenizers and their exact `MazeTokenizerModular` analogs supported, not {maze_tokenizer}."
)
if isinstance(tokens, str):
tokens = tokens.split()
if maze_tokenizer.is_AOTP():
return cls._from_tokens_AOTP(tokens, maze_tokenizer)
else:
raise NotImplementedError("only AOTP tokenization is supported")
# ============================================================
# to and from pixels
# ============================================================
def _as_pixels_bw(self) -> BinaryPixelGrid:
assert self.lattice_dim == 2, "only 2D mazes are supported"
# Create an empty pixel grid with walls
pixel_grid: Int[np.ndarray, "x y"] = np.full(
(self.grid_shape[0] * 2 + 1, self.grid_shape[1] * 2 + 1),
False,
dtype=np.bool_,
)
# Set white nodes
pixel_grid[1::2, 1::2] = True
# Set white connections (downward)
for i, row in enumerate(self.connection_list[0]):
for j, connected in enumerate(row):
if connected:
pixel_grid[i * 2 + 2, j * 2 + 1] = True
# Set white connections (rightward)
for i, row in enumerate(self.connection_list[1]):
for j, connected in enumerate(row):
if connected:
pixel_grid[i * 2 + 1, j * 2 + 2] = True
return pixel_grid
def as_pixels(
self,
show_endpoints: bool = True,
show_solution: bool = True,
) -> PixelGrid:
if show_solution and not show_endpoints:
raise ValueError("show_solution=True requires show_endpoints=True")
# convert original bool pixel grid to RGB
pixel_grid_bw: BinaryPixelGrid = self._as_pixels_bw()
pixel_grid: PixelGrid = np.full(
(*pixel_grid_bw.shape, 3), PixelColors.WALL, dtype=np.uint8
)
pixel_grid[pixel_grid_bw == True] = PixelColors.OPEN
if self.__class__ == LatticeMaze:
return pixel_grid
# set endpoints for TargetedLatticeMaze
if self.__class__ == TargetedLatticeMaze:
if show_endpoints:
pixel_grid[self.start_pos[0] * 2 + 1, self.start_pos[1] * 2 + 1] = (
PixelColors.START
)
pixel_grid[self.end_pos[0] * 2 + 1, self.end_pos[1] * 2 + 1] = (
PixelColors.END
)
return pixel_grid
# set solution
if show_solution:
for coord in self.solution:
pixel_grid[coord[0] * 2 + 1, coord[1] * 2 + 1] = PixelColors.PATH
# set pixels between coords
for index, coord in enumerate(self.solution[:-1]):
next_coord = self.solution[index + 1]
# check they are adjacent using norm
assert (
np.linalg.norm(np.array(coord) - np.array(next_coord)) == 1
), f"Coords {coord} and {next_coord} are not adjacent"
# set pixel between them
pixel_grid[
coord[0] * 2 + 1 + next_coord[0] - coord[0],
coord[1] * 2 + 1 + next_coord[1] - coord[1],
] = PixelColors.PATH
# set endpoints (again, since path would overwrite them)
pixel_grid[self.start_pos[0] * 2 + 1, self.start_pos[1] * 2 + 1] = (
PixelColors.START
)
pixel_grid[self.end_pos[0] * 2 + 1, self.end_pos[1] * 2 + 1] = (
PixelColors.END
)
return pixel_grid
@classmethod
def _from_pixel_grid_bw(
cls, pixel_grid: BinaryPixelGrid
) -> tuple[ConnectionList, tuple[int, int]]:
grid_shape: tuple[int, int] = (
pixel_grid.shape[0] // 2,
pixel_grid.shape[1] // 2,
)
connection_list: ConnectionList = np.zeros((2, *grid_shape), dtype=np.bool_)
# Extract downward connections
connection_list[0] = pixel_grid[2::2, 1::2]
# Extract rightward connections
connection_list[1] = pixel_grid[1::2, 2::2]
return connection_list, grid_shape
@classmethod
def _from_pixel_grid_with_positions(
cls,
pixel_grid: PixelGrid | BinaryPixelGrid,
marked_positions: dict[str, RGB],
) -> tuple[ConnectionList, tuple[int, int], dict[str, CoordArray]]:
# Convert RGB pixel grid to Bool pixel grid
pixel_grid_bw: BinaryPixelGrid = ~np.all(
pixel_grid == PixelColors.WALL, axis=-1
)
connection_list: ConnectionList
grid_shape: tuple[int, int]
connection_list, grid_shape = cls._from_pixel_grid_bw(pixel_grid_bw)
# Find any marked positions
out_positions: dict[str, CoordArray] = dict()
for key, color in marked_positions.items():
pos_temp: Int[np.ndarray, "x y"] = np.argwhere(
np.all(pixel_grid == color, axis=-1)
)
pos_save: list[CoordTup] = list()
for pos in pos_temp:
# if it is a coordinate and not connection (transform position, %2==1)
if pos[0] % 2 == 1 and pos[1] % 2 == 1:
pos_save.append((pos[0] // 2, pos[1] // 2))
out_positions[key] = np.array(pos_save)
return connection_list, grid_shape, out_positions
@classmethod
def from_pixels(
cls,
pixel_grid: PixelGrid,
) -> "LatticeMaze":
connection_list: ConnectionList
grid_shape: tuple[int, int]
# if a binary pixel grid, return regular LatticeMaze
if len(pixel_grid.shape) == 2:
connection_list, grid_shape = cls._from_pixel_grid_bw(pixel_grid)
return LatticeMaze(connection_list=connection_list)
# otherwise, detect and check it's valid
cls_detected: typing.Type[LatticeMaze] = detect_pixels_type(pixel_grid)
if not cls in cls_detected.__mro__:
raise ValueError(
f"Pixel grid cannot be cast to {cls.__name__}, detected type {cls_detected.__name__}"
)
(
connection_list,
grid_shape,
marked_pos,
) = cls._from_pixel_grid_with_positions(
pixel_grid=pixel_grid,
marked_positions=dict(
start=PixelColors.START, end=PixelColors.END, solution=PixelColors.PATH
),
)
# if we wanted a LatticeMaze, return it
if cls == LatticeMaze:
return LatticeMaze(connection_list=connection_list)
# otherwise, keep going
temp_maze: LatticeMaze = LatticeMaze(connection_list=connection_list)
# start and end pos
start_pos_arr, end_pos_arr = marked_pos["start"], marked_pos["end"]
assert start_pos_arr.shape == (
1,
2,
), f"start_pos_arr {start_pos_arr} has shape {start_pos_arr.shape}, expected shape (1, 2) -- a single coordinate"
assert end_pos_arr.shape == (
1,
2,
), f"end_pos_arr {end_pos_arr} has shape {end_pos_arr.shape}, expected shape (1, 2) -- a single coordinate"
start_pos: Coord = start_pos_arr[0]
end_pos: Coord = end_pos_arr[0]
# return a TargetedLatticeMaze if that's what we wanted
if cls == TargetedLatticeMaze:
return TargetedLatticeMaze(
connection_list=connection_list,
start_pos=start_pos,
end_pos=end_pos,
)
# raw solution, only contains path elements and not start or end
solution_raw: CoordArray = marked_pos["solution"]
if len(solution_raw.shape) == 2:
assert (
solution_raw.shape[1] == 2
), f"solution {solution_raw} has shape {solution_raw.shape}, expected shape (n, 2)"
elif solution_raw.shape == (0,):
# the solution and end should be immediately adjacent
assert (
np.sum(np.abs(start_pos - end_pos)) == 1
), f"start_pos {start_pos} and end_pos {end_pos} are not adjacent, but no solution was given"
# order the solution, by creating a list from the start to the end
# add end pos, since we will iterate over all these starting from the start pos
solution_raw_list: list[CoordTup] = [tuple(c) for c in solution_raw] + [
tuple(end_pos)
]
# solution starts with start point
solution: list[CoordTup] = [tuple(start_pos)]
while solution[-1] != tuple(end_pos):
# use `get_coord_neighbors` to find connected neighbors
neighbors: CoordArray = temp_maze.get_coord_neighbors(solution[-1])
# TODO: make this less ugly
assert (len(neighbors.shape) == 2) and (
neighbors.shape[1] == 2
), f"neighbors {neighbors} has shape {neighbors.shape}, expected shape (n, 2)\n{neighbors = }\n{solution = }\n{solution_raw = }\n{temp_maze.as_ascii()}"
# neighbors = neighbors[:, [1, 0]]
# filter out neighbors that are not in the raw solution
neighbors_filtered: CoordArray = np.array(
[
coord
for coord in neighbors
if (
tuple(coord) in solution_raw_list
and not tuple(coord) in solution
)
]
)
# assert only one element is left, and then add it to the solution
assert neighbors_filtered.shape == (
1,
2,
), f"neighbors_filtered has shape {neighbors_filtered.shape}, expected shape (1, 2)\n{neighbors = }\n{neighbors_filtered = }\n{solution = }\n{solution_raw_list = }\n{temp_maze.as_ascii()}"
solution.append(tuple(neighbors_filtered[0]))
# assert the solution is complete
assert solution[0] == tuple(
start_pos
), f"solution {solution} does not start at start_pos {start_pos}"
assert solution[-1] == tuple(
end_pos
), f"solution {solution} does not end at end_pos {end_pos}"
return cls(
connection_list=np.array(connection_list),
solution=np.array(solution),
)
# ============================================================
# to and from ASCII
# ============================================================
def _as_ascii_grid(self) -> Shaped[np.ndarray, "x y"]:
# Get the pixel grid using to_pixels().
pixel_grid: Bool[np.ndarray, "x y"] = self._as_pixels_bw()
# Replace pixel values with ASCII characters.
ascii_grid: Shaped[np.ndarray, "x y"] = np.full(
pixel_grid.shape, AsciiChars.WALL, dtype=str
)