# FRINGE - struktura danych przechowująca wierzchołki do odwiedzenia # EXPLORED - lista odwiedzonych stanów # istate - stan początkowy # Succ - funkcja następnika # Goaltest -test spełnienia celu def breadth_first_search(istate, goal, agent_direction): # COORDINATES OF A START PLACE fringe = [] explored = [] start = Node(istate, agent_direction) fringe.append(start) path = [] while True: if not fringe: return False elem = fringe.pop(0) if elem.state == goal: # return ciag akcji zbudowany z wykorzystaniem pol parent i action while elem.parent is not None: if type(elem.action) == list: elem.action.reverse() for each_action in elem.action: path.append(each_action) else: path.append(elem.action) elem = elem.parent path.reverse() return path explored.append(elem) for action, state, direction in elem.successor(): fringe_states = [] explored_states = [] for node in fringe: fringe_states.append(node.state) for node2 in explored: explored_states.append(node2.state) if state not in fringe_states and state not in explored_states: x = Node(state, direction) x.parent = elem x.action = action fringe.append(x) class Node: def __init__(self, state, agent_direction, action=None, parent=None): self.state = state self.action = action self.parent = parent self.agent_direction = agent_direction def successor(self): neighbours = [] x = self.state[0] y = self.state[1] if x < 8: # RIGHT NEIGHBOUR if self.agent_direction == "right": actions = "move" elif self.agent_direction == "left": actions = ["rotate_right", "rotate_right", "move"] elif self.agent_direction == "up": actions = ["rotate_right", "move"] elif self.agent_direction == "down": actions = ["rotate_left", "move"] neighbours.append((actions, (x + 1, y), "right")) if x > 0: # LEFT NEIGHBOUR if self.agent_direction == "right": actions = ["rotate_left", "rotate_left", "move"] elif self.agent_direction == "left": actions = "move" elif self.agent_direction == "up": actions = ["rotate_left", "move"] elif self.agent_direction == "down": actions = ["rotate_right", "move"] neighbours.append((actions, (x - 1, y), "left")) if y > 0: # UP NEIGHBOUR if self.agent_direction == "right": actions = ["rotate_left", "move"] elif self.agent_direction == "left": actions = ["rotate_right", "move"] elif self.agent_direction == "up": actions = "move" elif self.agent_direction == "down": actions = ["rotate_left", "rotate_left", "move"] neighbours.append((actions, (x, y - 1), "up")) if y < 8: # DOWN NEIGHBOUR if self.agent_direction == "right": actions = ["rotate_right", "move"] elif self.agent_direction == "left": actions = ["rotate_left", "move"] elif self.agent_direction == "up": actions = ["rotate_left", "rotate_left", "move"] elif self.agent_direction == "down": actions = "move" neighbours.append((actions, (x, y + 1), "down")) return neighbours