From bdb0ed2d3cb5268b2b0693ed974a9c5746b1d349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maksymilian=20Miko=C5=82ajczak?= Date: Wed, 19 Apr 2023 22:12:02 +0200 Subject: [PATCH] basic bfs implementation --- bfs.py | 15 +++++++++++++++ state.py | 5 +++-- succ.py | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 bfs.py diff --git a/bfs.py b/bfs.py new file mode 100644 index 0000000..b0d3da5 --- /dev/null +++ b/bfs.py @@ -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 diff --git a/state.py b/state.py index 40a3133..a6b5e2b 100644 --- a/state.py +++ b/state.py @@ -1,6 +1,7 @@ class State: - def __init__(self, parent, xpos, ypos, orientation): + def __init__(self, parent, action, xpos, ypos, orientation): self.parent = parent self.xpos = xpos self.ypos = ypos - self.orientation = orientation \ No newline at end of file + self.orientation = orientation + self.action = action \ No newline at end of file diff --git a/succ.py b/succ.py index 69a8df0..70b5f0f 100644 --- a/succ.py +++ b/succ.py @@ -4,27 +4,27 @@ def succ(st: State): successors = [] if st.orientation == 'N': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'W')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'E')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W')) + successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E')) 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': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'E')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'W')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E')) + successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W')) 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': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'S')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'N')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S')) + successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N')) 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': - successors.append(['LEFT', State(st, st.xpos, st.ypos, 'N')]) - successors.append(['RIGHT', State(st, st.xpos, st.ypos, 'S')]) + successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N')) + successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S')) 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