From ce698aa14adc2fb3bba33f4f4f06af495478ab62 Mon Sep 17 00:00:00 2001 From: Mateusz Czajka Date: Fri, 12 Apr 2024 22:11:30 +0200 Subject: [PATCH] Made BFS algorithm. --- Node.py | 8 +++++ Tractor.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 Node.py diff --git a/Node.py b/Node.py new file mode 100644 index 0000000..cc88608 --- /dev/null +++ b/Node.py @@ -0,0 +1,8 @@ +class Node: + state = None #[{stan}] + parent = None #[Node] + action = None #[Forward/Right/Left] + + def __init__(self, state): + self.state = state + diff --git a/Tractor.py b/Tractor.py index 84710b9..b4f9598 100644 --- a/Tractor.py +++ b/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.") \ No newline at end of file + print("Brak akcji przypisanych do tego sprzętu.") +