This commit is contained in:
Adam Osiowy 2020-04-28 18:18:13 +02:00
parent 282e0d39f7
commit ec572b93cb
7 changed files with 33 additions and 13 deletions

View File

@ -23,10 +23,10 @@ def astar(obiekty, start, cel):
hscore = {start: heurystyka(start, cel)}
gscore = {start: 0}
fscore = {start: heurystyka(start, cel)}
oheap = []
heapq.heappush(oheap, (fscore[start], start))
while oheap:
current = heapq.heappop(oheap)[1]
fringe = []
heapq.heappush(fringe, (fscore[start], start))
while fringe:
current = heapq.heappop(fringe)[1]
if current == cel:
data = []
while current in came_from:
@ -34,23 +34,25 @@ def astar(obiekty, start, cel):
current = came_from[current]
return data[::-1]
close_set.add(current)
for i, j in sasiedzi:
sasiad = current[0] + i, current[1] + j
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
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):
tentative_h_score = heurystyka(sasiad, cel) + stepcost(obiekty, sasiad)
if sasiad in [i[1] for i in fringe] and tentative_h_score < hscore.get(current, 0):
continue
elif sasiad not in close_set and sasiad not in [i[1] for i in oheap]:
elif sasiad not in close_set and sasiad not in [i[1] for i in fringe]:
came_from[sasiad] = current
hscore[sasiad] = tentative_h_score
fscore[sasiad] = tentative_h_score + gscore[current]
gscore[sasiad] = gscore[current] + heurystyka(current, sasiad)
heapq.heappush(oheap, (fscore[sasiad], sasiad))
return False
heapq.heappush(fringe, (fscore[sasiad], sasiad))
return False

View File

@ -215,9 +215,6 @@ class Kratka(pygame.sprite.Sprite):
self.jestPrzeszkoda = False
self.kolor = GREY
self.obiekt = None
self.g = 0 # Distance to start node
self.h = 0 # Distance to goal node
self.f = 0 # Total cost
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.__class__
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -22,6 +22,16 @@ po czym wybiera następny najbliższy nieodwiedzony kontener.
![petla](resources/screenShots/petlaGlowna.png)
- w pętli głównej wykorzystujemy przeszukiwanie grafu (graphsearch)
- tworzymy kolejkę priorytetową, po czym dodajemy do niej bieżący węzeł
- przeprowadzamy test osiągnięcia celu:
- jeżeli cel został osiągnięty, to odtwarzamy ścieżkę przechodząc po rodzicach
- do listy odwiedzonych elementów dodajemy bieżący element
- wybieramy następnika
- następnikowi przypisujemy rodzica
- wyznaczamy priorytet następnika
- dodajemy go do kolejki zgodnie z priorytetem
---
## 3. Funkcja następnika:
@ -33,6 +43,10 @@ gdzie sąsiedzi to:
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
```
- następnik jest wyznaczany spośród pól sąsiadujących z danym polem (z pominięciem pól po skosie)
- następnie sprawdzane jest czy nie znajduje się on poza mapą
- potem sprawdzane jest czy wybrany punkt nie jest przeszkodą
---
## 4. Przyjęta heurystyka:
@ -40,6 +54,13 @@ sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
![heurystyka](resources/screenShots/heurystyka.png)
- Heurystyka to suma odległości Manhattan
---
## 5. Koszt wjechania na pole
![stepcost](resources/screenShots/stepcost.png)
- Koszt wjechania na pole, na którym jest dom wynosi 3
- Koszt wjechania na pole, które jest wysypiskiem wynosi 2
- Koszt wjechania na pole, które jest kontenerem wynosi 3