Made BFS algorithm.
This commit is contained in:
parent
901c246681
commit
ce698aa14a
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
|
||||||
|
|
83
Tractor.py
83
Tractor.py
@ -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'
|
||||||
@ -101,3 +183,4 @@ class Tractor:
|
|||||||
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.")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user