diff --git a/astar.py b/astar.py index bb3e370..68837e8 100644 --- a/astar.py +++ b/astar.py @@ -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 \ No newline at end of file + heapq.heappush(fringe, (fscore[sasiad], sasiad)) + return False + + diff --git a/modele.py b/modele.py index eea9117..48d1a13 100644 --- a/modele.py +++ b/modele.py @@ -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, diff --git a/resources/screenShots/funkcjaNastepnika.png b/resources/screenShots/funkcjaNastepnika.png index f61ef2e..94fab05 100644 Binary files a/resources/screenShots/funkcjaNastepnika.png and b/resources/screenShots/funkcjaNastepnika.png differ diff --git a/resources/screenShots/heurystyka.png b/resources/screenShots/heurystyka.png index 1f92268..8efc27d 100644 Binary files a/resources/screenShots/heurystyka.png and b/resources/screenShots/heurystyka.png differ diff --git a/resources/screenShots/petlaGlowna.png b/resources/screenShots/petlaGlowna.png index 41bd604..6d2bb72 100644 Binary files a/resources/screenShots/petlaGlowna.png and b/resources/screenShots/petlaGlowna.png differ diff --git a/resources/screenShots/stepcost.png b/resources/screenShots/stepcost.png new file mode 100644 index 0000000..31abcbf Binary files /dev/null and b/resources/screenShots/stepcost.png differ diff --git a/route-planning.md b/route-planning.md index a8e9b10..0d9edf5 100644 --- a/route-planning.md +++ b/route-planning.md @@ -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