-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheightPuzzle.py
173 lines (147 loc) · 4.87 KB
/
eightPuzzle.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#for taking input from user
def inputState():
mat = [[0,0,0],[0,0,0],[0,0,0]]
a=0
for i in range(0,3):
for j in range(0,3):
mat[i][j] = int(input("enter a state[{}][{}]:".format(i,j)))
return mat
#for printing State
def showState(mat):
for k in range(0,3):
for l in range(0,3):
print(mat[k][l],end=" ")
print("")
#generates the sucessor having best h(n)
def generateSucessor(current,goal):
x = getXPos(current,0)
y = getYpos(current,0)
heurasticleft = 0
heurasticright = 0
heurastictop = 0
heurasticbottom = 0
#left successor
if(x == 1 or x == 2):
leftSuccessor = leftChild(current)
heurasticleft = calculateHeuristic(leftSuccessor,goal)
#right successor
if(x == 0 or x == 1):
rightSuccessor = rightChild(current)
heurasticright = calculateHeuristic(rightSuccessor,goal)
#top successor
if(y == 0 or y == 1):
topSuccessor = topChild(current)
heurastictop = calculateHeuristic(topSuccessor,goal)
#bottom successor
if(y == 1 or y == 2):
bottomSuccessor = bottomChild(current)
heurasticbottom = calculateHeuristic(bottomSuccessor,goal)
nextCurrentFlag = compareHeuristic(heurasticleft,heurasticright,heurastictop,heurasticbottom)
if nextCurrentFlag == 1:
return leftChild
elif nextCurrentFlag == 2:
return rightSuccessor
elif nextCurrentFlag == 3:
return topSuccessor
else:
return bottomSuccessor
#generate left child
def leftChild(current):
for i in range(0,3):
for j in range(0,3):
if current[i][j] == 0:
temp = current[i][j]
current[i][j] = current[i-1][j]
current[i-1][j] = temp
return current
#generate right child
def rightChild(current):
for i in range(0,2):
for j in range(0,3):
if current[i][j] == 0:
temp = current[i][j]
current[i][j] = current[i+1][j]
current[i+1][j] = temp
return current
#generate top child
def topChild(current):
for i in range(0,3):
for j in range(0,2):
if current[i][j] == 0:
temp = current[i][j]
current[i][j] = current[i][j+1]
current[i][j+1] = temp
return current
#generate bottom child
def bottomChild(current):
for i in range(0,3):
for j in range(0,3):
if current[i][j] == 0:
temp = current[i][j]
current[i][j] = current[i][j-1]
current[i][j-1] = temp
return current
#calculates the h(n)
def calculateHeuristic(current,goal):
h = 0
for i in range(0,3):
for j in range(0,3):
x = getXPos(current,current[i][j])
y = getYPos(current,current[i][j])
x1 = getXPos(goal,goal[i][j])
y1 = getYPos(goal[i][j])
manhattan = math.abs(x1 - x) + math.abs(y1 - y)
h = h + manhattan
return h
#calculates the x position of zero
def getXPos(current,val):
for i in range(0,3):
for j in range(0,3):
if current[i][j] == val:
return i
else:
continue
#caluclates the y position of zero
def getYpos(current,val):
for i in range(0,3):
for j in range(0,3):
if current[i][j] == val:
return j
else:
continue
#find the best(greatest) heurastic value
def compareHeuristic(left,right,top,bottom):
if left >= right & left >= top & left >= bottom:
return 1
elif right >= left & right >= top & right >= bottom:
return 2
elif top >= left & top >= right & top >= bottom:
return 4
else:
return 5
# main function that runs first
def main():
initial = [[0,0,0],[0,0,0],[0,0,0]]
goal = [[0,0,0],[0,0,0],[0,0,0]]
print("Input the initial state of eight puzzle")
initial = inputState()
print("Input the goal state of eight puzzle")
goal = inputState()
print("inital node")
showState(initial)
print("goal node")
showState(goal)
current = initial #at first initial state is current State
#current = ast.literal_eval(current)
if(goal == current):
print(current)
print("initial condtion is solution")
else:
sucessor = [[0,0,0],[0,0,0],[0,0,0]]
sucessor = generateSucessor(current,goal)
while calculateHeuristic(current,goal) <= calculateHeuristic(sucessor,goal):
current = sucessor
print(current)
sucessor = generateSucessor(current,goal)
if __name__ == "__main__":
main()