Skip to content

Commit

Permalink
Discovered optimal path highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanjeeev-K committed Mar 7, 2020
1 parent 7164465 commit 04ef8a0
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 5 deletions.
95 changes: 93 additions & 2 deletions Astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ def main():
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)
# plt.pause(0.01)
# print(x," ",y)
current = open_list[0]
if current[1]==end2[0] and current[2]==end2[1]:
plt.plot(current[2]-1, a-current[1],'yo', fillstyle='full', markersize=22)
flag = "Goal found"
print(explored_matrix)
plt.pause(5)
plt.pause(2)
break
# print(open_list)
# print(closed_list)
Expand All @@ -210,6 +210,97 @@ def main():
# end of while loop
print(flag)
print(explored_matrix)

# plt.plot(end[1]+1-1, a-end[0]-1,'rs', fillstyle='full', markersize=27)
# plt.pause(2)

# input()
if flag=="Goal found":
path = []
reach_goal = 0
path.append(current)
node = path[-1]
x = node[1]
y = current[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#While checking neighbors, also check goal.
while(reach_goal== 0):
#find nearest neighbor with least calues explored_matrix value that is greater than 0.
# add that neighbot to path.
#if that neighbot == start:
# plot and break;
neighbors = []
#Neighbor Up
print("Neighbour Up")
x_neighbor = x
y_neighbor = y+1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Right
print("Neighbour Right")
x_neighbor = x+1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Down
print("Neighbour Down")
x_neighbor = x
y_neighbor = y-1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Left
print("Neighbour Left")
x_neighbor = x-1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
neighbors.sort()
print(neighbors)
if(len(neighbors)==0):
print("Path not found")
break
prev = node
node = neighbors[0]
x = node[1]
y = node[2]
x_prev = prev[1]
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")
if(node[1]==start2[0] and node[2]==start2[1]):
reach_goal = 1
x = node[1]
y = node[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(5)
break





# x_next = x
# y_next = y+1 #First check Up.
# next_score = explored_matrix[x_next][y_next]
# if(next_score<0):
# x_next = x+1
# y_next = y






# # naming the axes
# plt.xlabel('x - axis')
Expand Down
87 changes: 84 additions & 3 deletions BFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def main():
[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],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] )


# res = maze[::-1]
# maze = res
Expand All @@ -40,7 +51,7 @@ def main():
# plt.plot(i, j,'bs', fillstyle='full', markersize=25)
plt.plot(j-1, a-i,'bs', fillstyle='full', markersize=25)

start = (0,0)
start = (5,5)
# end = (len(maze)-1,len(maze[0])-1)
# end = (9,8)
end = (0,len(maze[0])-1)
Expand Down Expand Up @@ -167,14 +178,14 @@ def main():
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)
# plt.pause(0.1)
# print(x," ",y)
current = open_list[0]
if current[1]==end2[0] and current[2]==end2[1]:
plt.plot(current[2]-1, a-current[1],'yo', fillstyle='full', markersize=22)
flag = "Goal found"
print(explored_matrix)
plt.pause(5)
plt.pause(2)
break
# print(open_list)
# print(closed_list)
Expand All @@ -187,6 +198,76 @@ def main():
# end of while loop
print(flag)
print(explored_matrix)
if flag=="Goal found":
path = []
reach_goal = 0
path.append(current)
node = path[-1]
x = node[1]
y = current[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#While checking neighbors, also check goal.
while(reach_goal== 0):
#find nearest neighbor with least calues explored_matrix value that is greater than 0.
# add that neighbot to path.
#if that neighbot == start:
# plot and break;
neighbors = []
#Neighbor Up
print("Neighbour Up")
x_neighbor = x
y_neighbor = y+1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Right
print("Neighbour Right")
x_neighbor = x+1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Down
print("Neighbour Down")
x_neighbor = x
y_neighbor = y-1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Left
print("Neighbour Left")
x_neighbor = x-1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
neighbors.sort()
print(neighbors)
if(len(neighbors)==0):
print("Path not found")
break
prev = node
node = neighbors[0]
x = node[1]
y = node[2]
x_prev = prev[1]
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")
if(node[1]==start2[0] and node[2]==start2[1]):
reach_goal = 1
x = node[1]
y = node[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(5)
break

# # naming the axes
# plt.xlabel('x - axis')
Expand Down
75 changes: 75 additions & 0 deletions DFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,81 @@ def main():
# end of while loop
print(flag)
print(explored_matrix)
if flag=="Goal found":
path = []
reach_goal = 0
path.append(current)
node = path[-1]
x = node[1]
y = current[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#While checking neighbors, also check goal.
while(reach_goal== 0):
#find nearest neighbor with least calues explored_matrix value that is greater than 0.
# add that neighbot to path.
#if that neighbot == start:
# plot and break;
neighbors = []
#Neighbor Up
print("Neighbour Up")
x_neighbor = x
y_neighbor = y+1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Right
print("Neighbour Right")
x_neighbor = x+1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Down
print("Neighbour Down")
x_neighbor = x
y_neighbor = y-1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Left
print("Neighbour Left")
x_neighbor = x-1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
neighbors.sort()
print(neighbors)
if(len(neighbors)==0):
print("Path not found")
break
prev = node
node = neighbors[0]
x = node[1]
y = node[2]
x_prev = prev[1]
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")
if(node[1]==start2[0] and node[2]==start2[1]):
reach_goal = 1
x = node[1]
y = node[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(5)
break






# # naming the axes
# plt.xlabel('x - axis')
Expand Down
70 changes: 70 additions & 0 deletions wAstar.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,76 @@ def main():
# end of while loop
print(flag)
print(explored_matrix)
if flag=="Goal found":
path = []
reach_goal = 0
path.append(current)
node = path[-1]
x = node[1]
y = current[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(.1)
#While checking neighbors, also check goal.
while(reach_goal== 0):
#find nearest neighbor with least calues explored_matrix value that is greater than 0.
# add that neighbot to path.
#if that neighbot == start:
# plot and break;
neighbors = []
#Neighbor Up
print("Neighbour Up")
x_neighbor = x
y_neighbor = y+1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Right
print("Neighbour Right")
x_neighbor = x+1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Down
print("Neighbour Down")
x_neighbor = x
y_neighbor = y-1
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
#Neighbor Left
print("Neighbour Left")
x_neighbor = x-1
y_neighbor = y
score = explored_matrix[x_neighbor][y_neighbor]
print(x_neighbor," ",y_neighbor," ",score)
if(score>0):
neighbors.append([score,x_neighbor,y_neighbor] )
neighbors.sort()
print(neighbors)
if(len(neighbors)==0):
print("Path not found")
break
prev = node
node = neighbors[0]
x = node[1]
y = node[2]
x_prev = prev[1]
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")
if(node[1]==start2[0] and node[2]==start2[1]):
reach_goal = 1
x = node[1]
y = node[2]
plt.plot(y-1, a-x,'cx', fillstyle='full', markersize=22)
plt.pause(5)
break

# # naming the axes
# plt.xlabel('x - axis')
Expand Down

0 comments on commit 04ef8a0

Please sign in to comment.