2022-04-07 00:55:36 +02:00
|
|
|
import heapq # dla utrzymania fringe
|
2022-04-07 18:27:35 +02:00
|
|
|
from classes import node, minesweeper, system
|
2022-04-07 00:55:36 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
class BFS:
|
2022-04-07 18:27:35 +02:00
|
|
|
window: system.Window
|
2022-04-07 00:55:36 +02:00
|
|
|
agent: minesweeper.Minesweeper
|
|
|
|
node: node.Node
|
|
|
|
|
2022-04-07 18:27:35 +02:00
|
|
|
def __init__(self, agent, window):
|
2022-04-07 00:55:36 +02:00
|
|
|
self.agent = agent
|
2022-04-07 18:27:35 +02:00
|
|
|
self.window = window
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
def successor(self, current_position):
|
|
|
|
new_nodes = []
|
|
|
|
neighbours_list = self.agent.sensor(current_position[0], current_position[1])
|
2022-04-16 00:11:24 +02:00
|
|
|
#print(f'Current position {current_position}')
|
2022-04-07 17:51:51 +02:00
|
|
|
#print(neighbours_list[0])
|
|
|
|
#print(neighbours_list[1])
|
|
|
|
#print(neighbours_list[2])
|
2022-04-24 11:18:17 +02:00
|
|
|
#print(neighbours_list)
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
|
2022-04-16 00:11:24 +02:00
|
|
|
# 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])
|
|
|
|
|
|
|
|
# if current_position[2] == 0: # jesli patrzy na polnoc
|
|
|
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('forward', [current_position[0] - 1, current_position[1], 0])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('left', [current_position[0],current_position[1], 270])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('right', [current_position[0], current_position[1], 90])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
#
|
|
|
|
# if current_position[2] == 90: # jesli patrzy na wschod
|
|
|
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('forward', [current_position[0], current_position[1] + 1, 90])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('left', [current_position[0], current_position[1], 0])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('right', [current_position[0], current_position[1], 180])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
#
|
|
|
|
# if current_position[2] == 180: # jesli patczy na poludzie
|
|
|
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('forward', [current_position[0] + 1, current_position[1], 180])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('left', [current_position[0], current_position[1], 90])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('right', [current_position[0],current_position[1], 270])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
#
|
|
|
|
# if current_position[2] == 270: # jesli patczy na wschod
|
|
|
|
# if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('forward', [current_position[0],current_position[1] - 1, 270])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('left', [current_position[0], current_position[1], 180])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
# if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
|
|
|
# tmp = ('right', [current_position[0], current_position[1], 0])
|
|
|
|
# new_nodes.append(tmp)
|
|
|
|
|
|
|
|
if current_position[2] == 180: # jesli patrzy na polnoc
|
|
|
|
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[0][1] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[0][1] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[0][1] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('forward', [current_position[0], current_position[1] - 1, 180], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[1][0] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[1][0] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[1][0] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('left', [current_position[0],current_position[1], 270], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[1][2] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[1][2] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[1][2] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('right', [current_position[0], current_position[1], 90], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
|
|
|
|
if current_position[2] == 90: # jesli patrzy na wschod
|
|
|
|
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[1][2] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[1][2] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[1][2] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('forward', [current_position[0] + 1, current_position[1], 90], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[0][1] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[0][1] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[0][1] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('left', [current_position[0], current_position[1], 180], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[2][1] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[2][1] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[2][1] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('right', [current_position[0], current_position[1], 0], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
|
|
|
|
if current_position[2] == 0: # jesli patczy na poludzie
|
|
|
|
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[2][1] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[2][1] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[2][1] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('forward', [current_position[0], current_position[1] + 1, 0], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[1][2] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[1][2] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[1][2] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('left', [current_position[0], current_position[1], 90], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[1][0] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[1][0] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[1][0] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('right', [current_position[0],current_position[1], 270], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
|
|
|
|
if current_position[2] == 270: # jesli patczy na wschod
|
|
|
|
if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[1][0] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[1][0] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[1][0] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('forward', [current_position[0] - 1,current_position[1], 270], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[2][1] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[2][1] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[2][1] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('left', [current_position[0], current_position[1], 0], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
|
|
|
if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
|
2022-04-24 11:18:17 +02:00
|
|
|
if neighbours_list[0][1] == 'grass':
|
|
|
|
cost = 2
|
|
|
|
elif neighbours_list[0][1] == 'sand':
|
|
|
|
cost = 1
|
|
|
|
elif neighbours_list[0][1] == 'mine':
|
|
|
|
cost = 0
|
|
|
|
tmp = ('right', [current_position[0], current_position[1], 180], cost)
|
2022-04-16 00:11:24 +02:00
|
|
|
new_nodes.append(tmp)
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
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):
|
2022-04-24 11:18:17 +02:00
|
|
|
|
|
|
|
# def manhattan(state, target_state):
|
|
|
|
# return abs(state.get_x() - target_state.get_x()) + abs(state.get_y() - target_state.get_y())
|
2022-04-07 18:27:35 +02:00
|
|
|
self.window.pause(True)
|
2022-04-07 00:55:36 +02:00
|
|
|
|
2022-04-16 00:11:24 +02:00
|
|
|
positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy
|
|
|
|
print(f'Position {positiont_at_beginning}')
|
|
|
|
final_action_list = [] # lista co ma robic zeby dojechac do miny
|
2022-04-24 11:18:17 +02:00
|
|
|
root = node.Node(None, None, positiont_at_beginning, 0) # parent, action, position
|
2022-04-07 00:55:36 +02:00
|
|
|
counter = 0
|
|
|
|
heapq.heappush(fringe, (counter, root))
|
2022-04-16 00:11:24 +02:00
|
|
|
#visited_position = []
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
while len(fringe) != 0:
|
|
|
|
flag = True
|
|
|
|
|
|
|
|
|
|
|
|
if len(fringe) == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
tmp_node = heapq.heappop(fringe) # node
|
2022-04-16 00:11:24 +02:00
|
|
|
tmp_node_position = tmp_node[1].get_position() # x, y , gdzie patczy
|
|
|
|
#print(f'Position before succ {tmp_node_position}')
|
2022-04-07 00:55:36 +02:00
|
|
|
|
2022-04-16 00:11:24 +02:00
|
|
|
#visited_position.append(tmp_node_position)
|
|
|
|
explored.append(tmp_node_position)
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
if tmp_node_position[:2] == goaltest:
|
2022-04-16 00:11:24 +02:00
|
|
|
print('Find')
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
while tmp_node[1].get_parent() is not None:
|
|
|
|
final_action_list.append(tmp_node[1].get_action())
|
|
|
|
tmp_node = tmp_node[1].get_parent()
|
2022-04-07 05:33:10 +02:00
|
|
|
final_action_list.reverse()
|
2022-04-07 00:55:36 +02:00
|
|
|
print(final_action_list)
|
2022-04-07 18:27:35 +02:00
|
|
|
self.window.pause(False)
|
2022-04-07 05:33:10 +02:00
|
|
|
return final_action_list
|
2022-04-07 00:55:36 +02:00
|
|
|
|
2022-04-16 00:11:24 +02:00
|
|
|
# nie wiem na razie po co to
|
|
|
|
# chyba visited_position lepszy odpowiednik
|
|
|
|
# explored.append(tmp_node)
|
2022-04-07 00:55:36 +02:00
|
|
|
|
|
|
|
|
2022-04-16 00:11:24 +02:00
|
|
|
neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij
|
2022-04-24 11:18:17 +02:00
|
|
|
print(neighbours_list_of_our_node)
|
2022-04-16 00:11:24 +02:00
|
|
|
|
2022-04-07 00:55:36 +02:00
|
|
|
for node_ in neighbours_list_of_our_node:
|
2022-04-16 00:11:24 +02:00
|
|
|
# jesli pozucja wezla nie jest w fringe i nie jest w visited
|
|
|
|
if [node_[1][0], node_[1][1], node_[1][2]] not in explored and node_[1][0] >= 0 and node_[1][1] >=0:
|
2022-04-07 00:55:36 +02:00
|
|
|
counter += 1
|
2022-04-24 11:18:17 +02:00
|
|
|
x = node.Node(tmp_node, node_[0], node_[1], node_[2]) # action
|
2022-04-07 00:55:36 +02:00
|
|
|
heapq.heappush(fringe, (counter, x))
|
2022-04-16 00:11:24 +02:00
|
|
|
self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]])
|