Exemple #1
0
def UCS (maze,maxRunTime):

    # initialization
    isHeuristic = False
    exploredCounter = 0

    frontierPriorityQueue = HeapDict()
    frontierHashTable = {}
    exploredHashTable = {}

    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None
    # inserting first node
    frontierPriorityQueue.push(startPoint)
    frontierHashTable[startPoint.key] = startPoint

    # Algorithm
    startTime = time.time()
    while  time.time() < (startTime + maxRunTime) :
        if frontierPriorityQueue.isEmpty():
            runTime = time.time() - startTime
            evaluateStats('UCS', maze, False, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic)
            return False

        # deleting node from frontierPriorityQueue
        node = frontierPriorityQueue.pop()
        frontierHashTable.pop(node.key)

        # appending childs so we simulate a tree
        if node != startPoint:
            node.fatherNode.childNodes.append(node)

        if maze.isGoal(node):

            # stop the timer
            runTime = time.time() - startTime
            evaluateStats('UCS', maze, True, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic)
            return True

        exploredHashTable[node.key] = node
        exploredCounter += 1
        expandNode(maze,node,frontierPriorityQueue,frontierHashTable,exploredHashTable)

    # time's up!
    runTime = time.time() - startTime
    evaluateStats('UCS', maze, False, node, frontierPriorityQueue, exploredCounter, runTime,isHeuristic)
    return False
Exemple #2
0
def IDS (maze,maxRunTime):
    # initialization
    global currentDepthLimit
    global globalExploredCounter
    currentDepthLimit = 0
    globalExploredCounter = 0
    cutOffs = []
    isHeuristic = False
    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None
    currentDepthLimit = -1
    numOfPrevExplored = None

    #algorithm
    startTime = time.time()
    while time.time() < (startTime + maxRunTime):

        currentDepthLimit += 1
        exploredCounter = 0

        frontierPriorityQueue = HeapDict()
        frontierHashTable = {}
        exploredHashTable = {}



        # calculating heuristic to first node
        startPoint.heuristicCost = -startPoint.depth
        startPoint.pathCostWithHeuristic = startPoint.heuristicCost

        # inserting first node
        frontierHashTable[startPoint.key] = startPoint
        frontierPriorityQueue.push(startPoint)

        # Algorithm
        while True:
            if frontierPriorityQueue.isEmpty():
                # checking if no solution
                if numOfPrevExplored == len(exploredHashTable):
                    runTime = time.time() - startTime
                    node.depth = currentDepthLimit
                    evaluateStats('IDS', maze, False, node, cutOffs, globalExploredCounter, runTime, isHeuristic,
                                  maxIDSdepth=currentDepthLimit - 1)
                    return False
                numOfPrevExplored = len(exploredHashTable)
                break;

            # deleting node from frontierPriorityQueue
            node = frontierPriorityQueue.pop()
            frontierHashTable.pop(node.key)

            # this case indicates a cut-off, this algorithm generates a cutoff when the depth limit is reached
            if node.depth == currentDepthLimit:
                cutOffs.append(node)

            # adding 1 to expanded nodes count
            globalExploredCounter += 1

            # checking to see if we hit the solution
            if maze.isGoal(node):
                # stop the timer
                runTime = time.time() - startTime
                node.depth = currentDepthLimit
                evaluateStats('IDS', maze, True, node, cutOffs, globalExploredCounter, runTime, isHeuristic,maxIDSdepth=currentDepthLimit)
                return True

            if node.key not in exploredHashTable:
                exploredCounter += 1
            exploredHashTable[node.key] = node
            expandNode(maze, node, frontierPriorityQueue, frontierHashTable, exploredHashTable, currentDepthLimit)



    # time's up!
    runTime = time.time() - startTime
    node.depth = currentDepthLimit
    evaluateStats('IDS', maze, False, node, cutOffs, globalExploredCounter, runTime, isHeuristic,maxIDSdepth=currentDepthLimit)
    return False
