Compare commits

...

3 Commits

Author SHA1 Message Date
a58d49e94d bfs works, bfs_main.py 2023-05-06 14:59:23 +02:00
0268776045 updated bfs - works 2023-05-06 12:57:48 +02:00
94d7716728 update 2023-05-04 18:42:17 +02:00
11 changed files with 274 additions and 108 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

56
bfs_main.py Normal file
View File

@ -0,0 +1,56 @@
import sys
import time
import pygame
import regal
import paczka
from wozek import Wozek
from wyszukiwanie import Stan, SearchSpace, Search
pygame.init()
screen = pygame.display.set_mode((980, 980))
miejsce = pygame.image.load('images/miejsce_paczek.png')
pygame.display.set_caption("Inteligentny wozek")
icon = pygame.image.load('images/icon.png')
pygame.display.set_icon(icon)
def main():
wozek = Wozek(0,0)
grid_points = SearchSpace()
goal_state = Stan(5, 2, 0)
path = Search(grid_points).wyszukiwanie_bfs(wozek.state, goal_state)
if path:
for p in path:
wozek._move(p)
screen.fill((51,51,51)) # removes object trail
screen.blit(miejsce, (430, 400))
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)
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_bfs()
pygame.display.flip()
time.sleep(1)
if __name__ == "__main__":
main()

62
main.py
View File

