示例#1
0
def compare_algorithms(test_maze):

    algo_comparison_df = pd.DataFrame(columns=column_name_list)

    # BFS
    maze = copy.deepcopy(test_maze)
    result_dict = bfs_traversal(maze, len(maze))
    path = maze_runner.get_path(dim - 1, dim - 1, maze)
    df_entry = pd.DataFrame(
        [(len(path), result_dict["total_steps"],
          result_dict["max_fringe_length"], result_dict["avg_fringe_length"])],
        columns=column_name_list,
        index=['BFS'])
    algo_comparison_df = algo_comparison_df.append(df_entry)

    # DFS
    maze = copy.deepcopy(test_maze)
    result_dict = dfs_traversal(maze, len(maze))
    path = maze_runner.get_path(dim - 1, dim - 1, maze)
    df_entry = pd.DataFrame(
        [(len(path), result_dict["total_steps"],
          result_dict["max_fringe_length"], result_dict["avg_fringe_length"])],
        columns=column_name_list,
        index=['DFS'])
    algo_comparison_df = algo_comparison_df.append(df_entry)

    # BD-BFS
    maze = copy.deepcopy(test_maze)
    result_dict = bd_bfs(maze, len(maze))
    df_entry = pd.DataFrame(
        [(result_dict["path_length"], result_dict["total_steps"],
          result_dict["max_fringe_length"], result_dict["avg_fringe_length"])],
        columns=column_name_list,
        index=['BD-BFS'])
    algo_comparison_df = algo_comparison_df.append(df_entry)

    # a-star with manhattan heuristic
    maze = copy.deepcopy(test_maze)
    result_dict = a_star_traversal(maze, "manhattan")
    path = maze_runner.get_path(dim - 1, dim - 1, maze)
    df_entry = pd.DataFrame(
        [(len(path), result_dict["total_steps"],
          result_dict["max_fringe_length"], result_dict["avg_fringe_length"])],
        columns=column_name_list,
        index=['astar_manhattan'])
    algo_comparison_df = algo_comparison_df.append(df_entry)

    # a-star with euclidian heuristic
    maze = copy.deepcopy(test_maze)
    result_dict = a_star_traversal(maze, "euclidian")
    path = maze_runner.get_path(dim - 1, dim - 1, maze)
    df_entry = pd.DataFrame(
        [(len(path), result_dict["total_steps"],
          result_dict["max_fringe_length"], result_dict["avg_fringe_length"])],
        columns=column_name_list,
        index=['astar_euclidian'])
    algo_comparison_df = algo_comparison_df.append(df_entry)

    return algo_comparison_df
示例#2
0
def test_a_star(dim, p):
    test_maze = maze_runner.get_maze(dim, p)

    # a-star with manhattan heuristic
    maze = copy.deepcopy(test_maze)
    manhattan_result_dict = a_star_traversal(maze, "manhattan")
    if manhattan_result_dict["is_solvable"]:
        path = maze_runner.get_path(dim - 1, dim - 1, maze)
        print "Path", path
        print "Length of path: ", len(path)
        maze_runner.trace_path(maze, path)
    else:
        maze_runner.visualize_maze(maze)

    # a-star with euclidian heuristic
    maze = copy.deepcopy(test_maze)
    euclidian_result_dict = a_star_traversal(maze, "euclidian")
    if euclidian_result_dict["is_solvable"]:
        path = maze_runner.get_path(dim - 1, dim - 1, maze)
        print "Path", path
        print "Length of path: ", len(path)
        maze_runner.trace_path(maze, path)
    else:
        maze_runner.visualize_maze(maze)
示例#3
0
def source_dist(x, y, maze):
    path = maze_runner.get_path(x, y, maze)
    return len(path)
		if fringe_len>max_fringe_length:
			max_fringe_length = fringe_len
		avg_fringe_length = avg_fringe_length + (fringe_len - avg_fringe_length)/exploration_steps
		closed_set.add((x,y))
		# Spreads fire to the neighboring cell as mentioned in the question. Since we might have
		# more cells on fire, the boundary_cells also get updated
		maze, boundary_cells = spread_fire(maze, dim, q, boundary_cells)
		fringe = update_fringe_using_boundary_cells(fringe, maze, boundary_cells)
	# No Solution
	return 0, exploration_steps, max_fringe_length, avg_fringe_length, closed_set, -1, -1

# Main code
dim = 30
p = 0.22
q = 0.2
maze = mr.get_maze(dim, p)
mr.visualize_maze(maze)
manhattan_result, manhattan_steps, manhattan_max_fringe_length, manhattan_avg_fringe_length, manhattan_closed_set, x_cord, y_cord = fire_traversal(maze, q)
mr.visualize_maze(maze)
if manhattan_result == 1:
	print "Solution found"
	path = mr.get_path(dim-1, dim-1, maze)
	mr.trace_path(maze, path)