Exemple #3
0
def IDAstar(maze, maxRunTime, heuristicName):

    # starting the timer
    startTime = time.time()

    # checking if heuristic requires pre-processing
    if heuristicName == "minimumMoves":
        calculateMinimumMovesMatrix(maze, maze.goalNode)

    # initialization
    global currentFLimit
    global globalExploredCounter
    global heuristicSum
    global heuristicCounter
    global remaining
    frontierPriorityQueue = HeapDict()
    frontierHashTable = {}
    exploredHashTable = {}

    currentFLimit = 0
    globalExploredCounter = 0
    heuristicSum = 0
    heuristicCounter = 0

    heuristic = chooseHeuristic(heuristicName)
    startPoint = maze.startNode
    startPoint.heuristicCost = heuristic(startPoint.x, startPoint.y,
                                         maze.goalNode)
    startPoint.childNodes = []
    startPoint.fatherNode = None
    cutOffs = []
    isHeuristic = True
    currentFLimit = startPoint.heuristicCost + 1
    numOfPrevExplored = None
    while time.time() < (startTime + maxRunTime):
        #calculating on the run
        calcDepthRecursive(startPoint)

        currentFLimit += 1
        #exploredCounter = 0

        frontierPriorityQueue = HeapDict()
        frontierHashTable = {}
        exploredHashTable = {}

        # calculating heuristic to first node
        startPoint.heuristicCost = heuristic(startPoint.x, startPoint.y,
                                             maze.goalNode)
        startPoint.pathCostWithHeuristic = startPoint.pathCost + startPoint.heuristicCost

        # inserting first node
        frontierHashTable[startPoint.key] = startPoint
        frontierPriorityQueue.push(startPoint)

        # Algorithm
        while True:
            if frontierPriorityQueue.isEmpty():
                # checking if no solution
                if numOfPrevExplored == len(
                        exploredHashTable) and remaining is False:
                    # time's up!
                    runTime = time.time() - startTime
                    if heuristicCounter == 0:
                        heuristicSumOverHeuristicCounter = 0
                    else:
                        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                    evaluateStats('IDAstar', maze, False, node, cutOffs,
                                  globalExploredCounter, runTime, isHeuristic,
                                  heuristicName,
                                  heuristicSumOverHeuristicCounter)
                    return False
                numOfPrevExplored = len(exploredHashTable)
                break

            # deleting node from frontierPriorityQueue
            node = frontierPriorityQueue.pop()
            frontierHashTable.pop(node.key)

            # appending childs so we simulate a tree
            if node != startPoint:
                node.fatherNode.childNodes.append(node)

            # this case indicates a cut-off, this algorithm generates a cutoff when the depth limit is reached
            if node.pathCostWithHeuristic >= currentFLimit:
                cutOffs.append(node)

            # adding 1 to expanded nodes count
            globalExploredCounter += 1

            # checking to see if we hit the solution
            if maze.isGoal(node):
                # stop the timer
                runTime = time.time() - startTime
                if heuristicCounter == 0:
                    heuristicSumOverHeuristicCounter = 0
                else:
                    heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                evaluateStats('IDAstar', maze, True, node, cutOffs,
                              globalExploredCounter, runTime, isHeuristic,
                              heuristicName, heuristicSumOverHeuristicCounter)
                return True

            # if node.key not in exploredHashTable:
            #     exploredCounter += 1
            exploredHashTable[node.key] = node
            expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
                       exploredHashTable, currentFLimit, heuristic)

    # time's up!
    runTime = time.time() - startTime
    if heuristicCounter == 0:
        heuristicSumOverHeuristicCounter = 0
    else:
        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
    evaluateStats('IDAstar', maze, False, node, cutOffs, globalExploredCounter,
                  runTime, isHeuristic, heuristicName,
                  heuristicSumOverHeuristicCounter)
    return False
