naprawiony bfs

This commit is contained in:
s464859 2022-04-07 16:43:11 +02:00
parent 2aab028009
commit bec25a4e4a

View File

@ -14,16 +14,19 @@ class BFS:
def successor(self, current_position): def successor(self, current_position):
new_nodes = [] new_nodes = []
neighbours_list = self.agent.sensor(current_position[0], current_position[1]) neighbours_list = self.agent.sensor(current_position[0], current_position[1])
print(neighbours_list[0])
print(neighbours_list[1])
print(neighbours_list[2])
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']: if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
new_nodes.append([1 + current_position[0] - 1, 0 + current_position[1] - 1]) new_nodes.append([current_position[0] - 1, current_position[1]])
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']: if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
new_nodes.append([2 + current_position[0] - 1, 1 + current_position[1] - 1]) new_nodes.append([current_position[0], current_position[1] + 1])
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']: if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
new_nodes.append([1 + current_position[0] - 1, 2 + current_position[1] -1]) new_nodes.append([current_position[0] + 1, current_position[1]])
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']: if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
new_nodes.append([0 + current_position[0] - 1, 1 + current_position[1] - 1]) new_nodes.append([current_position[0],current_position[1] - 1])
return new_nodes return new_nodes
@ -70,7 +73,7 @@ class BFS:
#break #break
while tmp_node[1].get_parent() is not None: while tmp_node[1].get_parent() is not None:
#print('sdfhdfg') #print('sdfhdfg')
#print(tmp_node[1].get_parent()) #print(tmp_node[1].get_parent()[1].get_position())
final_action_list.append(tmp_node[1].get_action()) final_action_list.append(tmp_node[1].get_action())
tmp_node = tmp_node[1].get_parent() tmp_node = tmp_node[1].get_parent()
final_action_list.reverse() final_action_list.reverse()
@ -81,11 +84,12 @@ class BFS:
explored.append(tmp_node) explored.append(tmp_node)
neighbours_list_of_our_node = self.successor(tmp_node_position[:2]) neighbours_list_of_our_node = self.successor(tmp_node_position[:2])
# print(neighbours_list_of_our_node) print(neighbours_list_of_our_node)
for node_ in neighbours_list_of_our_node: for node_ in neighbours_list_of_our_node:
# jesli pozucja wezla nie jest w fringe i nie jest w explored # jesli pozucja wezla nie jest w fringe i nie jest w explored
if [node_[0], node_[1]] not in visited_position and node_[0] >= 0 and node_[1] >=0: if [node_[0], node_[1]] not in visited_position and node_[0] >= 0 and node_[1] >=0:
counter += 1 counter += 1
print([node_[0], node_[1]])
x = node.Node(tmp_node, None, [node_[0], node_[1]]) # action x = node.Node(tmp_node, None, [node_[0], node_[1]]) # action
heapq.heappush(fringe, (counter, x)) heapq.heappush(fringe, (counter, x))
# time.sleep(0.5) # time.sleep(0.5)