1
0
forked from s444417/ProjektAI

added BFS

This commit is contained in:
= 2020-04-27 10:49:30 +02:00
parent 914e9404f0
commit c0f5a8f7f7
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,49 @@
import queue
class BFS:
def __init__(self, grid):
self.grid = grid
self.visited = []
def get_neighbors(self, node, grid):
dr = [-1, +1, 0, 0]
dc = [0, 0, +1, -1]
x = node[0]
y = node[1]
node = (x, y)
neighbors = []
for i in range(4):
new_col = x + dc[i]
new_row = y + dr[i]
node = (new_col, new_row)
if 0 <= new_row < len(grid) and 0 <= new_col < len(grid[0]):
if (new_col, new_row) not in self.visited:
if grid[new_row][new_col] != 1:
neighbors.append((new_col, new_row))
return neighbors
def find_path(self, start, end):
grid = self.grid
queue = [(start, [])]
while len(queue) > 0:
node, path = queue.pop(0)
path.append(node)
self.visited.append(node)
if node == end:
if len(path) > 1:
path.pop(0)
return path
adj_nodes = self.get_neighbors(node, grid)
for item in adj_nodes:
if item not in self.visited:
queue.append((item, path[:]))
return None

View File

@ -3,6 +3,7 @@ import time
import sys
from kelner.src.components.Table import Status
from kelner.src.algorithms.AStar.Finder import Finder
from kelner.src.algorithms.BFS.BFS import BFS
# creates new thread
@ -20,11 +21,13 @@ class WaiterManager (threading.Thread):
if tables:
reservedPlaces = self.__drawableManager.getReservedPlaces(waiter)
finder = Finder(reservedPlaces)
# bfs = BFS(reservedPlaces)
origin = (waiter.getX(), waiter.getY())
for table in tables:
targets = finder.getNeighbours((table.getX(), table.getY()), False)
for target in targets:
if target is not None:
# path = bfs.find_path(origin, target)
path = finder.getPath(origin, target, True)
if path:
result = len(path)