bfs works, bfs_main.py

This commit is contained in:
s473561 2023-05-06 14:59:23 +02:00
parent 0268776045
commit a58d49e94d
7 changed files with 169 additions and 353 deletions

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()

214
main.py
View File

@ -1,12 +1,8 @@
import sys import sys
import time
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 Stan, SearchSpace, Search, GridCellType
from typing import Tuple, Dict
pygame.init() pygame.init()
screen = pygame.display.set_mode((980, 980)) screen = pygame.display.set_mode((980, 980))
@ -16,156 +12,84 @@ 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): def draw(self):
# screen.blit(self.image, (self.state.x, self.y)) screen.blit(self.image, (self.x, self.y))
def main(): def main():
wozek = Wozek()
wozek = Wozek(0,0) while True:
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)
# for event in pygame.event.get(): if event.type == pygame.KEYDOWN:
# if event.type == pygame.QUIT: if event.key == pygame.K_DOWN:
# sys.exit(0) wozek.y_change = 1
# if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: if event.key == pygame.K_UP:
# sys.exit(0) wozek.y_change = -1
if event.key == pygame.K_RIGHT:
wozek.x_change = 1
if event.key == pygame.K_LEFT:
wozek.x_change = -1
grid_points = SearchSpace() if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
goal_state = Stan(5, 2, 0) # for example wozek.y_change = 0
# perform BFS search if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
path = Search(grid_points).wyszukiwanie_bfs(wozek.state, goal_state) wozek.x_change = 0
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()
pygame.display.flip()
time.sleep(1)
# wozek._drive(path)
# print(path)
# for action in path:
# x = wozek.state.x
# y = wozek.state.y
# direction = wozek.state.kierunek
# print(x)
# if action == "Left":
# wozek.state.kierunek = 3 if direction == 0 else direction - 1
# # always legal
# elif action == "Right":
# # self._truck_direction(direction, action)
# wozek.state.kierunek = (direction + 1) % 4
# # check if its legal
# elif action == "Forward":
# t_x = x + 1 if direction == 1 else (x - 1 if direction == 3 else x)
# t_y = y - 1 if direction == 0 else (y + 1 if direction == 2 else y)
# # if self.points_grid[t_y][t_x].is_available:
# wozek.state.x = t_x
# wozek.state.y = t_y
# 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()
# pygame.display.flip()
# for p in path:
# print(p.x, p.y)
# print("Path found:", path)
# else:
# print("No path found.")
# 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)
# # Drawing
# screen.fill((51,51,51)) # removes object trail
# screen.blit(miejsce, (430, 400))
# # 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)
# regal.Regal(9, 1, 2, 8) wozek.x += wozek.x_change
# regal.Regal(10, 1, 2, 9) wozek.y += wozek.y_change
# regal.Regal(11, 1, 3, 8)
# regal.Regal(12, 1, 3, 9)
# regal.Regal(13, 1, 8, 8) if wozek.x <= 0:
# regal.Regal(14, 1, 8, 9) wozek.x = 0
# regal.Regal(15, 1, 9, 8) elif wozek.x >= 916:
# regal.Regal(16, 1, 9, 9) wozek.x = 916
if wozek.y <= 0:
wozek.y = 0
elif wozek.x >= 916:
wozek.x = 916
# wozek.draw() # Drawing
screen.fill((51,51,51)) # removes object trail
screen.blit(miejsce, (430, 400))
# pygame.display.flip() # updating frames # 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)
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)
draw(wozek)
if wozek.ln == 0:
for x in packageList.list:
draw(x)
#demo_paczka.narysuj(430,400,screen)
pygame.display.flip() # updating frames
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -12,8 +12,8 @@ class Wozek():
def __init__(self, y, x): 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")
@ -26,6 +26,11 @@ class Wozek():
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.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.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*98, self.state.y*98)) screen.blit(self.image, (self.state.x*98, self.state.y*98))
@ -61,8 +66,8 @@ class Wozek():
# from wyszukiwanie import Stan # from wyszukiwanie import Stan
# # self.obecnyStan = Stan(55, 55, 3) # # self.obecnyStan = Stan(55, 55, 3)
# self.obecnyStan = Stan(1, 1, 3) # self.obecnyStan = Stan(1, 1, 3)
# def _drive(self, actions):
# def _drive(self, actions):
# # for action in actions.reverse(): # # for action in actions.reverse():
# for action in actions: # for action in actions:
# self._move(action) # self._move(action)
@ -71,37 +76,25 @@ class Wozek():
# # self.screen.fill((51,51,51)) # removes object trail # # self.screen.fill((51,51,51)) # removes object trail
# # self.screen.blit(self.miejsce, (430, 400)) # # self.screen.blit(self.miejsce, (430, 400))
# self.draw() # self.draw()
# pygame.display.flip() # pygame.display.flip()
def _move(self, action): def _move(self, action):
x = self.state.x x = self.state.x
y = self.state.y y = self.state.y
direction = self.state.kierunek kierunek = self.state.kierunek
# always legal if action == "Lewo":
if action == "Left": self.state.kierunek = 3 if kierunek == 0 else kierunek - 1
# self._truck_direction(direction, action) elif action == "Prawo":
self.state.kierunek = 3 if direction == 0 else direction - 1 self.state.kierunek = (kierunek + 1) % 4
# always legal
elif action == "Right":
# self._truck_direction(direction, action)
self.state.kierunek = (direction + 1) % 4
# check if its legal
elif action == "Forward":
t_x = x + 1 if direction == 1 else (x - 1 if direction == 3 else x)
t_y = y - 1 if direction == 0 else (y + 1 if direction == 2 else y)
# if self.points_grid[t_y][t_x].is_available:
self.state.x = t_x
self.state.y = t_y
# else:
# print("[ MOVE LOG ] - You can't move in that direction!")
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