Exemple #4
0
def BiAstarVisual(maze, maxRunTime, heuristicName):
    # Algorithm
    startTime = time.time()

    global pen
    pen = Pen.getInstance()
    pen.maze_setup(maze)
    visual_counter = 1
    visual_turns = 2
    # initialization

    # preprocessing for heuristic
    if heuristicName == "minimumMoves":
        calculateMinimumMovesMatrix(maze, maze.goalNode)
        calculateMinimumMovesMatrixBi(maze, maze.startNode)

    isHeuristic = True
    exploredCounter = 0
    heuristic = chooseHeuristic(heuristicName)
    global heuristicSum
    global heuristicCounter
    global forwardContinue
    global backwardsContinue
    global turn
    heuristicCounter = 0
    heuristicSum = 0
    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None

    backwardsFrontierPriorityQueue = HeapDict()
    backwardsFrontierHashTable = {}
    backwardsExploredHashTable = {}

    frontierPriorityQueue = HeapDict()
    frontierHashTable = {}
    exploredHashTable = {}

    turn = False  # True = front turn, false = backwards turn

    # calculating heuristic to first node

    startPoint.heuristicCost = heuristic(startPoint.x, startPoint.y,
                                         maze.goalNode)
    startPoint.pathCostWithHeuristic = startPoint.pathCost + startPoint.heuristicCost

    # creating startpoint of backwards search
    backwardsStartPoint = Node(
        maze.goalNode.x, maze.goalNode.y, maze.goalNode.cost, None,
        maze.goalNode.cost,
        heuristic(maze.goalNode.x, maze.goalNode.y, startPoint) +
        maze.goalNode.cost, 0,
        heuristic(maze.goalNode.x, maze.goalNode.y, startPoint))

    # inserting first node at for both searches
    frontierHashTable[startPoint.key] = startPoint
    frontierPriorityQueue.push(startPoint)

    backwardsFrontierHashTable[backwardsStartPoint.key] = backwardsStartPoint
    backwardsFrontierPriorityQueue.push(backwardsStartPoint)

    forwardContinue = True
    backwardsContinue = True
    intersected = False
    optimalPathCost = None

    forwardSolutionNode = None
    backwardsSolutionNode = None

    if backwardsStartPoint.cost == -1 or startPoint.cost == -1:
        runTime = time.time() - startTime
        evaluateStats('BiAstar', maze, False, startPoint,
                      frontierPriorityQueue, exploredCounter, runTime,
                      isHeuristic, heuristicName, 0, backwardsStartPoint,
                      backwardsFrontierPriorityQueue, backwardsStartPoint)
        return False

    # Algorithm
    while time.time() < (startTime + maxRunTime):

        # checking the optimal step to stop.
        if intersected is True and backwardsFrontierPriorityQueue.isEmpty(
        ) is False and frontierPriorityQueue.isEmpty(
        ) is False and stopCondition(
                frontierPriorityQueue.peekFirst(),
                backwardsFrontierPriorityQueue.peekFirst(),
                optimalPathCost) is True:
            # stop the timer
            runTime = time.time() - startTime

            # mark - print path
            pen.paint_path(forwardSolutionNode, backwardsSolutionNode)
            if heuristicCounter == 0:
                heuristicSumOverHeuristicCounter = 0
            else:
                heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
            evaluateStats('BiAstar', maze, True, forwardSolutionNode,
                          frontierPriorityQueue, exploredCounter, runTime,
                          isHeuristic, heuristicName,
                          heuristicSumOverHeuristicCounter,
                          backwardsSolutionNode,
                          backwardsFrontierPriorityQueue, backwardsStartPoint)
            return True

        if (turn is True and forwardContinue is True) or (
                turn is False and backwardsContinue is False
        ):  # ============================= FRONT SEARCH TURN

            if frontierPriorityQueue.isEmpty():
                runTime = time.time() - startTime
                if heuristicCounter == 0:
                    heuristicSumOverHeuristicCounter = 0
                else:
                    heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                evaluateStats(
                    'BiAstar', maze, False, startPoint, frontierPriorityQueue,
                    exploredCounter, runTime, isHeuristic, heuristicName,
                    heuristicSumOverHeuristicCounter, backwardsStartPoint,
                    backwardsFrontierPriorityQueue, backwardsStartPoint)
                return False

            # deleting node from frontierPriorityQueue
            node = frontierPriorityQueue.pop()
            frontierHashTable.pop(node.key)

            # appending childs so we simulate a tree
            if node != startPoint:
                node.fatherNode.childNodes.append(node)

            # checking if we hit the solution
            if isIntersecting(node, backwardsFrontierHashTable,
                              backwardsExploredHashTable):

                # optimality condition
                if intersected == True:
                    if node.key in backwardsFrontierHashTable:
                        tmpBackwardsSolutionNode = backwardsFrontierHashTable[
                            node.key]
                    elif node.key in backwardsExploredHashTable:
                        tmpBackwardsSolutionNode = backwardsExploredHashTable[
                            node.key]

                if intersected is False or (
                        node.pathCost + tmpBackwardsSolutionNode.pathCost -
                        node.cost) < (forwardSolutionNode.pathCost +
                                      backwardsSolutionNode.pathCost -
                                      forwardSolutionNode.cost):
                    intersected = True
                    forwardSolutionNode = copy.copy(node)
                    # retrieve coliding node from backward search
                    if node.key in backwardsFrontierHashTable:
                        backwardsSolutionNode = copy.copy(
                            backwardsFrontierHashTable[node.key])
                    elif node.key in backwardsExploredHashTable:
                        backwardsSolutionNode = copy.copy(
                            backwardsExploredHashTable[node.key])

                    optimalPathCost = forwardSolutionNode.pathCost + backwardsSolutionNode.pathCost - forwardSolutionNode.cost
                    if stopCondition(
                            frontierPriorityQueue.peekFirst(),
                            backwardsFrontierPriorityQueue.peekFirst(),
                            optimalPathCost) is True:
                        # stop the timer
                        runTime = time.time() - startTime
                        if heuristicCounter == 0:
                            heuristicSumOverHeuristicCounter = 0
                        else:
                            heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                        evaluateStats('BiAstar', maze, True,
                                      forwardSolutionNode,
                                      frontierPriorityQueue, exploredCounter,
                                      runTime, isHeuristic, heuristicName,
                                      heuristicSumOverHeuristicCounter,
                                      backwardsSolutionNode,
                                      backwardsFrontierPriorityQueue,
                                      backwardsStartPoint)
                        return True

            # if node.key not in exploredHashTable:
            exploredCounter += 1
            exploredHashTable[node.key] = node
            expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
                       exploredHashTable, turn, heuristic,
                       backwardsFrontierHashTable, backwardsExploredHashTable)

            # visualize painting green expanded nodes + painting rate increase when algorithm has higher run time
            visual_counter += 1
            if visual_counter > visual_turns:
                pen.paint_tile(node.x, node.y, pen.dark_green, True)
                visual_turns *= 1.045
                if visual_turns > 110:
                    visual_turns = 110
                visual_counter = 0
            else:
                pen.paint_tile(node.x, node.y, pen.dark_green, False)

            turn = False

        elif (turn is False and backwardsContinue is True) or (
                turn is True and forwardContinue is False
        ):  # ================================ BACKWARDS SEARCH TURN

            if backwardsFrontierPriorityQueue.isEmpty():
                runTime = time.time() - startTime
                if heuristicCounter == 0:
                    heuristicSumOverHeuristicCounter = 0
                else:
                    heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                evaluateStats(
                    'BiAstar', maze, False, startPoint, frontierPriorityQueue,
                    exploredCounter, runTime, isHeuristic, heuristicName,
                    heuristicSumOverHeuristicCounter, backwardsStartPoint,
                    backwardsFrontierPriorityQueue, backwardsStartPoint)
                return False

            # deleting node from frontierPriorityQueue
            node = backwardsFrontierPriorityQueue.pop()
            backwardsFrontierHashTable.pop(node.key)

            # appending childs so we simulate a tree
            if node.key != backwardsStartPoint.key:
                node.fatherNode.childNodes.append(node)

            # checking if we hit the solution
            if isIntersecting(node, frontierHashTable, exploredHashTable):

                # optimality condition
                if intersected == True:
                    if node.key in frontierHashTable:
                        tmpForwardSolutionNode = frontierHashTable[node.key]
                    elif node.key in exploredHashTable:
                        tmpForwardSolutionNode = exploredHashTable[node.key]

                if intersected is False or (
                        node.pathCost + tmpForwardSolutionNode.pathCost -
                        node.cost) < (forwardSolutionNode.pathCost +
                                      backwardsSolutionNode.pathCost -
                                      forwardSolutionNode.cost):
                    intersected = True
                    backwardsSolutionNode = copy.copy(node)
                    # retrieve coliding node from front search
                    if node.key in frontierHashTable:
                        forwardSolutionNode = copy.copy(
                            frontierHashTable[node.key])
                    elif node.key in exploredHashTable:
                        forwardSolutionNode = copy.copy(
                            exploredHashTable[node.key])

                    optimalPathCost = forwardSolutionNode.pathCost + backwardsSolutionNode.pathCost - forwardSolutionNode.cost
                    if stopCondition(
                            frontierPriorityQueue.peekFirst(),
                            backwardsFrontierPriorityQueue.peekFirst(),
                            optimalPathCost) is True:
                        # stop the timer
                        runTime = time.time() - startTime

                        # mark - print path
                        pen.paint_path(forwardSolutionNode,
                                       backwardsSolutionNode)
                        if heuristicCounter == 0:
                            heuristicSumOverHeuristicCounter = 0
                        else:
                            heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                        evaluateStats('BiAstar', maze, True,
                                      forwardSolutionNode,
                                      frontierPriorityQueue, exploredCounter,
                                      runTime, isHeuristic, heuristicName,
                                      heuristicSumOverHeuristicCounter,
                                      backwardsSolutionNode,
                                      backwardsFrontierPriorityQueue,
                                      backwardsStartPoint)
                        return True

            # if node.key not in backwardsExploredHashTable:
            exploredCounter += 1

            backwardsExploredHashTable[node.key] = node
            expandNode(maze, node, backwardsFrontierPriorityQueue,
                       backwardsFrontierHashTable, backwardsExploredHashTable,
                       turn, heuristic, frontierHashTable, exploredHashTable)

            # mark - expanding node, node.x/node.y - hard yellow
            visual_counter += 1
            if visual_counter > visual_turns:
                pen.paint_tile(node.x, node.y, pen.dark_green, True)
                visual_turns *= 1.045
                if visual_turns > 110:
                    visual_turns = 110
                visual_counter = 0
            else:
                pen.paint_tile(node.x, node.y, pen.dark_green, False)

            turn = True

    # time's up!
    runTime = time.time() - startTime
    if heuristicCounter == 0:
        heuristicSumOverHeuristicCounter = 0
    else:
        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
    evaluateStats('BiAstar', maze, False, node, frontierPriorityQueue,
                  exploredCounter, runTime, isHeuristic, heuristicName,
                  heuristicSumOverHeuristicCounter, node,
                  backwardsFrontierPriorityQueue, backwardsStartPoint)
    return False
