cost for every node

This commit is contained in:
Yurii 2022-04-24 11:18:17 +02:00
parent 5cb1a13327
commit d874793228
4 changed files with 97 additions and 33 deletions

View File

@ -90,7 +90,7 @@ class AI:
dists = [] # how long way is to all mines from start([0, 0]) dists = [] # how long way is to all mines from start([0, 0])
for mine in self.current_map.mines: for mine in self.current_map.mines:
dists.append(sqrt((self.saper.position_x-mine.position_x)**2 + (self.saper.position_y-mine.position_y)**2)) dists.append(sqrt((self.saper.position_x-mine.position_x)**2 + (self.saper.position_y-mine.position_y)**2))
# print(dists) print(dists)
ind = dists.index(min(dists)) # choose nearest mine ind = dists.index(min(dists)) # choose nearest mine
goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y] goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y]
print(f'We go to {goal_state}') print(f'We go to {goal_state}')

View File

@ -19,6 +19,7 @@ class BFS:
#print(neighbours_list[0]) #print(neighbours_list[0])
#print(neighbours_list[1]) #print(neighbours_list[1])
#print(neighbours_list[2]) #print(neighbours_list[2])
#print(neighbours_list)
# 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']:
@ -76,46 +77,118 @@ 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']:
tmp = ('forward', [current_position[0], current_position[1] - 1, 180]) 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)
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']:
tmp = ('left', [current_position[0],current_position[1], 270]) 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)
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']:
tmp = ('right', [current_position[0], current_position[1], 90]) 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)
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
tmp = ('forward', [current_position[0] + 1, current_position[1], 90]) 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)
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']:
tmp = ('left', [current_position[0], current_position[1], 180]) 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)
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']:
tmp = ('right', [current_position[0], current_position[1], 0]) 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)
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
tmp = ('forward', [current_position[0], current_position[1] + 1, 0]) 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)
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']:
tmp = ('left', [current_position[0], current_position[1], 90]) 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)
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']:
tmp = ('right', [current_position[0],current_position[1], 270]) 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)
new_nodes.append(tmp) new_nodes.append(tmp)
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']:
tmp = ('forward', [current_position[0] - 1,current_position[1], 270]) 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)
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']:
tmp = ('left', [current_position[0], current_position[1], 0]) 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)
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']:
tmp = ('right', [current_position[0], current_position[1], 180]) 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)
new_nodes.append(tmp) new_nodes.append(tmp)
return new_nodes return new_nodes
@ -129,12 +202,15 @@ class BFS:
# goaltest = test spewnienia celu # goaltest = test spewnienia celu
def graphsearch(self, fringe, explored, succ, goaltest): def graphsearch(self, fringe, explored, succ, goaltest):
# def manhattan(state, target_state):
# return abs(state.get_x() - target_state.get_x()) + abs(state.get_y() - target_state.get_y())
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
print(f'Position {positiont_at_beginning}') print(f'Position {positiont_at_beginning}')
final_action_list = [] # lista co ma robic zeby dojechac do miny final_action_list = [] # lista co ma robic zeby dojechac do miny
root = node.Node(None, None, positiont_at_beginning) # parent, action, position root = node.Node(None, None, positiont_at_beginning, 0) # parent, action, position
counter = 0 counter = 0
heapq.heappush(fringe, (counter, root)) heapq.heappush(fringe, (counter, root))
#visited_position = [] #visited_position = []
@ -171,12 +247,12 @@ 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
if [node_[1][0], node_[1][1], node_[1][2]] not in explored and node_[1][0] >= 0 and node_[1][1] >=0: if [node_[1][0], node_[1][1], node_[1][2]] not in explored and node_[1][0] >= 0 and node_[1][1] >=0:
counter += 1 counter += 1
x = node.Node(tmp_node, node_[0], node_[1]) # 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.window.draw_search([self.agent.position_x, self.agent.position_y], [node_[1][0], node_[1][1]])

View File

@ -112,7 +112,7 @@ class Map:
rng = randrange(10) rng = randrange(10)
if ok and rng==0 and not (i<2 and j<3): if ok and rng==0 and not (i<2 and j<3):
matrix[i].append(10) matrix[i].append(10)
elif ok and rng>8 and not (i<2 and j<3): elif ok and rng>7 and not (i<2 and j<3):
matrix[i].append(5) matrix[i].append(5)
elif ok and rng<2 and not (i<2 and j<3): elif ok and rng<2 and not (i<2 and j<3):
matrix[i].append(0) matrix[i].append(0)

View File

@ -1,23 +1,9 @@
class Node: class Node:
def __init__(self, parent, action, state_array): def __init__(self, parent, action, state_array, cost):
self.parent = parent self.parent = parent
self.action = action self.action = action
self.position = state_array self.position = state_array
self.cost = cost
# if parent:
# parent_pos = parent[1].get_position()
# diff = [state_array[0] - parent_pos[0], state_array[1] - parent_pos[1]]
# if diff[0]==1:
# action="E"
# elif diff[0]==-1:
# action="W"
# elif diff[1]==1:
# action="S"
# elif diff[1]==-1:
# action="N"
# self.action=action
# else:
# self.action=action
def get_position(self): def get_position(self):
return self.position return self.position
@ -31,4 +17,6 @@ class Node:
def set_parent(self, parent): def set_parent(self, parent):
self.parent = parent self.parent = parent
def get_cost(self):
return self.cost