def benchmark(self): self.x_axis, self.y_axis, self.mazes = [], [], [] for iteration in range(self.start, self.start + self.cycles): dimension = iteration if not self.constant else self.start obj = Maze((dimension, dimension), logger=self.logger) obj.path_generator() path = sum([1 if node.state == 1 else 0 for node in obj.node_map]) total = iteration**2 ratio = path**2 / total self.mazes.append({ "index": len(self.mazes), "generation_order": iteration - self.start, "maze": obj, "path": path, "total": total, "ratio": ratio }) self.x_axis.append(total) self.y_axis.append(ratio) self.mazes = sorted(self.mazes, key=lambda x: x["ratio"], reverse=True)
def test_2(size=CONSTANTS["dimensions"], logger=CONSTANTS["logger"]): """Path generation test with ASCII representation.""" print("\nTEST 2") obj = Maze(size, logger=logger) obj.path_generator() print(obj.ascii())
def test_6(cycles=CONSTANTS["cycles"], logger=False): """Visual benchmarking utility (search methods).""" x_axis = range(3, cycles) y_axis_1, y_axis_2 = [], [] for iteration in x_axis: obj = Maze((iteration, iteration), logger=logger) obj.path_generator() time_0 = time() obj.dfs() time_1 = time() obj.gbfs() time_2 = time() y_axis_1.append(time_1 - time_0) y_axis_2.append(time_2 - time_1) curve_1, = plt.plot(x_axis, y_axis_1, color="orange", label="Depth-First Search") curve_2, = plt.plot(x_axis, y_axis_2, color="red", label="Greedy Best-First Search") plt.title("Elapsed times comparison") plt.xlabel("Order of dimensions") plt.ylabel("Time elapsed (s)") plt.legend(loc="center left") plt.show()
def test_1(size=CONSTANTS["dimensions"], logger=CONSTANTS["logger"]): """Instance creation test with ASCII representation.""" print("\nTEST 1") obj = Maze(size, logger=logger) print(obj)
def test_5(size=CONSTANTS["dimensions"], show_image=CONSTANTS["show_image"], save_image=CONSTANTS["save_image"], logger=CONSTANTS["logger"]): """Path solving (GBFS) with ASCII and image representation""" print("\nTEST 5") obj = Maze(size, logger=logger) obj.path_generator() obj.gbfs() print(obj.ascii()) obj.image()
def test_3(size=CONSTANTS["dimensions"], show_image=CONSTANTS["show_image"], save_image=CONSTANTS["save_image"], logger=CONSTANTS["logger"]): """Path generation test with image representation.""" print("\nTEST 3") obj = Maze(size, logger=logger) obj.path_generator() obj.image()
title = "DFS" elif self.solver.name.lower() == 'ase': title = "A* w/ Euclidean Distance" elif self.solver.name.lower() == 'asm': title = "A* w/ Manhattan Distance" plt.title(title) plt.show() # Handle any saving if self.fname: print('Saving animation...') mpeg_writer = animation.FFMpegWriter(fps=24, bitrate=-1) anim.save( f"{self.fname}_{self.maze.dim}x{self.maze.dim}_{self.maze.p}_solution.mp4", writer=mpeg_writer) print('Done.') else: print("Maze is not solvable.") if __name__ == '__main__': M = Maze(10, 0.3) D = DFSSolver(M) B = BFSSolver(M) A = Astar(M) Vis = MazeVisualization(M, solver=A, cell_size=1, fname=None) Vis.maze_animation()