Пример #1
0
def drawPath(maze, path):
    m = copy.deepcopy(maze)
    startX, startY = findStart(maze)
    x, y = startX, startY
    for i in range(len(path)):
        if path[i] == 'U': y -= 1
        elif path[i] == 'D': y += 1
        elif path[i] == 'L': x -= 1
        elif path[i] == 'R': x += 1
        if x != startX or y != startY:
            m[y][x] = '.'
    showMaze(m)
Пример #2
0
def firstSection(MAZE, DIMENSIONS, PROBABILITY_OF_BLOCK):
    # List of functions to store in array and execute in for loop on next line
    # Store as function to call with param as our maze and the dimensions of the maze
    # DFS Maze Function, BFS Maze Function
    funcList = [initDFS(MAZE, DIMENSIONS), initBFS(MAZE, DIMENSIONS)]

    for func in funcList:
        # Returns Completed Maze and if path is found
        completedMaze, foundPath = updateMaze(MAZE, func, DIMENSIONS)
        if foundPath:
            showMaze(completedMaze, DIMENSIONS)
        else:
            break
Пример #3
0
def initBFSS3(fireMaze, PROBABILITY_OF_FIRE_SPREAD, DIMENSIONS):
    GOAL = DIMENSIONS - 1
    agentDead = False
    startNode = Node(0, 0)
    visited_fire_coordinates = {}  # Remembers where the fire has spread to

    while not agentDead:
        pathCoordinates = []  # Stores the path that BFS has found
        bfsResult = BFS(
            fireMaze, startNode, DIMENSIONS
        )  # Call BFS        -> Send in a modified version of the fireMaze where we account for 2 - 3 steps ahead
        # However we still need to account for the original fire maze

        if (bfsResult is not None):  # If A Path Was Found
            print("BFS Path Found. Now checking Agent Status vs Fire...")

            while (bfsResult is not None):  # Store Coordinates For Path Found
                pathCoordinates.append([bfsResult.x, bfsResult.y])
                bfsResult = bfsResult.prev

            # Update agent's current position
            pathCoordinates = [
                ele for ele in reversed(pathCoordinates)
            ]  # Reverse the coordinate path Goal -> Start is not Start -> Goal Path
            #  print(pathCoordinates)
            agent_x_pos, agent_y_pos = pathCoordinates[1][0], pathCoordinates[
                1][1]
            fireMaze[agent_x_pos][agent_y_pos] = 4

            # Check if agent made it to goal
            if agent_x_pos == GOAL and agent_y_pos == GOAL:
                print('Succesffully made it to goal')
                break

            # Update agents' next position
            elif len(pathCoordinates) > 1:
                startNode = Node(agent_x_pos, agent_y_pos)
                print('Set new start node as x: ' + str(agent_x_pos) + 'y: ' +
                      str(agent_y_pos))

            updatedFireMaze, newFireCoordinates = spreadFire(
                fireMaze, DIMENSIONS, PROBABILITY_OF_FIRE_SPREAD
            )  # Get fire maze matrix and coordinates that it spread to per iteration

            ##### CREATE A NEW FIRE MAZE WHICH IS 2 - 3 STEPS AHEAD OF THE CURRENT MAZE ############
            predictFireMaze = copy.deepcopy(updatedFireMaze)
            for i in range(
                    3
            ):  # This loop could give us the maze which is x steps ahead
                PROBABILITY_OF_FIRE_SPREAD = 1  # Want to maximize possibility that the fire is spreading
                predictFireMaze, predictedFireCoordinates = spreadFire(
                    predictFireMaze, DIMENSIONS, PROBABILITY_OF_FIRE_SPREAD)
            #### NEED TO ACCOUNT FOR EDGE CASE THAT PREDICTION LEADS TO NO PATH EVEN THO THAT SPOT IS NOT ON FIRE YET BECAUSE IT IS PREDICTION
            #### HOWEVER ACCORIDNG TO THE CURRENT ALGORITHM, THAT SPACE WILL 'BE ON FIRE' SO IT WON'T TAKE THAT ROUTE
            ####

            # Update Fire Location and Check if agent is still alive
            # print(newFireCoordinates)
            if len(newFireCoordinates
                   ) != 0:  # Update maze with where the fire has spreaded too
                for fire in newFireCoordinates:
                    fx = fire[0]
                    fy = fire[1]

                    # Check if visited or not
                    if fx not in visited_fire_coordinates:  # Create and add coordinate key
                        visited_fire_coordinates[fx] = [fy]
                    else:
                        visited_fire_coordinates[fx].append(
                            fy)  # Add visited value

                # Check if current agent is on a spot on fire
                    if agent_x_pos in visited_fire_coordinates and agent_y_pos in visited_fire_coordinates[
                            fx]:
                        agentDead = True
                        print('Agent Burned at x: ' + str(fx) + ' y: ' +
                              str(fy))

                    else:
                        fireMaze[fx][fy] = 5  # Otherwise spot is now on fire
                        print('Coordinate Added')

            else:  # If fire didn't spread
                print('[]')

        else:  # New fire path results in no possible path to goal
            print(
                'Could not find path where agent survives due to new fire path'
            )
            agentDead = True

        showMaze(fireMaze, DIMENSIONS)