Exemple #5
0
def Astar(maze, maxRunTime, heuristicName):
    global h_time
    # starting the timer
    startTime = time.time()

    # checking if heuristic requires pre-processing
    if heuristicName == "minimumMoves":
        calculateMinimumMovesMatrix(maze, maze.goalNode)

    # initialization
    isHeuristic = True
    heuristic = chooseHeuristic(heuristicName)
    exploredCounter = 0

    global heuristicSum
    global heuristicCounter
    heuristicCounter = 0
    heuristicSum = 0

    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None
    frontierPriorityQueue = HeapDict()
    frontierHashTable = {}
    exploredHashTable = {}

    # calculating heuristic to first node
    startPoint.heuristicCost = heuristic(startPoint.x, startPoint.y,
                                         maze.goalNode)
    startPoint.pathCostWithHeuristic = startPoint.pathCost + startPoint.heuristicCost

    # inserting first node
    frontierHashTable[startPoint.key] = startPoint
    frontierPriorityQueue.push(startPoint)

    # Algorithm
    while time.time() < (startTime + maxRunTime):
        if frontierPriorityQueue.isEmpty():
            runTime = time.time() - startTime
            if heuristicCounter == 0:
                heuristicSumOverHeuristicCounter = 0
            else:
                heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
            evaluateStats('Astar', maze, False, node, frontierPriorityQueue,
                          exploredCounter, runTime, isHeuristic, heuristicName,
                          heuristicSumOverHeuristicCounter)
            return False

        # deleting node from frontierPriorityQueue
        node = frontierPriorityQueue.pop()
        frontierHashTable.pop(node.key)

        # appending childs so we simulate a tree
        if node != startPoint:
            node.fatherNode.childNodes.append(node)

        # checking to see if we hit the solution
        if maze.isGoal(node):
            # stop the timer
            runTime = time.time() - startTime
            if heuristicCounter == 0:
                heuristicSumOverHeuristicCounter = 0
            else:
                heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
            evaluateStats('Astar', maze, True, node, frontierPriorityQueue,
                          exploredCounter, runTime, isHeuristic, heuristicName,
                          heuristicSumOverHeuristicCounter)
            return True

        #if node.key not in exploredHashTable:
        exploredCounter += 1
        exploredHashTable[node.key] = node
        expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
                   exploredHashTable, heuristic)

    # time's up!
    runTime = time.time() - startTime
    if heuristicCounter == 0:
        heuristicSumOverHeuristicCounter = 0
    else:
        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
    evaluateStats('Astar', maze, False, node, frontierPriorityQueue,
                  exploredCounter, runTime, isHeuristic, heuristicName,
                  heuristicSumOverHeuristicCounter)
    return False
