|
| 1 | +import Path_Backtrack |
| 2 | + |
| 3 | +import copy |
| 4 | + |
| 5 | +import Draw |
| 6 | +import timeit |
| 7 | +import time |
| 8 | + |
| 9 | + |
| 10 | +class Queue: |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + |
| 14 | + self.Queue = [] |
| 15 | + |
| 16 | + |
| 17 | + def Add_element(self, data): |
| 18 | + |
| 19 | + #print("data : ",data) |
| 20 | + |
| 21 | + |
| 22 | + if data is None: |
| 23 | + return |
| 24 | + |
| 25 | + |
| 26 | + flag = any(isinstance(i, list) for i in data) |
| 27 | + |
| 28 | + |
| 29 | + if not flag: |
| 30 | + |
| 31 | + self.Queue.append(data) |
| 32 | + |
| 33 | + else: |
| 34 | + for i in data: |
| 35 | + |
| 36 | + self.Queue.append(i) |
| 37 | + |
| 38 | + |
| 39 | + def remove(self): |
| 40 | + |
| 41 | + if len(self.Queue) != 0: |
| 42 | + |
| 43 | + last_element = self.Queue[0] |
| 44 | + |
| 45 | + self.Queue = self.Queue[1:] |
| 46 | + |
| 47 | + return last_element |
| 48 | + |
| 49 | + else: |
| 50 | + |
| 51 | + return 0 |
| 52 | + |
| 53 | + |
| 54 | + def display(self): |
| 55 | + |
| 56 | + return self.Queue |
| 57 | + |
| 58 | + |
| 59 | + def is_empty(self): |
| 60 | + |
| 61 | + return True if len(self.Queue) == 0 else False |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +def read_file(FileLoc): |
| 66 | + |
| 67 | + Map = [] |
| 68 | + |
| 69 | + |
| 70 | + with open(FileLoc) as file: |
| 71 | + |
| 72 | + lines = file.readlines() |
| 73 | + |
| 74 | + for line in lines: |
| 75 | + |
| 76 | + temp = [] |
| 77 | + |
| 78 | + temp.extend(line) |
| 79 | + |
| 80 | + |
| 81 | + if "\n" in temp: |
| 82 | + |
| 83 | + temp.remove("\n") |
| 84 | + |
| 85 | + |
| 86 | + Map.append(temp) |
| 87 | + |
| 88 | + |
| 89 | + file.close() |
| 90 | + |
| 91 | + |
| 92 | + #print("File loaded successfully") |
| 93 | + |
| 94 | + |
| 95 | + return Map |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +def Map_Dimension(map): |
| 100 | + |
| 101 | + row, column = 0, 0 |
| 102 | + |
| 103 | + row, column = len(map), len(map[0]) |
| 104 | + |
| 105 | + |
| 106 | + return row, column |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +def Location(map, element): |
| 111 | + |
| 112 | + row, column = Map_Dimension(map) |
| 113 | + |
| 114 | + #print(row,column) |
| 115 | + |
| 116 | + walls = [] |
| 117 | + |
| 118 | + |
| 119 | + if element == "Walls": |
| 120 | + |
| 121 | + wall_element = "+" |
| 122 | + |
| 123 | + #print(row,column) |
| 124 | + |
| 125 | + for x in range(row): |
| 126 | + |
| 127 | + for y in range(column): |
| 128 | + |
| 129 | + #print(x,y) |
| 130 | + |
| 131 | + if map[x][y] == wall_element: |
| 132 | + |
| 133 | + walls.append([x, y]) |
| 134 | + |
| 135 | + |
| 136 | + return walls |
| 137 | + |
| 138 | + else: |
| 139 | + |
| 140 | + for x in range(row): |
| 141 | + |
| 142 | + for y in range(column): |
| 143 | + |
| 144 | + if map[x][y] == element: |
| 145 | + |
| 146 | + return [x, y] |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +def Available_move(map, pos, walls, explored): |
| 151 | + |
| 152 | + row, column = Map_Dimension(map) |
| 153 | + |
| 154 | + #print("pos : ", pos) |
| 155 | + |
| 156 | + up = [pos[0] - 1, pos[1]] |
| 157 | + |
| 158 | + down = [pos[0] + 1, pos[1]] |
| 159 | + |
| 160 | + left = [pos[0], pos[1] - 1] |
| 161 | + |
| 162 | + right = [pos[0], pos[1] + 1] |
| 163 | + |
| 164 | + |
| 165 | + moves = [up, down, left, right] |
| 166 | + |
| 167 | + |
| 168 | + result = copy.deepcopy(moves) |
| 169 | + |
| 170 | + |
| 171 | + for x in range(4): |
| 172 | + |
| 173 | + i = moves[x] |
| 174 | + |
| 175 | + # print(i) |
| 176 | + |
| 177 | + if (i[0]) < 0: |
| 178 | + if i in result: |
| 179 | + |
| 180 | + result.remove(i) |
| 181 | + |
| 182 | + if (i[1]) < 0: |
| 183 | + if i in result: |
| 184 | + |
| 185 | + result.remove(i) |
| 186 | + |
| 187 | + if i[0] >= row: |
| 188 | + if i in result: |
| 189 | + |
| 190 | + result.remove(i) |
| 191 | + |
| 192 | + if i[1] >= column: |
| 193 | + if i in result: |
| 194 | + |
| 195 | + result.remove(i) |
| 196 | + |
| 197 | + |
| 198 | + moves = copy.deepcopy(result) |
| 199 | + |
| 200 | + |
| 201 | + for i in moves: |
| 202 | + |
| 203 | + if i in walls: |
| 204 | + |
| 205 | + result.remove(i) |
| 206 | + |
| 207 | + |
| 208 | + moves = copy.deepcopy(result) |
| 209 | + |
| 210 | + |
| 211 | + for i in moves: |
| 212 | + |
| 213 | + if i in explored: |
| 214 | + |
| 215 | + result.remove(i) |
| 216 | + |
| 217 | + |
| 218 | + #print("available move : ", result) |
| 219 | + |
| 220 | + |
| 221 | + if len(result) != 0: |
| 222 | + return result |
| 223 | + else: |
| 224 | + |
| 225 | + return None |
| 226 | + |
| 227 | + |
| 228 | +def BFS_Algorithm(grid, start_point, end_point): |
| 229 | + |
| 230 | + t0 = timeit.default_timer() |
| 231 | + |
| 232 | + frontier = Queue() |
| 233 | + |
| 234 | + path = Path_Backtrack.Path_Finder() |
| 235 | + |
| 236 | + explored = [] |
| 237 | + |
| 238 | + destination = Location(grid, end_point) |
| 239 | + |
| 240 | + walls = Location(grid, "Walls") |
| 241 | + |
| 242 | + #print("Walls -->", walls) |
| 243 | + |
| 244 | + initial_position = Location(grid, start_point) |
| 245 | + |
| 246 | + frontier.Add_element(initial_position) |
| 247 | + |
| 248 | + while not frontier.is_empty(): |
| 249 | + |
| 250 | + #print("Frontier->", frontier.display()) |
| 251 | + |
| 252 | + previous = frontier.remove() |
| 253 | + |
| 254 | + #print(previous, "--> Pop Element") # printing pop element |
| 255 | + |
| 256 | + #print("Frontier->", frontier.display()) |
| 257 | + |
| 258 | + |
| 259 | + if previous == destination: |
| 260 | + |
| 261 | + #path.Print() |
| 262 | + |
| 263 | + res = path.Trace(initial_position, destination) |
| 264 | + |
| 265 | + t1 = timeit.default_timer() |
| 266 | + print(res) |
| 267 | + |
| 268 | + elapsed_time = round((t1 - t0) * 10 ** 6, 3) |
| 269 | + |
| 270 | + l = len(res) # type: ignore |
| 271 | + |
| 272 | + property = {"method":"BFS", |
| 273 | + |
| 274 | + "speed":str(elapsed_time)+str(" nano seconds"), |
| 275 | + |
| 276 | + "cost":str(len(explored)), |
| 277 | + |
| 278 | + "moves":str(l)} |
| 279 | + |
| 280 | + graph = Draw.Create_Graph(res,walls,Map_Dimension(grid),property,explored) |
| 281 | + |
| 282 | + graph.graph() |
| 283 | + |
| 284 | + return |
| 285 | + else: |
| 286 | + |
| 287 | + #time.sleep(1) |
| 288 | + |
| 289 | + explored.append(previous) |
| 290 | + |
| 291 | + moves = Available_move(grid, previous, walls, explored) |
| 292 | + |
| 293 | + |
| 294 | + #if moves is not None: |
| 295 | + |
| 296 | + # frontier.Add_element(moves) |
| 297 | + |
| 298 | + |
| 299 | + path.Add_element(previous, moves) |
| 300 | + |
| 301 | + |
| 302 | + if not moves: |
| 303 | + |
| 304 | + continue |
| 305 | + else: |
| 306 | + |
| 307 | + frontier.Add_element(moves) |
| 308 | + |
| 309 | + else: |
| 310 | + |
| 311 | + print("No Solutions Found !") |
| 312 | + |
| 313 | + |
| 314 | + #path.Trace(start_point, end_point) |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | +def runner(Loc,Start,Destination): |
| 319 | + |
| 320 | + #print("working 1") |
| 321 | + |
| 322 | + #location = Loc |
| 323 | + |
| 324 | + #location = File_Opener.get_path() |
| 325 | + |
| 326 | + Map = read_file(Loc) |
| 327 | + |
| 328 | + BFS_Algorithm(Map, Start, Destination) |
| 329 | + |
| 330 | + |
| 331 | +#runner() |
| 332 | + |
| 333 | + |
0 commit comments