Пример #4
0
def initBFSS2(fireMaze, PROBABILITY_OF_FIRE_SPREAD, DIMENSIONS):
    GOAL = DIMENSIONS - 1
    agentDead = False
    startNode = Node(0, 0)
    visited_fire_coordinates = {}  # Remembers where the fire has spread to

    while not agentDead:
        pathCoordinates = []  # Stores the path that BFS has found
        bfsResult = BFS(fireMaze, startNode, DIMENSIONS)  # Call BFS

        if (bfsResult is not None):  # If A Path Was Found
            print("BFS Path Found. Now checking Agent Status vs Fire...")

            while (bfsResult is not None):  # Store Coordinates For Path Found
                pathCoordinates.append([bfsResult.x, bfsResult.y])
                bfsResult = bfsResult.prev

            # Update agent's current position
            pathCoordinates = [
                ele for ele in reversed(pathCoordinates)
            ]  # Reverse the coordinate path Goal -> Start is not Start -> Goal Path
            #  print(pathCoordinates)
            agent_x_pos, agent_y_pos = pathCoordinates[1][0], pathCoordinates[
                1][1]
            fireMaze[agent_x_pos][agent_y_pos] = 4

            # Check if agent made it to goal
            if agent_x_pos == GOAL and agent_y_pos == GOAL:
                print('Succesffully made it to goal')
                break

            # Update agents' next position
            elif len(pathCoordinates) > 1:
                startNode = Node(agent_x_pos, agent_y_pos)
                print('Set new start node as x: ' + str(agent_x_pos) + 'y: ' +
                      str(agent_y_pos))

            updatedFireMaze, newFireCoordinates = spreadFire(
                fireMaze, DIMENSIONS, PROBABILITY_OF_FIRE_SPREAD
            )  # Get fire maze matrix and coordinates that it spread to per iteration

            # Update Fire Location and Check if agent is still alive
            # print(newFireCoordinates)
            if len(newFireCoordinates
                   ) != 0:  # Update maze with where the fire has spreaded too
                for fire in newFireCoordinates:
                    fx = fire[0]
                    fy = fire[1]

                    # Check if visited or not
                    if fx not in visited_fire_coordinates:  # Create and add coordinate key
                        visited_fire_coordinates[fx] = [fy]
                    else:
                        visited_fire_coordinates[fx].append(
                            fy)  # Add visited value

                # Check if current agent is on a spot on fire
                    if agent_x_pos in visited_fire_coordinates and agent_y_pos in visited_fire_coordinates[
                            fx]:
                        agentDead = True
                        print('Agent Burned at x: ' + str(fx) + ' y: ' +
                              str(fy))

                    else:
                        fireMaze[fx][fy] = 5  # Otherwise spot is now on fire
                        print('Coordinate Added')

            else:  # If fire didn't spread
                print('[]')

        else:  # New fire path results in no possible path to goal
            print(
                'Could not find path where agent survives due to new fire path'
            )
            agentDead = True

        showMaze(fireMaze, DIMENSIONS)
