def GBFS_Search(maze, vis, Dtype): Start = helper.find_pos(maze, what="S") End = helper.find_pos(maze, what="G") Result = [] Visited = [] PathCost = 0 AreaExplored = 0 MaxFrontier = 1 MaxDepth = 1 if (Start == End): Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") return Result Path = LifoQueue(maxsize=(len(maze) * len(maze[0]))) Path.put(Start) Visited.append(Start) while (Path.empty != False): if (Path.qsize() > MaxFrontier): MaxFrontier = Path.qsize() Node = Path.get() NewNode = Find_Child(maze, Node, End, Dtype, Visited) if (NewNode[0] != -1): if (NewNode == End): Result = getResult(maze) Result.append(MaxFrontier) Result.append(Result[0]) return Result else: Visited.append(NewNode) Path.put(Node) Path.put(NewNode) maze[NewNode[0]][NewNode[1]] = 'P' if (vis == True): helper.show_maze(maze) else: maze[Node[0]][Node[1]] = '.' Node = Path.get() maze[Node[0]][Node[1]] = 'P' Path.put(Node) if (vis == True): helper.show_maze(maze) Result.append("Path not found") Result.append("Path not found") Result.append("Path not found") Result.append("Path not found") return Result
def Weighted_A_Star_Search(maze, vis, Dtype, Weight): Start = helper.find_pos(maze, what="S") End = helper.find_pos(maze, what="G") Result = [] MaxFrontier = 1 MaxDepth = 1 counter = 0 # a counter to identify paths with same distance if (Start == End): Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") return Result q = [ ] # It's a sorted list with nodes (can be built as a path by tree) from shortest f(n) to largest f(n), like priority queue Root = Tree(Start) q.append([ Find_Weightd_F_Distance(maze, Start, Root, Root, End, Dtype, Weight), Root, counter ]) while (len(q) != 0): if (len(q) > MaxFrontier): MaxFrontier = len(q) tNode = q[0].copy() # Pull out the node with smallest f(n) q.pop(0) NodeonTree = tNode[1] Node = NodeonTree.data depth = Find_Distance(NodeonTree, Root) if (MaxDepth < depth): # Find the depth of the node MaxDepth = depth UpdateMaze(maze, NodeonTree, vis) # Update maze with each new path if (Node == End): newTreeNode = Tree(Node) NodeonTree.addChild(newTreeNode) tempNode = newTreeNode.getParent() while (tempNode != Root): maze[tempNode.data[0]][tempNode.data[1]] = 'P' tempNode = tempNode.getParent() maze[End[0]][End[1]] = 'G' # Remark Path and Destination's colors Result = getResult(maze) Result.append(MaxFrontier) Result.append(MaxDepth) return Result children = Find_A_Star_Children( maze, Node, Root, NodeonTree, End, Dtype) # Get the children of the current path for e in children: counter = counter + 1 # Update counter to make diffferent identification for the node newchild = Tree(e) NodeonTree.addChild(newchild) newchildDistance = Find_Distance(NodeonTree, Root) UpdateMaze(maze, newchild, vis) newDistance = Find_Weightd_F_Distance( maze, e, Root, newchild, End, Dtype, Weight) # Find the f(n) for that node find = False # insert the new node into the sorted list index = -1 has = False for a in range(len(q)): if (e == q[a][1].data ): # Find where the node is already in the container if (newDistance < q[a][0]): q[a] = [newDistance, newchild, counter] has = True if (has == False): # Insert it into the container for b in range(len(q)): if (newDistance < q[a][0]): q.insert(a, [newDistance, newchild, counter]) find = True break elif (newDistance == q[a][0]): if (counter >= q[a][2]): q.insert(a, [newDistance, newchild, counter]) find = True break if (find == False): q.append([newDistance, newchild, counter]) Result.append("Path not found") Result.append("Path not found") Result.append("Path not found") Result.append("Path not found") return Result
def BFS_Search(maze, vis): Start = helper.find_pos(maze, what="S") End = helper.find_pos(maze, what="G") # Set start point and end point Result = [] MaxFrontier = 1 MaxDepth = 1 if (Start == End): # If Start is End Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") return Result q = Queue(maxsize=(len(maze) * len(maze[0]))) # It is a FIFO queue q.put(Start) Root = Tree(Start) # Use a tree to store all the paths while (q.empty() == False): if (q.qsize() > MaxFrontier): MaxFrontier = q.qsize() Node = q.get() # Get a node from queue NodeonTree = Root.getNode(Node) depth = Find_Distance(NodeonTree, Root) if (MaxDepth < depth): # Find the depth of the node MaxDepth = depth children = Find_Children(maze, Node) if (vis == True): # Show paths for each round if (Node != Start): maze[Node[0]][Node[1]] = 'P' helper.show_maze(maze) maze[Node[0]][Node[1]] = '.' else: helper.show_maze(maze) for e in children: if (e == End): newTreeNode = Tree(e) NodeonTree.addChild(newTreeNode) tempNode = newTreeNode.getParent() while (tempNode != Root): maze[tempNode.data[0]][tempNode.data[1]] = 'P' tempNode = tempNode.getParent( ) # Rebuild the path based on the tree Result = getResult(maze) Result.append(MaxFrontier) Result.append(MaxDepth) return Result if (Root.getNode(e) == None): newTreeNode = Tree(e) maze[e[0]][e[1]] = '.' NodeonTree.addChild( newTreeNode) # Add child to the tree and to the queue q.put(e) Result.append("Path not found") Result.append("Path not found") # If no path founded Result.append("Path not found") Result.append("Path not found") # If no path founded return Result
def DLS_Search(maze, vis, depth): Start = helper.find_pos(maze, what="S") End = helper.find_pos(maze, what="G") Result = [] MaxFrontier = 1 MaxDepth = 1 # The maximum number will always show with longest distance if (Start == End): Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") Result.append("Start is End") return Result q = LifoQueue(maxsize=(len(maze) * len(maze[0]))) # It is a LIFO queue like DFS Root = Tree(Start) q.put(Root) layer = 0 while (q.empty() == False): if (q.qsize() > MaxFrontier): MaxFrontier = q.qsize() NodeonTree = q.get() Node = NodeonTree.data layer = Get_layer(NodeonTree, Root) if (layer > MaxDepth): MaxDepth = layer if (layer < depth): children = Find_DLS_Children(maze, Node, NodeonTree, Root) if (vis == True): if (Node != Start): maze[Node[0]][Node[1]] = 'P' helper.show_maze(maze) maze[Node[0]][Node[1]] = '.' else: helper.show_maze(maze) for e in children: if (e == End): newTreeNode = Tree(e) NodeonTree.addChild(newTreeNode) tempNode = newTreeNode.getParent() while (tempNode != Root): maze[tempNode.data[0]][tempNode.data[1]] = 'P' tempNode = tempNode.getParent( ) # Rebuild the path based on the tree Result = getResult(maze) Result.append(MaxFrontier) Result.append(MaxDepth) return Result newTreeNode = Tree(e) maze[e[0]][e[1]] = '.' NodeonTree.addChild( newTreeNode) # Add child to the tree and to the queue q.put(newTreeNode) Result.append("Path not found") Result.append("Path not found") Result.append("Path not found") Result.append("Path not found") return Result