a star, robot uwzglednia koszt

This commit is contained in:
Yurii 2022-04-28 13:40:35 +02:00
parent 4d91d86aef
commit 1913b12bcf
2 changed files with 10 additions and 9 deletions

View File

@ -91,14 +91,14 @@ class AI:
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(abs(self.saper.position_x - mine.position_x) + abs(self.saper.position_y - mine.position_y))
print(dists)
#print(dists)
ind = dists.index(min(dists)) # choose nearest mine
goal_state = [self.current_map.mines[ind].position_x, self.current_map.mines[ind].position_y]
print(f'We go to {goal_state}')
find_path = bfs.BFS(self.saper, self.window)
tmp = find_path.graphsearch([], [], bfs.BFS.successor, goal_state)
print(f'tmp = {tmp}')
#print(f'tmp = {tmp}')
if tmp is None:
raise Exception("Error, path does not exist")
else:

View File

@ -151,7 +151,7 @@ class BFS:
position_at_beginning = [self.agent.position_x, self.agent.position_y, self.agent.rotation_degrees] # x, y, gdzie_patczy
final_action_list = [] # lista co ma robic zeby dojechac do miny
root = node.Node(None, None, position_at_beginning, 0) # parent, action, position, cost
root = node.Node(None, None, position_at_beginning, 2) # parent, action, position, cost
heapq.heappush(fringe, (0, root)) # add first node to fringe
@ -162,6 +162,7 @@ class BFS:
if len(fringe) == 0:
return False
# get node from fringe
tmp_node = heapq.heappop(fringe) # tuple(priority, node)
tmp_node_position = tmp_node[1].get_position() # x, y , gdzie patczy
@ -184,14 +185,14 @@ class BFS:
neighbours_list_of_our_node = self.successor(tmp_node_position) # lista możliwych akcij
print(neighbours_list_of_our_node)
for node_ in neighbours_list_of_our_node:
# node_ is tuple(action, [x, y, gdzie_patczy], cost)
notInFringe = True # false if node in fringe
notInExplored = True # false if node in explored
# if node_[1] is None:
# continue
p = manhattan(node_[1], goaltest) + node_[2]
# wyznacza jaki jest priorytet nastempnika
@ -199,7 +200,7 @@ class BFS:
priority_in_fringe = 0
counter = 0
tmp_position_in_fringe = 0 # zero if node not in fringe
index_of_node_in_fringe = 0 # zero if node not in fringe
for fringeNode in fringe: # isc po wszystkich wezlach ktore juz sa w fringe
# jesli nasz wezel juz jest w fringe
@ -207,7 +208,7 @@ class BFS:
notInFringe = False
priority_in_fringe = fringeNode[0]
# number of element in fringe
tmp_position_in_fringe = counter
index_of_node_in_fringe = counter
counter = counter + 1
@ -224,10 +225,10 @@ class BFS:
# if node not in fringe
elif notInFringe is False and (priority_in_fringe > p):
x = node.Node(tmp_node, node_[0], node_[1], node_[2]) # parent, action, state_array, cost
tmp = list(fringe[tmp_position_in_fringe])
tmp = list(fringe[index_of_node_in_fringe])
tmp[0] = p
tmp[1] = x
fringe[tmp_position_in_fringe] = tuple(tmp)
fringe[index_of_node_in_fringe] = tuple(tmp)
#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)