Projekt_Sztuczna_Inteligencja/searching_algorithms/bfs.py

121 lines
3.8 KiB
Python
Raw Normal View History

2021-04-14 23:45:34 +02:00
from __future__ import annotations
2021-04-17 21:14:57 +02:00
from typing import List
2021-04-17 21:33:50 +02:00
import ctypes
2021-04-14 23:45:34 +02:00
from project_constants import Direction, Action
from minefield import Minefield
2021-04-14 23:45:34 +02:00
# temporary goal for testing
2021-04-18 18:23:14 +02:00
GOAL = (13, 9)
2021-04-14 23:45:34 +02:00
class State:
def __init__(self, row, column, direction: Direction):
self.row = row
self.column = column
self.direction = direction
class Node:
def __init__(self, state: State, parent: Node = None, action: Action = None):
self.state = state
self.parent = parent
self.action = action
def goal_test(state: State):
if (state.row, state.column) == GOAL:
return True
return False
def get_successors(state: State, minefield: Minefield):
2021-04-14 23:45:34 +02:00
successors = list()
state_left = State(state.row, state.column, state.direction.previous())
successors.append((Action.ROTATE_LEFT, state_left))
2021-04-14 23:45:34 +02:00
state_right = State(state.row, state.column, state.direction.next())
successors.append((Action.ROTATE_RIGHT, state_right))
2021-04-14 23:45:34 +02:00
target = go(state.row, state.column, state.direction)
if minefield.is_valid_move(target[0], target[1]):
state_go = State(target[0], target[1], state.direction)
successors.append((Action.GO, state_go))
2021-04-14 23:45:34 +02:00
return successors
def graphsearch(initial_state: State, minefield: Minefield, fringe: List[Node] = None, explored: List[Node] = None):
# fringe and explored initialization
if fringe is None:
fringe = list()
if explored is None:
explored = list()
2021-04-14 23:45:34 +02:00
2021-04-17 21:14:57 +02:00
explored_states = set()
fringe_states = set()
2021-04-14 23:45:34 +02:00
# root Node
2021-04-14 23:45:34 +02:00
fringe.append(Node(initial_state))
2021-04-17 21:14:57 +02:00
fringe_states.add((initial_state.row, initial_state.column, initial_state.direction))
2021-04-14 23:45:34 +02:00
while True:
# fringe empty -> solution not found
2021-04-17 21:33:50 +02:00
if not any(fringe):
2021-04-18 18:09:39 +02:00
ctypes.windll.user32.MessageBoxW(0, "Brak rozwiązania", "GAME OVER", 1)
2021-04-17 21:33:50 +02:00
return []
2021-04-14 23:45:34 +02:00
# get first element from fringe
2021-04-14 23:45:34 +02:00
element = fringe.pop(0)
2021-04-17 21:14:57 +02:00
fringe_states.remove((element.state.row, element.state.column, element.state.direction))
2021-04-14 23:45:34 +02:00
# if solution was found, prepare and return actions sequence
2021-04-14 23:45:34 +02:00
if goal_test(element.state):
actions_sequence = [element.action]
2021-04-14 23:45:34 +02:00
parent = element.parent
while parent is not None:
# root's action will be None, don't add it
if parent.action is not None:
actions_sequence.append(parent.action)
parent = parent.parent
actions_sequence.reverse()
return actions_sequence
2021-04-14 23:45:34 +02:00
# add current node to explored (prevents infinite cycles)
explored.append(element)
2021-04-17 21:14:57 +02:00
explored_states.add((element.state.row, element.state.column, element.state.direction))
2021-04-14 23:45:34 +02:00
# loop through every possible next action
for successor in get_successors(element.state, minefield):
2021-04-14 23:45:34 +02:00
# make sure not to fall into a cycle
2021-04-17 21:14:57 +02:00
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],
2021-04-14 23:45:34 +02:00
parent=element,
action=successor[0])
2021-04-14 23:45:34 +02:00
fringe.append(new_node)
2021-04-17 21:14:57 +02:00
fringe_states.add((new_node.state.row, new_node.state.column, new_node.state.direction))
# TEMPORARY METHOD
def go(row, column, direction):
target = tuple()
if direction == Direction.RIGHT:
target = row, column + 1
elif direction == Direction.LEFT:
target = row, column - 1
elif direction == Direction.UP:
target = row - 1, column
elif direction == Direction.DOWN:
target = row + 1, column
return target