@ -3,7 +3,7 @@ from enum import Enum
from typing import Tuple, Dict from typing import Tuple, Dict
class GridCellType(Enum): class GridCellType(Enum):
NOTHING = 0 FREE = 0
REGAL = 1 REGAL = 1
# dodać oznaczenie na miejsce dla paczek # dodać oznaczenie na miejsce dla paczek
@ -16,7 +16,7 @@ class SearchSpace:
def _init_grid(self) -> None: def _init_grid(self) -> None:
for i in range (0,10): for i in range (0,10):
for j in range(0,10): for j in range(0,10):
self.grid[(i, j)] = GridCellType.NOTHING 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)] = GridCellType.REGAL self.grid[(r,c)] = GridCellType.REGAL
@ -29,14 +29,14 @@ class Stan:
class Wezel: class Wezel:
def __init__(self, stan: Stan, action = None, parent = None): def __init__(self, stan: Stan, akcja = None, parent = None):
self.stan = stan self.stan = stan
self.action = action self.akcja = akcja
self.parent = parent self.parent = parent
class Search: class Search:
def __init__(self, points_grid: SearchSpace): def __init__(self, search_grid: SearchSpace):
self.points_grid = points_grid self.search_grid = search_grid
def wyszukiwanie_bfs(self, stan_poczatkowy: Stan, stan_docelowy: Stan): def wyszukiwanie_bfs(self, stan_poczatkowy: Stan, stan_docelowy: Stan):
pierwszy_wezel = Wezel(stan_poczatkowy) pierwszy_wezel = Wezel(stan_poczatkowy)
@ -52,9 +52,9 @@ class Search:
odwiedzone.append(wezel.stan) odwiedzone.append(wezel.stan)
for action, stan in self.nastepnik(wezel): for akcja, stan in self.nastepnik(wezel):
if wezel not in fringe and stan not in odwiedzone: if wezel not in fringe and stan not in odwiedzone:
x_node = Wezel(stan, action, wezel) x_node = Wezel(stan, akcja, wezel)
fringe.append(x_node) fringe.append(x_node)
return False return False
@ -69,23 +69,17 @@ class Search:
# 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
state = wezel.stan state = wezel.stan
right_state = Stan( state.x, state.y,(state.kierunek + 1) % 4)
right_node = ["Right", right_state] stan_prawego = Stan(state.x, state.y,(state.kierunek + 1) % 4)
ruch_w_prawo = ["Prawo", stan_prawego]
# 90 degree left state - cost of turn is 1 stan_lewego = Stan(state.x, state.y, 3 if state.kierunek == 0 else state.kierunek - 1)
left_state = Stan (state.x, state.y,3 if state.kierunek == 0 else state.kierunek - 1) ruch_w_lewo = ["Lewo", stan_lewego]
# storing temporarily node as list nodes = [ruch_w_prawo, ruch_w_lewo]
left_node = ["Left", left_state]
# always two nodes are possible because we can turn in both sides
nodes = [right_node, left_node]
y = state.x y = state.x
x = state.y x = state.y
# obrot_w_prawo = Stan(x, y, (wezel.stan.kierunek + 1) % 4)
# obrot_w_lewo = Stan(x, y, (wezel.stan.kierunek - 1) % 4)
if wezel.stan.kierunek == 0: if wezel.stan.kierunek == 0:
y -= 1 y -= 1
@ -96,187 +90,36 @@ class Search:
elif wezel.stan.kierunek == 3: elif wezel.stan.kierunek == 3:
x -= 1 x -= 1
place = None place = None
if 0 <= y < 98 and 0 <= x < 98: if 0 <= y < 98 and 0 <= x < 98:
place = self.points_grid.grid[(y,x)] place = self.search_grid.grid[(x,y)]
if place is not None and place is GridCellType.NOTHING: if place is not None and place is GridCellType.FREE:
ruch_w_przod = Stan(y, x, wezel.stan.kierunek) stan_w_przod = Stan(y, x, wezel.stan.kierunek)
forward_node = ["Forward", ruch_w_przod] ruch_w_przod = ["Do przodu", stan_w_przod]
nodes.append(forward_node) nodes.append(ruch_w_przod)
# sprawdzenie czy nie wyjdzie poza plansze
# if 0 <= x <= 916 and 0 <= y <= 916:
# wezly.append(ruch_w_przod)
return nodes return nodes
def get_actions(self, wezel: Wezel): def get_actions(self, wezel: Wezel):
actions = [] akcje = []
moves_forward = 0 moves_forward = 0
turns = 0 turns = 0
parent = wezel parent = wezel
while True: while True:
action = parent.action akcja = parent.akcja
parent = parent.parent parent = parent.parent
if action is None:
if akcja is None:
break break
if action == "Forward": if akcja == "Forward":
moves_forward = moves_forward + 1 moves_forward = moves_forward + 1
else: else:
turns = turns + 1 turns = turns + 1
actions.append(action) akcje.append(akcja)
akcje.reverse()
actions.reverse() return akcje
return actions
# class Kierunek(Enum):
# POLNOC = 0
# POLUDNIE = 1
# ZACHOD = 2
# WSCHOD = 3
# def kierunekNaLewo(self):
# if self == Kierunek.POLNOC:
# return Kierunek.ZACHOD
# elif self == Kierunek.POLUDNIE:
# return Kierunek.WSCHOD
# elif self == Kierunek.ZACHOD:
# return Kierunek.POLUDNIE
# elif self == Kierunek.WSCHOD:
# return Kierunek.POLNOC
# def kierunekNaPrawo(self):
# if self == Kierunek.POLNOC:
# return Kierunek.WSCHOD
# elif self == Kierunek.POLUDNIE:
# return Kierunek.ZACHOD
# elif self == Kierunek.ZACHOD:
# return Kierunek.POLNOC
# elif self == Kierunek.WSCHOD:
# return Kierunek.POLUDNIE
# class Akcja(Enum):
# OBROT_W_LEWO = 0
# OBROT_W_PRAWO = 1
# KROK_W_PRZOD = 2
# class Stan:
# def __init__(self, kierunek: Kierunek, x, y):
# self.x = x
# self.y = y
# self.kierunek = kierunek
# def skopiuj(self):
# return Stan(self.x, self.y, self.kierunek)
# class Nastepnik:
# def __init__(self, akcja: Akcja or None, stan: Stan, poprzednik):
# self.akcja = akcja
# self.stan = stan
# self._poprzednik = poprzednik
# def getPoprzednik(self):
# return self._poprzednik
# def setPoprzednik(self, x):
# raise Exception
# poprzednik = property(getPoprzednik, setPoprzednik)
# def skopiuj(self):
# return Nastepnik(self.akcja, self.stan.skopiuj(), self.poprzednik)
# def goaltest(stan: Stan, cel: Stan):
# if stan.x != cel.x:
# return False
# elif stan.y != cel.y:
# return False
# else:
# return True
# def stos_akcji(stan_koncowy: Nastepnik):
# stos = deque()
# while stan_koncowy.poprzednik is not None:
# stos.append(stan_koncowy.akcja)
# stan_koncowy = stan_koncowy.poprzednik
# return stos
# def stan_w_liscie_nastepnikow(stan: Stan, lista_nastepnikow):
# for i in lista_nastepnikow:
# if i.stan.kierunek != stan.kierunek:
# continue
# elif i.stan.x != stan.x:
# continue
# elif i.stan.y != stan.y:
# continue
# else:
# return True
# return False
# def nastepnik_kroku_w_przod(nastepnik: Nastepnik):
# akcja = Akcja.KROK_W_PRZOD
# stan = Stan(nastepnik.stan.kierunek, nastepnik.stan.x, nastepnik.stan.y)
# if stan.kierunek == Kierunek.POLNOC:
# stan.x -= 1
# elif stan.kierunek == Kierunek.POLUDNIE:
# stan.x += 1
# elif stan.kierunek == Kierunek.ZACHOD:
# stan.y -= 1
# elif stan.kierunek == Kierunek.WSCHOD:
# stan.y += 1
# return Nastepnik(akcja, stan, nastepnik)
# def nastepnik_obrotu_w_lewo(nastepnik: Nastepnik):
# akcja = Akcja.OBROT_W_LEWO
# stan = Stan(nastepnik.stan.kierunek.kierunekNaLewo(), nastepnik.stan.x, nastepnik.stan.y)
# return Nastepnik(akcja, stan, nastepnik)
# #Nastepnik(Akcja.OBROT_W_LEWO, Stan(nastepnik.stan.kierunek.kierunekNaLewo(), nastepnik.stan.poleStartoweGorne), nastepnik)
# #
# def nastepnik_obrotu_w_prawo(nastepnik: Nastepnik):
# akcja = Akcja.OBROT_W_PRAWO
# stan = Stan(nastepnik.stan.kierunek.kierunekNaPrawo(),nastepnik.stan.x, nastepnik.stan.y)
# return Nastepnik(akcja, stan, nastepnik)
# def succ(nastepnik: Nastepnik):
# wynik = []
# pom = nastepnik.skopiuj()
# #wynik.append(nastepnik_obrotu_w_lewo(pom))
# wynik.append(nastepnik_obrotu_w_lewo(pom))
# pom = nastepnik.skopiuj()
# wynik.append(nastepnik_obrotu_w_prawo(pom))
# pom = nastepnik.skopiuj()
# wynik.append(nastepnik_kroku_w_przod(pom))
# return wynik
# def graphsearch(istate: Stan, cel: Stan):
# fringe = deque()
# explored = []
# fringe.append(Nastepnik(None, istate, None))
# while fringe:
# # for i in fringe:
# # print("F",i.stan.kierunek,i.stan.poleStartoweGorne.wiersz,i.stan.poleStartoweGorne.kolumna,end=" ")
# # print()
# element: Nastepnik = fringe.popleft()
# if goaltest(element.stan, cel):
# return stos_akcji(element)
# explored.append(element)
# for nastepnik in succ(element):
# if not stan_w_liscie_nastepnikow(nastepnik, fringe) and not stan_w_liscie_nastepnikow(nastepnik, explored):
# fringe.append(nastepnik)
# return False