manchattan

This commit is contained in:
Yurii 2022-04-26 00:04:13 +02:00
parent a376cd63c4
commit 5af2f22a64

View File

@ -78,7 +78,7 @@ class BFS:
if current_position[2] == 180: # jesli patrzy na polnoc 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']: if neighbours_list[0][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
if neighbours_list[0][1] == 'grass': if neighbours_list[0][1] == 'grass':
cost = 2 cost = 5
elif neighbours_list[0][1] == 'sand': elif neighbours_list[0][1] == 'sand':
cost = 1 cost = 1
elif neighbours_list[0][1] == 'mine': elif neighbours_list[0][1] == 'mine':
@ -87,7 +87,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[1][0] == 'grass': if neighbours_list[1][0] == 'grass':
cost = 2 cost = 5
elif neighbours_list[1][0] == 'sand': elif neighbours_list[1][0] == 'sand':
cost = 1 cost = 1
elif neighbours_list[1][0] == 'mine': elif neighbours_list[1][0] == 'mine':
@ -96,7 +96,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[1][2] == 'grass': if neighbours_list[1][2] == 'grass':
cost = 2 cost = 5
elif neighbours_list[1][2] == 'sand': elif neighbours_list[1][2] == 'sand':
cost = 1 cost = 1
elif neighbours_list[1][2] == 'mine': elif neighbours_list[1][2] == 'mine':
@ -107,7 +107,7 @@ class BFS:
if current_position[2] == 90: # jesli patrzy na wschod 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']: if neighbours_list[1][2] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
if neighbours_list[1][2] == 'grass': if neighbours_list[1][2] == 'grass':
cost = 2 cost = 5
elif neighbours_list[1][2] == 'sand': elif neighbours_list[1][2] == 'sand':
cost = 1 cost = 1
elif neighbours_list[1][2] == 'mine': elif neighbours_list[1][2] == 'mine':
@ -116,7 +116,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[0][1] == 'grass': if neighbours_list[0][1] == 'grass':
cost = 2 cost = 5
elif neighbours_list[0][1] == 'sand': elif neighbours_list[0][1] == 'sand':
cost = 1 cost = 1
elif neighbours_list[0][1] == 'mine': elif neighbours_list[0][1] == 'mine':
@ -125,7 +125,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[2][1] == 'grass': if neighbours_list[2][1] == 'grass':
cost = 2 cost = 5
elif neighbours_list[2][1] == 'sand': elif neighbours_list[2][1] == 'sand':
cost = 1 cost = 1
elif neighbours_list[2][1] == 'mine': elif neighbours_list[2][1] == 'mine':
@ -136,7 +136,7 @@ class BFS:
if current_position[2] == 0: # jesli patczy na poludzie 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']: if neighbours_list[2][1] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
if neighbours_list[2][1] == 'grass': if neighbours_list[2][1] == 'grass':
cost = 2 cost = 5
elif neighbours_list[2][1] == 'sand': elif neighbours_list[2][1] == 'sand':
cost = 1 cost = 1
elif neighbours_list[2][1] == 'mine': elif neighbours_list[2][1] == 'mine':
@ -145,7 +145,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[1][2] == 'grass': if neighbours_list[1][2] == 'grass':
cost = 2 cost = 5
elif neighbours_list[1][2] == 'sand': elif neighbours_list[1][2] == 'sand':
cost = 1 cost = 1
elif neighbours_list[1][2] == 'mine': elif neighbours_list[1][2] == 'mine':
@ -154,7 +154,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[1][0] == 'grass': if neighbours_list[1][0] == 'grass':
cost = 2 cost = 5
elif neighbours_list[1][0] == 'sand': elif neighbours_list[1][0] == 'sand':
cost = 1 cost = 1
elif neighbours_list[1][0] == 'mine': elif neighbours_list[1][0] == 'mine':
@ -165,7 +165,7 @@ class BFS:
if current_position[2] == 270: # jesli patczy na wschod 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']: if neighbours_list[1][0] not in ['wall', 'cliff_south', 'cliff_east', 'cliff_north', 'cliff_west']:
if neighbours_list[1][0] == 'grass': if neighbours_list[1][0] == 'grass':
cost = 2 cost = 5
elif neighbours_list[1][0] == 'sand': elif neighbours_list[1][0] == 'sand':
cost = 1 cost = 1
elif neighbours_list[1][0] == 'mine': elif neighbours_list[1][0] == 'mine':
@ -174,7 +174,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[2][1] == 'grass': if neighbours_list[2][1] == 'grass':
cost = 2 cost = 5
elif neighbours_list[2][1] == 'sand': elif neighbours_list[2][1] == 'sand':
cost = 1 cost = 1
elif neighbours_list[2][1] == 'mine': elif neighbours_list[2][1] == 'mine':
@ -183,7 +183,7 @@ class BFS:
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
if neighbours_list[0][1] == 'grass': if neighbours_list[0][1] == 'grass':
cost = 2 cost = 5
elif neighbours_list[0][1] == 'sand': elif neighbours_list[0][1] == 'sand':
cost = 1 cost = 1
elif neighbours_list[0][1] == 'mine': elif neighbours_list[0][1] == 'mine':
@ -203,8 +203,9 @@ class BFS:
def graphsearch(self, fringe, explored, succ, goaltest): def graphsearch(self, fringe, explored, succ, goaltest):
# def manhattan(state, target_state): def manhattan(state, target_state):
# return abs(state.get_x() - target_state.get_x()) + abs(state.get_y() - target_state.get_y()) return abs(state[0] - target_state[0]) + abs(state[1] - target_state[1])
self.window.pause(True) self.window.pause(True)
positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy positiont_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy
@ -247,7 +248,7 @@ class BFS:
neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij
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 visited # jesli pozucja wezla nie jest w fringe i nie jest w visited
@ -256,3 +257,9 @@ class BFS:
x = node.Node(tmp_node, node_[0], node_[1], node_[2]) # action x = node.Node(tmp_node, node_[0], node_[1], node_[2]) # action
heapq.heappush(fringe, (counter, x)) heapq.heappush(fringe, (counter, x))
self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]], self.agent.current_map.tile_size, self.agent.current_map, self.agent) self.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]], self.agent.current_map.tile_size, self.agent.current_map, self.agent)
p = manhattan(node_[1], goaltest) + node_[2]
# print(f'Mancgeten = {p}')