-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrongSrc.py
146 lines (111 loc) · 4.61 KB
/
WrongSrc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# This Code Does Not Work , Just Here to Review My Mistakes
""" import numpy as np
from anytree import Node, RenderTree, AsciiStyle
import anytree
gridMap = np.array([[1, 1, 1, 0, 0],[0, 0, 0, 0, 1],[0, 1, 1, 0, 0],[0, 0, 1, 1, 0],[1, 0, 0, 0, 0]])
X=gridMap.shape[0]-1
Y=gridMap.shape[1]-1
#print(X)
#print(Y)
## Start Location
startrow = 2
startcol = 0
## End Location
endrow = 2
endcol = 4
def heuristic(currentrow,currentcol,endrow,endcol):
# Estimating Distance Between Point and End Location
d_x=endrow-currentrow
d_y=endcol-currentcol
d=((d_x**2)+(d_y**2))
d=np.sqrt(d)
return d
def goalTest(currentrow,currentcol,endrow,endcol):
# Testing if the current position is goal position
return (currentrow==endrow and currentcol==endcol)
# Generating Succesors
Succesors = lambda x, y : [(x2, y2) for x2 in range(x-1, x+2)
for y2 in range(y-1, y+2)
if (-1 < x <= X and -1 < y <= Y and
(x != x2 or y != y2) and
(0 <= x2 <= X) and
(0 <= y2 <= Y))]
# Generating Heuristic Matrix
h=np.zeros((5,5))
g=np.zeros((5,5))
f=np.zeros((5,5))
for i in range(5):
for j in range(5):
h[i,j]=heuristic(i,j,endrow,endcol)
if gridMap[i,j]!=0:
g[i,j]=100
# print(h)
#print(g)
current_node=[startrow,startcol] # Current Location Co-ordinates
open_list=[current_node] # List of Frontier Locations -- Visited But Not Explored
# l=len(open_list)
#print('Frontier',len(open_list))
closed_list=[] # List of Visted and Explored Locations
f=np.zeros((5,5)) # Total Cost Function
f[startrow,startcol]=h[startrow,startcol] # Initial Cost is Heuristic Cost
k=50
while k!=0:
# print(open_list)
l=len(open_list)
minval = 1000
minrow=0
mincol=0
for i in range(l):
f[open_list[i][0],open_list[i][1]]=g[open_list[i][0],open_list[i][1]]+heuristic(open_list[i][0],open_list[i][1],
endrow,endcol)
if minval>=f[open_list[i][0],open_list[i][1]]:
minval=f[open_list[i][0],open_list[i][1]]
minrow=open_list[i][0]
mincol=open_list[i][1]
current_node=[minrow,mincol]
print('current node',current_node)
if(goalTest(current_node[0],current_node[1],endrow,endcol)):
print('-------------found--------------',k)
break
node_successor=Succesors(current_node[0],current_node[1])
# print(node_successor)
nsl=len(node_successor)
# print(nsl)
for i in range(nsl):
succesor_cost=g[current_node[0],current_node[1]]+heuristic(current_node[0],current_node[1],
node_successor[i][0],node_successor[i][1])
# print('Inside For Loop')
#succesor_cost=g[current_node[0],current_node[1]]+h[node_successor[i][0],node_successor[i][1]]
# print(succesor_cost)
if [node_successor[i][0],node_successor[i][1]] in open_list:
if g[node_successor[i][0],node_successor[i][1]] <=succesor_cost: continue
elif [node_successor[i][0],node_successor[i][1]] in closed_list:
if g[node_successor[i][0],node_successor[i][1]] <=succesor_cost: continue
closed_list.remove([node_successor[i][0],node_successor[i][1]])
open_list.append([node_successor[i][0],node_successor[i][1]])
else:
open_list.append([node_successor[i][0],node_successor[i][1]])
# print('Appending to open list')
g[node_successor[i][0],node_successor[i][1]]=succesor_cost
closed_list.append(current_node)
k=k-1
copy_succ=Succesors(2,4)
print(copy_succ)
copy_openlist=[[1,1],[1,2],[1,3],[1,4]]
if[copy_succ[1][0],copy_succ[1][1]] in copy_openlist:
print('y')
copy_openlist.remove([copy_succ[1][0],copy_succ[1][1]])
print(copy_openlist)
nodex_open=[]
nodex_close=[]
nodex=[]
nodex.append(Node([2,0]))
nodex.append(Node([1,2],parent=nodex[0]))
nodex.append(Node([0,1],parent=nodex[0]))
nodex.append(Node([0,2],parent=nodex[2],state="f"))
print(RenderTree(nodex[0]))
print(()!=anytree.search.findall_by_attr(nodex[0], name="state",value="f"))
nodex_open.append(Node([startrow,startcol]))
f[startrow,startcol]=g[startrow,startcol]
print([2,0]==nodex_open[0])
"""