示例#1
0
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
示例#2
0
def UpdateMaze(maze, NodeonTree, vis):
    for i in range(len(maze)):
        for j in range(len(maze[0])):
            if (maze[i][j] == 'P'):
                maze[i][j] = '.'  # The original path becomes explored

    path = []
    while (NodeonTree.isRoot() == False):
        path.append(NodeonTree.data)
        NodeonTree = NodeonTree.getParent()  # Get the new path

    for e in path:
        maze[e[0]][e[1]] = 'P'  # Put new path to red
    if (len(path) != 0):
        maze[path[0][0]][path[0]
                         [1]] = '.'  # The head is still under exploration
    if (vis == True):
        helper.show_maze(maze)
    return path
示例#3
0
def BFS_SearchRunner(maze, vis):
    mazes = []
    mazes.append("small_maze.txt")
    mazes.append("medium_maze.txt")
    mazes.append("large_maze.txt")
    mazes.append("empty_maze.txt")
    mazes.append("wall_maze.txt")
    mazes.append("loops_maze.txt")
    mazes.append("open_maze.txt")

    for e in mazes:
        f = open(e, "r")
        maze_str = f.read()
        maze = helper.parse_maze(maze_str)
        result = []
        print("Running BFS with ", e)
        localResult = BFS_Search(maze, False)
        helper.show_maze(maze)
        print("Path cost: ", localResult[0])
        print("Explored squares: ", localResult[1])
        print("Maximum size of the frontier: ", localResult[2])
        print("Maximum tree depth: ", localResult[3])
示例#4
0
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
示例#5
0
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
示例#6
0
    mazes.append("medium_maze.txt")
    mazes.append("large_maze.txt")
    mazes.append("empty_maze.txt")
    mazes.append("wall_maze.txt")
    mazes.append("loops_maze.txt")
    mazes.append("open_maze.txt")

    for e in mazes:
        f = open(e, "r")
        maze_str = f.read()
        maze = helper.parse_maze(maze_str)
        result = []
        print("Running BFS with ", e)
        localResult = BFS_Search(maze, False)
        helper.show_maze(maze)
        print("Path cost: ", localResult[0])
        print("Explored squares: ", localResult[1])
        print("Maximum size of the frontier: ", localResult[2])
        print("Maximum tree depth: ", localResult[3])


f = open("open_maze.txt", "r")
maze_str = f.read()
maze = helper.parse_maze(maze_str)
result = A_Star_Search(maze, False, "EU")
print("Path cost: ", result[0])
print("Explored squares: ", result[1])
print("Max Size: ", result[2])
print("Max Depth: ", result[3])
helper.show_maze(maze)