From 1b1451452c237b9cc9cfb63495e49e5bc13f7fa7 Mon Sep 17 00:00:00 2001 From: s444349 Date: Sat, 25 Apr 2020 22:30:39 +0200 Subject: [PATCH 1/5] algorytm astar v1 --- astar.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ game.py | 6 ++++++ modele.py | 23 +++++++++++++++++++++-- 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 astar.py diff --git a/astar.py b/astar.py new file mode 100644 index 0000000..0a35700 --- /dev/null +++ b/astar.py @@ -0,0 +1,48 @@ +import heapq + + +def heurystyka(a, b): + return abs((b[0] - a[0])) + abs((b[1] - a[1])) + + +def astar(start, cel): + import game + + obiekty = game.utworzObiekty() + sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] + close_set = set() + came_from = {} + gscore = {start: 0} + fscore = {start: heurystyka(start, cel)} + oheap = [] + heapq.heappush(oheap, (fscore[start], start)) + while oheap: + current = heapq.heappop(oheap)[1] + if current == cel: + data = [] + while current in came_from: + data.append(current) + current = came_from[current] + return data[::-1] + 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 + 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) + heapq.heappush(oheap, (fscore[sasiad], sasiad)) + return False diff --git a/game.py b/game.py index 8dfd7f1..efd970c 100644 --- a/game.py +++ b/game.py @@ -4,6 +4,7 @@ import numpy as np import random import os import shutil +import astar pygame.init() @@ -67,8 +68,13 @@ def game(): if event.key == pygame.K_DOWN: obiekty["smieciarka"].w_dol() + obiekty["smieciarka"].rand_move() clock.tick(7) + #start = obiekty["plansza"][0, 14] + #koniec = obiekty["plansza"][14, 0] + print(astar.astar((0, 14), (14, 0))) + #print(len(astar.astar(start, koniec))) pygame.quit() diff --git a/modele.py b/modele.py index 8a62cb2..e74d4c6 100644 --- a/modele.py +++ b/modele.py @@ -139,10 +139,10 @@ class Smieciarka(pygame.sprite.Sprite): def getOdwiedzoneDomy(self): return self.odwiedzone_domy - def setPlansza(self,plansza): + def setPlansza(self, plansza): self.plansza = plansza - def setObiekty(self,obiekty): + def setObiekty(self, obiekty): self.obiekty = obiekty @@ -190,21 +190,40 @@ class Kratka(pygame.sprite.Sprite): def __init__(self, poz_x, poz_y): self.pozX = poz_x self.pozY = poz_y + self.pozycja = (self.pozX, self.pozY) + self.rodzic = None self.jestDomem = False self.jestKontenerem = False self.jestWysypiskiem = False 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, self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN, WIDTH, HEIGHT) + # Sort nodes + def __lt__(self, other): + return self.f < other.f + + def __repr__(self): + return ('{0}'.format(self.pozycja)) + + # Compare nodes + def __eq__(self, other): + return True if self.pozycja == other.pozycja else False + def setImage(self, image): self.image = image + def setRodzic(self, rodzic): + self.rodzic = rodzic + def setObiekt(self, obiekt): self.obiekt = obiekt From a19449a1c4b8b6deae70991bb03e57d9644bb85d Mon Sep 17 00:00:00 2001 From: s444349 Date: Sat, 25 Apr 2020 23:03:25 +0200 Subject: [PATCH 2/5] algorytm astar v1.1 --- astar.py | 24 +++++++++++------------- game.py | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/astar.py b/astar.py index 0a35700..182ccac 100644 --- a/astar.py +++ b/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 diff --git a/game.py b/game.py index efd970c..c8e6969 100644 --- a/game.py +++ b/game.py @@ -73,7 +73,7 @@ def game(): clock.tick(7) #start = obiekty["plansza"][0, 14] #koniec = obiekty["plansza"][14, 0] - print(astar.astar((0, 14), (14, 0))) + print(astar.astar(obiekty, (0, 14), (14, 0))) #print(len(astar.astar(start, koniec))) pygame.quit() From 83c00349d582706fc13b49c045e664471e118921 Mon Sep 17 00:00:00 2001 From: s444349 Date: Sat, 25 Apr 2020 23:46:33 +0200 Subject: [PATCH 3/5] astar ruch smieciarki --- game.py | 9 +++++++-- modele.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/game.py b/game.py index c8e6969..9943c08 100644 --- a/game.py +++ b/game.py @@ -41,6 +41,7 @@ def game(): # Petla az uzytkownik zamknie program done = False clock = pygame.time.Clock() + temp = True # -------- Glowna petla programu ----------- while not done: @@ -69,11 +70,15 @@ def game(): obiekty["smieciarka"].w_dol() - obiekty["smieciarka"].rand_move() + #obiekty["smieciarka"].rand_move() + while(temp): + obiekty["smieciarka"].astar_move(obiekty) + temp = False clock.tick(7) #start = obiekty["plansza"][0, 14] #koniec = obiekty["plansza"][14, 0] - print(astar.astar(obiekty, (0, 14), (14, 0))) + + #print(astar.astar(obiekty, (0, 14), (14, 0))) #print(len(astar.astar(start, koniec))) pygame.quit() diff --git a/modele.py b/modele.py index e74d4c6..a52d992 100644 --- a/modele.py +++ b/modele.py @@ -3,6 +3,7 @@ import game import random import os import shutil +import astar # wysokosc i szerokosc kazdej kratki WIDTH = 60 @@ -24,6 +25,7 @@ class Smieciarka(pygame.sprite.Sprite): def __init__(self, x, y): self.x = x self.y = y + self.pozycja = (self.x, self.y) self.image = pygame.image.load('resources/plansza/smieciarka.png') self.obraz = None self.ruch = 1 @@ -54,6 +56,20 @@ class Smieciarka(pygame.sprite.Sprite): elif rand_int == 3: self.w_dol() + def astar_move(self, obiekty): + sciezka = astar.astar(obiekty, self.pozycja, (14, 0)) + print(sciezka) + for koord in sciezka: + if koord[0] == self.x - 1 and koord[1] == self.y: + self.w_lewo() + elif koord[0] == self.x + 1 and koord[1] == self.y: + self.w_prawo() + elif koord[0] == self.x and koord[1] + 1 == self.y: + self.w_gore() + elif koord[0] == self.x and koord[1] - 1 == self.y: + self.w_dol() + print("skonczylem") + def w_lewo(self): if self.x > 0: if self.plansza[self.x - 1, self.y].jestPrzeszkoda is not True: From 9127c46938aa934ea3ee76f1138e38ea2db61ecd Mon Sep 17 00:00:00 2001 From: s444349 Date: Sun, 26 Apr 2020 11:14:26 +0200 Subject: [PATCH 4/5] algoryth astar v2 --- astar.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/astar.py b/astar.py index 182ccac..8e73f5a 100644 --- a/astar.py +++ b/astar.py @@ -12,6 +12,7 @@ def astar(obiekty, start, cel): sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} + hscore = {start: heurystyka(obiekty, start, cel)} gscore = {start: 0} fscore = {start: heurystyka(obiekty, start, cel)} oheap = [] @@ -20,12 +21,14 @@ def astar(obiekty, start, cel): while oheap: current = heapq.heappop(oheap)[1] if current == cel: + obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) data = [] while current in came_from: data.append(current) current = came_from[current] return data[::-1] close_set.add(current) + obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) for i, j in sasiedzi: sasiad = current[0] + i, current[1] + j @@ -35,12 +38,15 @@ def astar(obiekty, start, cel): continue elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11: continue - tentative_g_score = gscore[current] + heurystyka(obiekty, current, sasiad) - if sasiad in close_set and tentative_g_score >= gscore.get(sasiad, 0): + tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad) + + if sasiad in close_set and tentative_h_score > hscore.get(current, 0): continue - if tentative_g_score < gscore.get(sasiad, 0) or sasiad not in [i[1] for i in oheap]: + if tentative_h_score <= hscore.get(current, 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(obiekty, sasiad, cel) + hscore[sasiad] = tentative_h_score + fscore[sasiad] = tentative_h_score + gscore[current] + gscore[sasiad] = gscore[current] + heurystyka(obiekty, current, sasiad) heapq.heappush(oheap, (fscore[sasiad], sasiad)) + obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0)) return False From 7cb3aa904cd3d316c84df69942bbfdf66561a2e9 Mon Sep 17 00:00:00 2001 From: s444349 Date: Sun, 26 Apr 2020 12:58:52 +0200 Subject: [PATCH 5/5] kolejna poprawa astar --- astar.py | 8 ++++++-- modele.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/astar.py b/astar.py index 8e73f5a..96258d7 100644 --- a/astar.py +++ b/astar.py @@ -5,10 +5,13 @@ 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 + if obiekty["plansza"][b[0], b[1]].jestPrzeszkoda is True: + heur += 100 return heur def astar(obiekty, start, cel): + sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} @@ -24,6 +27,7 @@ def astar(obiekty, start, cel): obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) data = [] while current in came_from: + data.append(current) current = came_from[current] return data[::-1] @@ -40,9 +44,9 @@ def astar(obiekty, start, cel): continue tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad) - if sasiad in close_set 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 - if tentative_h_score <= hscore.get(current, 0) or sasiad not in [i[1] for i in oheap]: + if sasiad not in close_set and sasiad not in [i[1] for i in oheap]: came_from[sasiad] = current hscore[sasiad] = tentative_h_score fscore[sasiad] = tentative_h_score + gscore[current] diff --git a/modele.py b/modele.py index a52d992..148db04 100644 --- a/modele.py +++ b/modele.py @@ -57,7 +57,7 @@ class Smieciarka(pygame.sprite.Sprite): self.w_dol() def astar_move(self, obiekty): - sciezka = astar.astar(obiekty, self.pozycja, (14, 0)) + sciezka = astar.astar(obiekty, self.pozycja, (random.randrange(15), random.randrange(15))) print(sciezka) for koord in sciezka: if koord[0] == self.x - 1 and koord[1] == self.y: