Made BFS algorithm. #13
8
Node.py
Normal file
8
Node.py
Normal 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
|
||||
|
85
Tractor.py
85
Tractor.py
@ -4,6 +4,88 @@ import random
|
||||
import displayControler as dCon
|
||||
import Slot
|
||||
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:
|
||||
DIRECTION_NORTH = 'N'
|
||||
@ -100,4 +182,5 @@ class Tractor:
|
||||
for akcja in self.osprzet.akcje:
|
||||
print("- Typ:", akcja.typ)
|
||||
else:
|
||||
print("Brak akcji przypisanych do tego sprzętu.")
|
||||
print("Brak akcji przypisanych do tego sprzętu.")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user