performance boost by Jakub Radowicz TM
This commit is contained in:
parent
fd6fabfc00
commit
28cb1c1343
@ -1,11 +1,11 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import List, Set
|
from typing import List
|
||||||
|
|
||||||
from project_constants import Direction, Action
|
from project_constants import Direction, Action
|
||||||
from minefield import Minefield
|
from minefield import Minefield
|
||||||
|
|
||||||
# temporary goal for testing
|
# temporary goal for testing
|
||||||
GOAL = (2, 2)
|
GOAL = (9, 9)
|
||||||
|
|
||||||
|
|
||||||
class State:
|
class State:
|
||||||
@ -14,12 +14,6 @@ class State:
|
|||||||
self.column = column
|
self.column = column
|
||||||
self.direction = direction
|
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:
|
class Node:
|
||||||
def __init__(self, state: State, parent: Node = None, action: Action = None):
|
def __init__(self, state: State, parent: Node = None, action: Action = None):
|
||||||
@ -27,12 +21,6 @@ class Node:
|
|||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.action = action
|
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):
|
def goal_test(state: State):
|
||||||
if (state.row, state.column) == GOAL:
|
if (state.row, state.column) == GOAL:
|
||||||
@ -65,8 +53,12 @@ def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] =
|
|||||||
if explored is None:
|
if explored is None:
|
||||||
explored = list()
|
explored = list()
|
||||||
|
|
||||||
|
explored_states = set()
|
||||||
|
fringe_states = set()
|
||||||
|
|
||||||
# root Node
|
# root Node
|
||||||
fringe.append(Node(initial_state))
|
fringe.append(Node(initial_state))
|
||||||
|
fringe_states.add((initial_state.row, initial_state.column, initial_state.direction))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# fringe empty -> solution not found
|
# fringe empty -> solution not found
|
||||||
@ -75,6 +67,7 @@ def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] =
|
|||||||
|
|
||||||
# get first element from fringe
|
# get first element from fringe
|
||||||
element = fringe.pop(0)
|
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 solution was found, prepare and return actions sequence
|
||||||
if goal_test(element.state):
|
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)
|
# add current node to explored (prevents infinite cycles)
|
||||||
explored.append(element)
|
explored.append(element)
|
||||||
|
explored_states.add((element.state.row, element.state.column, element.state.direction))
|
||||||
|
|
||||||
# loop through every possible next action
|
# loop through every possible next action
|
||||||
for successor in get_successors(element.state, minefield):
|
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
|
# make sure not to fall into a cycle
|
||||||
if successor[1] not in fringe_states and \
|
successor_state = (successor[1].row, successor[1].column, successor[1].direction)
|
||||||
successor[1] not in explored_states:
|
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
|
# create new Node and add it at the end of fringe
|
||||||
new_node = Node(state=successor[1],
|
new_node = Node(state=successor[1],
|
||||||
parent=element,
|
parent=element,
|
||||||
action=successor[0])
|
action=successor[0])
|
||||||
fringe.append(new_node)
|
fringe.append(new_node)
|
||||||
|
fringe_states.add((new_node.state.row, new_node.state.column, new_node.state.direction))
|
||||||
|
|
||||||
|
|
||||||
# TEMPORARY METHOD
|
# TEMPORARY METHOD
|
||||||
|
Loading…
Reference in New Issue
Block a user