From 1b2a4a376bc659e9771ff907d9c5c748af1b0cfb Mon Sep 17 00:00:00 2001 From: Dawid Pylak Date: Thu, 7 Apr 2022 12:49:00 +0200 Subject: [PATCH] fixed bfs algorythm --- .idea/misc.xml | 2 +- main.py | 5 +++++ src/constants.py | 8 ++++---- src/utils/bfs.py | 32 +++++++++++++++++--------------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 3fc3916..d56657a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/main.py b/main.py index f65ce1d..adb4033 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ import pygame from src.world import World from src.tractor import Tractor from src.settings import Settings +from src.utils.bfs import BFSSearcher +from src.constants import Constants def main(): @@ -17,6 +19,9 @@ def main(): screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) pygame.display.set_caption('TRAKTOHOLIK') + search = BFSSearcher().search((8, 1), (6, 2), Constants.UP) + print(search) + run = True while run: clock.tick(settings.fps) diff --git a/src/constants.py b/src/constants.py index f4d1853..0097a54 100644 --- a/src/constants.py +++ b/src/constants.py @@ -2,9 +2,9 @@ class Constants: """ Class to represent all constants for the app """ MOVE = "move" - UP = "up" - DOWN = "down" - RIGHT = "right" - LEFT = "left" + UP = [0, 1] + DOWN = [0, -1] + RIGHT = [1, 0] + LEFT = [-1, 0] ROTATE_RIGHT = "rotate_right" ROTATE_LEFT = "rotate_left" diff --git a/src/utils/bfs.py b/src/utils/bfs.py index 06083c5..05e1e93 100644 --- a/src/utils/bfs.py +++ b/src/utils/bfs.py @@ -5,6 +5,7 @@ class Node: def __init__(self, current_x, current_y, agent_direction, action=None, parent=None): self.current_x = current_x self.current_y = current_y + self.state = tuple([self.current_x, self.current_y]) self.action = action self.parent = parent self.agent_direction = agent_direction @@ -39,7 +40,7 @@ class Node: neighbours.append((actions, (x - 1, y), C.LEFT)) - if y > 0: # upper neighbour + if y < 9: # upper neighbour if self.agent_direction == C.RIGHT: actions = [C.ROTATE_LEFT, C.MOVE] elif self.agent_direction == C.LEFT: @@ -49,9 +50,9 @@ class Node: elif self.agent_direction == C.DOWN: actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE] - neighbours.append((actions, (x, y - 1), C.UP)) + neighbours.append((actions, (x, y + 1), C.UP)) - if y < 9: # down neighbour + if y > 0: # down neighbour if self.agent_direction == C.RIGHT: actions = [C.ROTATE_RIGHT, C.MOVE] elif self.agent_direction == C.LEFT: @@ -61,10 +62,12 @@ class Node: elif self.agent_direction == C.DOWN: actions = C.MOVE - neighbours.append((actions, (x, y + 1), C.DOWN)) - + neighbours.append((actions, (x, y - 1), C.DOWN)) return neighbours + def __str__(self): + return self.state + ' ' + self.parent + class BFSSearcher: @@ -73,30 +76,27 @@ class BFSSearcher: self.explored_states = [] self.path = [] - def search(self, first_state: tuple, goal, agent_direction): + def search(self, first_state: tuple, goal: tuple, agent_direction: list): self.fringe.append(Node(first_state[0], first_state[1], agent_direction)) while True: if not self.fringe: return False - element = self.fringe.pop(0) + element: Node = self.fringe.pop(0) if element.state == goal: return self.__state_eq_goal_action(element) self.explored_states.append(element) - - for action, state, direction in element.succesor(): + for action, state, direction in element.successor(): fringe_states = [] explored_states = [] for node in self.fringe: fringe_states.append(node.state) for node in self.explored_states: - explored_states.append(node) - if state not in fringe_states and state not in explored_states: - x = Node(state[0], state[1], direction) - x.parent = element - x.action = action + explored_states.append(node.state) + if (state not in fringe_states) and (state not in explored_states): + x = Node(state[0], state[1], direction, action, element) self.fringe.append(x) def __state_eq_goal_action(self, current_element: Node): @@ -107,5 +107,7 @@ class BFSSearcher: self.path.append(action) else: self.path.append(current_element.action) + current_element = current_element.parent - return self.path.reverse() + self.path.reverse() + return self.path