def countPaths(self): pathCounts = {} for sourceNode in self.graph: for sinkNode in self.graph: if sourceNode is not sinkNode: # Calculate the paths between the nodes paths = ksp_yen(self.graph,sourceNode,sinkNode,self.k) for pathInfo in paths: self._incrementLinksOnPath(pathInfo["path"], pathCounts) return pathCounts
def countPaths(self): for sourceNode in self.graph: for sinkNode in self.graph: if sinkNode >= sourceNode: # only traverse path in one direction continue paths = ksp_yen(self.graph,sourceNode,sinkNode,self.k) if len(paths) == 0: continue # Increment path counts for KSP (always), ECMP (if cost = cost of shortest path) minimumPathCost = paths[0]["cost"] for pathInfo in paths: self._incrementLinksOnPath(pathInfo["path"], self.kspPathCounts) if pathInfo["cost"] == minimumPathCost: # EC self._incrementLinksOnPath(pathInfo["path"], self.ecmpPathCounts)
def _pairwise_shortest_routes(self, H): # Find k shortest routes between all 2 nodes k_shortests = [] for pair in itertools.product(range(0, self.n*self.m), repeat=2): u,v = pair[0], pair[1] if u == v: continue # dijkstra would be routes.append(nx.shortest_path(G,source=v,target=w)) k_shortest = algorithms.ksp_yen(H, u, v, max_k = self.r) saved_routes = [] for shortest in k_shortest: if GridNetwork.has_duplicate(shortest['path']): k_shortest.remove(shortest) shortest['o'], shortest['d'], shortest['flow'] = u, v, 0 saved_routes.append(shortest) k_shortests.extend(saved_routes) return k_shortests
def getRoutes(network,agents,k,cost=False): emptyNetwork=deepcopy(network) G=DiGraph()#graph class to create the object for YENs algorithm for rowIdx, row in enumerate(emptyNetwork): for colIdx, col in enumerate(row): if col is not None: col.seizeRequest()#add one car to each edge in the empty network G.add_edge(rowIdx, colIdx, col.evalCost())#add an edge to the fixed network #Get each agent's routes from Yen's algorithm and the fixed empty network agentsRoutes=[None]*len(agents) agentsRoutesCost=[None]*len(agents) for agentIdx in range(len(agentsRoutes)): agentsRoutes[agentIdx]=list() agentsRoutesCost[agentIdx]=list() for path in Yen.ksp_yen(G, agents[agentIdx].o, agents[agentIdx].d, k): agentsRoutes[agentIdx].append(path['path']) agentsRoutesCost[agentIdx].append(path['cost']) costosZero=[[0 for i in range(len(agentsRoutesCost[j]))] for j in range(len(agentsRoutesCost))] if cost: return {'routes':agentsRoutes,'costs':costosZero} else: return(agentsRoutes)