diff --git a/searching_algorithms/bfs.py b/searching_algorithms/bfs.py index bb59dd7..a41b258 100644 --- a/searching_algorithms/bfs.py +++ b/searching_algorithms/bfs.py @@ -1,11 +1,11 @@ from __future__ import annotations -from typing import List, Set +from typing import List from project_constants import Direction, Action from minefield import Minefield # temporary goal for testing -GOAL = (2, 2) +GOAL = (9, 9) class State: @@ -14,12 +14,6 @@ class State: self.column = column self.direction = direction - # def __eq__(self, other): - # if not isinstance(other, State): - # # don't attempt to compare against unrelated types - # return NotImplemented - # return self.row == other.row and self.column == other.column and self.direction == other.direction - class Node: def __init__(self, state: State, parent: Node = None, action: Action = None): @@ -27,12 +21,6 @@ class Node: self.parent = parent self.action = action - # def __eq__(self, other): - # if not isinstance(other, Node): - # # don't attempt to compare against unrelated types - # return NotImplemented - # return self.state == other.state and self.parent == other.parent and self.action == other.action - def goal_test(state: State): if (state.row, state.column) == GOAL: @@ -65,8 +53,12 @@ def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] = if explored is None: explored = list() + explored_states = set() + fringe_states = set() + # root Node fringe.append(Node(initial_state)) + fringe_states.add((initial_state.row, initial_state.column, initial_state.direction)) while True: # fringe empty -> solution not found @@ -75,6 +67,7 @@ def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] = # get first element from fringe element = fringe.pop(0) + fringe_states.remove((element.state.row, element.state.column, element.state.direction)) # if solution was found, prepare and return actions sequence if goal_test(element.state): @@ -92,23 +85,21 @@ def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] = # add current node to explored (prevents infinite cycles) explored.append(element) + explored_states.add((element.state.row, element.state.column, element.state.direction)) # loop through every possible next action for successor in get_successors(element.state, minefield): - # either me or the pseudocode is dumb - # somebody has to verify it - fringe_states = [el.state for el in fringe] - explored_states = [el.state for el in explored] # make sure not to fall into a cycle - if successor[1] not in fringe_states and \ - successor[1] not in explored_states: - + successor_state = (successor[1].row, successor[1].column, successor[1].direction) + if successor_state not in fringe_states and \ + successor_state not in explored_states: # create new Node and add it at the end of fringe new_node = Node(state=successor[1], parent=element, action=successor[0]) fringe.append(new_node) + fringe_states.add((new_node.state.row, new_node.state.column, new_node.state.direction)) # TEMPORARY METHOD