def test_ucs_vs_a_star(): print("Test UCS vs A*") grid = GridGenerator(width=120, height=100).gen_grid() search_ucs = GraphSearch(grid) search_euclidean = GraphSearch(grid, heuristic=euclidean_admissible) result_ucs = search_ucs.search() result_euclidean = search_euclidean.search() if result_ucs is None: print("ucs result is None, serializing grid to ucs_result.txt") with open("ucs_result.txt", "w") as f: f.write(grid.serialize()) assert (result_euclidean is not None) ucs_cost = grid.path_cost(result_ucs) euclidean_cost = grid.path_cost(result_euclidean) if ucs_cost != euclidean_cost: print("UCS result:") print(result_ucs) print("Euclidean result:") print(result_euclidean) print("Costs, ucs, euclidean:") print(ucs_cost) print(euclidean_cost) else: print("Test passed")
def test_sequential_search(): print("Test sequential search") grid = GridGenerator(width=120, height=100).gen_grid() search_ucs = GraphSearch(grid) search_seq = SequentialSearch(grid, 1, 1) # this should be equivalent to ucs result_seq = search_seq.search() result_ucs = search_ucs.search() seq_cost = grid.path_cost(result_seq) ucs_cost = grid.path_cost(result_ucs) assert (ucs_cost == seq_cost) print("Test passed")
def main(): ALLOWED = (("Image", ("*.png", "*.jpg", "*.gif", "*.jpeg", "*.bmp")), ("All Files", "*.*")) fn = "" while (not fn): fn = filedialog.askopenfilename(defaultextension=".jpg", filetypes=ALLOWED, parent=root, title="Choose Image", initialdir="/") img = Image.open(fn) gridGen = GridGenerator(img) grid = gridGen.genGrid() res = floor(450 / max(img.size)) automata = Automata(master=root, grid=grid, resolution=res) automata.mainloop()
def test_sequential_weights(): print("Test sequential weights") grid = GridGenerator(width=120, height=100).gen_grid() search_ucs = GraphSearch(grid) search_seq = SequentialSearch(grid, 1.25, 2) result_seq = search_seq.search() result_ucs = search_ucs.search() seq_cost = grid.path_cost(result_seq) ucs_cost = grid.path_cost(result_ucs) assert (ucs_cost <= seq_cost) if ucs_cost == seq_cost: print( "Costs were equal, this is fine. If this occurs every time something is wrong." ) print("Test passed")
def test_weighted_a_star(): print("Test weighted A*") grid = GridGenerator(width=120, height=100).gen_grid() search_ucs = GraphSearch(grid) search_weighted = GraphSearch(grid, heuristic=euclidean_admissible, weight=5) result_ucs = search_ucs.search() result_weighted = search_weighted.search() assert (result_weighted is not None) ucs_cost = grid.path_cost(result_ucs) weighted_cost = grid.path_cost(result_weighted) assert (weighted_cost <= 5 * ucs_cost) print("weighted cost:", weighted_cost) print("ucs cost:", ucs_cost) print("Test passed")
def test_chebyshev_heuristic(): print("Test chebyshev admissible heuristic") grid = GridGenerator(width=120, height=100).gen_grid() search_ucs = GraphSearch(grid) search_chebyshev = GraphSearch(grid, heuristic=chebyshev) search_chebyshev_adm = GraphSearch(grid, heuristic=chebyshev_admissible) result_ucs = search_ucs.search() result_chebyshev = search_chebyshev.search() result_chebyshev_adm = search_chebyshev_adm.search() assert (result_chebyshev is not None) ucs_cost = grid.path_cost(result_ucs) chebyshev_cost = grid.path_cost(result_chebyshev) chebyshev_adm_cost = grid.path_cost(result_chebyshev_adm) assert (ucs_cost <= chebyshev_cost) assert (ucs_cost == chebyshev_adm_cost) print("Test passed")
def test_gen(): gen = GridGenerator(width=100, height=80) print(gen.gen_grid().serialize())