-
Notifications
You must be signed in to change notification settings - Fork 0
/
RiverProblem_BFS.py
221 lines (198 loc) · 6.36 KB
/
RiverProblem_BFS.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
class RiverProblem:
State=[1,1,1,1,1]
Path=[]
R=RiverProblem()
#0=Wolf,1=Goat,2=corn,3=Farmer,4=Boat
Goal=[0,0,0,0,0]
Qeuee=[]
ExploredSet=[]
def PrintRP(Temp):
# i=0
# while i < 3:
print(" ",Temp.Path)
# i+=1
def CheckGoal(Goal,State):
if Goal != State:
return False
else:
return True
def CheckExploredSet(State,ExploredSet):
c = len(ExploredSet)
i = 0
while i < c:
if ExploredSet[i] == State:
return False
i+=1
return True
def Validation(State):
if State[2]==0 and State[1]==0 and State[3]==1:
return False
elif State[0]==0 and State[1]==0 and State[3]==1:
return False
elif State[0] == 1 and State[1] == 1 and State[3]==0:
return False
elif State[1] == 1 and State[2] == 1 and State[3]==0:
return False
else:
return True
def FarmerAndGoatRight(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[1] = 0
P.State[3] = 0
P.State[4] = 0
if Validation(P.State) == True:
P.Path.append("FarmerMoveGoatRight")
return P
else:
return 0
def FarmerAndGoatleft(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[1] = 1
P.State[3] = 1
P.State[4] = 1
if Validation(P.State) == True:
P.Path.append("FarmerMoveGoatLeft")
return P
else:
return 0
def FarmerAndWolfRight(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[0] = 0
P.State[3] = 0
P.State[4] = 0
if Validation(P.State) == True:
P.Path.append("FarmerMoveWolfRight")
return P
else:
return 0
def FarmerAndWolfleft(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[0] = 1
P.State[3] = 1
P.State[4] = 1
if Validation(P.State) == True:
P.Path.append("FarmerMoveWolfLeft")
return P
else:
return 0
def FarmerAndCornRight(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[2] = 0
P.State[3] = 0
P.State[4] = 0
if Validation(P.State) == True:
P.Path.append("FarmerMoveCornRight")
# if CheckExploredSet(P.State,ExploredSet) == False:
# ExploredSet.append(P.State)
return P
else:
return 0
def FarmerAndCornleft(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[2] = 1
P.State[3] = 1
P.State[4] = 1
if Validation(P.State) == True:
P.Path.append("FarmerMoveCornLeft")
return P
else:
return 0
def FarmerBackToLeft(Temp):
P = RiverProblem()
import copy
P.State=copy.deepcopy(Temp.State)
P.Path=copy.deepcopy(Temp.Path)
P.State[3] = 1
P.State[4] = 1
if Validation(P.State) == True:
P.Path.append("FarmerMoveLeft")
return P
else:
return 0
def CheckLeft(Temp):
if Temp.State[3]==0 and Temp.State[4]==0:
return 1
else:
return 0
def BreakLoop(Check,Goal,ExploredSet,Qeuee):
if Check != 0:
if CheckExploredSet(Check.State,ExploredSet) == False and CheckGoal(Goal,Check.State) == False:
return False
elif CheckExploredSet(Check.State,ExploredSet) == True and CheckGoal(Goal,Check.State) == False:
Qeuee.append(Check)
return False
else:
print("Goal Found")
# PrintRP(Check)
print("Path :: ")
print(Check.Path)
return True
else:
return False
Check=RiverProblem()
if CheckGoal(Goal,Check.State) == False:
Qeuee.append(Check)
while True:
if len(Qeuee)==0:
break
else:
Temp =Qeuee.pop(0)
#else:
if CheckGoal(Goal,Temp.State) == True:
break
if CheckExploredSet(Temp.State,ExploredSet) == True:
ExploredSet.append(Temp.State)
if(CheckLeft(Temp) == 1):
Check = FarmerBackToLeft(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
if Temp.State[1]==0 and Temp.State[3]==0 and Temp.State[4]==0:
Check = FarmerAndGoatleft(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
if Temp.State[0]==0 and Temp.State[3]==0 and Temp.State[4]==0:
Check = FarmerAndWolfleft(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
if Temp.State[2]==0 and Temp.State[3]==0 and Temp.State[4]==0:
Check = FarmerAndCornleft(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
else:
if Temp.State[1]==1 and Temp.State[3]==1 and Temp.State[4]==1:
Check = FarmerAndGoatRight(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
if Temp.State[0]==1 and Temp.State[3]==1 and Temp.State[4]==1:
Check = FarmerAndWolfRight(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
if Temp.State[2]==1 and Temp.State[3]==1 and Temp.State[4]==1:
Check = FarmerAndCornRight(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
else:
print("Goal found")
#PrintRP(Check.State)
print("Path :: ")
print(Check.Path)
input("Press Enter to close!!!")
input("Press Enter to close!!!")