def run(rows, columns, depth, weights): def update_board_with_matches(board, matches): for piece, clusters in matches: for r, c in clusters: board.update(r, c, type(piece)) b = Board.create_randomized_board(5, 6) # b = Board([Fire, Wood, Water, Dark, Light, Light, # Water, Fire, Water, Light, Heart, Light, # Fire, Water, Dark, Heart, Heart, Wood, # Light, Water, Light, Fire, Wood, Wood, # Dark, Heart, Dark, Light, Heart, Light], 5, 6) m = Board.create_empty_board(rows, columns) update_board_with_matches(m, b.get_matches()) h = GreedyDfs(weights) if False else PrunedBfs(weights) h.diagonals = True start = time.time() moves = h.solve(b, depth) performance = time.time() - start print('---------------------------------------------') print(b) print(m) print('run time : ' + str(performance)) print('best move score : ' + str(moves[0])) print('start : ' + str(moves[1][0])) print('moves : ' + str(moves[1][1:])) print(moves[2]) m = Board.create_empty_board(rows, columns) update_board_with_matches(m, moves[2].get_matches()) print(m)
def test_greedy_dfs(weights): def compare(original, final, moves, row, column, assert_error_message): for delta_r, delta_c in moves: original.swap(row, column, row + delta_r, column + delta_c) row += delta_r column += delta_c for r in range(original.rows): for c in range(original.columns): assert isinstance(original.cell(r, c), type(final.cell(r, c))), assert_error_message # solution should contain move list that gets you to the final board state starting from original board state original = Board.create_randomized_board(5, 6) _, moves, final_board = GreedyDfs(weights).solve(original, 20) compare(original, final_board, moves[1:], moves[0][0], moves[0][1], "Greedy DFS yielded an incorrect 4-way move list!") # same with diagonals enabled original = Board.create_randomized_board(5, 6) h = GreedyDfs(weights) h.diagonals = True _, moves, final_board = h.solve(original, 20) compare(original, final_board, moves[1:], moves[0][0], moves[0][1], "Greedy DFS yielded an incorrect 8-way move list!")
def test_finding_swaps_4_way(board): # diagonal-disabled setup should return 4 possible swaps h = GreedyDfs(weights) swaps = h._swaps(board, 2, 2) assert len(swaps) == 4, "Swaps @ 2,2 did not yield expected swap count of 4!" assert len(set(swaps).intersection(set([(-1, 0), (0, 1), (1, 0), (0, -1)]))) == 4, "Swaps @ 2,2 did not yield correct set of deltas!" # swapping at corners should yield 2 swaps = h._swaps(board, 0, 0) assert len(swaps) == 2, "Corner swaps @ 0,0 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([(0, 1), (1, 0)]))) == 2, "Corner swaps @ 0,0 did not yield correct set of deltas!" swaps = h._swaps(board, 0, 5) assert len(swaps) == 2, "Corner swaps @ 0,5 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([(1, 0), (0, -1)]))) == 2, "Corner swaps @ 0,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 5) assert len(swaps) == 2, "Corner swaps @ 4,5 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([(-1, 0), (0, -1)]))) == 2, "Corner swaps @ 4,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 0) assert len(swaps) == 2, "Corner swaps @ 4,0 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([(-1, 0), (0, 1)]))) == 2, "Corner swaps @ 4,0 did not yield correct set of deltas!" # swapping at edges should yield 3 swaps = h._swaps(board, 0, 3) assert len(swaps) == 3, "Edge swaps @ 0,3 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(0, 1), (1, 0), (0, -1)]))) == 3, "Edge swaps @ 0,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 5) assert len(swaps) == 3, "Edge swaps @ 2,5 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(-1, 0), (1, 0), (0, -1)]))) == 3, "Edge swaps @ 2,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 3) assert len(swaps) == 3, "Edge swaps @ 4,3 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(-1, 0), (0, 1), (0, -1)]))) == 3, "Edge swaps @ 4,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 0) assert len(swaps) == 3, "Edge swaps @ 2,0 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(-1, 0), (0, 1), (1, 0)]))) == 3, "Edge swaps @ 2,0 did not yield correct set of deltas!"
from pazudorasolver.heuristics.pruned_bfs import PrunedBfs weights = { Fire.symbol: 1.0, Wood.symbol: 1.0, Water.symbol: 1.0, Dark.symbol: 1.0, Light.symbol: 1.0, Heart.symbol: 1.0, Poison.symbol: 0.5, Jammer.symbol: 0.5, Unknown.symbol: 0.0 } board = Board.create_randomized_board(5, 6) matches = board.get_matches() print(board) print(matches) # try GreedyDfs heuristic solver1 = GreedyDfs(weights) solution = solver1.solve(board, 50) print(solution) # try PrunedBfs heuristic solver2 = PrunedBfs(weights) solution = solver2.solve(board, 50) print(solution)
def test_finding_swaps_4_way(board): # diagonal-disabled setup should return 4 possible swaps h = GreedyDfs(weights) swaps = h._swaps(board, 2, 2) assert len( swaps) == 4, "Swaps @ 2,2 did not yield expected swap count of 4!" assert len( set(swaps).intersection(set([ (-1, 0), (0, 1), (1, 0), (0, -1) ]))) == 4, "Swaps @ 2,2 did not yield correct set of deltas!" # swapping at corners should yield 2 swaps = h._swaps(board, 0, 0) assert len( swaps ) == 2, "Corner swaps @ 0,0 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([ (0, 1), (1, 0) ]))) == 2, "Corner swaps @ 0,0 did not yield correct set of deltas!" swaps = h._swaps(board, 0, 5) assert len( swaps ) == 2, "Corner swaps @ 0,5 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([ (1, 0), (0, -1) ]))) == 2, "Corner swaps @ 0,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 5) assert len( swaps ) == 2, "Corner swaps @ 4,5 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([ (-1, 0), (0, -1) ]))) == 2, "Corner swaps @ 4,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 0) assert len( swaps ) == 2, "Corner swaps @ 4,0 did not yield expected swap count of 2!" assert len(set(swaps).intersection(set([ (-1, 0), (0, 1) ]))) == 2, "Corner swaps @ 4,0 did not yield correct set of deltas!" # swapping at edges should yield 3 swaps = h._swaps(board, 0, 3) assert len( swaps) == 3, "Edge swaps @ 0,3 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (0, 1), (1, 0), (0, -1) ]))) == 3, "Edge swaps @ 0,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 5) assert len( swaps) == 3, "Edge swaps @ 2,5 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (-1, 0), (1, 0), (0, -1) ]))) == 3, "Edge swaps @ 2,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 3) assert len( swaps) == 3, "Edge swaps @ 4,3 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (-1, 0), (0, 1), (0, -1) ]))) == 3, "Edge swaps @ 4,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 0) assert len( swaps) == 3, "Edge swaps @ 2,0 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (-1, 0), (0, 1), (1, 0) ]))) == 3, "Edge swaps @ 2,0 did not yield correct set of deltas!"
def test_scoring(weights, board): # scoring should yield a score using the number of matches found and 10% of a random number score = GreedyDfs(weights)._score(board) assert 30.0 <= score <= 31.0, "Scoring of board with just matches did not yield expected value between 9.0 and 19.0!"
def test_finding_swaps_8_way(board): # diagonal-enabled setup should return 8 possible swaps h = GreedyDfs(weights) h.diagonals = True swaps = h._swaps(board, 2, 2) assert len( swaps ) == 8, "Swaps @ 2,2 (diagonals)did not yield expected swap count of 8!" assert len(set(swaps).intersection(set([(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]))) == 8, \ "Swaps @ 2,2 did not yield correct set of deltas!" # swapping at corners should yield 3 swaps = h._swaps(board, 0, 0) assert len( swaps ) == 3, "Corner swaps @ 0,0 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (0, 1), (1, 1), (1, 0) ]))) == 3, "Corner swaps @ 0,0 did not yield correct set of deltas!" swaps = h._swaps(board, 0, 5) assert len( swaps ) == 3, "Corner swaps @ 0,5 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (1, 0), (1, -1), (0, -1) ]))) == 3, "Corner swaps @ 0,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 5) assert len( swaps ) == 3, "Corner swaps @ 4,5 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (-1, -1), (-1, 0), (0, -1) ]))) == 3, "Corner swaps @ 4,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 0) assert len( swaps ) == 3, "Corner swaps @ 4,0 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([ (-1, 0), (-1, 1), (0, 1) ]))) == 3, "Corner swaps @ 4,0 did not yield correct set of deltas!" # swapping at edges should yield 5 swaps = h._swaps(board, 0, 3) assert len( swaps ) == 5, "Edge swaps @ 0,3 did not yield expected swap count of 5!" assert len( set(swaps).intersection(set([ (0, 1), (1, 1), (1, 0), (1, -1), (0, -1) ]))) == 5, "Edge swaps @ 0,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 5) assert len( swaps ) == 5, "Edge swaps @ 2,5 did not yield expected swap count of 5!" assert len( set(swaps).intersection( set([ (-1, -1), (-1, 0), (1, 0), (1, -1), (0, -1) ]))) == 5, "Edge swaps @ 2,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 3) assert len( swaps ) == 5, "Edge swaps @ 4,3 did not yield expected swap count of 5!" assert len( set(swaps).intersection( set([ (-1, -1), (-1, 0), (-1, 1), (0, 1), (0, -1) ]))) == 5, "Edge swaps @ 4,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 0) assert len( swaps ) == 5, "Edge swaps @ 2,0 did not yield expected swap count of 5!" assert len( set(swaps).intersection(set([ (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0) ]))) == 5, "Edge swaps @ 2,0 did not yield correct set of deltas!"
def test_finding_swaps_8_way(board): # diagonal-enabled setup should return 8 possible swaps h = GreedyDfs(weights) h.diagonals = True swaps = h._swaps(board, 2, 2) assert len(swaps) == 8, "Swaps @ 2,2 (diagonals)did not yield expected swap count of 8!" assert len(set(swaps).intersection(set([(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]))) == 8, \ "Swaps @ 2,2 did not yield correct set of deltas!" # swapping at corners should yield 3 swaps = h._swaps(board, 0, 0) assert len(swaps) == 3, "Corner swaps @ 0,0 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(0, 1), (1, 1), (1, 0)]))) == 3, "Corner swaps @ 0,0 did not yield correct set of deltas!" swaps = h._swaps(board, 0, 5) assert len(swaps) == 3, "Corner swaps @ 0,5 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(1, 0), (1, -1), (0, -1)]))) == 3, "Corner swaps @ 0,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 5) assert len(swaps) == 3, "Corner swaps @ 4,5 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(-1, -1), (-1, 0), (0, -1)]))) == 3, "Corner swaps @ 4,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 0) assert len(swaps) == 3, "Corner swaps @ 4,0 did not yield expected swap count of 3!" assert len(set(swaps).intersection(set([(-1, 0), (-1, 1), (0, 1)]))) == 3, "Corner swaps @ 4,0 did not yield correct set of deltas!" # swapping at edges should yield 5 swaps = h._swaps(board, 0, 3) assert len(swaps) == 5, "Edge swaps @ 0,3 did not yield expected swap count of 5!" assert len(set(swaps).intersection(set([(0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]))) == 5, "Edge swaps @ 0,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 5) assert len(swaps) == 5, "Edge swaps @ 2,5 did not yield expected swap count of 5!" assert len(set(swaps).intersection(set([(-1, -1), (-1, 0), (1, 0), (1, -1), (0, -1)]))) == 5, "Edge swaps @ 2,5 did not yield correct set of deltas!" swaps = h._swaps(board, 4, 3) assert len(swaps) == 5, "Edge swaps @ 4,3 did not yield expected swap count of 5!" assert len(set(swaps).intersection(set([(-1, -1), (-1, 0), (-1, 1), (0, 1), (0, -1)]))) == 5, "Edge swaps @ 4,3 did not yield correct set of deltas!" swaps = h._swaps(board, 2, 0) assert len(swaps) == 5, "Edge swaps @ 2,0 did not yield expected swap count of 5!" assert len(set(swaps).intersection(set([(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0)]))) == 5, "Edge swaps @ 2,0 did not yield correct set of deltas!"