2023-05-06 12:57:48 +02:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
from typing import Tuple, Dict
|
|
|
|
|
|
|
|
class GridCellType(Enum):
|
2023-05-06 14:59:23 +02:00
|
|
|
FREE = 0
|
2023-05-06 12:57:48 +02:00
|
|
|
REGAL = 1
|
|
|
|
# dodać oznaczenie na miejsce dla paczek
|
|
|
|
|
2023-04-20 23:16:34 +02:00
|
|
|
class SearchSpace:
|
2023-05-06 12:57:48 +02:00
|
|
|
grid: Dict[Tuple[int, int], GridCellType] = {}
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self._init_grid()
|
|
|
|
|
|
|
|
def _init_grid(self) -> None:
|
|
|
|
for i in range (0,10):
|
|
|
|
for j in range(0,10):
|
2023-05-06 14:59:23 +02:00
|
|
|
self.grid[(i, j)] = GridCellType.FREE
|
2023-04-20 23:16:34 +02:00
|
|
|
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)]:
|
2023-05-06 12:57:48 +02:00
|
|
|
self.grid[(r,c)] = GridCellType.REGAL
|
2023-04-20 23:16:34 +02:00
|
|
|
|
2023-04-20 20:54:58 +02:00
|
|
|
class Stan:
|
|
|
|
def __init__(self, x, y, kierunek):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.kierunek = kierunek
|
|
|
|
|
2023-05-06 12:57:48 +02:00
|
|
|
|
2023-04-20 20:54:58 +02:00
|
|
|
class Wezel:
|
2023-05-06 14:59:23 +02:00
|
|
|
def __init__(self, stan: Stan, akcja = None, parent = None):
|
2023-04-20 20:54:58 +02:00
|
|
|
self.stan = stan
|
2023-05-06 14:59:23 +02:00
|
|
|
self.akcja = akcja
|
2023-05-06 12:57:48 +02:00
|
|
|
self.parent = parent
|
|
|
|
|
|
|
|
class Search:
|
2023-05-06 14:59:23 +02:00
|
|
|
def __init__(self, search_grid: SearchSpace):
|
|
|
|
self.search_grid = search_grid
|
2023-05-06 12:57:48 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
for akcja, stan in self.nastepnik(wezel):
|
2023-05-06 12:57:48 +02:00
|
|
|
if wezel not in fringe and stan not in odwiedzone:
|
2023-05-06 14:59:23 +02:00
|
|
|
x_node = Wezel(stan, akcja, wezel)
|
2023-05-06 12:57:48 +02:00
|
|
|
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 -> lewo -> dol -> prawo | obrot w lewo
|
|
|
|
# 0 gora 1 prawo 2 dol 3 lewo
|
|
|
|
state = wezel.stan
|
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
stan_prawego = Stan(state.x, state.y,(state.kierunek + 1) % 4)
|
|
|
|
ruch_w_prawo = ["Prawo", stan_prawego]
|
2023-05-06 12:57:48 +02:00
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
stan_lewego = Stan(state.x, state.y, 3 if state.kierunek == 0 else state.kierunek - 1)
|
|
|
|
ruch_w_lewo = ["Lewo", stan_lewego]
|
2023-05-06 12:57:48 +02:00
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
nodes = [ruch_w_prawo, ruch_w_lewo]
|
2023-05-06 12:57:48 +02:00
|
|
|
|
|
|
|
y = state.x
|
|
|
|
x = state.y
|
|
|
|
|
|
|
|
if wezel.stan.kierunek == 0:
|
|
|
|
y -= 1
|
|
|
|
elif wezel.stan.kierunek == 1:
|
|
|
|
x += 1
|
|
|
|
elif wezel.stan.kierunek == 2:
|
|
|
|
y += 1
|
|
|
|
elif wezel.stan.kierunek == 3:
|
|
|
|
x -= 1
|
|
|
|
|
|
|
|
place = None
|
|
|
|
|
|
|
|
if 0 <= y < 98 and 0 <= x < 98:
|
2023-05-06 14:59:23 +02:00
|
|
|
place = self.search_grid.grid[(x,y)]
|
2023-05-06 12:57:48 +02:00
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
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)
|
2023-05-06 12:57:48 +02:00
|
|
|
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
def get_actions(self, wezel: Wezel):
|
2023-05-06 14:59:23 +02:00
|
|
|
akcje = []
|
2023-05-06 12:57:48 +02:00
|
|
|
moves_forward = 0
|
|
|
|
turns = 0
|
|
|
|
parent = wezel
|
|
|
|
while True:
|
2023-05-06 14:59:23 +02:00
|
|
|
akcja = parent.akcja
|
2023-05-06 12:57:48 +02:00
|
|
|
parent = parent.parent
|
2023-05-06 14:59:23 +02:00
|
|
|
|
|
|
|
if akcja is None:
|
2023-05-06 12:57:48 +02:00
|
|
|
break
|
2023-04-20 20:54:58 +02:00
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
if akcja == "Forward":
|
2023-05-06 12:57:48 +02:00
|
|
|
moves_forward = moves_forward + 1
|
|
|
|
else:
|
|
|
|
turns = turns + 1
|
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
akcje.append(akcja)
|
|
|
|
akcje.reverse()
|
2023-04-20 20:54:58 +02:00
|
|
|
|
2023-05-06 14:59:23 +02:00
|
|
|
return akcje
|