@ -1,10 +1,8 @@
import sys import sys
import pygame import pygame
import regal import regal
import paczka from paczka import Paczka
from wozek import Wozek from wozek import Wozek
from wyszukiwanie import wyszukiwanie_bfs, Stan, SearchSpace
pygame.init() pygame.init()
screen = pygame.display.set_mode((980, 980)) screen = pygame.display.set_mode((980, 980))
@ -14,9 +12,14 @@ pygame.display.set_caption("Inteligentny wozek")
icon = pygame.image.load('images/icon.png') icon = pygame.image.load('images/icon.png')
pygame.display.set_icon(icon) pygame.display.set_icon(icon)
def draw(self):
screen.blit(self.image, (self.x, self.y))
def main(): def main():
wozek = Wozek() wozek = Wozek()
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -24,32 +27,34 @@ def main():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0) sys.exit(0)
grid = SearchSpace() if event.type == pygame.KEYDOWN:
# create start and goal states if event.key == pygame.K_DOWN:
start_state = Stan(x=0, y=0, kierunek=0) # for example wozek.y_change = 1
goal_state = Stan(x=0, y=1, kierunek=0) # for example 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
# perform BFS search if event.type == pygame.KEYUP:
path = wyszukiwanie_bfs(start_state, goal_state, grid) 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
# 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.")
for state in path: wozek.x += wozek.x_change
pygame.time.delay(200) wozek.y += wozek.y_change
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 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
# Drawing # Drawing
screen.fill((51,51,51)) # removes object trail screen.fill((51,51,51)) # removes object trail
@ -77,11 +82,14 @@ def main():
regal.Regal(15, 1, 9, 8) regal.Regal(15, 1, 9, 8)
regal.Regal(16, 1, 9, 9) regal.Regal(16, 1, 9, 9)
wozek.draw() draw(wozek)
if wozek.ln == 0:
for x in packageList.list:
draw(x)
#demo_paczka.narysuj(430,400,screen)
pygame.display.flip() # updating frames pygame.display.flip() # updating frames
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,24 +1,39 @@
import time
from typing import Tuple
import pygame import pygame
from wyszukiwanie import Stan, SearchSpace
# from main import screen, miejsce
# screen nie działa # screen nie działa
class Wozek(): class Wozek():
def __init__(self): def __init__(self, y, x):
self.x = 55 # self.x = 55
self.y = 55 # self.y = 55
self.x_change = 0 self.x_change = 0
self.y_change = 0 self.y_change = 0
self.height = 64 self.height = 64
self.width = 64 self.width = 64
self.image = pygame.image.load("images/pusty_wozek.png") self.image = pygame.image.load("images/pusty_wozek.png")
# self.screen = pygame.display.set_mode((980, 980))
# self.miejsce = pygame.image.load('images/miejsce_paczek.png')
# Credit: Forklift icons created by Smashicons - Flaticon # Credit: Forklift icons created by Smashicons - Flaticon
# https://www.flaticon.com/free-icons/forklift # https://www.flaticon.com/free-icons/forklift
self.__zainicjuj_stan_poczatkowy() #self.__zainicjuj_stan_poczatkowy()
self.state = Stan(y, x, 0)
def draw(self): def draw(self):
from main import screen from main import screen
# screen.blit(self.image, (self.x, self.y)) # screen.blit(self.image, (self.x, self.y))
screen.blit(self.image, (self.obecnyStan.x, self.obecnyStan.y)) #screen.blit(self.image, (self.obecnyStan.x, self.obecnyStan.y))
screen.blit(self.image, (self.state.x, self.state.y))
def draw_bfs(self):
from main import screen
# screen.blit(self.image, (self.x, self.y))
#screen.blit(self.image, (self.obecnyStan.x, self.obecnyStan.y))
screen.blit(self.image, (self.state.x*98, self.state.y*98))
# storage = ["none"] * 10 # storage = ["none"] * 10
storage = [] storage = []
@ -47,10 +62,39 @@ class Wozek():
elif (len(storage) == 10): elif (len(storage) == 10):
wozek.image = pygame.image.load("images/pelny_wozek_full_3_crates.png") wozek.image = pygame.image.load("images/pelny_wozek_full_3_crates.png")
def __zainicjuj_stan_poczatkowy(self): # def __zainicjuj_stan_poczatkowy(self):
from wyszukiwanie import Stan # from wyszukiwanie import Stan
# self.obecnyStan = Stan(55, 55, 3) # # self.obecnyStan = Stan(55, 55, 3)
self.obecnyStan = Stan(0, 0, 3) # self.obecnyStan = Stan(1, 1, 3)
# def _drive(self, actions):
# # for action in actions.reverse():
# for action in actions:
# self._move(action)
# pygame.event.pump()
# time.sleep(0.01)
# # self.screen.fill((51,51,51)) # removes object trail
# # self.screen.blit(self.miejsce, (430, 400))
# self.draw()
# pygame.display.flip()
def _move(self, action):
x = self.state.x
y = self.state.y
kierunek = self.state.kierunek
if action == "Lewo":
self.state.kierunek = 3 if kierunek == 0 else kierunek - 1
elif action == "Prawo":
self.state.kierunek = (kierunek + 1) % 4
elif action == "Do przodu":
new_x = x + 1 if kierunek == 1 else (x - 1 if kierunek == 3 else x)
new_y = y - 1 if kierunek == 0 else (y + 1 if kierunek == 2 else y)
self.state.x = new_x
self.state.y = new_y
# def ustaw_wozek_w_kierunku(self, kierunek): # def ustaw_wozek_w_kierunku(self, kierunek):
# TODO # TODO

View File

