BFS #17

Merged
s481834 merged 20 commits from BFS into master 2024-04-24 17:44:08 +02:00
2 changed files with 92 additions and 1 deletions
Showing only changes of commit ce698aa14a - Show all commits

8
Node.py Normal file
View File

@ -0,0 +1,8 @@
class Node:
state = None #[{stan}]
parent = None #[Node]
action = None #[Forward/Right/Left]
def __init__(self, state):
self.state = state

View File

@ -4,6 +4,88 @@ import random
import displayControler as dCon import displayControler as dCon
import Slot import Slot
import Osprzet import Osprzet
import Node
def goalTest(hIndex):
for i in list(hIndex.values()):
if i == 0:
return False
return True
def succ(state):
resp = []
hIndex = state["hydradeIndex"].copy()
if state["direction"] == "N":
if state["y"] > 0:
if hIndex[state["x"], state["y"]-1] == 0:
hIndex[state["x"], state["y"] - 1] = 1
resp.append(["forward", {'x': state["x"], 'y': state["y"]-1, 'direction': state["direction"], 'hydradeIndex': hIndex}])
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "E", 'hydradeIndex': state["hydradeIndex"].copy()}])
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "W", 'hydradeIndex': state["hydradeIndex"].copy()}])
elif state["direction"] == "S":
if state["y"] < dCon.NUM_Y-1:
if hIndex[state["x"], state["y"]+1] == 0:
hIndex[state["x"], state["y"] + 1] = 1
resp.append(["forward", {'x': state["x"], 'y': state["y"]+1, 'direction': state["direction"], 'hydradeIndex': hIndex}])
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "W", 'hydradeIndex': state["hydradeIndex"].copy()}])
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "E", 'hydradeIndex': state["hydradeIndex"].copy()}])
elif state["direction"] == "E":
if state["x"] < dCon.NUM_X-1:
if hIndex[state["x"]+1, state["y"]] == 0:
hIndex[state["x"] + 1, state["y"]] = 1
resp.append(["forward", {'x': state["x"]+1, 'y': state["y"], 'direction': state["direction"], 'hydradeIndex': hIndex}])
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "S", 'hydradeIndex': state["hydradeIndex"].copy()}])
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "N", 'hydradeIndex': state["hydradeIndex"].copy()}])
else: #state["direction"] == "W"
if state["x"] > 0:
if hIndex[state["x"]-1, state["y"]] == 0:
hIndex[state["x"] - 1, state["y"]] = 1
resp.append(["forward", {'x': state["x"]-1, 'y': state["y"], 'direction': state["direction"], 'hydradeIndex': hIndex}])
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "N", 'hydradeIndex': state["hydradeIndex"].copy()}])
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "S", 'hydradeIndex': state["hydradeIndex"].copy()}])
return resp
def check(tab, state):
for i in tab:
if i.state == state:
return False
return True
def BFS(istate):
fringe = []
explored = []
x = Node.Node(istate)
fringe.append(x)
while True:
if fringe == []:
return False
elem = fringe.pop(0)
if goalTest(elem.state["hydradeIndex"]):
return elem #TODO ciąg akcji zbudowany z wykorzystaniem pól parent i action
explored.append(elem)
for resp in succ(elem.state):
if check(fringe, resp[1]) and check(explored, resp[1]):
x = Node.Node(resp[1])
x.parent = elem
x.action = resp[0]
fringe.append(x)
class Tractor: class Tractor:
DIRECTION_NORTH = 'N' DIRECTION_NORTH = 'N'
@ -100,4 +182,5 @@ class Tractor:
for akcja in self.osprzet.akcje: for akcja in self.osprzet.akcje:
print("- Typ:", akcja.typ) print("- Typ:", akcja.typ)
else: else:
print("Brak akcji przypisanych do tego sprzętu.") print("Brak akcji przypisanych do tego sprzętu.")