Merge pull request 'Made BFS1 and BFS3 algorithm in BFS.py file.' (#15) from algorytm_bfs2 into snake_move
Reviewed-on: #15 reviewed by Jakub Zaręba
This commit is contained in:
commit
a8c5b1b434
254
BFS.py
Normal file
254
BFS.py
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
import random
|
||||||
|
import Node
|
||||||
|
from displayControler import NUM_X, NUM_Y
|
||||||
|
|
||||||
|
|
||||||
|
def goalTest1(hIndex):
|
||||||
|
for i in list(hIndex.values()):
|
||||||
|
if i == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def succ1(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"] < 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"] < 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["zwrot"] == "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 check1(tab, state):
|
||||||
|
for i in tab:
|
||||||
|
if i.state == state:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def BFS1(istate):
|
||||||
|
|
||||||
|
|
||||||
|
fringe = []
|
||||||
|
explored = []
|
||||||
|
|
||||||
|
x = Node.Node(istate)
|
||||||
|
fringe.append(x)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
|
||||||
|
if fringe == []:
|
||||||
|
return False
|
||||||
|
|
||||||
|
elem = fringe.pop(0)
|
||||||
|
|
||||||
|
if goalTest1(elem.state["hydradeIndex"]):
|
||||||
|
x = elem
|
||||||
|
tab = []
|
||||||
|
while x.parent != None:
|
||||||
|
tab.append([x.parent, x.action])
|
||||||
|
x = x.parent
|
||||||
|
return tab
|
||||||
|
explored.append(elem)
|
||||||
|
|
||||||
|
for resp in succ1(elem.state):
|
||||||
|
if check1(fringe, resp[1]) and check1(explored, resp[1]):
|
||||||
|
x = Node.Node(resp[1])
|
||||||
|
x.parent = elem
|
||||||
|
x.action = resp[0]
|
||||||
|
fringe.append(x)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def goalTest2(state, goalTreassure):
|
||||||
|
if state["x"] == goalTreassure[0] and state["y"] == goalTreassure[1]:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def succ2(state):
|
||||||
|
resp = []
|
||||||
|
if state["direction"] == "N":
|
||||||
|
if state["y"] > 0:
|
||||||
|
resp.append(["forward", {'x': state["x"], 'y': state["y"]-1, 'direction': state["direction"]}])
|
||||||
|
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "E"}])
|
||||||
|
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "W"}])
|
||||||
|
elif state["direction"] == "S":
|
||||||
|
if state["y"] < NUM_Y:
|
||||||
|
resp.append(["forward", {'x': state["x"], 'y': state["y"]+1, 'direction': state["direction"]}])
|
||||||
|
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "W"}])
|
||||||
|
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "E"}])
|
||||||
|
elif state["direction"] == "E":
|
||||||
|
if state["x"] < NUM_X:
|
||||||
|
resp.append(["forward", {'x': state["x"]+1, 'y': state["y"], 'direction': state["direction"]}])
|
||||||
|
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "S"}])
|
||||||
|
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "N"}])
|
||||||
|
else: #state["zwrot"] == "W"
|
||||||
|
if state["x"] > 0:
|
||||||
|
resp.append(["forward", {'x': state["x"]-1, 'y': state["y"], 'direction': state["direction"]}])
|
||||||
|
resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "N"}])
|
||||||
|
resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "S"}])
|
||||||
|
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
def check2(tab, state):
|
||||||
|
for i in tab:
|
||||||
|
if i.state == state:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def BFS2(istate):
|
||||||
|
goalTreassuere = (random.randint(0,NUM_X-1), random.randint(0,NUM_Y-1))
|
||||||
|
print(goalTreassuere)
|
||||||
|
fringe = []
|
||||||
|
explored = []
|
||||||
|
|
||||||
|
x = Node.Node(istate)
|
||||||
|
fringe.append(x)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
if fringe == []:
|
||||||
|
return False
|
||||||
|
|
||||||
|
elem = fringe.pop(0)
|
||||||
|
|
||||||
|
if goalTest2(elem.state, goalTreassuere):
|
||||||
|
x = elem
|
||||||
|
tab = []
|
||||||
|
while x.parent != None:
|
||||||
|
tab.append([x.parent, x.action])
|
||||||
|
x = x.parent
|
||||||
|
return tab
|
||||||
|
|
||||||
|
explored.append(elem)
|
||||||
|
|
||||||
|
for resp in succ2(elem.state):
|
||||||
|
if check2(fringe, resp[1]) and check2(explored, resp[1]):
|
||||||
|
x = Node.Node(resp[1])
|
||||||
|
x.parent = elem
|
||||||
|
x.action = resp[0]
|
||||||
|
fringe.append(x)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
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"]):
|
||||||
|
x = elem
|
||||||
|
tab = []
|
||||||
|
while x.parent != None:
|
||||||
|
tab.append(x.action)
|
||||||
|
x = x.parent
|
||||||
|
return tab
|
||||||
|
|
||||||
|
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)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
95
Tractor.py
95
Tractor.py
@ -8,90 +8,6 @@ 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"]):
|
|
||||||
x = elem
|
|
||||||
tab = []
|
|
||||||
while x.parent != None:
|
|
||||||
tab.append(x.action)
|
|
||||||
x = x.parent
|
|
||||||
return tab
|
|
||||||
|
|
||||||
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'
|
||||||
DIRECTION_SOUTH = 'S'
|
DIRECTION_SOUTH = 'S'
|
||||||
@ -198,7 +114,9 @@ class Tractor:
|
|||||||
def snake_move(self,pole,x,y):
|
def snake_move(self,pole,x,y):
|
||||||
next_slot_coordinates=(x,y)
|
next_slot_coordinates=(x,y)
|
||||||
if(self.do_move_if_valid(pole,next_slot_coordinates)):
|
if(self.do_move_if_valid(pole,next_slot_coordinates)):
|
||||||
if pole.get_slot_from_cord((x,y)).get_hydrate_stats() < 60:
|
if x == 0 and y == 0:
|
||||||
|
hydrateIndex = -1
|
||||||
|
elif pole.get_slot_from_cord((x,y)).get_hydrate_stats() < 60:
|
||||||
hydrateIndex = 0
|
hydrateIndex = 0
|
||||||
else:
|
else:
|
||||||
hydrateIndex = 1
|
hydrateIndex = 1
|
||||||
@ -207,14 +125,15 @@ class Tractor:
|
|||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
def move_by_root(self, root, pole, actions = None):
|
def move_by_root(self, root, pole, actions = None):
|
||||||
for move in root:
|
for move in root:
|
||||||
self.slot.redraw_image()
|
self.slot.redraw_image()
|
||||||
if move == 'forward':
|
if move[1] == 'forward':
|
||||||
self.move_forward(pole)
|
self.move_forward(pole)
|
||||||
if move == 'right':
|
if move[1] == 'right':
|
||||||
self.turn_right()
|
self.turn_right()
|
||||||
if move == 'left':
|
if move[1] == 'left':
|
||||||
self.turn_left()
|
self.turn_left()
|
||||||
for a in actions:
|
for a in actions:
|
||||||
a()
|
a()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
CUBE_SIZE = 64
|
CUBE_SIZE = 64
|
||||||
NUM_X = 5
|
NUM_X = 6
|
||||||
NUM_Y = 3
|
NUM_Y = 3
|
||||||
|
|
||||||
#returns true if tractor can move to specified slot
|
#returns true if tractor can move to specified slot
|
||||||
|
Loading…
Reference in New Issue
Block a user