def search (self, initialState): states = [] open = [] new_n = Node(initialState, None) open.append((new_n, new_n.f())) while (len(open) > 0): #list sorted by f() open.sort(key = sortFunction, reverse = True) n = open.pop()[0] logging.debug(n.state.env()+" -- "+str(n.f())+" -- "+str(n.h())) if (n.state.is_goal()): return n for i in n.state.sucessors(): new_n = Node(i,n) # eh necessario descrever o conteudo do estado # para verificar se ele já foi instanciado ou nao if (new_n.state.env() not in states): open.append((new_n,new_n.f())) # nao eh adiciona o estado ao vetor. # eh adicionado o conteudo states.append(new_n.state.env()) logging.debug(len(states)) else: logging.debug('nao entrou') return None
def astar_search(graph, heuristics, src, dest): open = [] explored = [] # Create the start node and the goal node start = Node(src, None) goal = Node(dest, None) # Add the first node to the open list open.append(start) # Loop until the list is empty while len(open) > 0: # Keep the list sorted so we extract only the smallest element open.sort() # Get the node with the lowest cost current_node = open.pop(0) # Add the current node to the explored nodes list explored.append(current_node) # Check if the current node is the goal node # If we reached the goal, return the path if current_node == goal: # Store the path from destination node to the source node path = [] while current_node != start: path.append(current_node.name) current_node = current_node.parent path.append(src) # Return reversed path return path[::-1] # Get neighbors neighbors = graph.get(current_node.name) # Loop through neighbors for key, value in neighbors.items(): # Create a neighbor node neighbor = Node(key, current_node) # Check if the neighbor is in the closed list if(neighbor in explored): continue # Calculate full path cost neighbor.g = current_node.g + graph.get(current_node.name, neighbor.name) neighbor.h = heuristics[neighbor.name] neighbor.f = neighbor.g + neighbor.h # Check if neighbor is in open list and if it has a lower f value if(add_to_open(open, neighbor) == True): # Everything is green, add neighbor to open list open.append(neighbor) # Return None if no path is found return None