Skip to content

Commit

Permalink
Updated BFS, DFS, A*, weighter A* and Greedy
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanjeeev-K committed Mar 8, 2020
1 parent 04ef8a0 commit 2e57cf6
Show file tree
Hide file tree
Showing 12 changed files with 590 additions and 145 deletions.
Binary file modified Astar.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 50 additions & 27 deletions Astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ def main():
# [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]] )

# maze = np.array( [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
# [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]] )

maze = np.array( [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]] )
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0]] )


# maze = np.array( [[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
Expand All @@ -40,9 +51,10 @@ def main():
# [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]] )


# res = maze[::-1]
# maze = res
# maze = np.transpose(maze)
start = (0,0)
# end = (0,len(maze[0])-1)
end = (len(maze)-1,len(maze[0])-1)
# end = (9,8)

a = len(maze) + 2
b = len(maze[0]) + 2
Expand All @@ -62,11 +74,6 @@ def main():
else:
# plt.plot(i, j,'bs', fillstyle='full', markersize=25)
plt.plot(j-1, a-i,'bs', fillstyle='full', markersize=25)

start = (0,0)
end = (len(maze)-1,len(maze[0])-1)
# end = (9,8)
# end = (0,len(maze[0])-1)

# plt.plot(start[0]+1, start[1]+1,'rs', fillstyle='full', markersize=27)
# plt.plot(end[0]+1, end[1]+1,'gs', fillstyle='full', markersize=27)
Expand Down Expand Up @@ -139,14 +146,20 @@ def main():
# input()

while(len(open_list)!=0):
x = current[1]
y = current[2]
plt.plot(y-1, a-x,'yo', fillstyle='full', markersize=22)
#Neighbor Up
x = current[1]
y = current[2]+1
if(explored_matrix[x][y]== -1): # Checking if this neighboring node is not obstacle or already explored.
Heuristic = abs(end2[0]-x)+ abs(end2[1]-y) #Manhattan distance
Cost = current[3]+1
g = Cost + Heuristic
# g = Heuristic
open_list.append([g,x,y,Cost])
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
explored_matrix[x][y]=-3
#Neighbor Right
x = current[1]+1
Expand All @@ -155,7 +168,10 @@ def main():
Heuristic = abs(end2[0]-x)+ abs(end2[1]-y) #Manhattan distance
Cost = current[3]+1
g = Cost + Heuristic
# g = Heuristic
open_list.append([g,x,y,Cost])
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
explored_matrix[x][y]=-3
#Neighbor Down
x = current[1]
Expand All @@ -164,7 +180,10 @@ def main():
Heuristic = abs(end2[0]-x)+ abs(end2[1]-y) #Manhattan distance
Cost = current[3]+1
g = Cost + Heuristic
# g = Heuristic
open_list.append([g,x,y,Cost])
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
explored_matrix[x][y]=-3
#Neighbor Left
x = current[1]-1
Expand All @@ -173,8 +192,12 @@ def main():
Heuristic = abs(end2[0]-x)+ abs(end2[1]-y) #Manhattan distance
Cost = current[3]+1
g = Cost + Heuristic
# g = Heuristic
open_list.append([g,x,y,Cost])
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
explored_matrix[x][y]=-3
# input()
open_list.remove(current)
closed_list.append(current)
x = current[1]
Expand All @@ -187,9 +210,9 @@ def main():
break
else:
open_list.sort()
plt.pause(0.1)
# plt.pause(0.1)
# plt.plot(x, y,'yo', fillstyle='full', markersize=22)
plt.plot(y-1, a-x,'yo', fillstyle='full', markersize=22)
# plt.plot(y-1, a-x,'yo', fillstyle='full', markersize=22)
# plt.pause(0.01)
# print(x," ",y)
current = open_list[0]
Expand Down Expand Up @@ -232,39 +255,39 @@ def main():
# plot and break;
neighbors = []
#Neighbor Up
print("Neighbour Up")
# print("Neighbour Up")
x_neighbor = x
y_neighbor = y+1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Right
print("Neighbour Right")
# print("Neighbour Right")
x_neighbor = x+1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Down
print("Neighbour Down")
# print("Neighbour Down")
x_neighbor = x
y_neighbor = y-1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Left
print("Neighbour Left")
# print("Neighbour Left")
x_neighbor = x-1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
neighbors.sort()
print(neighbors)
# print(neighbors)
if(len(neighbors)==0):
print("Path not found")
break
Expand All @@ -276,8 +299,8 @@ def main():
y_prev = prev[2]
# plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.plot([y_prev-1, y -1],[a - x_prev,a-x],'go-',linewidth=2)
plt.pause(1)
print("here")
plt.pause(.2)
# print("here")
if(node[1]==start2[0] and node[2]==start2[1]):
reach_goal = 1
x = node[1]
Expand Down
Binary file modified BFS.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 48 additions & 39 deletions BFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@

