forked from s444417/ProjektAI
added BFS
This commit is contained in:
parent
914e9404f0
commit
c0f5a8f7f7
49
kelner/src/algorithms/BFS/BFS.py
Normal file
49
kelner/src/algorithms/BFS/BFS.py
Normal 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
|
@ -3,6 +3,7 @@ import time
|
|||||||
import sys
|
import sys
|
||||||
from kelner.src.components.Table import Status
|
from kelner.src.components.Table import Status
|
||||||
from kelner.src.algorithms.AStar.Finder import Finder
|
from kelner.src.algorithms.AStar.Finder import Finder
|
||||||
|
from kelner.src.algorithms.BFS.BFS import BFS
|
||||||
|
|
||||||
|
|
||||||
# creates new thread
|
# creates new thread
|
||||||
@ -20,11 +21,13 @@ class WaiterManager (threading.Thread):
|
|||||||
if tables:
|
if tables:
|
||||||
reservedPlaces = self.__drawableManager.getReservedPlaces(waiter)
|
reservedPlaces = self.__drawableManager.getReservedPlaces(waiter)
|
||||||
finder = Finder(reservedPlaces)
|
finder = Finder(reservedPlaces)
|
||||||
|
# bfs = BFS(reservedPlaces)
|
||||||
origin = (waiter.getX(), waiter.getY())
|
origin = (waiter.getX(), waiter.getY())
|
||||||
for table in tables:
|
for table in tables:
|
||||||
targets = finder.getNeighbours((table.getX(), table.getY()), False)
|
targets = finder.getNeighbours((table.getX(), table.getY()), False)
|
||||||
for target in targets:
|
for target in targets:
|
||||||
if target is not None:
|
if target is not None:
|
||||||
|
# path = bfs.find_path(origin, target)
|
||||||
path = finder.getPath(origin, target, True)
|
path = finder.getPath(origin, target, True)
|
||||||
if path:
|
if path:
|
||||||
result = len(path)
|
result = len(path)
|
||||||
|
Loading…
Reference in New Issue
Block a user