modified the bfs search - prints path
This commit is contained in:
parent
ea49df80bd
commit
2a3e6b006a
BIN
__pycache__/etykieta.cpython-311.pyc
Normal file
BIN
__pycache__/etykieta.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/main.cpython-311.pyc
Normal file
BIN
__pycache__/main.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/paczka.cpython-311.pyc
Normal file
BIN
__pycache__/paczka.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/regal.cpython-311.pyc
Normal file
BIN
__pycache__/regal.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wozek.cpython-311.pyc
Normal file
BIN
__pycache__/wozek.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wyszukiwanie.cpython-311.pyc
Normal file
BIN
__pycache__/wyszukiwanie.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wyszukiwanie1.cpython-311.pyc
Normal file
BIN
__pycache__/wyszukiwanie1.cpython-311.pyc
Normal file
Binary file not shown.
109
main.py
109
main.py
@ -3,7 +3,8 @@ import pygame
|
||||
import regal
|
||||
import paczka
|
||||
from wozek import Wozek
|
||||
import wyszukiwanie
|
||||
from wyszukiwanie import wyszukiwanie_bfs, Stan, SearchSpace
|
||||
|
||||
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((980, 980))
|
||||
@ -22,75 +23,65 @@ def main():
|
||||
sys.exit(0)
|
||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
sys.exit(0)
|
||||
|
||||
grid = SearchSpace()
|
||||
# create start and goal states
|
||||
start_state = Stan(x=0, y=0, kierunek=0) # for example
|
||||
goal_state = Stan(x=0, y=1, kierunek=0) # for example
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
# lewy przycisk myszy
|
||||
if event.button == 1:
|
||||
x = pygame.mouse.get_pos()[0]
|
||||
y = pygame.mouse.get_pos()[1]
|
||||
# perform BFS search
|
||||
path = wyszukiwanie_bfs(start_state, goal_state, grid)
|
||||
|
||||
docelowy_stan = wyszukiwanie.Stan(x, y, 0)
|
||||
# print(f'{docelowy_stan.x} + {docelowy_stan.y}')
|
||||
wyszukiwanie.wyszukiwanie_bfs(wozek.obecnyStan, docelowy_stan)
|
||||
# print the path, if found
|
||||
if path:
|
||||
for p in path:
|
||||
print(p.x, p.y)
|
||||
print("Path found:", path)
|
||||
else:
|
||||
print("No path found.")
|
||||
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_DOWN:
|
||||
wozek.y_change = 1
|
||||
if event.key == pygame.K_UP:
|
||||
wozek.y_change = -1
|
||||
if event.key == pygame.K_RIGHT:
|
||||
wozek.x_change = 1
|
||||
if event.key == pygame.K_LEFT:
|
||||
wozek.x_change = -1
|
||||
for state in path:
|
||||
pygame.time.delay(200)
|
||||
wozek.x = state.x
|
||||
wozek.y = state.y
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit(0)
|
||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
sys.exit(0)
|
||||
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
|
||||
wozek.y_change = 0
|
||||
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
|
||||
wozek.x_change = 0
|
||||
|
||||
wozek.x += wozek.x_change
|
||||
wozek.y += wozek.y_change
|
||||
# Drawing
|
||||
screen.fill((51,51,51)) # removes object trail
|
||||
screen.blit(miejsce, (430, 400))
|
||||
|
||||
if wozek.x <= 0:
|
||||
wozek.x = 0
|
||||
elif wozek.x >= 916:
|
||||
wozek.x = 916
|
||||
if wozek.y <= 0:
|
||||
wozek.y = 0
|
||||
elif wozek.x >= 916:
|
||||
wozek.x = 916
|
||||
# idRegału, Długość regału podana w kratkach, Współrzędne od których ma być tworzony regał (wiersz,kolumna) - poziomo
|
||||
# Współrzędne od (1,1) do (10,10)
|
||||
regal.Regal(1, 1, 2, 2)
|
||||
regal.Regal(2, 1, 2, 3)
|
||||
regal.Regal(3, 1, 3, 2)
|
||||
regal.Regal(4, 1, 3, 3)
|
||||
|
||||
regal.Regal(5, 1, 8, 2)
|
||||
regal.Regal(6, 1, 8, 3)
|
||||
regal.Regal(7, 1, 9, 2)
|
||||
regal.Regal(8, 1, 9, 3)
|
||||
|
||||
# Drawing
|
||||
screen.fill((51, 51, 51)) # removes object trail
|
||||
screen.blit(miejsce, (430, 400))
|
||||
regal.Regal(9, 1, 2, 8)
|
||||
regal.Regal(10, 1, 2, 9)
|
||||
regal.Regal(11, 1, 3, 8)
|
||||
regal.Regal(12, 1, 3, 9)
|
||||
|
||||
# idRegału, Długość regału podana w kratkach, Współrzędne od których ma być tworzony regał (wiersz,kolumna) - poziomo
|
||||
# Współrzędne od (1,1) do (10,10)
|
||||
regal.Regal(1, 1, 2, 2)
|
||||
regal.Regal(2, 1, 2, 3)
|
||||
regal.Regal(3, 1, 3, 2)
|
||||
regal.Regal(4, 1, 3, 3)
|
||||
regal.Regal(13, 1, 8, 8)
|
||||
regal.Regal(14, 1, 8, 9)
|
||||
regal.Regal(15, 1, 9, 8)
|
||||
regal.Regal(16, 1, 9, 9)
|
||||
|
||||
regal.Regal(5, 1, 8, 2)
|
||||
regal.Regal(6, 1, 8, 3)
|
||||
regal.Regal(7, 1, 9, 2)
|
||||
regal.Regal(8, 1, 9, 3)
|
||||
wozek.draw()
|
||||
|
||||
regal.Regal(9, 1, 2, 8)
|
||||
regal.Regal(10, 1, 2, 9)
|
||||
regal.Regal(11, 1, 3, 8)
|
||||
regal.Regal(12, 1, 3, 9)
|
||||
|
||||
regal.Regal(13, 1, 8, 8)
|
||||
regal.Regal(14, 1, 8, 9)
|
||||
regal.Regal(15, 1, 9, 8)
|
||||
regal.Regal(16, 1, 9, 9)
|
||||
|
||||
wozek.draw()
|
||||
|
||||
pygame.display.flip() # updating frames
|
||||
pygame.display.flip() # updating frames
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
5
wozek.py
5
wozek.py
@ -1,7 +1,5 @@
|
||||
import pygame
|
||||
|
||||
|
||||
|
||||
# screen nie działa
|
||||
|
||||
class Wozek():
|
||||
@ -51,7 +49,8 @@ class Wozek():
|
||||
|
||||
def __zainicjuj_stan_poczatkowy(self):
|
||||
from wyszukiwanie import Stan
|
||||
self.obecnyStan = Stan(55, 55, 3)
|
||||
# self.obecnyStan = Stan(55, 55, 3)
|
||||
self.obecnyStan = Stan(0, 0, 3)
|
||||
|
||||
# def ustaw_wozek_w_kierunku(self, kierunek):
|
||||
# TODO
|
||||
|
@ -1,17 +1,25 @@
|
||||
class SearchSpace:
|
||||
def __init__(self):
|
||||
self.grid = [['1']*10 for _ in range(10)] # create a 10x10 grid of cells
|
||||
for r, c in [(2,2), (2,3), (3,2), (3,3), (8,2), (8,3), (9,2), (9,3),
|
||||
(2,8), (2,9), (3,8), (3,9), (8,8), (8,9), (9,8), (9,9)]:
|
||||
self.grid[r][c] = 'X' # set the cells with a shelf to not passable
|
||||
|
||||
def is_passable(self, x, y):
|
||||
return 0 <= x < 10 and 0 <= y < 10 and self.grid[y][x] != 'X'
|
||||
|
||||
class Stan:
|
||||
def __init__(self, x, y, kierunek):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.kierunek = kierunek
|
||||
|
||||
|
||||
class Wezel:
|
||||
def __init__(self, stan):
|
||||
self.stan = stan
|
||||
|
||||
|
||||
def poprzednik(wezel):
|
||||
# gora -> prawo -> dol -> lewo | obrot w prawo
|
||||
def poprzednik(wezel, search_space):
|
||||
# gora -> prawo -> dol -> lewo | obrot w prawo
|
||||
# gora -> lewo -> dol -> prawo | obrot w lewo
|
||||
# 0 gora 1 prawo 2 dol 3 lewo
|
||||
y = wezel.stan.x
|
||||
@ -37,23 +45,23 @@ def poprzednik(wezel):
|
||||
|
||||
return wezly
|
||||
|
||||
|
||||
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy):
|
||||
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy, search_space):
|
||||
pierwszy_wezel = Wezel(stan_poczatkowy)
|
||||
fringe = [pierwszy_wezel]
|
||||
|
||||
odwiedzone = list()
|
||||
|
||||
odwiedzone = set()
|
||||
|
||||
while fringe:
|
||||
# kolejka
|
||||
wezel = fringe.pop(0)
|
||||
|
||||
if stan_docelowy.x == wezel.stan.x and stan_docelowy.y == wezel.stan.y:
|
||||
return odwiedzone
|
||||
|
||||
odwiedzone.append(wezel)
|
||||
odwiedzone.add(wezel.stan)
|
||||
|
||||
for stan in poprzednik(wezel):
|
||||
if wezel not in fringe and stan not in odwiedzone:
|
||||
for stan in poprzednik(wezel, search_space):
|
||||
if stan not in odwiedzone:
|
||||
nowy_wezel = Wezel(stan)
|
||||
fringe.append(nowy_wezel)
|
||||
odwiedzone.add(stan)
|
||||
return odwiedzone
|
||||
|
Loading…
Reference in New Issue
Block a user