-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.py
More file actions
239 lines (174 loc) Β· 5.19 KB
/
problem.py
File metadata and controls
239 lines (174 loc) Β· 5.19 KB
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
import ids , Astar
import Bidirectional_Search
## this function check if next state for butter is deadlock or not
def isDeadlock(butter,robot, search ,direction , graph , butters = [] ):
robotsNewPlace = placeRobot(direction , butter)
if len(robotsNewPlace) >= 3 :
return True
xmax = 0
ymax = 0
for ii in graph.keys():
xx = int(ii[1])
yy = int(ii[0])
xmax = max(xmax , xx)
ymax = max(ymax , yy)
ry = int(robotsNewPlace[0])
rx = int(robotsNewPlace[1])
if rx > xmax or ry > ymax or rx < 0 or ry < 0 :
return True
if(checktwobefor(graph ,robotsNewPlace, butters)):
if(search == "ids"):
(q, d) =ids.iterativeDeepening(graph,robot , robotsNewPlace, 20)
if(q):
return False
else:
return True
elif search =="astar" or search == "bidirectional":
(q , c , d) = Astar.a_star( graph , robot , robotsNewPlace , True , None , None , butter )
if(q):
return False
else:
return True
# return False
else:
return True
def deadlockbd(graph , currentpos, nextpos ,parentpos, butters = [] ):
xmax = 0
ymax = 0
for ii in graph.keys():
xx = int(ii[1])
yy = int(ii[0])
xmax = max(xmax , xx)
ymax = max(ymax , yy)
cy = int(currentpos[0])
cx = int(currentpos[1])
ny = int(nextpos[0])
nx = int(nextpos[1])
py = int(parentpos[0])
px = int(parentpos[1])
rx = -1
ry = -1
rnx = -1
rny = -1
if cy == ny :
ry = cy
if nx - cx > 0 :
rx = nx + 1
else :
rx = nx - 1
elif cx == nx :
rx = cx
if ny - cy > 0 :
ry = ny + 1
else :
ry = ny - 1
if cy == py :
rny = cy
if cx - px > 0 :
rnx = cx + 1
else :
rnx = cx - 1
elif cx == px :
rnx = cx
if cy - py > 0 :
rny = cy + 1
else :
rny = cy - 1
if rx > xmax or rnx > xmax or rx < 0 or rnx < 0 :
return True
if ry > ymax or rny > ymax or ry < 0 or rny < 0 :
return True
robotpos = str(ry) + str(rx)
robotnewpos = str(rny) + str(rnx)
checkresult = checktwobefor(graph , robotpos , butters)
if checkresult is False :
return True
checkresult = checktwobefor(graph , robotnewpos , butters)
if checkresult is False :
return True
if(checkfourside(graph ,robotpos , nextpos )):
return True
(q, c , d) = Astar.a_star(graph , robotpos , robotnewpos , True ,None , butters , nextpos )
if(q) :
return False
else:
return True
return False
## this function return which direction butter is going to go ('r' , 'l' ,'u', 'd')
def whichDirection(first , second):
xFirst = int(first[0:1])
yFirst = int(first[-1:])
xSecond = int(second[0:1])
ySecond = int(second[-1:])
if xSecond == (xFirst +1):
return "d"
elif xSecond == (xFirst -1):
return "u"
elif ySecond == (yFirst +1):
return "r"
elif ySecond == (yFirst - 1):
return "l"
## this function check if next state is empty or not
def checkAvailable(graph , next , butters , robot):
if next in graph :
if graph[next][0] == 'x' or (next in butters) or (next == robot):
return False
return True
def checktwobefor(graph , next , butters =None):
if next in graph :
if graph[next][0] == 'x' :
return False
return True
## this function return a state that robot should go to push butter
def placeRobot(direction , butter):
rowButter = int(butter[0:1])
colButter = int(butter[-1:])
if(direction == "r"):
endX = rowButter
endY = colButter - 1
if(direction == "l"):
endX = rowButter
endY = colButter + 1
if(direction == "u"):
endX = rowButter +1
endY = colButter
if(direction == "d"):
endX = rowButter -1
endY = colButter
return str(endX) + str(endY)
def checkfourside(graph , pos , nextpos) :
tmp = graph[nextpos][0]
graph[nextpos][0] = 'x'
x = int(pos[1])
y = int(pos[0])
xmax = 0
ymax = 0
for ii in graph.keys():
xx = int(ii[1])
yy = int(ii[0])
xmax = max(xmax , xx)
ymax = max(ymax , yy)
if y != 0 :
xy1 = str(y-1) + str(x)
xy1 = graph[xy1][0]
else :
xy1 = "x"
if y != ymax :
xy2 = str(y+1) + str(x)
xy2 = graph[xy2][0]
else :
xy2 = "x"
if x != 0 :
xy3 = str(y) + str(x-1)
xy3 = graph[xy3][0]
else :
xy3 = "x"
if x != xmax :
xy4 = str(y) + str(x+1)
xy4 = graph[xy4][0]
else :
xy4 = "x"
graph[nextpos][0] = tmp
if xy1 != "x" or xy2 != "x" or xy3 != "x" or xy4 != "x" :
return False
return True