-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpnba_island_class.py
More file actions
379 lines (302 loc) · 12.6 KB
/
pnba_island_class.py
File metadata and controls
379 lines (302 loc) · 12.6 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from algorithm_abstract import Algorithm
from display_grid_2d import display_2d;
from euclidian_heuristic import EucliHeur;
from print_class import PrintClass;
from traj_publisher import TrajPub;
import threading;
import multiprocessing;
class pnba_isl (Algorithm):
Dsp = [];
Heur = [];
#Static
M = [];
L = [ [0,0,0,3000], [1,1,1,3000] ];
solution_found = False;
printfile = None;
ThreadID = None;
#staticFlags
SOLUTION_FOUND = "solution_found"
GOAL_FOUND = "goal_found"
NO_SUCC = "no_succ"
SUCC = "succ"
UNDISCOVERED = "undisc"
DISCOVERED = "disc"
FALIURE = "faliure"
def __init__(self, boundry, start, goal, obstacle,thread_id):
self.OPEN1 = [];
self.OPEN2 = [];
self.CLOSED1 = [];
self.CLOSED2 = [];
self.Boundry = boundry;
self.start = start; # x,y,F,g
self.goal = goal;
#self.Islands = [[9, 15], [15, 17]];
self.Islands=[];
self.obstacle = obstacle;
# Riz Objects
#pnba_isl.Dsp = display_2d(self.Boundry);
self.Heur = EucliHeur(self.start, self.goal, self.obstacle , self.Islands);
self.printfile = PrintClass(thread_id);
self.ThreadID = thread_id;
pnba_isl.L[thread_id - 1] = [goal[0], goal[1], 2000, 2000];
print "potential islands ", pnba_isl.L[thread_id - 1];
def isDiscovered(self, n):
if(pnba_isl.M[n[0]][n[1]] == self.ThreadID):
return True;
else:
return False;
#For deciding from two lists
def getNextElement(self):
x = [];
if (len(self.OPEN1) == 0):
x = self.OPEN2.pop(0);
return 2, x;
elif (len(self.OPEN2) == 0):
x = self.OPEN1.pop(0);
return 1, x;
else:
if (self.OPEN1[0][2] > self.OPEN2[0][2]):
x = self.OPEN2.pop(0);
return 2, x;
else:
x = self.OPEN1.pop(0);
return 1, x
def getNext(self):
list_name, n = self.getNextElement();
if(pnba_isl.M[n[0]][n[1]] > 0):
if(pnba_isl.M[n[0]][n[1]] == self.ThreadID):
return pnba_isl.DISCOVERED, list_name, n
elif(self.isGoal(n)):
return pnba_isl.GOAL_FOUND, list_name, n
elif(pnba_isl.M[n[0]][n[1]] != self.ThreadID):
return pnba_isl.SOLUTION_FOUND, list_name, n
else:
return pnba_isl.FALIURE, list_name, n
elif(pnba_isl.M[n[0]][n[1]] < 0):
return pnba_isl.UNDISCOVERED, list_name, n
else:
return pnba_isl.FALIURE, list_name, n;
def updateFCost(self, n):
n[2] = self.Heur.getHeuristic(n) + self.Heur.getFixedCost();
def Expand(self, n):
expanded = []
x = [n[0] + 1, n[1], 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0], n[1] + 1, 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0] + 1, n[1] + 1, 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0] - 1, n[1], 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0], n[1] - 1, 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0] - 1, n[1] - 1, 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0] + 1, n[1] - 1, 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
x = [n[0] - 1, n[1] + 1, 0, 1000]
if (self.isInLimits(x) and not self.isDiscovered(x)):
self.updateFCost(x);
expanded.append(x);
#pnba_isl.Dsp.addSubPoint(x);
if (len(expanded) == 0):
return False, expanded
else:
return True, expanded
def run(self):
MyThread.threadLock.acquire();
#pnba_isl.Dsp.addPoint(self.start);
#pnba_isl.Dsp.addGoalPoint(self.goal);
self.printfile.writeToFile(self.start);
pnba_isl.solution_found = False;
MyThread.threadLock.release();
##pnba_isl.Dsp.addIslandPoints(self.Islands);
##pnba_isl.Dsp.addObstaclePoints(self.obstacle);
# step1
self.start[2] = self.Heur.getHeuristic(self.start);
self.OPEN1.append(self.start);
self.Islands.append(self.goal);
self.Heur.Islands = self.Islands;
while(not pnba_isl.solution_found):
if (self.isOpenEmpty()):
print("OPEN is empty");
pnba_isl.solution_found = False;
break;
t, list_name, x = self.getNext();
if(t == pnba_isl.GOAL_FOUND):
print "GOAL_FOUND by: ", self.ThreadID
break;
elif(t == pnba_isl.SOLUTION_FOUND):
print "SOLUTION_FOUND by: ", self.ThreadID
MyThread.threadLock.acquire();
pnba_isl.solution_found = True;
MyThread.threadLock.release();
break;
elif(t == pnba_isl.DISCOVERED):
print "Already Discovered"
continue;
elif(t == pnba_isl.FALIURE):
print "FALIURE";
MyThread.threadLock.acquire();
self.printfile.writeToFile(x);
MyThread.threadLock.release();
if(x[2] <= pnba_isl.L[2 - self.ThreadID][3]):
MyThread.threadLock.acquire();
self.Islands = [];
self.Islands.append(pnba_isl.L[self.ThreadID-1]);
self.Heur.Islands = self.Islands;
MyThread.threadLock.release();
succ_exist, succ = self.Expand(x);
if (not succ_exist):
print("No Successor Found");
continue;
for s in succ:
#s[3] = s[3] + self.Heur.getFixedCost();
#if (not self.isInList(s, self.OPEN1) and not self.isInList(s, self.OPEN2)):
# d = self.Heur.getMinH(s) - self.Heur.getHeuristic(s);
# if (d > 0):
# s[2] = self.Heur.getMinH(s);
# self.OPEN1.append(s);
# else:
# s[2] = self.Heur.getHeuristic(s);
# self.OPEN2.append(s);
#elif (self.isInList(s, self.OPEN1)):
# temp, element = self.getFromList(s, self.OPEN1);
# self.OPEN1.pop(element);
# s[2] = self.Heur.getMinH(s);
# self.OPEN1.append(s);
#elif (self.isInList(s, self.OPEN2)):
# temp, element = self.getFromList(s, self.OPEN2);
# self.OPEN2.pop(element);
# s[2] = self.Heur.getHeuristic(s);
# self.OPEN2.append(s);
#If Island or from OPEN2
if(list_name == 2 or self.isIsland(x)):
if (self.isInList(s, self.OPEN1)):
temp, element = self.getFromList(s, self.OPEN1);
self.OPEN1.pop(element);
elif (self.isInList(s, self.OPEN2)):
temp, element = self.getFromList(s, self.OPEN2);
self.OPEN2.pop(element);
self.OPEN2.append(s);
#If not Island or from OPEN1
if(list_name == 1 or not self.isIsland(x)):
if(not self.isInList(s, self.OPEN1) and not self.isInList(s, self.OPEN2)):
d = self.Heur.getMinH(s) - self.Heur.getHeuristic(s);
if (d > 0):
s[2] = self.Heur.getMinH(s);
self.OPEN1.append(s);
else:
s[2] = self.Heur.getHeuristic(s);
self.OPEN2.append(s);
elif(self.isInList(s, self.OPEN1)):
temp, element = self.getFromList(s, self.OPEN1);
self.OPEN1.pop(element);
s[2] = self.Heur.getMinH(s);
self.OPEN1.append(s);
elif(self.isInList(s, self.OPEN2)):
temp, element = self.getFromList(s, self.OPEN2);
self.OPEN2.pop(element);
s[2] = self.Heur.getHeuristic(s);
self.OPEN2.append(s);
#---------Old code---------
#if(self.isInList(s,self.OPEN1)):
# temp, element = self.getFromList(s, self.OPEN1);
# self.OPEN1.pop(element);
#self.OPEN1.append(s);
h = self.Heur.getHeuristic(s) + self.Heur.getCost(s);
if(h < pnba_isl.L[2-self.ThreadID][3]):
print "Locking Thread on ", self.ThreadID;
MyThread.threadLock.acquire();
h = self.Heur.getHeuristic(s) + self.Heur.getCost(s);
if (h < pnba_isl.L[2-self.ThreadID][3]):
pnba_isl.L[2 - self.ThreadID] = x;
pnba_isl.L[2 - self.ThreadID][3] = h;
pnba_isl.L[self.ThreadID-1][3] = h;
MyThread.threadLock.release();
else:
print "Not Entering Main Algorithm"
pnba_isl.M[x[0]][x[1]] = self.ThreadID;
self.sortList(self.OPEN1);
self.sortList(self.OPEN2);
self.printfile.file.close();
class MyThread(threading.Thread):
threadLock = threading.Lock();
Algo = [];
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def setParams(self, boundry, start, goal, obstacle):
self.Algo = pnba_isl(boundry,start,goal, obstacle, self.threadID);
def run(self):
self.Algo.run();
########################################
def main():
threads = [];
Boundry = [[0, 0], [20, 20]];
start = [3, 0, 0, 0]; # x,y,F,g
goal = [20, 20, 0, 20];
obstacle = [
[12, 12], [11, 13], [10, 14], [13, 11], [14, 10],
[13, 13], [12, 14], [11, 15], [14, 12], [15, 11],
[11, 11], [10, 13], [12, 10], [13, 9],
[13, 12], [12, 13], [11, 12], [12, 11],
[11, 14], [14, 11], [13, 10]
];
pnba_isl.M = [[-1 for x in range(Boundry[1][0] - Boundry[0][0] + 1)] for y in range(Boundry[1][0] - Boundry[0][0] + 1)];
#pnba_isl.Dsp = display_2d(Boundry);
# use the multiprocessing module to perform the plotting activity in another process (i.e., on another core):
#job_for_another_core = multiprocessing.Process(target=#pnba_isl.Dsp, args=())
#job_for_another_core.start()
thread1 = MyThread(1, "Thread-1");
thread2 = MyThread(2, "Thread-2");
thread1.setParams(Boundry, start, goal, obstacle);
thread2.setParams(Boundry,goal,start, obstacle);
# Start new Threads
thread1.start();
thread2.start();
# Add threads to thread list
threads.append(thread1)
threads.append(thread2)
# Wait for all threads to complete
for t in threads:
t.join()
print "Thread Length: ", len(threads)
print "Exiting Both Thread"
#pnba_isl.Dsp.BlockGraph();
print "Now Parsing Files"
parser = TrajPub("Thread-1.txt");
Thread1Sol = parser.parseFile();
parser.File.close();
print Thread1Sol;
parser = TrajPub("Thread-2.txt");
Thread2Sol = parser.parseFile();
parser.File.close();
print Thread2Sol
print "Displaying Data"
parser.display(Boundry,start,goal,obstacle,Thread1Sol, Thread2Sol);
print "End"
if __name__ == "__main__":
main()