Skip to content

Commit d4beb72

Browse files
committed
Break NumPy compatibility
1 parent 9833985 commit d4beb72

2 files changed

Lines changed: 41 additions & 73 deletions

File tree

pelita/maze_generator.py

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,6 @@ def distribute_food(all_tiles, chamber_tiles, trapped_food, total_food, rng=None
133133
return tf_pos | ff_pos | leftover_food_pos
134134

135135

136-
def sample(x, k, rng):
137-
# temporary replacement wrapper for `rng.shuffle` conformant with
138-
# the `random.sample` API (minus the `count` parameter)
139-
140-
# copy population
141-
result = x.copy()
142-
143-
# shuffle all items
144-
rng.shuffle(result)
145-
146-
# return the first `k` results
147-
return result[:k]
148-
149-
150136
def identity(point):
151137
# identity transformation
152138
return point
@@ -248,25 +234,14 @@ def add_inner_walls(walls, pmin, pmax, ngaps, vertical, rng=None):
248234
above = 1 if wmin in walls else 2
249235
below = 1 if wmax in walls else 2
250236

251-
# sliced continuous wall in `x`-`y`-space
252-
wall = {transform((upos, v)) for v in range(vmin + above, vmax - below + 1)}
253-
# sample gap coordinates along the wall, i.e in `v`-direction
254-
#
255-
# TODO:
256-
# when we drop compatibility with numpy mazes, the range of sampled
257-
# gaps can be adjusted to remove them directly from the full wall
258-
# OR we sample the wall segments to keep with k = len(wall) - ngaps
259-
gaps = list(range(vmin + 1, vmax))
260-
gaps = sample(gaps, ngaps, rng)
261-
262-
# combine gap coordinates to wall gaps in `x`-`y`-space
263-
sampled = {transform((upos, v)) for v in gaps}
264-
265-
# remove sampled gaps from the wall
266-
wall -= sampled
237+
# sample inner wall tiles in `x`-`y`-space including the end index;
238+
# ensure a connected graph by always subtracting `ngaps` from
239+
# number of candidates
240+
candidates = list(range(vmin + above, vmax - below + 1))
241+
sampled = rng.sample(candidates, k=max(0, len(candidates) - ngaps))
267242

268-
# collect this wall into the global wall set
269-
walls |= wall
243+
# add the inner wall tiles to the global wall set
244+
walls |= set(transform((upos, v)) for v in sampled)
270245

271246
#
272247
# PARTITIONING
@@ -286,10 +261,7 @@ def add_inner_walls(walls, pmin, pmax, ngaps, vertical, rng=None):
286261
)
287262

288263
# queue the new partitions next
289-
#
290-
# TODO:
291-
# when we drop compatibility with numpy mazes, remove inversion
292-
partitions.extend(new[::-1])
264+
partitions.extend(new)
293265

294266

295267
def generate_half_maze(trapped_food, total_food, width, height, rng=None):
@@ -338,12 +310,8 @@ def generate_half_maze(trapped_food, total_food, width, height, rng=None):
338310

339311
# possible locations for gaps;
340312
# these gaps need to be symmetric around the center
341-
#
342-
# TODO:
343-
# when we drop compatibility with numpy mazes, this might be rewritten to
344-
# sample wall segments to keep with k = len(wall) - ngaps
345313
candidates = list(range(y_border))
346-
candidates = sample(candidates, ngaps, rng)
314+
candidates = rng.sample(candidates, k=ngaps)
347315

348316
# save gaps and bridges for chamber finding
349317
border_gaps = set()
@@ -392,6 +360,11 @@ def generate_half_maze(trapped_food, total_food, width, height, rng=None):
392360
# see the `FOOD` section below for application
393361
graph = walls_to_graph(walls, shape=(width // 2, height))
394362

363+
# the algorithm should actually guarantee this, but just to make sure, let's
364+
# fail if the graph is not fully connected
365+
if not nx.is_connected(graph):
366+
raise ValueError("Generated maze is not fully connected, try a different random seed")
367+
395368
# emulate the presence of the right maze side by wiring up
396369
# pairs of left border gaps, i.e. "bridges" from the
397370
# `BORDER WALLS, GAPS AND BRIDGES` section above,
@@ -417,11 +390,6 @@ def generate_half_maze(trapped_food, total_food, width, height, rng=None):
417390
# wall segment in the middle on odd heights
418391
graph.add_edges_from(border_bridges)
419392

420-
# the algorithm should actually guarantee this, but just to make sure, let's
421-
# fail if the graph is not fully connected
422-
if not nx.is_connected(graph):
423-
raise ValueError("Generated maze is not fully connected, try a different random seed")
424-
425393
#
426394
# FOOD
427395
#

test/test_maze_generation.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,38 @@ def layout_str_to_graph(l_str):
2727

2828
maze_103525239_even = """
2929
################################
30-
#. .....#....## . . y#
31-
# .# ######### # . # x#
32-
# # # . . # ###### ##
33-
# # . # ##. # #
34-
# ####### ### #. .# ..#
35-
# .# . .. ### # ##.#####
36-
# ### ###### #### . #. . #
37-
# . .# . #### ###### ### #
38-
#####.## # ### .. . #. #
39-
#.. #. .# ### ####### #
40-
# # .## # . # #
41-
## ###### # . . # # #
42-
#a # . # ######### #. #
43-
#b . . ##....#..... .#
30+
#.. ... . # . y#
31+
#######.## # ### . x#
32+
# .. # . ####### ###
33+
#. .#.#.# # # # #. . #
34+
###.#### # .. ## #..#.#. #
35+
# #.#.### #### ### # # ### ##
36+
# # # . . # # #. #
37+
# .# # # . . # # #
38+
## ### # # ### #### ###.#.# #
39+
# .#.#..# ## .. # ####.###
40+
# . .# # # # #.#.#. .#
41+
### ####### . # .. #
42+
#a . ### # ##.#######
43+
#b . # . ... ..#
4444
################################
4545
"""
4646

4747
maze_103525239_odd = """
4848
################################
49-
#. . #.. . ## . . y#
50-
# # ###### ### ##### #x#
51-
# . ## # .# # . . #. .#
52-
#. # .#....#.## # . . # #
53-
# #. ### ## # . ## ###
54-
# #.. . ########## # #
55-
# # .#.# #.##.# #.#. # #
56-
# # ########## . ..# #
57-
### ## . # ## ### .# #
58-
# # . . # ##.#....#. # .#
59-
#. .# . . # #. # ## . #
60-
#a# ##### ### ###### # #
61-
#b . . ## . ..# . .#
49+
# # .# . .# . #. y#
50+
#. #..# ## . # .#.##x#
51+
# . # ####### ##..# #. #. #
52+
#. # .. # # .# #
53+
#### #### # ## ####### # ###
54+
# . .#.# . ...# . .#
55+
### .####### ###### #######. ###
56+
#. . #... . #.#. . #
57+
### # ####### ## # #### ####
58+
# #. # # .. # .#
59+
# .# .# #..## ####### # . #
60+
#a##.#. # . ## #..# .#
61+
#b .# . #. . #. # #
6262
################################
6363
"""
6464

0 commit comments

Comments
 (0)