def generateDfsBfsPlot(n): """ Generates a plot. :param n: # of random problems to test algorithm on. """ plt.figure() plt.xlabel("Problem Number") plt.ylabel("Complexity") X = [] Y_B = [] Y_D = [] for i in range(int(n)): p = generateRandomProblem() bfs_trace, bfs_nodes, bfs_depth, bfs_time, bfs_memory, bfs_cost = S.bfs( p) dfs_trace, dfs_nodes, dfs_depth, dfs_time, dfs_memory, dfs_cost = S.dfs( p) y_bfs = bfs_nodes + bfs_depth + bfs_time + bfs_memory + bfs_cost y_dfs = dfs_nodes + dfs_depth + dfs_time + dfs_memory + dfs_cost Y_B.append(y_bfs) Y_D.append(y_dfs) X.append(i) plt.plot(X, Y_B, marker='o', linestyle='-', color='r') plt.plot(X, Y_D, marker='o', linestyle='-', color='g') plt.legend(('BFS', 'DFS')) plt.show()
def main(): rawClauses = read_stdin() clauseInst = Clause(rawClauses) resolution = Problem(initialState=[], possibleActions=clauseInst.clausesList, goalState=[]) status = Search.bfs(resolution) print("\n{}".format(status))
try: # Get event pos and then convert coordinates to an actual location on the grid # Color in the cell and set the coordinates in matrix to non-visitable pos_x, pos_y = event.pos pos_x, pos_y = myGrid.getCell(pos_x, pos_y) rec = pygame.Rect(pos_x, pos_y, 20, 20) pygame.draw.rect(myGrid.screen, config.fill, rec) myMatrix.setCell(pos_y // 20, pos_x // 20, "B") pygame.display.update() except: pass pygame.display.flip() # Start a thread of the algorithm user selected if algorithm == "BFS": t1 = threading.Thread(target=myMatrix.bfs()) elif algorithm == "DFS": t1 = threading.Thread(target=myMatrix.dfs()) elif algorithm == "A-Star": t1 = threading.Thread(target=myMatrix.a_star()) t1.start() # Color in cells from the search algorithm and wait for quitgame while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if not gridQ.empty(): x, y, clr = gridQ.get()
def find_node(self): return Search.bfs(self.tree, self.cur_best_node)