100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
import heapq # dla utrzymania fringe
|
|
from classes import node, minesweeper
|
|
import time
|
|
|
|
|
|
class BFS:
|
|
|
|
agent: minesweeper.Minesweeper
|
|
node: node.Node
|
|
|
|
def __init__(self, agent):
|
|
self.agent = agent
|
|
|
|
def successor(self, current_position):
|
|
new_nodes = []
|
|
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']:
|
|
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']:
|
|
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']:
|
|
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']:
|
|
new_nodes.append([current_position[0],current_position[1] - 1])
|
|
|
|
return new_nodes
|
|
|
|
|
|
|
|
# fringe = struktura danych przeszowyjąca wierchowki do odwiedzenia
|
|
# explored = lista odwiedzonych stanow
|
|
# position_at_beginning = stan poczatkowy
|
|
# succ = funkcja nastempnika
|
|
# goaltest = test spewnienia celu
|
|
|
|
def graphsearch(self, fringe, explored, succ, goaltest):
|
|
|
|
positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy
|
|
final_action_list = []
|
|
root = node.Node(None, None, positiont_at_beginning[:2]) # parent, action, position
|
|
counter = 0
|
|
heapq.heappush(fringe, (counter, root))
|
|
visited_position = []
|
|
|
|
|
|
while len(fringe) != 0:
|
|
flag = True
|
|
|
|
|
|
if len(fringe) == 0:
|
|
return False
|
|
break
|
|
|
|
tmp_node = heapq.heappop(fringe) # node
|
|
#print(f'my parent is {tmp_node[1].get_parent()}')
|
|
tmp_node_position = tmp_node[1].get_position()
|
|
|
|
print(f'Position of our node {tmp_node_position[:2]}')
|
|
|
|
visited_position.append(tmp_node_position[:2])
|
|
#print(f'visited position: {visited_position}')
|
|
|
|
if tmp_node_position[:2] == goaltest:
|
|
print(' we find node')
|
|
# print(visited_position)
|
|
|
|
#print(fringe)
|
|
#break
|
|
while tmp_node[1].get_parent() is not None:
|
|
#print('sdfhdfg')
|
|
#print(tmp_node[1].get_parent()[1].get_position())
|
|
final_action_list.append(tmp_node[1].get_action())
|
|
tmp_node = tmp_node[1].get_parent()
|
|
final_action_list.reverse()
|
|
print(final_action_list)
|
|
return final_action_list
|
|
|
|
|
|
explored.append(tmp_node)
|
|
|
|
neighbours_list_of_our_node = self.successor(tmp_node_position[:2])
|
|
print(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
|
|
if [node_[0], node_[1]] not in visited_position and node_[0] >= 0 and node_[1] >=0:
|
|
counter += 1
|
|
print([node_[0], node_[1]])
|
|
x = node.Node(tmp_node, None, [node_[0], node_[1]]) # action
|
|
heapq.heappush(fringe, (counter, x))
|
|
# time.sleep(0.5)
|
|
|
|
|
|
|
|
|