Exemple #6
0
def AstarVisual (maze,maxRunTime,heuristicName):

    #starting the timer
    startTime = time.time()

    # checking if heuristic requires pre-processing
    if heuristicName == "minimumMoves":
        calculateMinimumMovesMatrix(maze, maze.goalNode)

    global pen
    pen = Pen.getInstance()
    pen.maze_setup(maze)
    visual_counter = 1
    visual_turns = 2

    # initialization
    isHeuristic = True
    heuristic = chooseHeuristic(heuristicName)
    exploredCounter = 0

    global heuristicSum
    global heuristicCounter
    heuristicCounter = 0
    heuristicSum = 0

    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None
    frontierPriorityQueue = HeapDict()
    frontierHashTable = {}
    exploredHashTable = {}

    # calculating heuristic to first node
    startPoint.heuristicCost = heuristic(startPoint.x,startPoint.y,maze.goalNode)
    startPoint.pathCostWithHeuristic = startPoint.pathCost + startPoint.heuristicCost

    # inserting first node
    frontierHashTable[startPoint.key] = startPoint
    frontierPriorityQueue.push(startPoint)

    # Algorithm

    while time.time() < (startTime + maxRunTime):
        if frontierPriorityQueue.isEmpty():
            if frontierPriorityQueue.isEmpty():
                runTime = time.time() - startTime
                if heuristicCounter == 0:
                    heuristicSumOverHeuristicCounter = 0
                else:
                    heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                evaluateStats('Astar', maze, False, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic,
                              heuristicName, heuristicSumOverHeuristicCounter)
                return False

        # deleting node from frontierPriorityQueue
        node = frontierPriorityQueue.pop()
        frontierHashTable.pop(node.key)

        # appending childs so we simulate a tree
        if node != startPoint:
            node.fatherNode.childNodes.append(node)

        # checking to see if we hit the solution
        if maze.isGoal(node):
            # stop the timer
            runTime = time.time() - startTime
            if heuristicCounter == 0:
                heuristicSumOverHeuristicCounter = 0
            else:
                heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
            evaluateStats('Astar', maze, True, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic,heuristicName,heuristicSumOverHeuristicCounter )
            # mark - get_path
            pen.paint_path(node)
            return True

        #if node.key not in exploredHashTable:
        exploredCounter += 1
        exploredHashTable[node.key] = node
        expandNode(maze, node, frontierPriorityQueue, frontierHashTable, exploredHashTable,heuristic)

        # visualize painting green expanded nodes + painting rate increase when algorithm has higher run time
        visual_counter +=1
        if visual_counter > visual_turns:
            pen.paint_tile(node.x, node.y, pen.dark_green, True)
            visual_turns*=1.045
            if visual_turns > 110:
                visual_turns = 110
            visual_counter = 0
        else:
            pen.paint_tile(node.x, node.y, pen.dark_green, False)

    # time's up!
    runTime = time.time() - startTime
    if heuristicCounter == 0:
        heuristicSumOverHeuristicCounter = 0
    else:
        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
    evaluateStats('Astar', maze, False, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic,heuristicName,heuristicSumOverHeuristicCounter)
    return False
