forked from suhailps93/Tower-of-Hanoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tower_of_Hanoi.py
325 lines (242 loc) · 8.2 KB
/
Tower_of_Hanoi.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#Assignment submitted by Ahalya Mandana and Suhail Pallath Sulaiman'
from copy import deepcopy
class Node:
def __init__(self):
self.state=[[],[],[]]
self.nodeNumber=0
self.status='idle'
self.neighbours=[]
self.parent=None
self.children=[]#For BFS
self.point=10
def evalFunc(node):
largest=0
l=[]
for peg in initialState:
if len(peg)>0:
l.append(max(peg))
largest=max(l)
#print 'largest=',largest
node.point=10
setPnts(node,largest)
def setPnts(node,largest):
global finalState
#print '\n starting setpnts with largest= ',largest,' nodestate=',node.state
if largest>0:
for fpeg in finalState:
if largest in fpeg:
pos=finalState.index(fpeg)
# print largest, 'the largest is on peg no ', pos ,fpeg,' in finalstate. finalstate[pos]='#,finalstate[pos]
if largest in node.state[pos]:
# print largest, 'the largest is on peg no ', pos ,' in node.state. node.state[pos]=',node.state[pos]
# print 'reducing point from ', node.point, ' to ', (node.point-1)
node.point=node.point-1
# print 'starting recursive with largest as ', largest-1
setPnts(node,largest-1)
def move(st1,st2):
s1=st1[:]
s2=st2[:]
if len(s1)>0:
topDisc=s1[len(s1)-1]
lastofS2=len(s2)-1
if len(s2)==0 or s2[lastofS2]>topDisc:
s2.append(topDisc)
s1.pop()
return s1,s2
else:
return None
else:
return None
def moveDisc(n):
global noOfPegs
stacks=[]
for x in xrange(0,noOfPegs):
for y in xrange(0,noOfPegs):
stacks=move(n.state[x],n.state[y])
if stacks!=None:
# print 'states after move', states
nextnode=Node()
nextnode=deepcopy(n)
nextnode.state[x]=deepcopy(stacks[0])
nextnode.state[y]=deepcopy(stacks[1])
# print 'states', states
# print '\n'
# print 'next node',nextnode.state
if nextnode.state in states:
#print 'nextnode in states'
a=1#dumb value
else:
nodenumber=nextnode.nodeNumber
# print nextnode.state, 'next not in states'
states.append(nextnode.state)
return nextnode
#print 'DEAD END'
return None
def printPath(node):
print 'Tracing back the Path'
while True:
print 'Node number: ', node.nodeNumber,' State: ', node.state
if node.parent!=None:
node=node.parent
else:
break
def dfs(node):
global targetFound
global nodenumber
if targetFound==False:
node.status='ongoing'
parent=deepcopy(node)
node=moveDisc(node)
if node!=None:
nodenumber+=1
node.nodeNumber=nodenumber
node.parent=parent
print 'Node ',node.nodeNumber, node.state,'\n'
if node.state==finalState:
print 'Final target reached'
printPath(node)
targetFound=True
dfs(node)
else:
#print 'node is None'
parent.status='done'
node=parent.parent
print 'moving back to Node',node.nodeNumber,'State',node.state
dfs(node)
else:
#print 'Target found'
return False
def BFS(node):
global parentList,nodenumber,childList,targetFound,step
print '\n STEP : ',step
step+=1
for node in parentList:
if targetFound==False :
print 'Parent Node:',node.nodeNumber,' State :',node.state
exhausted=False
parent=deepcopy(node)
i=1
while exhausted==False :
i+=1
childnode=moveDisc(node)
if childnode!=None:
nodenumber+=1
childnode.nodeNumber=nodenumber
childnode.parent=node
parent.children.append(childnode)
childList.append(childnode)
print ' Child Node:',childnode.nodeNumber,'State:', childnode.state
#print 'states', states
if childnode.state==finalState:
print 'Final target reached'
printPath(childnode)
targetFound=True
else:
exhausted=True
parentList=deepcopy(childList)
childList=[]
if targetFound==False :
BFS(parentList)
def bestFS():
print '\n'
global parentList,nodenumber,childList,targetFound,step,largestInTarget,largest
leastPoint=10
for node in parentList:
#setPoints(node)
evalFunc(node)
#print 'Node: ',node.nodeNumber, node.state,node.point
if node.point<leastPoint:
leastPoint=node.point
for node in parentList:
if targetFound==False and node.point==leastPoint:
print 'Parent Node:',node.nodeNumber,' State :',node.state, 'Cost = ', node.point
exhausted=False
parent=deepcopy(node)
i=1
while exhausted==False :
i+=1
childnode=moveDisc(node)
if childnode!=None:
nodenumber+=1
childnode.nodeNumber=nodenumber
childnode.parent=node
parent.children.append(childnode)
childList.append(childnode)
print ' Child Node:',childnode.nodeNumber,'State:', childnode.state
#print 'states', states
if childnode.state==finalState:
print 'Final target reached'
printPath(childnode)
targetFound=True
else:
exhausted=True
parentList=deepcopy(childList)
childList=[]
if targetFound==False :
bestFS()
def readState():
global noOfPegs
state=[]
for x in xrange(0,noOfPegs):
print 'Discs in Peg',x+1,' : ',
a = [int(x) for x in raw_input().split()]
state.append(a)
return state
noOfPegs=3
shouldContinue=True
while shouldContinue:
print '\n\nAssignment submitted by Ahalya Mandana and Suhail Pallath Sulaiman'
print '\n1. Depth First Search'
print '2. Breadth First Search'
print '3. Best First Search'
print '4. Exit'
algoNumber = raw_input("Please select the search algorithm --> ")
if algoNumber=='4':
print '\nExiting'
quit()
print '\nInstructions for input:'
print '-->An example input for discs in a peg >>> 3 2 1'
print '-->This means your peg have 3 discs with disc of size 3 at bottom and disc of size 1 at top'
print '-->If the peg is empty, just click ENTER; Do not input anything in that case'
noOfPegs =int(raw_input("\nEnter number of pegs--> "))
print '\nEnter details for initial State'
initialState=readState()
print '\nEnter details for final State'
finalState=readState()
print '\nInitial state : ',initialState
print 'Final states : ',finalState
# initialState=[[1],[3],[2]]
# finalState=[[3,1],[2],[]]
# initialState=[[3],[1],[2]]
# finalState=[[3,2,1],[],[]]
states=[]
states=[initialState]
nodenumber=1
time=1
targetFound=False
node=Node()
node.state=initialState
node.nodeNumber=nodenumber
parentList=[node]
childList=[]
targetFound=False
largestInTarget=False
step=1
parentList=[node]
childList=[]
if algoNumber=='1':
print '\nYou selected Depth First Search'
print 'Node ',node.nodeNumber, node.state,'\n'#only for DFS
dfs(node)
elif algoNumber=='2':
print '\nYou selected Breadth First Search'
BFS(node)
elif algoNumber=='3':
print '\nYou selected Best First Search'
bestFS()
elif algoNumber=='4':
print '\nExiting'
quit()
else:
print 'Please select a valid option'
continue