def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices d[srcID] = 0 # first node to dist map since while won't catch it. while not pq.isEmpty(): min = pq.extractMin() # Look at neighbors for neighbor in self.adjList[min[0]]: # where neighbor has not yet been removed from pq? alt = min[1] + neighbor[1] if pq.hasKey(neighbor[0]) and alt < pq.get(neighbor[0]): # previously stored needs to be replayed # Update distances and parents everywhere pq.set(neighbor[0], alt) d[neighbor[0]] = alt pi[neighbor[0]] = min[0] return (d,pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # ensure that srcID is between 0 and size of graph. # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0,self.n): # Iterate through all vertex ID if i == srcID: # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices while not pq.isEmpty(): # loop until priority queue is empty. (node, dist) = pq.extractMin() # extract smallest dist node. d[node] = dist # update dictionary with shortest distance. for (vertex, weight) in self.adjList[node]: # check the adjacency list of popped node. newDist = dist + weight # calculate new distance. if pq.hasKey(vertex): # if we haven't already popped the node in the adjacency list yet. if newDist < pq.get(vertex): # check distance vs new calculated dist. pq.set(vertex, newDist) # update priority queue. pi[vertex] = node # update parent list. return (d, pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue cur = self for i in range(0, self.n): # Iterate through all vertex ID if (i == srcID): # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices minKey, minDist = pq.extractMin() d[minKey] = minDist #set orignal source distance to 0 pi[minKey] = minKey #set sources to orginal source while (pq.isEmpty() == False): # COMPLETE the Dijkstra code here lst = self.adjList[ minKey] #grap array of lists of format (vertices,weight) for node minKey for n in lst: #loop through said list ##grap the next node attached to this node somehow dist = n[1] + minDist #grab distance from list node = n[0] #grab node from list if (pq.hasKey(node) ): #check if that nodes min dist has been found if ( pq.get(node) > dist ): #check if path to the node from minKey is less then current path pq.set(node, dist) #set the nodes distance in the queue d[node] = dist #set the nodes distance in the return array pi[node] = minKey #sets the nodes parents minKey, minDist = pq.extractMin( ) #extract next node dont need to extract last node as all the paths will be filled would probably work better in a do while loop return (d, pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue cur=self for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices minKey, minDist=pq.extractMin() d[minKey]=minDist #set orignal source distance to 0 pi[minKey]=minKey #set sources to orginal source while(pq.isEmpty()==False):# COMPLETE the Dijkstra code here lst=self.adjList[minKey]#grap array of lists of format (vertices,weight) for node minKey for n in lst:#loop through said list ##grap the next node attached to this node somehow dist=n[1]+minDist#grab distance from list node=n[0] #grab node from list if(pq.hasKey(node)):#check if that nodes min dist has been found if(pq.get(node)>dist):#check if path to the node from minKey is less then current path pq.set(node,dist)#set the nodes distance in the queue d[node]=dist#set the nodes distance in the return array pi[node]=minKey#sets the nodes parents minKey, minDist=pq.extractMin()#extract next node dont need to extract last node as all the paths will be filled would probably work better in a do while loop return (d,pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0, self.n): # Iterate through all vertex ID if (i == srcID): # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices # COMPLETE the Dijkstra code here if srcID == None or self.adjList[ srcID] == None: #make sure srcId is not None return Dist = 0.0 for i in range(0, self.n): #iniatialize dictionaries to default values d[i] = pq.Inf pi[i] = i d[srcID] = 0.0 #set source to zero while (not pq.isEmpty()): #check if the queue is empty j = pq.extractMin( ) #tuple of min distance and vertex. j = (vertex,minDist) for i in self.adjList[j[0]]: if pq.hasKey( i[0] ): #assertion of pq.get(i) is that i must be in the priority queue if i[1] + j[1] < pq.get( i[0] ): #if distance of prior two nodes is less than current distance Dist = j[1] + i[1] pq.set(i[0], Dist) #update distance at vertex to Dist pi[i[0]] = j[0] #map vertex to its parent d[i[0]] = Dist #map vertex to its new distance return (d, pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0, self.n): # Iterate through all vertex ID if (i == srcID): # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices parent = None while not pq.isEmpty(): minNode = pq.extractMin() if parent is None: d[minNode[0]] = minNode[1] pi[minNode[0]] = minNode[0] elif minNode[1] != pq.Inf: d[minNode[0]] = float(round(decimal.Decimal(minNode[1]), 2)) else: d[minNode[0]] = pq.Inf lst = self.adjList[minNode[0]] for i in range(len(lst)): if pq.hasKey(lst[i][0]) and pq.get( lst[i][0]) > lst[i][1] + d[minNode[0]]: pi[lst[i][0]] = minNode[0] pq.set(lst[i][0], lst[i][1] + d[minNode[0]]) parent = minNode[0] return (d, pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices parent = None while not pq.isEmpty(): minNode = pq.extractMin() if parent is None: d[minNode[0]] = minNode[1] pi[minNode[0]] = minNode[0] elif minNode[1] != pq.Inf: d[minNode[0]] = float(round(decimal.Decimal(minNode[1]), 2)) else: d[minNode[0]] = pq.Inf lst = self.adjList[minNode[0]] for i in range(len(lst)): if pq.hasKey(lst[i][0]) and pq.get(lst[i][0]) > lst[i][1] + d[minNode[0]]: pi[lst[i][0]] = minNode[0] pq.set(lst[i][0], lst[i][1] + d[minNode[0]]) parent = minNode[0] return (d,pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices # COMPLETE the Dijkstra code here # self.n : number of vertices in the graph # self.adjList: Adjacency list stored as a list of lists. # self.adjList[i] stores all adjacent nodes to vertex ID i along with edge weights. # Eg., if vertex 2 has three edges (2,3) with weight 4.5, (2,4) with weight 3.2, and (2,5) with 2.1 # self.adjList[2] is the list [ (3,4.5), (4,3.2), (5,2.12) ] # your goal is to implement the singleSourceShortestPath function. while not pq.isEmpty(): (n, dist) = pq.extractMin() d[n] = dist neighbors = self.adjList[n] for i in range (0, len(neighbors)): (v, e) = neighbors[i] if pq.hasKey(v): oldDist = pq.get(v) #is it more efficient if these constants are declared outside the loop? newDist = dist + e if newDist < oldDist: pq.set(v, newDist) pi[v] = n return (d,pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n ) # ensure that srcID is between 0 and size of graph. # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0, self.n): # Iterate through all vertex ID if i == srcID: # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices while not pq.isEmpty(): # loop until priority queue is empty. (node, dist) = pq.extractMin() # extract smallest dist node. d[node] = dist # update dictionary with shortest distance. for (vertex, weight) in self.adjList[ node]: # check the adjacency list of popped node. newDist = dist + weight # calculate new distance. if pq.hasKey( vertex ): # if we haven't already popped the node in the adjacency list yet. if newDist < pq.get( vertex): # check distance vs new calculated dist. pq.set(vertex, newDist) # update priority queue. pi[vertex] = node # update parent list. return (d, pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0, self.n): # Iterate through all vertex ID if (i == srcID): # If ID is srcID pq.set(i, 0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices d[srcID] = 0 # first node to dist map since while won't catch it. while not pq.isEmpty(): min = pq.extractMin() # Look at neighbors for neighbor in self.adjList[min[ 0]]: # where neighbor has not yet been removed from pq? alt = min[1] + neighbor[1] if pq.hasKey(neighbor[0]) and alt < pq.get( neighbor[0]): # previously stored needs to be replayed # Update distances and parents everywhere pq.set(neighbor[0], alt) d[neighbor[0]] = alt pi[neighbor[0]] = min[0] return (d, pi)
def singleSourceShortestPath(self, srcID): assert (srcID >= 0 and srcID < self.n) # Implement Dijkstra's algorithm # Input: # self --> a reference to a MyGraph instance # srcID: the id of the source vertex. # Expected Output: (d,pi) # d --> Map each vertex id v to the distance from srcID # pi --> Map each reachable vertex id v (except for srcID) to a parent. # Initialize the priority queue pq = PriorityQueue(self.n) #create the priority queue for i in range(0,self.n): # Iterate through all vertex ID if ( i == srcID): # If ID is srcID pq.set(i,0.0) # Distance of srcID should be zero else: # ID is not srcID pq.set(i, pq.Inf) # Distance should be infinity d = {} # Initialize the map with distances to nodes pi = {} # Initialize the map with parents of vertices #Code Starts HERE d[srcID] = 0 #Set distances from source equal to 0 pi[srcID] = None #Set parents to None while not pq.isEmpty(): #While the priority queue is not empty (currentVertex,currentDistance) = pq.extractMin() #Extract the Min for (nextVertex, weight) in self.adjList[currentVertex]: newDist = weight + currentDistance if pq.hasKey(nextVertex): #Check if nextVertex exists in priority queue if newDist < pq.get(nextVertex): #If the new distance is less d[nextVertex] = newDist #Set new distnace pi[nextVertex] = currentVertex #Set the parents pq.set(nextVertex,newDist) #Set the new distance for the vertex return (d,pi)
class Sim: def __init__(self): self.q = PriorityQueue() self.time = 100 self.nodes = {} self.actors = [] self.done = False def add_actor(self, actor): actor.sim = self self.actors.append(actor) def at(self, event): if event.time < self.time: print "ERROR, time warp" else: self.q.put(event, event.time) def process(self): while not self.q.empty(): event = self.q.get() self.time = event.time try: (result, actor) = event.process(self) actor.send(result) except StopIteration: pass print "Sim done" def prime(self): for a in self.actors: a.prime() def go(self): self.prime() self.process()
def a_star_search(start, goal, moves): # Create the Frontier frontier = PriorityQueue() # add the start node to the frontier frontier.put(start, g_of_n(start) + h_of_n(start)) # explore the node explored = explored_dic(len(start.item)) # loop as long frontier is not empty while frontier.not_empty(): # get the node with the highest priority state = frontier.get() # check if it's the goal then return the moves if state.item == goal: return state.moves # get the index of zero for the dictionary zero = state.item.index(0) # add the node to explored using explored[zero].add(tuple(state.item)) # all possible moves for the current node for move in moves[zero]: # create new child adding to it the previous steps that taken to reach it new_child = State(state.moves[:], state.prevSteps + 1) # give it the new configuration new_child.create_item(move_list(move, state.item[:], zero)) # get the zero index of the child zero2 = new_child.item.index(0) # check if it's already explored or added to the frontier if tuple( new_child.item) in explored[zero2] or frontier.in_frontier( tuple(new_child.item)): continue else: # add the new move to the child new_child.add_move(move) frontier.put(new_child, g_of_n(new_child) + h_of_n(new_child)) return "nothing"
class Jeu(): cpt = 0 positions = {} caches = {} caches["l1"] = {} caches["l2"] = {} references = {} clock = 0 strategie = CacheStrategie() def __init__(self, game, player, nom, init, goal, wallStates, goalStates): self.game = game self.player = player self.nom = nom self.position = init self.goal = goal self.graph = Graph((wallStates, goalStates), game.spriteBuilder.rowsize, game.spriteBuilder.colsize) self.chemin = [] self.frontier = PriorityQueue() self.frontier.put(init, 0) self.came_from = {} self.cost_so_far = {} self.came_from[init] = None self.cost_so_far[init] = 0 self.priority = 0 self.avoid = [] self.ID = Jeu.cpt Jeu.cpt += 1 Jeu.positions[self.nom] = init Jeu.caches["l1"][self.nom] = [] Jeu.references[self.nom] = self def play(self): i = 1 while (not self.frontier.empty()): position, cout = self.frontier.get() if (position == self.goal): self.frontier.clear() break for nextP in self.graph.neighbors(position): new_cost = self.cost_so_far[position] + self.graph.cost( position, nextP) if nextP not in self.cost_so_far or new_cost < self.cost_so_far[ nextP]: self.cost_so_far[nextP] = new_cost priority = new_cost + self.heuristic(self.goal, nextP) self.frontier.put(nextP, priority) self.came_from[nextP] = position i += 1 result = self.path() result.reverse() self.chemin = result Jeu.strategie.apply(self) def reset(self): self.graph.wall = list(set(self.graph.wall) - set(self.avoid)) self.chemin = [] self.avoid.clear() self.frontier.clear() self.frontier.put(self.position, 0) self.came_from = {} self.cost_so_far = {} self.came_from[self.position] = None self.cost_so_far[self.position] = 0 self.pause = 0 Jeu.caches["l2"][self.nom] = self.caches["l1"][self.nom] Jeu.caches["l1"][self.nom] = [] def done(self): for i in Jeu.references.values(): if (i.goal != (-1, -1)): return False return True def heuristic(self, a, b): xa, ya = a xb, yb = b return abs(xa - xb) + abs(ya - yb) def path(self): final = [] if (self.goal not in self.came_from): return final current = self.goal while (current != None): final.append(current) current = self.came_from[current] return final def freeze(self, n=1): self.priority += n def isObstacle(self, case): Jeu.positions[self.nom] = (-1, -1) temoin = case in Jeu.positions.values() Jeu.positions[self.nom] = self.position return temoin def move(self): if (Jeu.references[list(Jeu.references.keys())[0]] is self): Jeu.clock += 1 print("clock", Jeu.clock) if (self.priority > 0): print(self.nom, "FREEEEEEEEEEEEZE") self.priority -= 1 return # print("chemin: "+str(self.chemin)) if (len(self.chemin) == 0): # print("joueur {0}: RIEN A CHERCHER".format(self.nom)) Jeu.positions[self.nom] = self.position return if (self.isObstacle(self.chemin[0])): print("joueur {0}: conflit avec un autre joueur".format(self.nom)) Jeu.strategie.reply(self) return Jeu.caches["l1"][self.nom].append(self.position) row, col = self.chemin[0] self.player.set_rowcol(row, col) self.position = (row, col) if (len(self.chemin) > 0): self.chemin.pop(0) Jeu.positions[self.nom] = self.position #print("pos :", self.nom, self.position) self.game.mainiteration()
class Astar(WalkerBase): def __init__(self, maze): #super already sets the start as the current cell super(Astar, self).__init__(maze, maze.start()) self.strategy = "Euclidean" #make it possible to choose an option which heuristic can be used #current position not necessary, stored in walker #open list containing all cells to explore self.open = PriorityQueue() self.closed = dict()# node:parent self.g = 10 #total costs the agent took so far self.last = None self.closed[self._cell] = None def heuristic(self,cell): ''' :param currentPoint: :return: the distance (aka the costs) from the current point to reach the goal based on the current heuristic ''' return np.sqrt(np.square(maze_constants.XCELLS - cell._xLoc)+np.square(maze_constants.YCELLS - cell._yLoc)) #Function tracking back to the last decision point, recursive? def backToDecisionPoint(self,cell): #paint the current cell which will be temporarily discarded self.paint(self._cell,TEMPORARILYDISCARDED_COLOR) self.last = self._cell self._cell = cell def step(self): if self._cell is self._maze.start(): paths = self._cell.get_paths(last=self.last) #self._cell.parent where we came from for i in paths: costs = self.heuristic(self._cell) + self.g self.open.put([i,self._cell,0,self.g],costs) self.paint(i,OPENLIST_COLOR) self.last = self._cell #finished? if self._cell is self._maze.finish(): road = [] tmp = self._cell i = 0 while not (tmp._yLoc == 0 and tmp._xLoc == 0): road.append(tmp) tmp = self.closed[tmp] i+=1 road = road[::-1] for i in road: self.paint(i, FOUND_COLOR) self._isDone = True return #what are the possible steps from here? paths = self._cell.get_paths(last=self.last) #as long as there are cells in the open list, # get the one with the lowest costs next = self.open.get() while next[0] in self.closed.keys(): next = self.open.get() #continue the walk from where we are right now if next[1] == self._cell: self.paint(next[0],CLOSEDLIST_COLOR) self.closed[next[0]] = self._cell self._cell = next[0] self.g += 10 #is the next cell an adjacent cell of the current cell? elif not next[0] in paths: #discard the current position self.paint(self._cell,TEMPORARILYDISCARDED_COLOR) self._cell = next[0] self.last = next[1] self.g = next[2] self.closed[self._cell] = self.last self.paint(self.last,CLOSEDLIST_COLOR) self.paint(self._cell,CLOSEDLIST_COLOR) #Append the open list for the current position newPaths = self._cell.get_paths(last=self.last) if len(newPaths) > 0: for i in newPaths: if not i in self.closed: self.paint(i,OPENLIST_COLOR) f = self.heuristic(i) + self.g self.open.put([i,self._cell,self.g],f) if len(newPaths) == 0 and self.open.empty(): print("This is not suppossed to happen, please start new")