From 56e4194cda536bcf1eb16af5da65e88c31fb3bbe Mon Sep 17 00:00:00 2001 From: Maciej Sobkowiak Date: Mon, 15 Apr 2019 12:19:26 +0200 Subject: [PATCH 1/2] Add A* --- UI/Apath.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ UI/__init__.py | 0 2 files changed, 70 insertions(+) create mode 100644 UI/Apath.py create mode 100644 UI/__init__.py diff --git a/UI/Apath.py b/UI/Apath.py new file mode 100644 index 0000000..223139b --- /dev/null +++ b/UI/Apath.py @@ -0,0 +1,70 @@ +import numpy +from heapq import * + +def heuristic(a, b, pathing): + + x = abs(a[0]-b[0]) + y = abs(a[1]-b[1]) + + if pathing == '*': + + if x > y: + return 14*y + 10*(x - y) + else: + return 14*x + 10*(y - x) + else: + return 10*(x + y) + +def astar(array, start, goal, pathing): + + if pathing == '+': + neighbors = [(0,1),(0,-1),(1,0),(-1,0)] + else: + neighbors = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)] + + came_from = {} + gscore = {start:0} + fscore = {start:heuristic(start, goal, pathing)} + oheap = [] + checked = [] + + heappush(oheap, (fscore[start], start)) + + while oheap: + + current = heappop(oheap)[1] + checked.append(current) + + if current == goal: + data = [] + while current in came_from: + data.append(current) + current = came_from[current] + + return list(reversed(data)), checked + + array[current[0], current[1]] = 2 + for i, j in neighbors: + neighbor = current[0] + i, current[1] + j + tentative_g_score = gscore[current] + heuristic(current, neighbor, pathing) + if 0 <= neighbor[0] < array.shape[0]: + if 0 <= neighbor[1] < array.shape[1]: + if array.flat[array.shape[1] * neighbor[0]+neighbor[1]] == 1: + continue + else: + # array bound y walls + continue + else: + # array bound x walls + continue + + if array[neighbor[0]][neighbor[1]] == 2 and tentative_g_score >= gscore.get(neighbor, 0): + continue + + if tentative_g_score < gscore.get(neighbor, 0) or neighbor not in [i[1]for i in oheap]: + came_from[neighbor] = current + gscore[neighbor] = tentative_g_score + fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal, pathing) + heappush(oheap, (fscore[neighbor], neighbor)) + + return False \ No newline at end of file diff --git a/UI/__init__.py b/UI/__init__.py new file mode 100644 index 0000000..e69de29 From bc666615631e02576516b88b8373d7c1784bf8f9 Mon Sep 17 00:00:00 2001 From: Maciej Sobkowiak Date: Mon, 15 Apr 2019 12:54:29 +0200 Subject: [PATCH 2/2] Pylint disable no member error --- UI/Apath.py | 29 +++++++++++++++++------------ UI/window.py | 4 ++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/UI/Apath.py b/UI/Apath.py index 223139b..26c1c80 100644 --- a/UI/Apath.py +++ b/UI/Apath.py @@ -1,8 +1,9 @@ import numpy -from heapq import * +from heapq import * # pylint: disable=unused-wildcard-import + def heuristic(a, b, pathing): - + x = abs(a[0]-b[0]) y = abs(a[1]-b[1]) @@ -15,21 +16,23 @@ def heuristic(a, b, pathing): else: return 10*(x + y) + def astar(array, start, goal, pathing): if pathing == '+': - neighbors = [(0,1),(0,-1),(1,0),(-1,0)] + neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)] else: - neighbors = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)] + neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] + neighbors = [(i, j)for i in range(-1, 2, 1) for j in range(2)] came_from = {} - gscore = {start:0} - fscore = {start:heuristic(start, goal, pathing)} + gscore = {start: 0} + fscore = {start: heuristic(start, goal, pathing)} oheap = [] checked = [] heappush(oheap, (fscore[start], start)) - + while oheap: current = heappop(oheap)[1] @@ -45,8 +48,11 @@ def astar(array, start, goal, pathing): array[current[0], current[1]] = 2 for i, j in neighbors: - neighbor = current[0] + i, current[1] + j + + neighbor = current[0] + i, current[1] + j + tentative_g_score = gscore[current] + heuristic(current, neighbor, pathing) + if 0 <= neighbor[0] < array.shape[0]: if 0 <= neighbor[1] < array.shape[1]: if array.flat[array.shape[1] * neighbor[0]+neighbor[1]] == 1: @@ -57,14 +63,13 @@ def astar(array, start, goal, pathing): else: # array bound x walls continue - + if array[neighbor[0]][neighbor[1]] == 2 and tentative_g_score >= gscore.get(neighbor, 0): continue - - if tentative_g_score < gscore.get(neighbor, 0) or neighbor not in [i[1]for i in oheap]: + + if tentative_g_score < gscore.get(neighbor, 0) or neighbor not in [i[1]for i in oheap]: came_from[neighbor] = current gscore[neighbor] = tentative_g_score fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal, pathing) heappush(oheap, (fscore[neighbor], neighbor)) - return False \ No newline at end of file diff --git a/UI/window.py b/UI/window.py index 08a597e..e2916ca 100644 --- a/UI/window.py +++ b/UI/window.py @@ -4,7 +4,7 @@ from UI.grid import Grid, Node class Window(): def __init__(self, grid: Grid): - pg.init() + pg.init() # pylint: disable=no-member # setup window pg.display.set_caption('Inteligentna śmieciarka') @@ -35,4 +35,4 @@ class Window(): self.grid.draw_node(self.screen, x - 1, y - 1) self.grid.draw_node(self.screen, x, y) pg.time.delay(500) - pg.quit() + pg.quit() # pylint: disable=no-member