def astar_search(search_problem, heuristic_fn): # initializing on first time pqueue = PriorityQueue() visited_states = {} solution = SearchSolution(search_problem, "Astar with heuristic " + heuristic_fn.__name__) start_node = AstarNode(search_problem.start_state, heuristic_fn(search_problem.start_state)) pqueue.add_task(start_node, heuristic_fn(start_node.state)) visited_states[start_node.state[1:]] = 0 # while there are nodes in the priority queue while pqueue: # take the first node out and update location in maze current_node = pqueue.pop_task() current_state = current_node.state[1:] visited_states[ current_state] = current_node.transition_cost + current_node.heuristic search_problem.maze.robotloc = list(current_node.state)[1:] # if the solution has been found stop and backchain path if search_problem.is_goal(current_node.state): solution.path = backchain(current_node) solution.cost = current_node.total_cost solution.nodes_visited = len(visited_states) return solution else: # this is for the snesorless problem so the robot does not go through walls # essentially removing a wall node from the possilities and setting the state to the previous one if not search_problem.maze.is_floor(current_state[0], current_state[1]): options_holder = list( search_problem.get_successors(current_node.parent.state)) current_node = current_node.parent current_node.options = options_holder replacement = (current_node.options[0][0], current_state[0], current_state[1]) current_node.options.remove(replacement) current_options = current_node.options else: # normal traversal - get options current_node.options = list( search_problem.get_successors(current_node.state)) current_options = current_node.options # for the next moves if they haven't been visited or if they are more optimal, add them to the queue for option in current_options: next_cost = current_node.transition_cost + 1 heuristic_value = heuristic_fn(option) if option[1:] not in visited_states or visited_states[ option[1:]] > (next_cost + heuristic_value): move = AstarNode(option, heuristic_value, current_node, next_cost) pqueue.add_task(move, move.heuristic) return solution
class PriorityEdgeList: def __init__(self): self.edges = [] self.priorityEdges = PriorityQueue() self.empty = True def InsertEdge(self, y, w): self.priorityEdges.add_task(y, w) self.edges.append(Graphs.Edge(y, w)) self.empty = False
class PriorityEdgeList: def __init__(self): self.edges = [] self.priorityEdges = PriorityQueue() self.empty = True def InsertEdge(self, y, w): self.priorityEdges.add_task(y, w) self.edges.append( Graphs.Edge(y,w)) self.empty = False
def Prim(G, w, s): inf = float("inf") H = PriorityQueue() T = [] s.priorite = 0 H.add_task(s, 0) i = 1 for u in G.sommets: u.pred = None if u != s : H.add_task(u, inf) u.priorite = inf try: while True: u = H.pop_task() u.couleur = "noir" if u.priorite != inf: if u.pred is not None: T.append((u.pred, u)) for v in u.voisins: if v.couleur == "blanc": #v n'est pas sorti du tas if w[(u, v)] < v.priorite: v.priorite = w[(u, v)] H.add_task(v, v.priorite) v.pred = u except: pass return T
def Prims(graph, start): ''' Creates a minimum spanning tree using Prims algorithm ''' print "Start vertex = ", start spanningTree = Graphs.Graph(graph.maxVertices, False) inTree = [False] * graph.maxVertices inTree[start] = True spanningVertices = [] #[0]*graph.maxVertices spanningVertices.append(start) # Initialize the heap (PriorityQueue) with the min edge of the first # vertex for its key, and ininity for all other keys (vertexs). heapQueue = PriorityQueue() #for vertex in range(0, len(graph.vertices)): # if( not (graph.vertices[vertex]).empty ): # heapQueue.add_task(vertex, 35850023) #sys.maxint) startEdges = (graph.vertices[start]).priorityEdges nextVertex, minWeight, noMoreValidEdges = startEdges.pop_task() heapQueue.add_task((start, nextVertex), minWeight) totalCost = 0 while (not noMoreValidEdges): # Pop the next cheapest edge off the list off the heap (prioirty queue) (this will remove it) nextEdge, cheapestWeight, noMoreValidEdges = heapQueue.pop_task() if (not noMoreValidEdges): nextVertex = nextEdge[1] vertexInTreeGettingUpdate = nextEdge[0] if (not inTree[nextVertex]): #print "Next vertex, weight, total = ", nextVertex, cheapestWeight, totalCost totalCost += cheapestWeight inTree[nextVertex] = True spanningVertices.append(nextVertex) spanningTree.InsertEdge(vertexInTreeGettingUpdate, nextVertex, False, cheapestWeight) # Need to find the cheapest edges for both vertex's on the next edge that is # coming into the spanning tree. # Note that the edge that led to this vertex being selected has now been # removed from the "priorityEdges" for that vertex by the "pop_task()" function below nextEdges = (graph.vertices[nextVertex]).priorityEdges edgesEmpty = False cheapestEdgeVertex = nextVertex # We keep popping the edges until we find one that's not already in the tree. while ((not edgesEmpty) and (inTree[cheapestEdgeVertex])): cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task( ) #if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])): # print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight if (not edgesEmpty): heapQueue.remove_task((nextVertex, cheapestEdgeVertex)) heapQueue.add_task((nextVertex, cheapestEdgeVertex), cheapestEdgeWeight) nextEdges = ( graph.vertices[vertexInTreeGettingUpdate]).priorityEdges edgesEmpty = False cheapestEdgeVertex = vertexInTreeGettingUpdate # We keep popping the edges until we find one that's not already in the tree. while ((not edgesEmpty) and (inTree[cheapestEdgeVertex])): cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task( ) #if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])): # print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight if (not edgesEmpty): heapQueue.remove_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex)) heapQueue.add_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex), cheapestEdgeWeight) return totalCost
def Prims( graph, start ): ''' Creates a minimum spanning tree using Prims algorithm ''' print "Start vertex = ", start spanningTree = Graphs.Graph( graph.maxVertices, False ) inTree = [False]*graph.maxVertices inTree[start] = True spanningVertices = [] #[0]*graph.maxVertices spanningVertices.append(start) # Initialize the heap (PriorityQueue) with the min edge of the first # vertex for its key, and ininity for all other keys (vertexs). heapQueue = PriorityQueue() #for vertex in range(0, len(graph.vertices)): # if( not (graph.vertices[vertex]).empty ): # heapQueue.add_task(vertex, 35850023) #sys.maxint) startEdges = (graph.vertices[start]).priorityEdges nextVertex, minWeight, noMoreValidEdges = startEdges.pop_task() heapQueue.add_task( (start, nextVertex), minWeight) totalCost = 0 while( not noMoreValidEdges ): # Pop the next cheapest edge off the list off the heap (prioirty queue) (this will remove it) nextEdge, cheapestWeight, noMoreValidEdges = heapQueue.pop_task() if( not noMoreValidEdges ): nextVertex = nextEdge[1] vertexInTreeGettingUpdate = nextEdge[0] if( not inTree[nextVertex] ): #print "Next vertex, weight, total = ", nextVertex, cheapestWeight, totalCost totalCost += cheapestWeight inTree[nextVertex] = True spanningVertices.append(nextVertex) spanningTree.InsertEdge(vertexInTreeGettingUpdate, nextVertex, False, cheapestWeight) # Need to find the cheapest edges for both vertex's on the next edge that is # coming into the spanning tree. # Note that the edge that led to this vertex being selected has now been # removed from the "priorityEdges" for that vertex by the "pop_task()" function below nextEdges = (graph.vertices[nextVertex]).priorityEdges edgesEmpty = False cheapestEdgeVertex = nextVertex # We keep popping the edges until we find one that's not already in the tree. while( (not edgesEmpty) and (inTree[cheapestEdgeVertex])): cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task() #if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])): # print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight if( not edgesEmpty ): heapQueue.remove_task( (nextVertex, cheapestEdgeVertex) ) heapQueue.add_task((nextVertex, cheapestEdgeVertex), cheapestEdgeWeight) nextEdges = (graph.vertices[vertexInTreeGettingUpdate]).priorityEdges edgesEmpty = False cheapestEdgeVertex = vertexInTreeGettingUpdate # We keep popping the edges until we find one that's not already in the tree. while( (not edgesEmpty) and (inTree[cheapestEdgeVertex])): cheapestEdgeVertex, cheapestEdgeWeight, edgesEmpty = nextEdges.pop_task() #if( (not edgesEmpty) and (inTree[cheapestEdgeVertex])): # print "edge discarded because already in tree = ", cheapestEdgeVertex, cheapestEdgeWeight if( not edgesEmpty ): heapQueue.remove_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex) ) heapQueue.add_task( (vertexInTreeGettingUpdate, cheapestEdgeVertex), cheapestEdgeWeight) return totalCost