elif manhattan_result == 0:
	print "Solution not found"
	mr.visualize_maze(maze)
else:
	print "Burnt at " + str(x_cord) + ", " + str(y_cord)
	maze[x_cord][y_cord].value = 1.5
	path = mr.get_path(x_cord, y_cord, maze)
	mr.trace_path(maze, path)
def bd_bfs(maze, dim):
    fringe1 = deque()
    fringe2 = deque()
    closed1 = []
    closed2 = []
    fringe1.append((0, 0))
    fringe2.append((dim - 1, dim - 1))
    maze[0][0].parent = None
    maze[dim - 1][dim - 1].parent = None
    max_fringe_length = 0
    avg_fringe_length = 0
    exploration_steps = 0
    result = 0
    while len(fringe1) != 0 and len(fringe2) != 0:
        current1 = fringe1.popleft()
        exploration_steps = exploration_steps + 1
        m1 = current1[0]
        n1 = current1[1]
        if current1 not in closed1:
            if maze[m1][n1].value != 1:
                intersect1 = CheckNeighbours_BBFS.checkneighbours_bbfs(
                    maze, m1, n1, dim, fringe1, closed1)
                if intersect1 is not None:
                    # print "Intersecting node 1:", intersect1
                    result = 1
                    break
                closed1.append((m1, n1))

        current2 = fringe2.popleft()
        exploration_steps = exploration_steps + 1
        m2 = current2[0]
        n2 = current2[1]
        if current2 not in closed2:
            if maze[m2][n2].value != 1:
                intersect2 = CheckNeighbours_BBFS.checkneighbours_bbfs(
                    maze, m2, n2, dim, fringe2, closed2)
                if intersect2 is not None:
                    # print "Intersecting node 2:", intersect2
                    result = 1
                    break
                closed2.append((m2, n2))
        closed_set = closed1 + closed2
        fringe_length = len(fringe1) + len(fringe2)
        avg_fringe_length = avg_fringe_length + (
            fringe_length - avg_fringe_length) / exploration_steps
        if fringe_length > max_fringe_length:
            max_fringe_length = fringe_length

    if result == 0:
        result_dict = {
            "is_solvable": False,
            "total_steps": exploration_steps,
            "max_fringe_length": max_fringe_length,
            "avg_fringe_length": avg_fringe_length,
            "closed_set": closed_set
        }
        return result_dict
    else:
        path1 = []
        path2 = []
        if intersect1 is not None:
            path1 = maze_runner.get_path(current1[0], current1[1], maze)
            while intersect1[0] <= dim - 1 and intersect1[1] <= dim - 1:
                path2.append(intersect1)
                if intersect1 == (dim - 1, dim - 1):
                    break
                intersect1 = maze[intersect1[0]][intersect1[1]].parent
            # maze_runner.trace_path_bbfs(maze, path1, path2)

        elif intersect2 is not None:
            path1 = maze_runner.get_path(intersect2[0], intersect2[1], maze)
            while current2[0] <= dim - 1 and current2[1] <= dim - 1:
                path2.append(current2)
                if current2 == (dim - 1, dim - 1):
                    break
                current2 = maze[current2[0]][current2[1]].parent
            # maze_runner.trace_path_bbfs(maze, path2, path1)
        # print path
        # print len(path)

        result_dict = {
            "is_solvable": True,
            "total_steps": exploration_steps,
            "max_fringe_length": max_fringe_length,
            "avg_fringe_length": avg_fringe_length,
            "closed_set": closed_set,
            "path_length": len(path1) + len(path2)
        }
        return result_dict
示例#6
0
from cell import Cell
import maze_runner as mr
from dfs import dfs_traversal
import copy
maze = []

for row in xrange(15):
    maze.append([])
    for col in xrange(15):
        maze[row].append(Cell(0))

maze[0][1].value = 1
for col in range(2, 15, 1):
    maze[5][col].value = 1
maze[6][2].value = 1
for col in range(2, 15, 1):
    maze[11][col].value = 1
maze[12][2].value = 1

mr.visualize_maze(maze)
result = dfs_traversal(maze, 15)
path = mr.get_path(14, 14, copy.deepcopy(maze))
print path
mr.trace_path(copy.deepcopy(maze), path)
fringe = result["fringe"]
print result["max_fringe_length"]
mr.visualize_explored_cells(copy.deepcopy(maze), fringe)