fixed bfs algorythm 2
This commit is contained in:
parent
a67d160f9a
commit
7df7885cbf
@ -5,6 +5,7 @@ class Node:
|
|||||||
def __init__(self, current_x, current_y, agent_direction, action=None, parent=None):
|
def __init__(self, current_x, current_y, agent_direction, action=None, parent=None):
|
||||||
self.current_x = current_x
|
self.current_x = current_x
|
||||||
self.current_y = current_y
|
self.current_y = current_y
|
||||||
|
self.state = tuple([self.current_x, self.current_y])
|
||||||
self.action = action
|
self.action = action
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.agent_direction = agent_direction
|
self.agent_direction = agent_direction
|
||||||
@ -39,7 +40,7 @@ class Node:
|
|||||||
|
|
||||||
neighbours.append((actions, (x - 1, y), C.LEFT))
|
neighbours.append((actions, (x - 1, y), C.LEFT))
|
||||||
|
|
||||||
if y > 0: # upper neighbour
|
if y < 9: # upper neighbour
|
||||||
if self.agent_direction == C.RIGHT:
|
if self.agent_direction == C.RIGHT:
|
||||||
actions = [C.ROTATE_LEFT, C.MOVE]
|
actions = [C.ROTATE_LEFT, C.MOVE]
|
||||||
elif self.agent_direction == C.LEFT:
|
elif self.agent_direction == C.LEFT:
|
||||||
@ -49,9 +50,9 @@ class Node:
|
|||||||
elif self.agent_direction == C.DOWN:
|
elif self.agent_direction == C.DOWN:
|
||||||
actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE]
|
actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE]
|
||||||
|
|
||||||
neighbours.append((actions, (x, y - 1), C.UP))
|
neighbours.append((actions, (x, y + 1), C.UP))
|
||||||
|
|
||||||
if y < 9: # down neighbour
|
if y > 0: # down neighbour
|
||||||
if self.agent_direction == C.RIGHT:
|
if self.agent_direction == C.RIGHT:
|
||||||
actions = [C.ROTATE_RIGHT, C.MOVE]
|
actions = [C.ROTATE_RIGHT, C.MOVE]
|
||||||
elif self.agent_direction == C.LEFT:
|
elif self.agent_direction == C.LEFT:
|
||||||
@ -61,10 +62,12 @@ class Node:
|
|||||||
elif self.agent_direction == C.DOWN:
|
elif self.agent_direction == C.DOWN:
|
||||||
actions = C.MOVE
|
actions = C.MOVE
|
||||||
|
|
||||||
neighbours.append((actions, (x, y + 1), C.DOWN))
|
neighbours.append((actions, (x, y - 1), C.DOWN))
|
||||||
|
|
||||||
return neighbours
|
return neighbours
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.state + ' ' + self.parent
|
||||||
|
|
||||||
|
|
||||||
class BFSSearcher:
|
class BFSSearcher:
|
||||||
|
|
||||||
@ -73,30 +76,27 @@ class BFSSearcher:
|
|||||||
self.explored_states = []
|
self.explored_states = []
|
||||||
self.path = []
|
self.path = []
|
||||||
|
|
||||||
def search(self, first_state: tuple, goal, agent_direction):
|
def search(self, first_state: tuple, goal: tuple, agent_direction: list):
|
||||||
self.fringe.append(Node(first_state[0], first_state[1], agent_direction))
|
self.fringe.append(Node(first_state[0], first_state[1], agent_direction))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if not self.fringe:
|
if not self.fringe:
|
||||||
return False
|
return False
|
||||||
element = self.fringe.pop(0)
|
element: Node = self.fringe.pop(0)
|
||||||
|
|
||||||
if element.state == goal:
|
if element.state == goal:
|
||||||
return self.__state_eq_goal_action(element)
|
return self.__state_eq_goal_action(element)
|
||||||
|
|
||||||
self.explored_states.append(element)
|
self.explored_states.append(element)
|
||||||
|
for action, state, direction in element.successor():
|
||||||
for action, state, direction in element.succesor():
|
|
||||||
fringe_states = []
|
fringe_states = []
|
||||||
explored_states = []
|
explored_states = []
|
||||||
for node in self.fringe:
|
for node in self.fringe:
|
||||||
fringe_states.append(node.state)
|
fringe_states.append(node.state)
|
||||||
for node in self.explored_states:
|
for node in self.explored_states:
|
||||||
explored_states.append(node)
|
explored_states.append(node.state)
|
||||||
if state not in fringe_states and state not in explored_states:
|
if (state not in fringe_states) and (state not in explored_states):
|
||||||
x = Node(state[0], state[1], direction)
|
x = Node(state[0], state[1], direction, action, element)
|
||||||
x.parent = element
|
|
||||||
x.action = action
|
|
||||||
self.fringe.append(x)
|
self.fringe.append(x)
|
||||||
|
|
||||||
def __state_eq_goal_action(self, current_element: Node):
|
def __state_eq_goal_action(self, current_element: Node):
|
||||||
@ -107,5 +107,7 @@ class BFSSearcher:
|
|||||||
self.path.append(action)
|
self.path.append(action)
|
||||||
else:
|
else:
|
||||||
self.path.append(current_element.action)
|
self.path.append(current_element.action)
|
||||||
|
current_element = current_element.parent
|
||||||
|
|
||||||
return self.path.reverse()
|
self.path.reverse()
|
||||||
|
return self.path
|
||||||
|
Loading…
Reference in New Issue
Block a user