def main():

maze = np.array( [[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1, 0, 0, 0, 1],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]] )

maze = np.array( [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
maze = np.array( [[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] )

# maze = np.array( [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] )

start = (0,0)
end = (0,len(maze[0])-1)
# end = (len(maze)-1,len(maze[0])-1)
# end = (9,8)

# res = maze[::-1]
# maze = res
# maze = np.transpose(maze)


a = len(maze) + 2
b = len(maze[0]) + 2
Expand All @@ -51,10 +53,7 @@ def main():
# plt.plot(i, j,'bs', fillstyle='full', markersize=25)
plt.plot(j-1, a-i,'bs', fillstyle='full', markersize=25)

start = (5,5)
# end = (len(maze)-1,len(maze[0])-1)
# end = (9,8)
end = (0,len(maze[0])-1)


# plt.plot(start[0]+1, start[1]+1,'rs', fillstyle='full', markersize=27)
# plt.plot(end[0]+1, end[1]+1,'gs', fillstyle='full', markersize=27)
Expand Down Expand Up @@ -127,6 +126,9 @@ def main():
# input()

while(len(open_list)!=0):
x = current[1]
y = current[2]
plt.plot(y-1, a-x,'yo', fillstyle='full', markersize=22)
#Neighbor Up
x = current[1]
y = current[2]+1
Expand All @@ -136,6 +138,8 @@ def main():
g = Cost + Heuristic
open_list.append([g,x,y,Cost])
explored_matrix[x][y]=-3
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#Neighbor Right
x = current[1]+1
y = current[2]
Expand All @@ -145,6 +149,8 @@ def main():
g = Cost + Heuristic
open_list.append([g,x,y,Cost])
explored_matrix[x][y]=-3
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#Neighbor Down
x = current[1]
y = current[2]-1
Expand All @@ -154,6 +160,8 @@ def main():
g = Cost + Heuristic
open_list.append([g,x,y,Cost])
explored_matrix[x][y]=-3
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#Neighbor Left
x = current[1]-1
y = current[2]
Expand All @@ -163,6 +171,8 @@ def main():
g = Cost + Heuristic
open_list.append([g,x,y,Cost])
explored_matrix[x][y]=-3
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
open_list.remove(current)
closed_list.append(current)
x = current[1]
Expand All @@ -177,7 +187,6 @@ def main():
# open_list.sort()
plt.pause(0.1)
# plt.plot(x, y,'yo', fillstyle='full', markersize=22)
plt.plot(y-1, a-x,'yo', fillstyle='full', markersize=22)
# plt.pause(0.1)
# print(x," ",y)
current = open_list[0]
Expand Down Expand Up @@ -215,39 +224,39 @@ def main():
# plot and break;
neighbors = []
#Neighbor Up
print("Neighbour Up")
# print("Neighbour Up")
x_neighbor = x
y_neighbor = y+1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Right
print("Neighbour Right")
# print("Neighbour Right")
x_neighbor = x+1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Down
print("Neighbour Down")
# print("Neighbour Down")
x_neighbor = x
y_neighbor = y-1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Left
print("Neighbour Left")
# print("Neighbour Left")
x_neighbor = x-1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
# print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
neighbors.sort()
print(neighbors)
# print(neighbors)
if(len(neighbors)==0):
print("Path not found")
break
Expand All @@ -259,8 +268,8 @@ def main():
y_prev = prev[2]
# plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.plot([y_prev-1, y -1],[a - x_prev,a-x],'go-',linewidth=2)
plt.pause(1)
print("here")
plt.pause(.1)
# print("here")
if(node[1]==start2[0] and node[2]==start2[1]):
reach_goal = 1
x = node[1]
Expand Down
Binary file added DFS.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2e57cf6

Please sign in to comment.