basic bfs implementation

This commit is contained in:
Maksymilian Mikołajczak 2023-04-19 22:12:02 +02:00
parent c91e016294
commit bdb0ed2d3c
3 changed files with 30 additions and 14 deletions

15
bfs.py Normal file
View File

@ -0,0 +1,15 @@
from succ import succ as successors
def bfs(istate, goalstate):
fringe = [istate]
explored = []
while(fringe):
state = fringe.pop(0)
if state == goalstate :
return
element = successors(state)
explored.append(state)
for value in element :
if value not in explored :
fringe.append(value)
return False

View File

@ -1,6 +1,7 @@
class State: class State:
def __init__(self, parent, xpos, ypos, orientation): def __init__(self, parent, action, xpos, ypos, orientation):
self.parent = parent self.parent = parent
self.xpos = xpos self.xpos = xpos
self.ypos = ypos self.ypos = ypos
self.orientation = orientation self.orientation = orientation
self.action = action

24
succ.py
View File

@ -4,27 +4,27 @@ def succ(st: State):
successors = [] successors = []
if st.orientation == 'N': if st.orientation == 'N':
successors.append(['LEFT', State(st, st.xpos, st.ypos, 'W')]) successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W'))
successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'E')]) successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E'))
if st.ypos > 0: if st.ypos > 0:
successors.append(['FORWARD', State(st, st.xpos, st.ypos - 50, 'N')]) successors.append(State(st, 'FORWARD', st.xpos, st.ypos - 50, 'N'))
if st.orientation == 'S': if st.orientation == 'S':
successors.append(['LEFT', State(st, st.xpos, st.ypos, 'E')]) successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E'))
successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'W')]) successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W'))
if st.ypos < 750: if st.ypos < 750:
successors.append(['FORWARD', State(st, st.xpos, st.ypos + 50, 'S')]) successors.append(State(st, 'FORWARD', st.xpos, st.ypos + 50, 'S'))
if st.orientation == 'W': if st.orientation == 'W':
successors.append(['LEFT', State(st, st.xpos, st.ypos, 'S')]) successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S'))
successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'N')]) successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N'))
if st.xpos > 0: if st.xpos > 0:
successors.append(['FORWARD', State(st, st.xpos - 50, st.ypos, 'W')]) successors.append(State(st, 'FORWARD', st.xpos - 50, st.ypos, 'W'))
if st.orientation == 'E': if st.orientation == 'E':
successors.append(['LEFT', State(st, st.xpos, st.ypos, 'N')]) successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N'))
successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'S')]) successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S'))
if st.xpos < 750: if st.xpos < 750:
successors.append(['FORWARD', State(st, st.xpos + 50, st.ypos, 'E')]) successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E'))
return successors return successors