예제 #1
0
def expandNode(maze, node, frontierPriorityQueue, frontierHashTable, exploredHashTable,depthLimit):



    if (node.depth < depthLimit):

        # the expansion order is opposite because last element becomes first in the heap, thus it will expand in the right order
        for direction in ['U', 'LU', 'L', 'LD', 'D', 'RD', 'R', 'RU']:

            x,y = getCoordsFromDirection(direction, node.x, node.y)
            if maze.isValidMove(x,y):
                newNodeCost = maze.getCost(x, y)
                heuristicValue = -(node.depth+1)
                newNode = Node(x,y,newNodeCost,node,node.pathCost + newNodeCost,heuristicValue,node.depth+1,heuristicValue)


                # new node
                if newNode.key not in exploredHashTable and newNode.key not in frontierHashTable:
                    frontierPriorityQueue.push(newNode)
                    frontierHashTable[newNode.key] = newNode

                # node is in frontier
                elif newNode.key in frontierHashTable:
                    if newNode.depth < frontierHashTable[newNode.key].depth or (newNode.pathCostWithHeuristic == frontierHashTable[newNode.key].pathCostWithHeuristic and newNode.heuristicCost < frontierHashTable[newNode.key].heuristicCost):

                        frontierPriorityQueue.popSpecific(frontierHashTable[newNode.key])
                        frontierPriorityQueue.push(newNode)
                        frontierHashTable[newNode.key] = newNode

                # node in explored and not in frontier
                elif newNode.key in exploredHashTable and newNode.key not in frontierHashTable:
                    if newNode.depth < exploredHashTable[newNode.key].depth or ( newNode.depth == exploredHashTable[newNode.key].depth and newNode.heuristicCost < exploredHashTable[newNode.key].heuristicCost):
                        frontierPriorityQueue.push(newNode)
                        frontierHashTable[newNode.key] = newNode
예제 #2
0
def expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
               exploredHashTable, FLimit, heuristic):

    global heuristicSum
    global heuristicCounter
    global remaining
    remaining = False
    if (node.pathCostWithHeuristic < FLimit):

        # the expansion order is opposite because last element becomes first in the heap, thus it will expand in the right order
        for direction in ['U', 'LU', 'L', 'LD', 'D', 'RD', 'R', 'RU']:

            x, y = getCoordsFromDirection(direction, node.x, node.y)

            if maze.isValidMove(x, y):
                newNodeCost = maze.getCost(x, y)
                heuristicValue = heuristic(x, y, maze.goalNode)
                newNode = Node(x, y, newNodeCost, node,
                               node.pathCost + newNodeCost,
                               node.pathCost + newNodeCost + heuristicValue,
                               node.depth + 1, heuristicValue)

                heuristicSum += heuristicValue
                heuristicCounter += 1

                # new node
                if newNode.key not in exploredHashTable and newNode.key not in frontierHashTable:
                    frontierPriorityQueue.push(newNode)
                    frontierHashTable[newNode.key] = newNode
                    # painting frontier node in yellow
                    pen.paint_tile(newNode.x, newNode.y, pen.light_green,
                                   False)

                # node is in frontier
                elif newNode.key in frontierHashTable:
                    if newNode.pathCost < frontierHashTable[
                            newNode.key].pathCost or (
                                newNode.pathCostWithHeuristic
                                == frontierHashTable[
                                    newNode.key].pathCostWithHeuristic
                                and newNode.heuristicCost <
                                frontierHashTable[newNode.key].heuristicCost):

                        frontierPriorityQueue.popSpecific(
                            frontierHashTable[newNode.key])
                        frontierPriorityQueue.push(newNode)
                        frontierHashTable[newNode.key] = newNode

                # node in explored and not in frontier
                elif newNode.key in exploredHashTable and newNode.key not in frontierHashTable:
                    if newNode.pathCost < exploredHashTable[newNode.key].pathCost or\
                            (newNode.pathCost == exploredHashTable[newNode.key].pathCost and newNode.pathCostWithHeuristic < exploredHashTable[
                                newNode.key].pathCostWithHeuristic):
                        frontierPriorityQueue.push(newNode)
                        frontierHashTable[newNode.key] = newNode

    else:
        remaining = True