Exemple #7
0
def IDAstarVisual(maze, maxRunTime, heuristicName):

    # starting the timer
    startTime = time.time()

    # checking if heuristic requires pre-processing
    if heuristicName == "minimumMoves":
        calculateMinimumMovesMatrix(maze, maze.goalNode)

    global pen
    pen = Pen.getInstance()
    pen.maze_setup(maze)
    visual_counter = 1
    visual_turns = 2

    # initialization
    global remaining
    global currentFLimit
    global globalExploredCounter
    global heuristicSum
    global heuristicCounter
    currentFLimit = 0
    globalExploredCounter = 0
    heuristicSum = 0
    heuristicCounter = 0

    heuristic = chooseHeuristic(heuristicName)
    startPoint = maze.startNode
    cutOffs = []
    isHeuristic = True
    startPoint.heuristicCost = heuristic(startPoint.x, startPoint.y,
                                         maze.goalNode)
    startPoint.childNodes = []
    startPoint.fatherNode = None
    currentFLimit = startPoint.heuristicCost + 1
    numOfPrevExplored = None

    # algorithm
    while time.time() < (startTime + maxRunTime):

        calcDepthRecursive(startPoint)
        currentFLimit += 1
        exploredCounter = 0
        # mark - reset all map
        if currentFLimit > 2:
            pen.clearstamps(-(len(pen.stampItems) - pen.num_of_setup_stamps))
        frontierPriorityQueue = HeapDict()
        frontierHashTable = {}
        exploredHashTable = {}

        # calculating heuristic to first node
        startPoint.heuristicCost = heuristic(startPoint.x, startPoint.y,
                                             maze.goalNode)
        startPoint.pathCostWithHeuristic = startPoint.pathCost + startPoint.heuristicCost

        # inserting first node
        frontierHashTable[startPoint.key] = startPoint
        frontierPriorityQueue.push(startPoint)

        # Algorithm
        while True:
            if frontierPriorityQueue.isEmpty():
                # checking if no solution
                if numOfPrevExplored == len(
                        exploredHashTable) and remaining is False:
                    # time's up!
                    runTime = time.time() - startTime
                    if heuristicCounter == 0:
                        heuristicSumOverHeuristicCounter = 0
                    else:
                        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                    evaluateStats('IDAstar', maze, False, node, cutOffs,
                                  globalExploredCounter, runTime, isHeuristic,
                                  heuristicName,
                                  heuristicSumOverHeuristicCounter)
                    return False
                numOfPrevExplored = len(exploredHashTable)
                break

            # deleting node from frontierPriorityQueue
            node = frontierPriorityQueue.pop()
            frontierHashTable.pop(node.key)

            # appending childs so we simulate a tree
            if node != startPoint:
                node.fatherNode.childNodes.append(node)

            # this case indicates a cut-off, this algorithm generates a cutoff when the depth limit is reached
            if node.pathCostWithHeuristic >= currentFLimit:
                cutOffs.append(node)

            # adding 1 to expanded nodes count
            globalExploredCounter += 1

            # checking to see if we hit the solution
            if maze.isGoal(node):
                # stop the timer
                runTime = time.time() - startTime
                if heuristicCounter == 0:
                    heuristicSumOverHeuristicCounter = 0
                else:
                    heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
                evaluateStats('IDAstar', maze, True, node, cutOffs,
                              globalExploredCounter, runTime, isHeuristic,
                              heuristicName, heuristicSumOverHeuristicCounter)
                # mark - print path to goal
                pen.paint_path(node)
                return True

            if node.key not in exploredHashTable:
                exploredCounter += 1
            exploredHashTable[node.key] = node
            expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
                       exploredHashTable, currentFLimit, heuristic)

            # visualize painting green expanded nodes + painting rate increase when algorithm has higher run time
            visual_counter += 1
            if visual_counter > visual_turns:
                pen.paint_tile(node.x, node.y, pen.dark_green, True)
                visual_turns *= 1.045
                if visual_turns > 110:
                    visual_turns = 110
                visual_counter = 0
            else:
                pen.paint_tile(node.x, node.y, pen.dark_green, False)

    # time's up!
    runTime = time.time() - startTime
    if heuristicCounter == 0:
        heuristicSumOverHeuristicCounter = 0
    else:
        heuristicSumOverHeuristicCounter = heuristicSum / heuristicCounter
    evaluateStats('IDAstar', maze, False, node, cutOffs, globalExploredCounter,
                  runTime, isHeuristic, heuristicName,
                  heuristicSumOverHeuristicCounter)
    return False