Пример #5
0
def initBFSS1(fireMaze, PROBABILITY_OF_FIRE_SPREAD, DIMENSIONS):
    agentDead = False
    pathCoordinates = []
    fireCoordinates = []
    fireMazeCopy = copy.deepcopy(fireMaze)  # Want to save original maze
    bfsResult = BFS(fireMaze, Node(0, 0), DIMENSIONS)

    if (bfsResult is not None):  # If path was succesfully found
        print("BFS Path Found. Now checking Agent Status vs Fire...")

        while (bfsResult is not None):  # Getting coordinates for path
            pathCoordinates.append([bfsResult.x, bfsResult.y])
            bfsResult = bfsResult.prev

        # Reverse the coordinate path Goal -> Start is not Start -> Goal Path
        pathCoordinates = [ele for ele in reversed(pathCoordinates)]
        del pathCoordinates[
            0]  # Want the first move to be the agent moving        -> ================== IF THERE IS A BUG BECAUSE OF A LENGTH ERROR IT IS PROBABLY THIS BUT IS WORKING FINE IN MY TESTS   =================

        for i in range(
                len(pathCoordinates)
        ):  # Create Fire Spread and get the spots it spreads to for each iteration
            updatedFireMaze, newFireCoordinates = spreadFire(
                fireMazeCopy, DIMENSIONS, PROBABILITY_OF_FIRE_SPREAD
            )  # Get fire maze matrix and coordinates that it spread to per iteration
            fireCoordinates.append(
                newFireCoordinates
            )  # Save the coordinate it spread to for that iteration
            fireMazeCopy = updatedFireMaze.copy(
            )  # Update the returned maze with fire

        visited_fire_coordinates = {}  # Remembers where the fire has spread to

        for j in range(
                0, len(fireCoordinates)
        ):  # Iterate through fire spread coordinates to see if there is intersection with agent
            if agentDead:
                print('Agent Died')
                showMaze(fireMaze, DIMENSIONS)
                break

            agent_x_pos = pathCoordinates[j][0]
            agent_y_pos = pathCoordinates[j][1]
            fireMaze[agent_x_pos][
                agent_y_pos] = 4  # Update Agent's current location

            curr_fire = fireCoordinates[
                j]  # Get the current location where the fire is spreading to

            if (len(curr_fire) != 0):  # If the fire spread this iteration
                print("Fire spread to " +
                      str(curr_fire))  # Coordinate's it spread to

                for c in curr_fire:  # Each coordinate it spread to
                    fx = c[0]  # X Fire Coord
                    fy = c[1]  # Y Fire Coord

                    if fx not in visited_fire_coordinates:  # Create and add coordinate key
                        visited_fire_coordinates[fx] = [fy]
                    else:
                        visited_fire_coordinates[fx].append(
                            fy)  # Add visited value

                    if agent_x_pos in visited_fire_coordinates and agent_y_pos in visited_fire_coordinates[
                            fx]:  # Check if current agent is on a spot on fire
                        agentDead = True
                        print('Agent Burned at x: ' + str(fx) + ' y: ' +
                              str(fy))
                        break
                    else:
                        fireMaze[fx][fy] = 5  # Otherwise spot is now on fire

            else:
                print('[]')  # Fire did not spready this iteration

            showMaze(fireMaze, DIMENSIONS)

        if not agentDead:
            print('Agent Has Succesffuly Made It Out Of Maze')
    else:
        print("BFS found no solution")
    return None