예제 #3
0
def expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
               exploredHashTable, turn, heuristic):
    global heuristicSum
    global heuristicCounter

    # the expansion order is opposite because last element becomes first in the heap, thus it will expand in the right order
    for direction in ['U', 'LU', 'L', 'LD', 'D', 'RD', 'R', 'RU']:

        x, y = getCoordsFromDirection(direction, node.x, node.y)
        if maze.isValidMove(x, y):
            newNodeCost = maze.getCost(x, y)

            # setting heuristic according to which search we are currently at.
            if turn is True:  # front search
                heuristicValue = heuristic(x, y, maze.goalNode)
                # heuristicValue = minimumMoves(x,y,maze.goalNode)
            elif turn is False:  # backwards search
                heuristicValue = heuristic(x, y, maze.startNode)
                # heuristicValue = minimumMovesBi(x, y, maze.goalNode)
            newNode = Node(x, y, newNodeCost, node,
                           node.pathCost + newNodeCost,
                           node.pathCost + newNodeCost + heuristicValue,
                           node.depth + 1, heuristicValue)

            heuristicSum += heuristicValue
            heuristicCounter += 1

            # new node, insert it to PQ and Hashtable
            if newNode.key not in exploredHashTable and newNode.key not in frontierHashTable:

                frontierPriorityQueue.push(newNode)
                frontierHashTable[newNode.key] = newNode

            # node is already in frontier
            elif newNode.key in frontierHashTable:
                if newNode.pathCostWithHeuristic < frontierHashTable[
                        newNode.key].pathCostWithHeuristic or (
                            newNode.pathCostWithHeuristic == frontierHashTable[
                                newNode.key].pathCostWithHeuristic
                            and newNode.heuristicCost <
                            frontierHashTable[newNode.key].heuristicCost):

                    frontierPriorityQueue.popSpecific(
                        frontierHashTable[newNode.key])
                    frontierPriorityQueue.push(newNode)
                    frontierHashTable[newNode.key] = newNode

            # node in explored and not in frontier
            elif newNode.key in exploredHashTable and newNode.key not in frontierHashTable:
                if newNode.pathCostWithHeuristic < exploredHashTable[
                        newNode.key].pathCostWithHeuristic or (
                            newNode.pathCostWithHeuristic == exploredHashTable[
                                newNode.key].pathCostWithHeuristic
                            and newNode.heuristicCost <
                            exploredHashTable[newNode.key].heuristicCost):
                    frontierPriorityQueue.push(newNode)
                    frontierHashTable[newNode.key] = newNode
예제 #4
0
def expandNode(maze, node, frontierPriorityQueue, frontierHashTable, exploredHashTable):

    # the expansion order is opposite because last element becomes first in the heap, thus it will expand in the right order
    for direction in ['U', 'LU', 'L', 'LD', 'D', 'RD', 'R', 'RU']:

        x,y = getCoordsFromDirection(direction, node.x, node.y)

        if maze.isValidMove(x,y):
            nodeCost = maze.getCost(x,y)
            newNode = Node(x,y,nodeCost,node,node.pathCost + nodeCost,node.pathCost + nodeCost,node.depth+1)

            # new node
            if newNode.key not in exploredHashTable and newNode.key not in frontierHashTable:
                frontierPriorityQueue.push(newNode)
                frontierHashTable[newNode.key] = newNode
예제 #5
0
def killDeadEndsPath(maze, node):
    global movesMatrix
    x = node.x
    y = node.y

    successExpands = 0
    for direction in ['RU', 'R', 'RD', 'D', 'LD', 'L', 'LU', 'U']:
        newX, newY = getCoordsFromDirection(direction, x, y)
        if maze.isValidMove(
                newX, newY
        ) and node.fatherNode.x != newX and node.fatherNode.y != newY:
            successExpands += 1

    if successExpands == 1 and x != maze.goalNode.x and y != maze.goalNode.y:
        movesMatrix[x][y] = 99999
        killDeadEndsPath(maze, node.fatherNode)
    else:
        return
예제 #6
0
def expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
               exploredHashTable, heuristic):

    global heuristicSum
    global heuristicCounter
    global h_time

    # the expansion order is opposite because last element becomes first in the heap, thus it will expand in the right order
    for direction in ['U', 'LU', 'L', 'LD', 'D', 'RD', 'R', 'RU']:

        x, y = getCoordsFromDirection(direction, node.x, node.y)
        if maze.isValidMove(x, y):
            newNodeCost = maze.getCost(x, y)
            heuristicValue = heuristic(x, y, maze.goalNode)
            newNode = Node(x, y, newNodeCost, node,
                           node.pathCost + newNodeCost,
                           node.pathCost + newNodeCost + heuristicValue,
                           node.depth + 1, heuristicValue)

            heuristicSum += heuristicValue
            heuristicCounter += 1

            # new node
            if newNode.key not in exploredHashTable and newNode.key not in frontierHashTable:
                frontierPriorityQueue.push(newNode)
                frontierHashTable[newNode.key] = newNode

            # node is in frontier
            elif newNode.key in frontierHashTable:
                if newNode.pathCost < frontierHashTable[
                        newNode.key].pathCost or (
                            newNode.pathCostWithHeuristic == frontierHashTable[
                                newNode.key].pathCostWithHeuristic
                            and newNode.heuristicCost <
                            frontierHashTable[newNode.key].heuristicCost):

                    # heapdict
                    frontierPriorityQueue.popSpecific(
                        frontierHashTable[newNode.key])
                    frontierPriorityQueue.push(newNode)
                    frontierHashTable[newNode.key] = newNode
예제 #7
0
def iterativeBFS(maze):
    global movesMatrix
    global orderStack
    global visited
    global minVal

    while len(orderStack) is not 0:

        moves, x, y = orderStack.pop(0)
        visited[str(x) + "," + str(y)] = 1

        if movesMatrix[x][y] > moves:
            movesMatrix[x][y] = moves

            for direction in ['RU', 'R', 'RD', 'D', 'LD', 'L', 'LU', 'U']:

                newX, newY = getCoordsFromDirection(direction, x, y)
                if maze.isValidMove(newX, newY) and (str(newX) + "," +
                                                     str(newY)) not in visited:
                    orderStack.append((moves + 1, newX, newY))
                    if maze.maze[newX][newY] < minVal:
                        minVal = maze.maze[newX][newY]