diff options
Diffstat (limited to 'tests/perf_bench/bm_hexiom.py')
-rw-r--r-- | tests/perf_bench/bm_hexiom.py | 111 |
1 files changed, 62 insertions, 49 deletions
diff --git a/tests/perf_bench/bm_hexiom.py b/tests/perf_bench/bm_hexiom.py index 3a6f1f6c4b..84eda9a909 100644 --- a/tests/perf_bench/bm_hexiom.py +++ b/tests/perf_bench/bm_hexiom.py @@ -9,18 +9,12 @@ ################################## class Dir(object): - def __init__(self, x, y): self.x = x self.y = y -DIRS = [Dir(1, 0), - Dir(-1, 0), - Dir(0, 1), - Dir(0, -1), - Dir(1, 1), - Dir(-1, -1)] +DIRS = [Dir(1, 0), Dir(-1, 0), Dir(0, 1), Dir(0, -1), Dir(1, 1), Dir(-1, -1)] EMPTY = 7 @@ -37,8 +31,7 @@ class Done(object): def __init__(self, count, empty=False): self.count = count - self.cells = None if empty else [ - [0, 1, 2, 3, 4, 5, 6, EMPTY] for i in range(count)] + self.cells = None if empty else [[0, 1, 2, 3, 4, 5, 6, EMPTY] for i in range(count)] def clone(self): ret = Done(self.count, True) @@ -100,7 +93,7 @@ class Done(object): maxval = -1 maxi = -1 for i in range(self.count): - if (not self.already_done(i)): + if not self.already_done(i): maxvali = max(k for k in self.cells[i] if k != EMPTY) if maxval < maxvali: maxval = maxvali @@ -109,7 +102,7 @@ class Done(object): def next_cell_first(self): for i in range(self.count): - if (not self.already_done(i)): + if not self.already_done(i): return i return -1 @@ -119,8 +112,10 @@ class Done(object): for i in range(self.count): if not self.already_done(i): cells_around = pos.hex.get_by_id(i).links - n = sum(1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0 - for nid in cells_around) + n = sum( + 1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0 + for nid in cells_around + ) if n > maxn: maxn = n maxi = i @@ -132,8 +127,10 @@ class Done(object): for i in range(self.count): if not self.already_done(i): cells_around = pos.hex.get_by_id(i).links - n = sum(1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0 - for nid in cells_around) + n = sum( + 1 if (self.already_done(nid) and (self[nid][0] != EMPTY)) else 0 + for nid in cells_around + ) if n < minn: minn = n mini = i @@ -155,21 +152,21 @@ class Done(object): else: raise Exception("Wrong strategy: %d" % strategy) + ################################## class Node(object): - def __init__(self, pos, id, links): self.pos = pos self.id = id self.links = links + ################################## class Hex(object): - def __init__(self, size): self.size = size self.count = 3 * size * (size - 1) + 1 @@ -213,7 +210,6 @@ class Hex(object): ################################## class Pos(object): - def __init__(self, hex, tiles, done=None): self.hex = hex self.tiles = tiles @@ -222,6 +218,7 @@ class Pos(object): def clone(self): return Pos(self.hex, self.tiles, self.done.clone()) + ################################## @@ -231,8 +228,7 @@ def constraint_pass(pos, last_move=None): done = pos.done # Remove impossible values from free cells - free_cells = (range(done.count) if last_move is None - else pos.hex.get_by_id(last_move).links) + free_cells = range(done.count) if last_move is None else pos.hex.get_by_id(last_move).links for i in free_cells: if not done.already_done(i): vmax = 0 @@ -273,8 +269,7 @@ def constraint_pass(pos, last_move=None): changed = True # Force empty or non-empty around filled cells - filled_cells = (range(done.count) if last_move is None - else [last_move]) + filled_cells = range(done.count) if last_move is None else [last_move] for i in filled_cells: if done.already_done(i): num = done[i][0] @@ -320,8 +315,7 @@ def find_moves(pos, strategy, order): return [(cell_id, v) for v in done[cell_id]] else: # Try higher values first and EMPTY last - moves = list(reversed([(cell_id, v) - for v in done[cell_id] if v != EMPTY])) + moves = list(reversed([(cell_id, v) for v in done[cell_id] if v != EMPTY])) if EMPTY in done[cell_id]: moves.append((cell_id, EMPTY)) return moves @@ -378,7 +372,7 @@ def solved(pos, output, verbose=False): elif done.already_done(i): num = done[i][0] tiles[num] -= 1 - if (tiles[num] < 0): + if tiles[num] < 0: return IMPOSSIBLE vmax = 0 vmin = 0 @@ -448,8 +442,7 @@ def check_valid(pos): tiles[i] = 0 # check total if tot != hex.count: - raise Exception( - "Invalid input. Expected %d tiles, got %d." % (hex.count, tot)) + raise Exception("Invalid input. Expected %d tiles, got %d." % (hex.count, tot)) def solve(pos, strategy, order, output): @@ -459,6 +452,7 @@ def solve(pos, strategy, order, output): # TODO Write an 'iterator' to go over all x,y positions + def read_file(file): lines = [line.strip("\r\n") for line in file.splitlines()] size = int(lines[0]) @@ -467,10 +461,10 @@ def read_file(file): tiles = 8 * [0] done = Done(hex.count) for y in range(size): - line = lines[linei][size - y - 1:] + line = lines[linei][size - y - 1 :] p = 0 for x in range(size + y): - tile = line[p:p + 2] + tile = line[p : p + 2] p += 2 if tile[1] == ".": inctile = EMPTY @@ -489,7 +483,7 @@ def read_file(file): line = lines[linei][y:] p = 0 for x in range(y, size * 2 - 1): - tile = line[p:p + 2] + tile = line[p : p + 2] p += 2 if tile[1] == ".": inctile = EMPTY @@ -514,63 +508,76 @@ def solve_file(file, strategy, order, output): LEVELS = {} -LEVELS[2] = (""" +LEVELS[2] = ( + """ 2 . 1 . 1 1 1 . -""", """\ +""", + """\ 1 1 . . . 1 1 -""") +""", +) -LEVELS[10] = (""" +LEVELS[10] = ( + """ 3 +.+. . +. 0 . 2 . 1+2 1 . 2 . 0+. .+.+. -""", """\ +""", + """\ . . 1 . 1 . 2 0 . 2 2 . . . . . 0 . . -""") +""", +) -LEVELS[20] = (""" +LEVELS[20] = ( + """ 3 . 5 4 . 2+.+1 . 3+2 3 . +2+. 5 . . 3 . -""", """\ +""", + """\ 3 3 2 4 5 . 1 3 5 2 . . 2 . . . . . . -""") +""", +) -LEVELS[25] = (""" +LEVELS[25] = ( + """ 3 4 . . . . 2 . 4 3 2 . 4 2 2 3 . 4 2 4 -""", """\ +""", + """\ 3 4 2 2 4 4 . . . . 4 2 . 2 4 3 . 2 . -""") +""", +) -LEVELS[30] = (""" +LEVELS[30] = ( + """ 4 5 5 . . 3 . 2+2 6 @@ -579,7 +586,8 @@ LEVELS[30] = (""" 4 5 4 . 5 4 5+2 . . 3 4 . . . -""", """\ +""", + """\ 3 4 3 . 4 6 5 2 . 2 5 5 . . 2 @@ -587,9 +595,11 @@ LEVELS[30] = (""" . 3 5 4 5 4 . 2 . 3 3 . . . . -""") +""", +) -LEVELS[36] = (""" +LEVELS[36] = ( + """ 4 2 1 1 2 3 3 3 . . @@ -598,7 +608,8 @@ LEVELS[36] = (""" 2 2 . . . 2 4 3 4 . . 3 2 3 3 -""", """\ +""", + """\ 3 4 3 2 3 4 4 . 3 2 . . 3 4 3 @@ -606,7 +617,8 @@ LEVELS[36] = (""" 3 3 . 2 . 2 3 . 2 . 2 2 2 . 1 -""") +""", +) ########################################################################### @@ -618,6 +630,7 @@ bm_params = { (5000, 1000): (10, 25, DESCENDING, Done.FIRST_STRATEGY), } + def bm_setup(params): try: import uio as io @@ -641,7 +654,7 @@ def bm_setup(params): def result(): norm = params[0] * params[1] - out = '\n'.join(line.rstrip() for line in output.splitlines()) + out = "\n".join(line.rstrip() for line in output.splitlines()) return norm, ((out == expected), out) return run, result |