A* chages
This commit is contained in:
parent
d716eb1d79
commit
4ee1e5af4f
36
UI/Apath.py
36
UI/Apath.py
@ -1,33 +1,28 @@
|
|||||||
import numpy
|
import numpy as np
|
||||||
from heapq import * # pylint: disable=unused-wildcard-import
|
from heapq import * # pylint: disable=unused-wildcard-import
|
||||||
|
|
||||||
|
|
||||||
def heuristic(a, b, pathing):
|
def heuristic(a, b):
|
||||||
|
|
||||||
x = abs(a[0]-b[0])
|
x = abs(a[0]-b[0])
|
||||||
y = abs(a[1]-b[1])
|
y = abs(a[1]-b[1])
|
||||||
|
|
||||||
if pathing == '*':
|
if x > y:
|
||||||
|
return 14*y + 10*(x - y)
|
||||||
if x > y:
|
|
||||||
return 14*y + 10*(x - y)
|
|
||||||
else:
|
|
||||||
return 14*x + 10*(y - x)
|
|
||||||
else:
|
else:
|
||||||
return 10*(x + y)
|
return 14*x + 10*(y - x)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def astar(array, start, goal, pathing):
|
def Astar(array, start, goal):
|
||||||
|
|
||||||
if pathing == '+':
|
|
||||||
neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
|
||||||
else:
|
|
||||||
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 = {}
|
came_from = {}
|
||||||
gscore = {start: 0}
|
gscore = {start: 0}
|
||||||
fscore = {start: heuristic(start, goal, pathing)}
|
fscore = {start: heuristic(start, goal)}
|
||||||
oheap = []
|
oheap = []
|
||||||
checked = []
|
checked = []
|
||||||
|
|
||||||
@ -45,13 +40,14 @@ def astar(array, start, goal, pathing):
|
|||||||
current = came_from[current]
|
current = came_from[current]
|
||||||
|
|
||||||
return list(reversed(data)), checked
|
return list(reversed(data)), checked
|
||||||
|
print("array current",array[current[0],current[1]])
|
||||||
array[current[0], current[1]] = 2
|
array[current[0], current[1]]=2
|
||||||
|
|
||||||
for i, j in neighbors:
|
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)
|
tentative_g_score = gscore[current] + heuristic(current, neighbor)
|
||||||
|
|
||||||
if 0 <= neighbor[0] < array.shape[0]:
|
if 0 <= neighbor[0] < array.shape[0]:
|
||||||
if 0 <= neighbor[1] < array.shape[1]:
|
if 0 <= neighbor[1] < array.shape[1]:
|
||||||
@ -70,6 +66,6 @@ def astar(array, start, goal, pathing):
|
|||||||
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
|
came_from[neighbor] = current
|
||||||
gscore[neighbor] = tentative_g_score
|
gscore[neighbor] = tentative_g_score
|
||||||
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal, pathing)
|
fscore[neighbor] = tentative_g_score + heuristic(neighbor, goal)
|
||||||
heappush(oheap, (fscore[neighbor], neighbor))
|
heappush(oheap, (fscore[neighbor], neighbor))
|
||||||
return False
|
return False
|
24
UI/window.py
24
UI/window.py
@ -1,6 +1,9 @@
|
|||||||
import pygame as pg
|
import pygame as pg
|
||||||
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
from UI.grid import Grid, Node
|
from UI.grid import Grid, Node
|
||||||
|
from UI.Apath import Astar
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Window():
|
class Window():
|
||||||
@ -29,15 +32,26 @@ class Window():
|
|||||||
grid.change_field(19, 19, 2)
|
grid.change_field(19, 19, 2)
|
||||||
|
|
||||||
#random obsticle
|
#random obsticle
|
||||||
for x in range(25):
|
for x in range(40):
|
||||||
grid.change_field(random.randint(1,18),random.randint(1,18),3)
|
grid.change_field(random.randint(1,18),random.randint(1,18),3)
|
||||||
|
|
||||||
|
#path
|
||||||
path = [(i, i) for i in range(1, 20, 1)]
|
path = [(i, i) for i in range(1, 20, 1)]
|
||||||
self.grid.draw_map(self.screen)
|
self.grid.draw_map(self.screen)
|
||||||
|
|
||||||
|
#convert table to support Apath algoritm
|
||||||
|
array = [[self.grid.table[col][row] for row in range(cols)] for col in range(rows)]
|
||||||
|
for i,x in enumerate(array):
|
||||||
|
for j,y in enumerate(x):
|
||||||
|
if y.field_type == 3:
|
||||||
|
array[i][j] = None
|
||||||
|
|
||||||
|
nodes_array = np.array(array)
|
||||||
|
|
||||||
|
#Run A star
|
||||||
|
|
||||||
|
path, check = Astar(nodes_array, (0,0), (19, 19))
|
||||||
|
print(path,"\n\n",check,"\n\n")
|
||||||
|
|
||||||
|
|
||||||
for t in path:
|
for t in path:
|
||||||
@ -48,3 +62,7 @@ class Window():
|
|||||||
self.grid.draw_node(self.screen, x, y)
|
self.grid.draw_node(self.screen, x, y)
|
||||||
pg.time.delay(500)
|
pg.time.delay(500)
|
||||||
pg.quit() # pylint: disable=no-member
|
pg.quit() # pylint: disable=no-member
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user