Made BFS1 and BFS3 algorithm in BFS.py file.

This commit is contained in:
Mateusz Czajka 2024-04-14 21:39:04 +02:00
parent 066894d85c
commit 63229b2c60
4 changed files with 276 additions and 92 deletions

17
App.py

File diff suppressed because one or more lines are too long

254
BFS.py Normal file
View 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)
"""

View File

@ -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:
DIRECTION_NORTH = 'N'
DIRECTION_SOUTH = 'S'
@ -198,7 +114,9 @@ class Tractor:
def snake_move(self,pole,x,y):
next_slot_coordinates=(x,y)
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
else:
hydrateIndex = 1
@ -207,14 +125,15 @@ class Tractor:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
def move_by_root(self, root, pole, actions = None):
for move in root:
self.slot.redraw_image()
if move == 'forward':
if move[1] == 'forward':
self.move_forward(pole)
if move == 'right':
if move[1] == 'right':
self.turn_right()
if move == 'left':
if move[1] == 'left':
self.turn_left()
for a in actions:
a()

View File

@ -1,5 +1,5 @@
CUBE_SIZE = 64
NUM_X = 5
NUM_X = 6
NUM_Y = 3
#returns true if tractor can move to specified slot