stepcost dodany

This commit is contained in:
s444349 2020-04-28 17:31:16 +02:00
parent 69d8d74d26
commit 9eca938713
2 changed files with 16 additions and 14 deletions

View File

@ -1,25 +1,28 @@
import heapq import heapq
def heurystyka(obiekty, a, b): def heurystyka(a, b):
heur = abs((b[0] - a[0])) + abs((b[1] - a[1])) return abs((b[0] - a[0])) + abs((b[1] - a[1]))
def stepcost(obiekty, b):
cost = 1
if obiekty["plansza"][b[0], b[1]].jestDomem is True: if obiekty["plansza"][b[0], b[1]].jestDomem is True:
heur += 2 cost += 2
if obiekty["plansza"][b[0], b[1]].jestWysypiskiem is True: if obiekty["plansza"][b[0], b[1]].jestWysypiskiem is True:
heur += 1 cost += 1
if obiekty["plansza"][b[0], b[1]].jestKontenerem is True: if obiekty["plansza"][b[0], b[1]].jestKontenerem is True:
heur += 2 cost += 2
return heur return cost
def astar(obiekty, start, cel): def astar(obiekty, start, cel):
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
close_set = set() close_set = set()
came_from = {} came_from = {}
hscore = {start: heurystyka(obiekty, start, cel)} hscore = {start: heurystyka(start, cel)}
gscore = {start: 0} gscore = {start: 0}
fscore = {start: heurystyka(obiekty, start, cel)} fscore = {start: heurystyka(start, cel)}
oheap = [] oheap = []
heapq.heappush(oheap, (fscore[start], start)) heapq.heappush(oheap, (fscore[start], start))
while oheap: while oheap:
@ -27,7 +30,6 @@ def astar(obiekty, start, cel):
if current == cel: if current == cel:
data = [] data = []
while current in came_from: while current in came_from:
data.append(current) data.append(current)
current = came_from[current] current = came_from[current]
return data[::-1] return data[::-1]
@ -41,7 +43,7 @@ def astar(obiekty, start, cel):
continue continue
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
continue continue
tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad) tentative_h_score = heurystyka(sasiad, cel) + stepcost(obiekty, sasiad)
if sasiad in [i[1] for i in oheap] and tentative_h_score < hscore.get(current, 0): if sasiad in [i[1] for i in oheap] and tentative_h_score < hscore.get(current, 0):
continue continue
@ -49,6 +51,6 @@ def astar(obiekty, start, cel):
came_from[sasiad] = current came_from[sasiad] = current
hscore[sasiad] = tentative_h_score hscore[sasiad] = tentative_h_score
fscore[sasiad] = tentative_h_score + gscore[current] fscore[sasiad] = tentative_h_score + gscore[current]
gscore[sasiad] = gscore[current] + heurystyka(obiekty, current, sasiad) gscore[sasiad] = gscore[current] + heurystyka(current, sasiad)
heapq.heappush(oheap, (fscore[sasiad], sasiad)) heapq.heappush(oheap, (fscore[sasiad], sasiad))
return False return False

View File

@ -79,12 +79,12 @@ def game():
(obiekty["kontener_organiczne"].x, obiekty["kontener_organiczne"].y)] (obiekty["kontener_organiczne"].x, obiekty["kontener_organiczne"].y)]
temp = False temp = False
while nieodwiedzone_domy: while nieodwiedzone_domy:
nieodwiedzone_domy.sort(key=lambda x: astar.heurystyka(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), x)) nieodwiedzone_domy.sort(key=lambda x: astar.heurystyka((obiekty["smieciarka"].x, obiekty["smieciarka"].y), x))
cel = nieodwiedzone_domy.pop(0) cel = nieodwiedzone_domy.pop(0)
obiekty["smieciarka"].astar_move(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), cel) obiekty["smieciarka"].astar_move(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), cel)
while nieodwiedzone_kontenery: while nieodwiedzone_kontenery:
nieodwiedzone_kontenery.sort(key=lambda x: astar.heurystyka(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), x)) nieodwiedzone_kontenery.sort(key=lambda x: astar.heurystyka((obiekty["smieciarka"].x, obiekty["smieciarka"].y), x))
cel = nieodwiedzone_kontenery.pop(0) cel = nieodwiedzone_kontenery.pop(0)
obiekty["smieciarka"].astar_move(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), cel) obiekty["smieciarka"].astar_move(obiekty, (obiekty["smieciarka"].x, obiekty["smieciarka"].y), cel)