algorytm astar v1.1
This commit is contained in:
parent
1b1451452c
commit
a19449a1c4
24
astar.py
24
astar.py
@ -1,20 +1,21 @@
|
||||
import heapq
|
||||
|
||||
|
||||
def heurystyka(a, b):
|
||||
return abs((b[0] - a[0])) + abs((b[1] - a[1]))
|
||||
def heurystyka(obiekty, a, b):
|
||||
heur = abs((b[0] - a[0])) + abs((b[1] - a[1]))
|
||||
if obiekty["plansza"][b[0], b[1]].jestDomem is True:
|
||||
heur += 2
|
||||
return heur
|
||||
|
||||
|
||||
def astar(start, cel):
|
||||
import game
|
||||
|
||||
obiekty = game.utworzObiekty()
|
||||
def astar(obiekty, start, cel):
|
||||
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
||||
close_set = set()
|
||||
came_from = {}
|
||||
gscore = {start: 0}
|
||||
fscore = {start: heurystyka(start, cel)}
|
||||
fscore = {start: heurystyka(obiekty, start, cel)}
|
||||
oheap = []
|
||||
dodatkowy_koszt = 0
|
||||
heapq.heappush(oheap, (fscore[start], start))
|
||||
while oheap:
|
||||
current = heapq.heappop(oheap)[1]
|
||||
@ -27,22 +28,19 @@ def astar(start, cel):
|
||||
close_set.add(current)
|
||||
for i, j in sasiedzi:
|
||||
sasiad = current[0] + i, current[1] + j
|
||||
tentative_g_score = gscore[current] + heurystyka(current, sasiad)
|
||||
|
||||
if 14 < sasiad[0] or sasiad[0] < 0:
|
||||
continue
|
||||
elif 14 < sasiad[1] or sasiad[1] < 0:
|
||||
continue
|
||||
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
|
||||
continue
|
||||
elif 4 < sasiad[0] and 4 < sasiad[1]:
|
||||
continue
|
||||
elif 4 > sasiad[0] and 4 > sasiad[1]:
|
||||
continue
|
||||
tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad)
|
||||
if sasiad in close_set and tentative_g_score >= gscore.get(sasiad, 0):
|
||||
continue
|
||||
if tentative_g_score < gscore.get(sasiad, 0) or sasiad not in [i[1] for i in oheap]:
|
||||
came_from[sasiad] = current
|
||||
gscore[sasiad] = tentative_g_score
|
||||
fscore[sasiad] = tentative_g_score + heurystyka(sasiad, cel)
|
||||
fscore[sasiad] = tentative_g_score + heurystyka(obiekty, sasiad, cel)
|
||||
heapq.heappush(oheap, (fscore[sasiad], sasiad))
|
||||
return False
|
||||
|
Loading…
Reference in New Issue
Block a user