-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeH.py
130 lines (98 loc) · 3.57 KB
/
NodeH.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
import math
class NodeH:
def __init__(self,x,y,nim,goalY,goalX,chose):
self.x = x
self.y = y
self.pirent = None
self.childerin = list()
self.data=nim
self.location='(' + "%s" %x + ',' + "%s" %y + ')'
self.goalX=goalX
self.goalY=goalY
self.chose=chose
if self.chose == 1:
self.HU =self.Manhattan()
else:
self.HU = self.Euclidean()
def __lt__(self, other):
return self.HU < other.HU
def MoveToRight(self):
if(self.y+1<len(self.data[self.x]) and self.data[self.x][self.y+1]==1):
right = NodeH(self.x,self.y+1,self.data,self.goalX,self.goalY,self.chose)
self.childerin.append(right)
self.childerin[-1].pirent = self
#print("Right is ", right.x, " , ", right.y)
#print("right P",self.childerin[-1].pirent.y,",",self.childerin[-1].pirent.x)
def MoveToLift(self):
if(self.y-1>=0 and self.data[self.x][self.y-1]==1):
Lift = NodeH(self.x,self.y-1,self.data,self.goalX,self.goalY,self.chose)
self.childerin.append(Lift)
#print("Lift is ", Lift.x, " , ", Lift.y)
self.childerin[-1].pirent = self
#print("lift P",self.childerin[-1].pirent.y,",",self.childerin[-1].pirent.x)
def MovetoUp(self):
if(self.x-1>=0 and self.data[self.x-1][self.y]==1):
Up = NodeH(self.x-1,self.y,self.data,self.goalX,self.goalY,self.chose)
#print("Up is ",Up.x," , ",Up.y)
self.childerin.append(Up)
self.childerin[-1].pirent= self
#print("up P",self.childerin[-1].pirent.y,",",self.childerin[-1].pirent.x)
def MoveToDown(self):
if(self.x+1<len(self.data) and self.data[self.x+1][self.y]==1):
Down = NodeH(self.x+1,self.y,self.data,self.goalX,self.goalY,self.chose)
self.childerin.append(Down)
self.childerin[-1].pirent= self
# print("Down is ", Down.x, " , ", Down.y)
# print("Down P",self.childerin[-1].pirent.y,",",self.childerin[-1].pirent.x)
def isGoal(self,x,y):
isgoal = False
if(self.x==x and self.y == y):
isgoal = True
return isgoal
def print(self):
print(self.x,",",self.y)
def ExpandMove(self):
self.MoveToLift()
self.MoveToRight()
self.MoveToDown()
self.MovetoUp()
def Manhattan(self):
hx=0
hy=0
if self.goalX>self.x:
hx=self.goalX-self.x
else:
if self.goalX<self.x:
hx=self.x-self.goalX
else:
hx=0
if self.goalY > self.y:
hy = self.goalY - self.y
else:
if self.goalY < self.y:
hy = self.y -self.goalY
else:
hy = 0
return hx+hy
def Euclidean(self):
hx = 0
hy = 0
if self.goalX>self.x:
hx=self.goalX-self.x
else:
if self.goalX<self.x:
hx=self.x-self.goalX
else:
hx=0
hx=hx*hx
if self.goalY > self.y:
hy = self.goalY - self.y
else:
if self.goalY < self.y:
hy = self.y -self.goalY
else:
hy = 0
hy=hy*hy
resualtH=hx+hy
resualtH=int(math.sqrt(resualtH))
return resualtH