@ -1,12 +1,25 @@
from enum import Enum
from typing import Tuple, Dict
class GridCellType(Enum):
FREE = 0
REGAL = 1
# dodać oznaczenie na miejsce dla paczek
class SearchSpace: class SearchSpace:
def __init__(self): grid: Dict[Tuple[int, int], GridCellType] = {}
self.grid = [['1']*10 for _ in range(10)] # create a 10x10 grid of cells
def __init__(self) -> None:
self._init_grid()
def _init_grid(self) -> None:
for i in range (0,10):
for j in range(0,10):
self.grid[(i, j)] = GridCellType.FREE
for r, c in [(2,2), (2,3), (3,2), (3,3), (8,2), (8,3), (9,2), (9,3), 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)]: (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 self.grid[(r,c)] = GridCellType.REGAL
def is_passable(self, x, y):
return 0 <= x < 10 and 0 <= y < 10 and self.grid[y][x] != 'X'
class Stan: class Stan:
def __init__(self, x, y, kierunek): def __init__(self, x, y, kierunek):
@ -14,18 +27,59 @@ class Stan:
self.y = y self.y = y
self.kierunek = kierunek self.kierunek = kierunek
class Wezel:
def __init__(self, stan):
self.stan = stan
def poprzednik(wezel, search_space): class Wezel:
def __init__(self, stan: Stan, akcja = None, parent = None):
self.stan = stan
self.akcja = akcja
self.parent = parent
class Search:
def __init__(self, search_grid: SearchSpace):
self.search_grid = search_grid
def wyszukiwanie_bfs(self, stan_poczatkowy: Stan, stan_docelowy: Stan):
pierwszy_wezel = Wezel(stan_poczatkowy)
fringe = [pierwszy_wezel]
odwiedzone = []
while fringe:
wezel = fringe.pop(0)
if self.goal_test(state = wezel.stan, goal_state = stan_docelowy):
return self.get_actions(wezel)
odwiedzone.append(wezel.stan)
for akcja, stan in self.nastepnik(wezel):
if wezel not in fringe and stan not in odwiedzone:
x_node = Wezel(stan, akcja, wezel)
fringe.append(x_node)
return False
def goal_test(self, state: Stan, goal_state: Stan):
if state.x == goal_state.x and state.y == goal_state.y:
return True
else:
return False
def nastepnik(self, wezel: Wezel):
# gora -> prawo -> dol -> lewo | obrot w prawo # gora -> prawo -> dol -> lewo | obrot w prawo
# gora -> lewo -> dol -> prawo | obrot w lewo # gora -> lewo -> dol -> prawo | obrot w lewo
# 0 gora 1 prawo 2 dol 3 lewo # 0 gora 1 prawo 2 dol 3 lewo
y = wezel.stan.x state = wezel.stan
x = wezel.stan.y
obrot_w_prawo = Stan(x, y, (wezel.stan.kierunek + 1) % 4) stan_prawego = Stan(state.x, state.y,(state.kierunek + 1) % 4)
obrot_w_lewo = Stan(x, y, (wezel.stan.kierunek - 1) % 4) ruch_w_prawo = ["Prawo", stan_prawego]
stan_lewego = Stan(state.x, state.y, 3 if state.kierunek == 0 else state.kierunek - 1)
ruch_w_lewo = ["Lewo", stan_lewego]
nodes = [ruch_w_prawo, ruch_w_lewo]
y = state.x
x = state.y
if wezel.stan.kierunek == 0: if wezel.stan.kierunek == 0:
y -= 1 y -= 1
@ -36,32 +90,36 @@ def poprzednik(wezel, search_space):
elif wezel.stan.kierunek == 3: elif wezel.stan.kierunek == 3:
x -= 1 x -= 1
wezly = [obrot_w_prawo, obrot_w_lewo] place = None
ruch_w_przod = Stan(x, y, wezel.stan.kierunek)
# sprawdzenie czy nie wyjdzie poza plansze if 0 <= y < 98 and 0 <= x < 98:
if 0 <= x <= 916 and 0 <= y <= 916: place = self.search_grid.grid[(x,y)]
wezly.append(ruch_w_przod)
return wezly if place is not None and place is GridCellType.FREE:
stan_w_przod = Stan(y, x, wezel.stan.kierunek)
ruch_w_przod = ["Do przodu", stan_w_przod]
nodes.append(ruch_w_przod)
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy, search_space): return nodes
pierwszy_wezel = Wezel(stan_poczatkowy)
fringe = [pierwszy_wezel]
odwiedzone = set() def get_actions(self, wezel: Wezel):
akcje = []
moves_forward = 0
turns = 0
parent = wezel
while True:
akcja = parent.akcja
parent = parent.parent
while fringe: if akcja is None:
wezel = fringe.pop(0) break
if stan_docelowy.x == wezel.stan.x and stan_docelowy.y == wezel.stan.y: if akcja == "Forward":
return odwiedzone moves_forward = moves_forward + 1
else:
turns = turns + 1
odwiedzone.add(wezel.stan) akcje.append(akcja)
akcje.reverse()
for stan in poprzednik(wezel, search_space): return akcje
if stan not in odwiedzone:
nowy_wezel = Wezel(stan)
fringe.append(nowy_wezel)
odwiedzone.add(stan)
return odwiedzone