Exemple #8
0
def UCSVisual (maze,maxRunTime):

    global pen
    pen = Pen.getInstance()
    pen.maze_setup(maze)
    visual_counter = 1
    visual_turns = 2

    # initialization
    isHeuristic = False
    frontierCounter = 0
    exploredCounter = 0

    frontierPriorityQueue = HeapDict()
    frontierHashTable = {}
    exploredHashTable = {}

    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None
    # inserting first node
    frontierPriorityQueue.push(startPoint)
    frontierHashTable[startPoint.key] = startPoint

    # Algorithm
    startTime = time.time()
    while  time.time() < (startTime + maxRunTime) :
        if frontierPriorityQueue.isEmpty():
            runTime = time.time() - startTime
            evaluateStats('UCS', maze, False, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic)
            return False

        # deleting node from frontierPriorityQueue
        node = frontierPriorityQueue.pop()
        frontierHashTable.pop(node.key)

        # appending childs so we simulate a tree
        if node != startPoint:
            node.fatherNode.childNodes.append(node)

        if maze.isGoal(node):

            # stop the timer
            runTime = time.time() - startTime
            evaluateStats('UCS', maze, True, node, frontierPriorityQueue, exploredCounter, runTime, isHeuristic)
            # mark - print path to goal
            pen.paint_path(node)
            return True

        exploredHashTable[node.key] = node
        exploredCounter += 1
        expandNode(maze,node,frontierPriorityQueue,frontierHashTable,exploredHashTable)

        # visualize painting green expanded nodes + painting rate increase when algorithm has higher run time
        visual_counter += 1
        if visual_counter > visual_turns:
            pen.paint_tile(node.x, node.y, pen.dark_green, True)
            visual_turns *= 1.045
            if visual_turns > 110:
                visual_turns = 110
            visual_counter = 0
        else:
            pen.paint_tile(node.x, node.y, pen.dark_green, False)

    # time's up!
    runTime = time.time() - startTime
    evaluateStats('UCS', maze, False, node, frontierPriorityQueue, exploredCounter, runTime,isHeuristic)
    return False
Exemple #9
0
def IDSVisual(maze, maxRunTime):
    global pen
    pen = Pen.getInstance()
    pen.maze_setup(maze)
    visual_counter = 1
    visual_turns = 2

    # initialization
    global currentDepthLimit
    global globalExploredCounter
    currentDepthLimit = 0
    globalExploredCounter = 0
    cutOffs = []
    isHeuristic = False
    startPoint = maze.startNode
    startPoint.childNodes = []
    startPoint.fatherNode = None
    currentDepthLimit = -1
    numOfPrevExplored = None

    # algorithm
    startTime = time.time()
    while time.time() < (startTime + maxRunTime):
        currentDepthLimit += 1
        exploredCounter = 0
        # mark - reset all map
        pen.clearstamps(-(len(pen.stampItems) - pen.num_of_setup_stamps))
        pen.mazeScreen.update()
        frontierPriorityQueue = HeapDict()
        frontierHashTable = {}
        exploredHashTable = {}

        # calculating heuristic to first node
        startPoint.heuristicCost = -startPoint.depth
        startPoint.pathCostWithHeuristic = startPoint.heuristicCost

        # inserting first node
        frontierHashTable[startPoint.key] = startPoint
        frontierPriorityQueue.push(startPoint)

        # Algorithm
        while True:

            if frontierPriorityQueue.isEmpty():
                # checking if no solution
                if numOfPrevExplored == len(exploredHashTable):
                    runTime = time.time() - startTime
                    node.depth = currentDepthLimit
                    evaluateStats('IDS',
                                  maze,
                                  False,
                                  node,
                                  cutOffs,
                                  globalExploredCounter,
                                  runTime,
                                  isHeuristic,
                                  maxIDSdepth=currentDepthLimit - 1)
                    return False
                numOfPrevExplored = len(exploredHashTable)
                break

            # deleting node from frontierPriorityQueue
            node = frontierPriorityQueue.pop()
            frontierHashTable.pop(node.key)

            # this case indicates a cut-off, this algorithm generates a cutoff when the depth limit is reached
            if node.depth == currentDepthLimit:
                cutOffs.append(node)

            # adding 1 to expanded nodes count
            globalExploredCounter += 1

            # checking to see if we hit the solution
            if maze.isGoal(node):
                # stop the timer
                runTime = time.time() - startTime
                node.depth = currentDepthLimit
                evaluateStats('IDS',
                              maze,
                              True,
                              node,
                              cutOffs,
                              globalExploredCounter,
                              runTime,
                              isHeuristic,
                              maxIDSdepth=currentDepthLimit)

                # mark - print path to goal
                pen.paint_path(node)
                return True

            if node.key not in exploredHashTable:
                exploredCounter += 1
            exploredHashTable[node.key] = node
            expandNode(maze, node, frontierPriorityQueue, frontierHashTable,
                       exploredHashTable, currentDepthLimit)

            # visualize painting green expanded nodes + painting rate increase when algorithm has higher run time
            visual_counter += 1
            if visual_counter > visual_turns:
                pen.paint_tile(node.x, node.y, pen.dark_green, True)
                visual_turns *= 1.045
                if visual_turns > 110:
                    visual_turns = 110
                visual_counter = 0
            else:
                pen.paint_tile(node.x, node.y, pen.dark_green, False)

    # time's up!
    runTime = time.time() - startTime
    node.depth = currentDepthLimit
    evaluateStats('IDS',
                  maze,
                  False,
                  node,
                  cutOffs,
                  globalExploredCounter,
                  runTime,
                  isHeuristic,
                  maxIDSdepth=currentDepthLimit)
    return False