def calculate(self): # read start and end point startX = int(self.main.startPointX.text()) startY = int(self.main.startPointY.text()) endX = int(self.main.endPointX.text()) endY = int(self.main.endPointY.text()) # read image data costLayer = self.rasterLayers[self.main.costSelector.currentIndex()] cost_array = get_cost_array(costLayer) # progress bar progressBar = self.main.progressBar #self.worker = Worker() #self.worker.progress.connect(self.main.progressBar.setValue) #self.worker.start() # using algorithm if self.main.AlgorithmSelector.currentIndex() == 0: map = SPFA(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) elif self.main.AlgorithmSelector.currentIndex() == 1: map = SLF(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) elif self.main.AlgorithmSelector.currentIndex() == 2: map = LLL(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) elif self.main.AlgorithmSelector.currentIndex() == 3: map = SLFandLLL(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) elif self.main.AlgorithmSelector.currentIndex() == 4: map = dijkstra(cost_array, startX, startY, endX, endY) self.showItems(map, costLayer) progressBar.setValue(0) elif self.main.AlgorithmSelector.currentIndex() == 5: map = astar(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) elif self.main.AlgorithmSelector.currentIndex() == 6: map = SPFA(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) map = SLF(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) map = LLL(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) map = SLFandLLL(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0) map = dijkstra(cost_array, startX, startY, endX, endY) self.showItems(map, costLayer) progressBar.setValue(0) map = astar(cost_array, startX, startY, endX, endY, progressBar) self.showItems(map, costLayer) progressBar.setValue(0)
def determineNextMove(player_location, opponentLocation, coins): """Return the next direction""" global route, currentcoin, meta_route, best_weight, best_path, coins_to_search #the second test prevents the player from going to coins which have been taken by the opponent if currentcoin == player_location or opponentLocation in coins_to_search: dists_matrix, routes_matrix = u.update_dists_from_each( dist_matrix, route_matrix, player_location, mazeMap, coins) coins_to_search = get_n_shortest(3, coins, player_location, dist_matrix) ennemy_dists = algo.dijkstra(mazeMap, opponentLocation) may_be_lost_coins = [] # Remove from coins_to_search the first coin which is closer to the opponent than to the player for c in coins_to_search: if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[ player_location][c]: may_be_lost_coins.append(c) if len(may_be_lost_coins) != 0: coins_to_search.remove(may_be_lost_coins[0]) best_weight = float("inf") best_path = [] exhaustive(coins_to_search, player_location, [], 0, dist_matrix) meta_route = [player_location] + best_path route = u.location_list_to_route(meta_route, route_matrix) currentcoin = meta_route[1] return u.direction(player_location, route.pop(0))
def CreatTourSortGraph(graph): """ Parameters: ------------ ALGraph or dataframe Returns: --------- tour route list """ if utils.graph_type(graph)[0] == "ALGraph": return else: (dfs_visit, path), dist = algorithms.DFSTraverse(graph, start=graph.columns.values[0]) i = 0 while i < len(dfs_visit) - 1: # if is_fin(dfs_visit[:i+2], graph.index): # return dfs_visit[:i+2] if dfs_visit[i + 1] not in utils.LocateVex(graph, dfs_visit[i]): _, templist = algorithms.dijkstra(graph, start=dfs_visit[i], end=dfs_visit[i + 1]) for insert_poi in templist: dfs_visit.insert(i + 1, insert_poi) i += 1 else: i += 1 return dfs_visit
def print_min_distance(graph, source, target): try: distance, path = dijkstra(graph, source, target) print('- Min distance between ({},{}) is {} following this path: {}'. format(source, target, distance, path)) except InvalidPathError: print('- There is no path between ({},{})'.format(source, target))
def determineNextMove(player_location, opponentLocation, coins): """Return the next direction""" global route, currentcoin, meta_route, best_weight, best_path, coins_to_search, index if opponentLocation in coins_to_search: coins_to_search, meta_route, route = change_way(coins, opponentLocation, player_location)[:3] index = 0 elif currentcoin == player_location: if len(route) != 0: old_dist = algo.dijkstra(mazeMap, player_location)[1][meta_route[index+1]] coins_to_search2, meta_route2, route2, new_dist = change_way(coins, opponentLocation, player_location) #dist_matrix, route_matrix = u.update_dists_from_each(dists_matrix, routes_matrix, player_location, mazeMap, coins) #coins_to_search = get_n_shortest(3, coins, player_location, dists_matrix) #ennemy_dists = algo.dijkstra(mazeMap, opponentLocation) #for c in coins_to_search: #if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]: # coins_to_search.remove(c) #break #best_weight = float("inf") #best_path = [] #exhaustive(coins_to_search, player_location, [], 0, dist_matrix) #meta_route2 = [player_location] + best_path #route2 = u.location_list_to_route(meta_route2, route_matrix) #new_dist = dist_matrix[player_location][meta_route2[1]] if len(route) == 0 or old_dist - new_dist > 3: route = route2 meta_route = meta_route2 index = 0 index += 1 currentcoin = meta_route[index] #api.debug(route) return u.direction(player_location, route.pop(0))
def determineNextMove(player_location, opponentLocation, coins): """Return the next direction""" global route, currentcoin, meta_route, best_weight, best_path, coins_to_search #the second test prevents the player from going to coins which have been taken by the opponent if currentcoin == player_location or opponentLocation in coins_to_search: dists_matrix, routes_matrix = u.update_dists_from_each(dist_matrix, route_matrix, player_location, mazeMap, coins) coins_to_search = get_n_shortest(3, coins, player_location, dist_matrix) ennemy_dists = algo.dijkstra(mazeMap, opponentLocation) may_be_lost_coins = [] # Remove from coins_to_search the first coin which is closer to the opponent than to the player for c in coins_to_search: if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]: may_be_lost_coins.append(c) if len(may_be_lost_coins) != 0: coins_to_search.remove(may_be_lost_coins[0]) best_weight = float("inf") best_path = [] exhaustive(coins_to_search, player_location, [], 0, dist_matrix) meta_route = [player_location]+best_path route = u.location_list_to_route(meta_route, route_matrix) currentcoin = meta_route[1] return u.direction(player_location, route.pop(0))
def test_praktikum_5(arg): """ Reading and converting the graphs (web/file). """ graph_url = 'http://www.hoever.fh-aachen.de/webDateien/mmi/Grafen/Wege1.txt' #graph_url = 'http://www.hoever.fh-aachen.de/webDateien/mmi/Grafen/G_10_20.txt' result = convert_edge_list(retrieve_information_web(graph_url)) # input_file = 'graphs/K_test.txt' # result = convert_edge_list(retrieve_information_file(input_file)) if arg == 'dij': """ Tests for Dijkstra-Algorithm """ print "=" * 40 print "Dijkstra-Algorithm" print "=" * 40 try: dijkstra(result, int(sys.argv[2])) except: print 'Usage python tests.py <dij> <source>' if arg == 'sp': """ Tests for Shortest-Path (Source -> Target) """ print "=" * 40 print "Shortest-Path (Source -> Target)" print "=" * 40 try: dijkstra(result, int(sys.argv[2]), int(sys.argv[3])) except: print 'Usage: python tests.py <sp> <start> <target>' if arg == 'bell': """ Tests for Bellman-Ford """ print "=" * 40 print "Bellman-Ford" print "=" * 40 try: bellman_ford(result, int(sys.argv[2])) except: print 'Usage python tests.py <bell> <source>'
def test_beispiel(show: bool): """Stellt ein Beispiel anhand eines konstruierten Graphes dar, den die Algorithmen Dijkstra, Floyd-Warshall und Bellman-Ford einmal ablaufen. Von jedem Algorithmus werden die Ergebnisse ausgegeben. A-Stern wurde ausgelassen, da die Haversine Distance nicht mit simplen Knoten funktioniert. """ graph = nx.MultiDiGraph() graph.add_nodes_from([1, 2, 3, 4, 5]) graph.add_edge(1, 4, length=10) graph.add_edge(1, 2, length=5) graph.add_edge(2, 3, length=3) graph.add_edge(3, 4, length=1) graph.add_edge(3, 5, length=8) graph.add_edge(4, 3, length=1) if (show): nx.draw(graph) plt.show() print("*** DURCHLAUF TEST_BEISPIEL(): ") print(""" 10 (1)------->(4) | /|\ 5 | | | | 1 \|/ | (2)------->(3)------->(5) 3 8 """) print(f"===== START Dijkstra") dist, pred, count = dijkstra(graph, 1) print(f"= Distanz von Startpunkt zum jeweiligen Key: {dist}".replace( "{", "").replace("}", "")) print(f"= Vorheriger Knoten zum jeweiligen Key: {pred}".replace( "{", "").replace("}", "")) print(f"===== ENDE Dijkstra") print(f"===== START Bellman-Ford") dist, pred, count = bellman_ford(graph, 1) print(f"= Distanz von Startpunkt zum jeweiligen Key: {dist}".replace( "{", "").replace("}", "")) print(f"= Vorheriger Knoten zum jeweiligen Key: {pred}".replace( "{", "").replace("}", "")) print(f"===== ENDE Bellman-Ford") print(f"===== START Floyd-Warshall") dist, next, count = floyd_warshall(graph) result_dist = prepare_matrix_for_printing(dist) result_next = prepare_matrix_for_printing(next) print("= Distanz von Startpunkt zum jeweiligen Key:") print(result_dist) print( f"= Nächster Knoten auf dem optimalen Pfad von A (Links) Nach B (Oben):" ) print(result_next) print(f"===== ENDE Floyd-Warshall")
def update_dists_from_each(dists_matrix, routes_matrix, new_location, maze_map, coins): routing_table, dists_table = algo.dijkstra(maze_map, new_location) dists_matrix[new_location] = {} routes_matrix[new_location] = {} for loc in coins: route = u.way_width(routing_table, new_location, loc) dists_matrix[new_location][loc] = dists_table[loc] routes_matrix[new_location][loc] = route dists_matrix[loc][new_location] = dists_table[loc] routes_matrix[loc][new_location] = [l for l in reversed(route[:-1])] + [new_location] return dists_matrix, routes_matrix
def dijkstra_function(self): self.draw_button_function() # this is to be sure that the plotted graph and the graph from memory are same node1 = self.dijkstra1.get(1.0, 'end-1c') # this take first node node2 = self.dijkstra2.get(1.0, 'end-1c') # this take second node node1 = str(node1) node2 = str(node2) not_found = False # this for error if one of the nodes in not in the graph if node1 not in self.G: # this check if first node is in the graph self.insert_in_textbox(self.dijkstra_answer, "First node is not in the graph") self.insert_in_textbox(self.dijkstra_answer2, '') not_found = True if node2 not in self.G: # this check if the second node is in the graph if node1 in self.G: self.insert_in_textbox(self.dijkstra_answer, '') txt = "Second node is not in the graph" self.insert_in_textbox(self.dijkstra_answer2, txt) not_found = True # if first node is not in the graph, this will be displayed in dijkstra_answer # if second node is not in the graph, this will be displayed in dijkstra_answer2 if not_found: return # this take the meeting point and distances between (calculated in algorithm) meeting_node, distance1, distance2 = algorithms.dijkstra(self.G, node1, node2, self.is_weighted) # here is the display process txt = str() if not meeting_node == '-1': txt = "One of the optimal meeting points is " + meeting_node + "." else: txt = "There is no meeting point." self.insert_in_textbox(self.dijkstra_answer, txt) txt = str() if distance1 < 999999999999999999999: txt = "Distance from first node to second is " + str(distance1) + "." else: txt = "There is no way from first node to second." if distance2 < 999999999999999999999: txt = txt + " Distance from second node to first is " + str(distance2) + "." else: txt = txt + " There is no way from second node to first." self.insert_in_textbox(self.dijkstra_answer2, txt)
def update_dists_from_each(dists_matrix, routes_matrix, new_location, maze_map, coins): routing_table, dists_table = algo.dijkstra(maze_map, new_location) dists_matrix[new_location] = {} routes_matrix[new_location] = {} for loc in coins: route = u.way_width(routing_table, new_location, loc) dists_matrix[new_location][loc] = dists_table[loc] routes_matrix[new_location][loc] = route dists_matrix[loc][new_location] = dists_table[loc] routes_matrix[loc][new_location] = [l for l in reversed(route[:-1]) ] + [new_location] return dists_matrix, routes_matrix
def test_dijkstra(): g1 = nx.Graph() g1.add_nodes_from([i for i in range(6)]) g1.add_weighted_edges_from([(0, 2, 4), (0, 4, 8), (1, 2, 3), (1, 4, 5), (1, 3, 6), (2, 5, 12), (3, 5, 9), (4, 5, 1)]) min_paths_dict = a.dijkstra(g1, 0) new_nodes = {i: (i, j) for i, j in min_paths_dict.items()} nx.relabel_nodes(g1, new_nodes, copy=False) pos = nx.spring_layout(g1) nx.draw(g1, pos=pos, with_labels=True) labels = {(u, v): g1.get_edge_data(u, v).get('weight') for (u, v) in g1.edges} nx.draw_networkx_edge_labels(g1, pos=pos, edge_labels=labels) plt.show()
def change_way(coins, opponentLocation, player_location): """Return the new coin to search, coin sequence, route and the distance from the player to the first coin of the route""" global best_weight, best_path dist_matrix, route_matrix = u.update_dists_from_each(dists_matrix, routes_matrix, player_location, mazeMap, coins) coins_to_search = get_n_shortest(5, coins, player_location, dists_matrix) ennemy_dists = algo.dijkstra(mazeMap, opponentLocation) for c in coins_to_search: if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]: coins_to_search.remove(c) break best_weight = float("inf") best_path = [] api.debug(coins_to_search) exhaustive(coins_to_search, player_location, [], 0, dist_matrix) meta_route = [player_location] + best_path api.debug(meta_route) route = u.location_list_to_route(meta_route, route_matrix) return coins_to_search, meta_route, route, dist_matrix[player_location][meta_route[1]]
def change_way(coins, opponentLocation, player_location): """Return the new coin to search, coin sequence, route and the distance from the player to the first coin of the route""" global best_weight, best_path dist_matrix, route_matrix = u.update_dists_from_each( dists_matrix, routes_matrix, player_location, mazeMap, coins) coins_to_search = get_n_shortest(5, coins, player_location, dists_matrix) ennemy_dists = algo.dijkstra(mazeMap, opponentLocation) for c in coins_to_search: if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[ player_location][c]: coins_to_search.remove(c) break best_weight = float("inf") best_path = [] api.debug(coins_to_search) exhaustive(coins_to_search, player_location, [], 0, dist_matrix) meta_route = [player_location] + best_path api.debug(meta_route) route = u.location_list_to_route(meta_route, route_matrix) return coins_to_search, meta_route, route, dist_matrix[player_location][ meta_route[1]]
def generate_g(file_name, nodes=10000): #seed = nx.generators.random_graphs.random_regular_graph(4, 80000) seed = build_graph(nodes, 8) edges = [ (u, v, data['weight'] * nodes) for (u, v, data) in seed.edges(data=True) ] g = nx.DiGraph() for (u, v, w) in edges: g.add_edge(u,v, weight=w) s = g.nodes()[0] cover, distances = dijkstra(g, s) t = choice(cover.nodes()) reachable = dict((node, True) for node in cover.nodes()) edges= [ (u, v, w) for (u,v,w) in edges if u in reachable and v in reachable ] nodes = [ (node, seed.node[node]['coords'][0] * nodes, seed.node[node]['coords'][1] * nodes) for node in cover.nodes() ] with open(file_name, 'a') as file: pickle.dump((nodes, edges, s), file)
def determineNextMove(player_location, opponentLocation, coins): """Return the next direction""" global route, currentcoin, meta_route, best_weight, best_path, coins_to_search, index if opponentLocation in coins_to_search: coins_to_search, meta_route, route = change_way( coins, opponentLocation, player_location)[:3] index = 0 elif currentcoin == player_location: if len(route) != 0: old_dist = algo.dijkstra(mazeMap, player_location)[1][meta_route[index + 1]] coins_to_search2, meta_route2, route2, new_dist = change_way( coins, opponentLocation, player_location) #dist_matrix, route_matrix = u.update_dists_from_each(dists_matrix, routes_matrix, player_location, mazeMap, coins) #coins_to_search = get_n_shortest(3, coins, player_location, dists_matrix) #ennemy_dists = algo.dijkstra(mazeMap, opponentLocation) #for c in coins_to_search: #if len(coins_to_search) >= 2 and ennemy_dists[1][c] < dists_matrix[player_location][c]: # coins_to_search.remove(c) #break #best_weight = float("inf") #best_path = [] #exhaustive(coins_to_search, player_location, [], 0, dist_matrix) #meta_route2 = [player_location] + best_path #route2 = u.location_list_to_route(meta_route2, route_matrix) #new_dist = dist_matrix[player_location][meta_route2[1]] if len(route) == 0 or old_dist - new_dist > 3: route = route2 meta_route = meta_route2 index = 0 index += 1 currentcoin = meta_route[index] #api.debug(route) return u.direction(player_location, route.pop(0))
def dists_from_each(locations, maze_map): """ Return the dists and routes from each locations TODO : - Cache routes and dists """ dists_matrix = {l: {} for l in locations} routes_matrix = {l: {} for l in locations} for i in range(len(locations)): l1 = locations[i] routing_table, dists_table = algo.dijkstra(maze_map, l1) for j in range(i + 1, len(locations)): l2 = locations[j] route = u.way_width(routing_table, l1, l2) dists_matrix[l1][l2] = dists_table[l2] routes_matrix[l1][l2] = route dists_matrix[l2][l1] = dists_table[l2] routes_matrix[l2][l1] = [l for l in reversed(route[:-1])] + [l1] return dists_matrix, routes_matrix
def determineNextMove(playerLocation, opponentLocation, coins): global route, currentcoin if coinsNumber != len(coins): routingTable = algo.dijkstra(mazeMap, playerLocation) route = u.way_width(routingTable, playerLocation, coins[0]) return u.direction(playerLocation, route.pop(0))
def main(display, width, rows): grid = helpers.make_grid(rows, WIDTH) start_pos = None end_pos = None running = True already_executed = False generated_walls = False # game loop while running: helpers.draw(display, grid, rows, width, buttons) # event loop for event in pygame.event.get(): pos = pygame.mouse.get_pos() if event.type == pygame.QUIT: running = False # process mouse input if pygame.mouse.get_pressed()[0]: # left mouseclick # if click is in the bottom of the game screen (buttons) if pos[1] > WIDTH: # check each button for a click (needs refactoring) for button in buttons: if button.is_clicked(pos): print("Clicked the button " + button.label) if button.label == "A*": if start_pos and end_pos and already_executed is False: for row in grid: for cell in row: cell.update_neighbors(grid) a_star( lambda: helpers.draw( display, grid, rows, width), grid, start_pos, end_pos) already_executed = True elif already_executed is True: Tk().wm_withdraw() messagebox.showwarning( "Invalid state", "You must clear the board first") else: Tk().wm_withdraw() messagebox.showwarning( "Invalid state", "You must set start and end positions") if button.label == "Clear": start_pos = None end_pos = None already_executed = False generated_walls = False grid = helpers.make_grid(rows, WIDTH) if button.label == "Visited": for row in grid: for cell in row: if cell.is_visited( ) or cell.is_available( ) or cell.is_path(): cell.reset() already_executed = False if button.label == "Dijkstra": if start_pos and end_pos and already_executed is False: for row in grid: for cell in row: cell.update_neighbors(grid) # dijkstra(lambda: helpers.draw(display, grid, # rows, width), grid, start_pos, end_pos) dijkstra( lambda: helpers.draw( display, grid, rows, width), grid, start_pos, end_pos) already_executed = True elif already_executed is True: Tk().wm_withdraw() messagebox.showwarning( "Invalid state", "You must clear the board first") else: Tk().wm_withdraw() messagebox.showwarning( "Invalid state", "You must set start and end positions") if button.label == "Walls" and generated_walls is False: draw_borders( lambda: helpers.draw( display, grid, rows, width), grid, start_pos, end_pos) generate_walls( lambda: helpers.draw( display, grid, rows, width), grid, start_pos, end_pos) generated_walls = True button.clicked = False else: row, col = helpers.get_mouse_pos(pos, rows, WIDTH) cell = grid[row][col] if not start_pos and cell != end_pos: start_pos = cell start_pos.set_start() elif not end_pos and cell != start_pos: end_pos = cell end_pos.set_end() elif cell != end_pos and cell != start_pos: cell.set_barrier() elif pygame.mouse.get_pressed()[2]: # right mouseclick row, col = helpers.get_mouse_pos(pos, rows, WIDTH) cell = grid[row][col] cell.reset() if cell == start_pos: start_pos = None if cell == end_pos: end_pos = None pygame.quit()
def get_route_compare_algs(file_path): all_node = [] all_node_alone = [] all_edge = [] all_edge_alone = [] all_edge_alone_order = [] one_edge = [] one_edge1 = [] one_edge_order = [] one_node = [] source = [] terminal = [] weight = [] flow_sfc = [] fd = file_path + '/SIMPLE_path.txt' if os.path.exists(fd): os.remove(fd) fd = file_path + '/PDA_path.txt' if os.path.exists(fd): os.remove(fd) k = 0 for line in open(file_path + "/flow_feasible_vnf.txt"): flow_path = txt_wrap_by("'", "'", line, 0) # #print flow_path,flow_path[0] flow_sfc.append(flow_path) if k % 3 == 0: source.append(flow_path[0]) terminal.append(flow_path[len(flow_path) - 2]) weight.append(flow_path[len(flow_path) - 1]) k = k + 1 print len(source), len(flow_sfc), k flow_number = len(source) print "flow_number", flow_number for sr in range(0, flow_number): if sr % 100 == 0: print "source is ", sr, source[sr], terminal[sr] main_path_all = [] one_source_edge = [] one_source_edge_order = [] one_source_node = [] main_path = [] for q in range(0, len(flow_sfc[3 * sr]) - 2): #print "sfc",flow_sfc[3*sr][q+1] path = dijkstra(g, str(flow_sfc[3 * sr][q]), str(flow_sfc[3 * sr][q + 1])) main_path_seg = path.get('path') for i in range(0, len(main_path_seg)): main_path_seg[i] = main_path_seg[i].encode(CODEC) for i in range(0, len(main_path_seg) - 1): main_path.append(main_path_seg[i]) if q == len(flow_sfc[3 * sr]) - 3: main_path.append(main_path_seg[len(main_path_seg) - 1]) #print main_path for i in range(0, len(main_path)): main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) one_edge_order = main_path[f + 1] + main_path[f + 2] if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4: siwtch2 = siwtch2 * 100 + int(main_path[f + 2][2]) * 10 + int( main_path[f + 2][3]) if siwtch1 < siwtch2: one_edge = main_path[f + 1] + main_path[f + 2] one_edge1 = main_path[f + 2] + main_path[f + 1] else: one_edge = main_path[f + 2] + main_path[f + 1] one_edge1 = main_path[f + 1] + main_path[f + 2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) # print all_node_alone,all_edge_alone,all_edge_alone_order #sleep(600) one_source_edge_order = [] one_source_node = [] one_source_edge = [] main_path = [] for q in range(0, len(flow_sfc[3 * sr + 1]) - 2): #print "sfc",flow_sfc[3*sr+1][q+1] path = dijkstra2(g, str(flow_sfc[3 * sr + 1][q]), str(flow_sfc[3 * sr + 1][q + 1]), main_path_all) main_path_seg = path.get('path') for i in range(0, len(main_path_seg)): main_path_seg[i] = main_path_seg[i].encode(CODEC) for i in range(0, len(main_path_seg) - 1): main_path.append(main_path_seg[i]) if q == len(flow_sfc[3 * sr + 1]) - 3: main_path.append(main_path_seg[len(main_path_seg) - 1]) #print main_path for i in range(0, len(main_path)): main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) one_edge_order = main_path[f + 1] + main_path[f + 2] if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4: siwtch2 = siwtch2 * 100 + int(main_path[f + 2][2]) * 10 + int( main_path[f + 2][3]) if siwtch1 < siwtch2: one_edge = main_path[f + 1] + main_path[f + 2] one_edge1 = main_path[f + 2] + main_path[f + 1] else: one_edge = main_path[f + 2] + main_path[f + 1] one_edge1 = main_path[f + 1] + main_path[f + 2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) one_source_node = [] one_source_edge = [] one_source_edge_order = [] main_path = [] for q in range(0, len(flow_sfc[3 * sr + 2]) - 2): #print "sfc",flow_sfc[3*sr+2][q+1] path = dijkstra3(g, str(flow_sfc[3 * sr + 2][q]), str(flow_sfc[3 * sr + 2][q + 1]), main_path_all) main_path_seg = path.get('path') for i in range(0, len(main_path_seg)): main_path_seg[i] = main_path_seg[i].encode(CODEC) for i in range(0, len(main_path_seg) - 1): main_path.append(main_path_seg[i]) if q == len(flow_sfc[3 * sr + 2]) - 3: main_path.append(main_path_seg[len(main_path_seg) - 1]) #print main_path for i in range(0, len(main_path)): main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) one_edge_order = main_path[f + 1] + main_path[f + 2] if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4: siwtch2 = siwtch2 * 100 + int(main_path[f + 2][2]) * 10 + int( main_path[f + 2][3]) if siwtch1 < siwtch2: one_edge = main_path[f + 1] + main_path[f + 2] one_edge1 = main_path[f + 2] + main_path[f + 1] else: one_edge = main_path[f + 2] + main_path[f + 1] one_edge1 = main_path[f + 1] + main_path[f + 2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) #print all_node_alone #print all_edge_alone_order weight[sr] = int(weight[sr]) for flow_traffic in range(1, 2): for randome_time in range(0, 1): for entries in range(31, 32): #300-4200 entries = entries * 10000 ce = 10000 #print entries for alg in range(0, 10): if alg == 2: #SIMPLE alg, linke load balancing used_ce = defaultdict(lambda: None) used_entries = defaultdict(lambda: None) for v in all_node: used_entries[v] = 0 for e in all_edge: used_ce[e] = 0 lambda1 = 0 max_entries = 0 temp2 = 0 for i in range(0, flow_number): temp = [[0 for col in range(2)] for row in range(3)] for row in range(0, 3): for col in range(0, 2): temp[row][col] = 0 for j in range(0, 3): for n in range(0, len(all_edge_alone[3 * i + j])): q = 0 for m in range( 0, len(all_edge_alone[3 * i + j])): if all_edge_alone[ 3 * i + j][n] == all_edge_alone[3 * i + j][m]: q = q + 1 if temp[j][1] < (used_ce[all_edge_alone[ 3 * i + j][n]] + q * weight[i]) / float(ce): temp[j][1] = ( used_ce[all_edge_alone[3 * i + j][n]] + q * weight[i]) / float(ce) temp1 = 1000000 k = 0 #Indicate which of several feasible paths is selected for j in range(0, 3): if temp[j][ 1] < temp1: #Select the feasible path that meets the flow table constraints and has the lightest load. k = j temp1 = temp[j][1] for n in range( 0, len(all_edge_alone[3 * i + k]) ): #Select one of the feasible paths (Article k), and use link capacity and flow entries. used_ce[all_edge_alone[3 * i + k] [n]] = used_ce[all_edge_alone[ 3 * i + k][n]] + weight[i] for n in range(0, len(all_node_alone[3 * i + k])): used_entries[all_node_alone[ 3 * i + k][n]] = used_entries[ all_node_alone[3 * i + k][n]] + 1 alg2_choosed_path = [] alg2_choosed_path.append(source[i]) for j in range(0, len(all_node_alone[3 * i + k])): alg2_choosed_path.append(all_node_alone[3 * i + k][j]) alg2_choosed_path.append(terminal[i]) alg2_choosed_path.append(weight[i]) fd = open(file_path + '/SIMPLE_path.txt', 'a') fd.write(str(alg2_choosed_path) + '\n') fd.close() alg2_entries = {} for j in used_entries: if 's' in str(j): alg2_entries[j] = used_entries[j] if max_entries < used_entries[j]: max_entries = used_entries[j] p1 = file_path + "/SIMPLE_entries.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(alg2_entries, indent=1)) fd.close() p1 = file_path + "/SIMPLE_ce.json" #rounding p2 = file_path + "/SIMPLE_NF_load.json" # rounding fd1 = open(p1, 'w') fd2 = open(p2, 'w') alg2_ce_link = {} alg2_ce_middlebox = {} for e in all_edge: isMark = 0 #Link load and midllebox load for key in capacity: if key in e: alg2_ce_middlebox[key] = float( used_ce[e] ) / 2 #eg, path: s4-F2-s4. ce(F2s4)= double, but load(F2) = single isMark = 1 if isMark == 0: alg2_ce_link[e] = float(used_ce[e]) / float(ce) fd1.write(json.dumps(alg2_ce_link, indent=1)) fd1.close() fd2.write(json.dumps(alg2_ce_middlebox, indent=1)) fd2.close() lambda1 = 0 for j in alg2_ce_link: if lambda1 < alg2_ce_link[j]: lambda1 = alg2_ce_link[j] SIMPLE_max_NF = 0 for j in alg2_ce_middlebox: if SIMPLE_max_NF < alg2_ce_middlebox[j]: SIMPLE_max_NF = alg2_ce_middlebox[j] print "SIMPLE", lambda1, max_entries p1 = file_path + "/SIMPLE_lambda.json" fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.close() p1 = file_path + "/SIMPLE_max_entries.json" fd = open(p1, 'w') fd.write(json.dumps(max_entries)) fd.close() p1 = file_path + "/SIMPLE_max_NF.json" fd = open(p1, 'w') fd.write(json.dumps(SIMPLE_max_NF)) fd.close() if alg == 3: #PDA alg used_ce = defaultdict(lambda: None) used_entries = defaultdict(lambda: None) for v in all_node: used_entries[v] = 0 for e in all_edge: used_ce[e] = 0 lambda1 = 0 max_entries = 0 temp2 = 0 for i in range(0, flow_number): temp = [[0 for col in range(2)] for row in range(3)] for row in range(0, 3): for col in range(0, 2): temp[row][col] = 0 for j in range(0, 3): for n in range(0, len(all_node_alone[3 * i + j])): q = 0 for m in range( 0, len(all_node_alone[3 * i + j])): if all_node_alone[ 3 * i + j][n] == all_node_alone[3 * i + j][m]: q = q + 1 if temp[j][1] < used_entries[ all_node_alone[3 * i + j][n]] + q: temp[j][1] = used_entries[ all_node_alone[3 * i + j][n]] + q temp1 = 10000000 k = 0 #Indicate which of several feasible paths is selected for j in range(0, 3): if temp[j][ 1] < temp1: #Select the feasible path that meets the flow table constraints and has the lightest load. k = j temp1 = temp[j][1] for n in range( 0, len(all_edge_alone[3 * i + k]) ): #Select one of the feasible paths (Article k), and use link capacity and flow entries. used_ce[all_edge_alone[3 * i + k] [n]] = used_ce[all_edge_alone[ 3 * i + k][n]] + weight[i] #if used_ce[all_edge_alone[3*i+k][n]]/float(ce) > lambda1: #lambda1 = used_ce[all_edge_alone[3*i+k][n]]/float(ce) #Record the largest lambda for n in range(0, len(all_node_alone[3 * i + k])): used_entries[all_node_alone[ 3 * i + k][n]] = used_entries[ all_node_alone[3 * i + k][n]] + 1 alg3_choosed_path = [] alg3_choosed_path.append(source[i]) for j in range(0, len(all_node_alone[3 * i + k])): alg3_choosed_path.append(all_node_alone[3 * i + k][j]) alg3_choosed_path.append(terminal[i]) alg3_choosed_path.append(weight[i]) fd = open(file_path + '/PDA_path.txt', 'a') fd.write(str(alg3_choosed_path) + '\n') fd.close() print "PDA_used_ce", used_ce["s3F3"], used_ce[ "F3s3"], used_ce["s1s3"], used_ce["s3s1"], all_edge alg3_entries = {} for j in used_entries: if 's' in str(j): alg3_entries[j] = used_entries[j] if max_entries < used_entries[j]: max_entries = used_entries[j] p1 = file_path + "/PDA_entries.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(alg3_entries, indent=1)) fd.close() p1 = file_path + "/PDA_ce.json" #rounding p2 = file_path + "/PDA_NF_load.json" # rounding fd1 = open(p1, 'w') fd2 = open(p2, 'w') alg3_ce_link = {} alg3_ce_middlebox = {} for e in all_edge: isMark = 0 #Link load and midllebox load for key in capacity: if key in e: alg3_ce_middlebox[key] = float( used_ce[e]) / 2 isMark = 1 if isMark == 0: alg3_ce_link[e] = float(used_ce[e]) / float(ce) fd1.write(json.dumps(alg3_ce_link, indent=1)) fd1.close() fd2.write(json.dumps(alg3_ce_middlebox, indent=1)) fd2.close() lambda1 = 0 for j in alg3_ce_link: if lambda1 < alg3_ce_link[j]: lambda1 = alg3_ce_link[j] PDA_max_NF = 0 for j in alg3_ce_middlebox: if PDA_max_NF < alg3_ce_middlebox[j]: PDA_max_NF = alg3_ce_middlebox[j] print "PDA", lambda1, max_entries p1 = file_path + "/PDA_lambda.json" fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.close() p1 = file_path + "/PDA_max_entries.json" fd = open(p1, 'w') fd.write(json.dumps(max_entries)) fd.close() p1 = file_path + "/PDA_max_NF.json" fd = open(p1, 'w') fd.write(json.dumps(PDA_max_NF)) fd.close() print "Compare_algs Finished"
if (node.position_x, node.position_y) != grid.start_node and ( node.position_x, node.position_y) != grid.end_node: if node.visited: draw_node(node.position_x, node.position_y, yellow) if node.obstacle: draw_node(node.position_x, node.position_y, dark_grey) for node in old_visited_list: if node not in visited_list and not node.obstacle: draw_node(node.position_x, node.position_y, white) if run_a_star: visited_list, old_visited_list, total_cost, total_time = algorithms.a_star( nodes_list, grid.start_node, grid.end_node, visited_list) elif run_dijkstra: visited_list, old_visited_list, total_cost, total_time = algorithms.dijkstra( nodes_list, grid.start_node, grid.end_node, visited_list) if run_a_star or run_dijkstra: start_node = nodes_list[grid.start_node[0]][grid.start_node[1]] end_node = nodes_list[grid.end_node[0]][grid.end_node[1]] path = end_node path_dist = 0 path_list.clear() while path.parent is not None: if path.position_x == start_node.position_x and path.position_y == start_node.position_y: break path_dist = path_dist + path.parent_dist path_list.append(path.parent) path = path.parent # Draw path for node in path_list:
def initializationCode(mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins): global route routingTable = algo.dijkstra(mazeMap, playerLocation) route = u.way_width(routingTable, playerLocation, (0, mazeWidth - 1))
def initializationCode (mazeWidth, mazeHeight, mazeMap, timeAllowed, playerLocation, opponentLocation, coins) : global route routingTable = algo.dijkstra(mazeMap, playerLocation) route = u.way_width(routingTable, playerLocation, (0, mazeWidth - 1))
def test_regular(self): with open(os.path.join(base_path, 'data/regular.json')) as graph_file: graph = Graph(graph_file) distance, path = dijkstra(graph, "1", "2") self.assertEqual(path, ['1', '3', '4', '5', '2']) self.assertEqual(distance, 5)
def gen_data(number_of_flow, net, start_node): all_node = [] #所有节点 [] all_node_alone = [] #每个路径的所有节点 [[][]] all_edge = [] #所有边 [] all_edge_alone = [] #每个路径的所有边 [[][]] source = [] terminal = [] source_random = [] terminal_random = [] one_edge = [] one_node = [] source_tmp = [] source_number = len(start_node) for i in range(0, source_number - 1): source_tmp.extend([i for jj in range(0, source_number - 1 - i)]) #0...0 1...1 2...2 terminal_tmp = [] for i in range(0, source_number - 1): terminal_tmp.extend(range(source_number - 1, i, -1)) #128...1 128...2 128...3 random.shuffle(source_tmp) random.shuffle(terminal_tmp) # print source_tmp # print terminal_tmp # exit() count = 0 source_random = [] terminal_random = [] while count < number_of_flow: s = random.choice(source_tmp) d = random.choice(terminal_tmp) while s == d: s = random.choice(source_tmp) d = random.choice(terminal_tmp) source_random.append(s) #源节点的索引 terminal_random.append(d) #目的节点的索引 count += 1 source = [ start_node[source_random[i]] for i in range(0, len(source_random)) ] #6000 terminal = [ start_node[terminal_random[i]] for i in range(0, len(terminal_random)) ] #6000 number_of_bigflow = len(source_random) / 4 number_of_smallflow = len(source_random) - number_of_bigflow for sr in range(0, len(source_random)): main_path_all = [] one_source_edge = [] one_source_node = [] path = dijkstra(g, source[sr], terminal[sr]) main_path = path.get('path') #每条流的路径 for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): switch1 = toInt(main_path[f + 1]) switch2 = toInt(main_path[f + 2]) if switch1 < switch2: one_edge = main_path[f + 1] + main_path[f + 2] else: one_edge = main_path[f + 2] + main_path[f + 1] if one_edge not in all_edge: all_edge.append(one_edge) one_source_edge.append(one_edge) all_edge_alone.append(one_source_edge) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) #path2 one_source_edge = [] one_source_node = [] path = dijkstra2(g, source[sr], terminal[sr], main_path_all) main_path = path.get('path') # 每条流的路径 for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): switch1 = toInt(main_path[f + 1]) switch2 = toInt(main_path[f + 2]) if switch1 < switch2: one_edge = main_path[f + 1] + main_path[f + 2] else: one_edge = main_path[f + 2] + main_path[f + 1] if one_edge not in all_edge: all_edge.append(one_edge) one_source_edge.append(one_edge) all_edge_alone.append(one_source_edge) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) #path3 one_source_edge = [] one_source_node = [] path = dijkstra3(g, source[sr], terminal[sr], main_path_all) main_path = path.get('path') # 每条流的路径 for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): switch1 = toInt(main_path[f + 1]) switch2 = toInt(main_path[f + 2]) if switch1 < switch2: one_edge = main_path[f + 1] + main_path[f + 2] else: one_edge = main_path[f + 2] + main_path[f + 1] if one_edge not in all_edge: all_edge.append(one_edge) one_source_edge.append(one_edge) all_edge_alone.append(one_source_edge) #print all_edge_alone for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) #print all_node_alone # print main_path_all # print all_edge # print all_node #all_node 所有出现的节点 all_edge 所有出现的边 all_node_alone 一个list中为一条路径中出现的节点 all_edge_alone 一个list中为一条路径中出现的边 pickle.dump( all_node_alone, open( "./paper_data2/all_node_alone" + "_" + str(number_of_flow) + ".dat", "wb"), True) pickle.dump( all_edge_alone, open( "./paper_data2/all_edge_alone" + "_" + str(number_of_flow) + ".dat", "wb"), True) pickle.dump( source_random, open( "./paper_data2/source_random" + "_" + str(number_of_flow) + ".dat", "wb"), True) pickle.dump( terminal_random, open( "./paper_data2/terminal_random" + "_" + str(number_of_flow) + ".dat", "wb"), True) pickle.dump( all_node, open("./paper_data2/all_node" + "_" + str(number_of_flow) + ".dat", "wb"), True) pickle.dump( all_edge, open("./paper_data2/all_edge" + "_" + str(number_of_flow) + ".dat", "wb"), True)
if node is False: continue v = node way = [] while v != -1: way.append(v) v = prev[v] last = way[-1] s = 'd={}: ({})'.format(dist[node], last + 1) for v in reversed(way[:-1]): s += '-{}->({})'.format(int(g_mat[last][v]), v + 1) last = v print s TASK1_START = 0 print '-' * 35 + '\ntask1 - shortest ways from ({})\n'.format(TASK1_START + 1) + '-' * 35 g_list, g_mat = read_graph_list('task1.in') dist, prev, table = dijkstra(g_list, TASK1_START) user_interaction(prev, dist, g_mat) write_debug_table('task1.out.md', table) print '\n' + '-' * 35 + '\ntask2\n' + '-' * 35 os.remove('task2.out.md') g_mat = read_graph_matrix('task2.in') dist, path = floyd(g_mat)
vnf.append(I) for i in range(0,10): P="P"+str(i+1) vnf.append(P) for i in range(0,10): D="D"+str(i+1) vnf.append(D) for i in range(0,10): W="W"+str(i+1) vnf.append(W) times = 0 for i in switch: print i for j in vnf: #print j path = dijkstra(g, i, j) main_path = path.get('path') main_cost = path.get('cost') file.write(str(main_path) + '\n') times +=1 #print (main_path) #sleep(30) for k in host: path = dijkstra(g,i,k) main_path = path.get('path') #for i in range(0,len(main_path)): # main_path[i] = main_path[i].encode(CODEC) # print "main_path",main_path[i] file.write(str(main_path) + '\n') times += 1 print times,"Finished"
for i in range(0, len(terminal_random)): terminal.append(start_node[int(terminal_random[i])]) number_of_bigflow = 3980 number_of_smallflow = len( source_random ) - number_of_bigflow #the number of elephant flow and mice flow, they obey 2-8 distribute #print"start SR" ,weight for sr in range(0, len(source_random)): if sr % 1000 == 0: print "source is ", sr main_path_all = [] one_source_edge = [] one_source_node = [] path = dijkstra(g, source[sr], terminal[sr]) main_path = path.get('path') for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4:
def get_route_alg1(file_path): all_node = [] all_node_alone = [] all_edge = [] all_edge_alone = [] all_edge_alone_order = [] one_edge = [] one_edge1 = [] one_edge_order = [] one_node = [] source = [] terminal = [] weight = [] fd = file_path + '/alg1_path_segment.txt' if os.path.exists(fd): os.remove(fd) fd = file_path + '/alg2_path_segment.txt' if os.path.exists(fd): os.remove(fd) fd = file_path + '/alg1_path.txt' if os.path.exists(fd): os.remove(fd) fd = file_path + '/alg2_path.txt' if os.path.exists(fd): os.remove(fd) fd = file_path + '/flows.txt' if os.path.exists(fd): os.remove(fd) fd = file_path + '/paths.txt' if os.path.exists(fd): os.remove(fd) for line in open(file_path + "/flow_vnf.txt"): flow_path = txt_wrap_by("'", "'", line, 0) #print flow_path,flow_path[0] for i in range(0, len(flow_path) - 2): #print flow_path[i],flow_path[i+1] source.append(flow_path[i]) terminal.append(flow_path[i + 1]) weight.append(flow_path[len(flow_path) - 1]) # The above is to randomly generate the stream and the stream bandwidth, that is, generate soure[], terminal[], weight[], as the input of the next part of the code routing. flow_number = len(source) print "flow_number", flow_number for sr in range(0, flow_number): if sr % 1000 == 0: print "source is ", sr, source[sr], terminal[sr] main_path_all = [] one_source_edge = [] one_source_edge_order = [] one_source_node = [] path = dijkstra(g, source[sr], terminal[sr]) main_path = path.get('path') for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) one_edge_order = main_path[f + 1] + main_path[f + 2] if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4: siwtch2 = siwtch2 * 100 + int(main_path[f + 2][2]) * 10 + int( main_path[f + 2][3]) if siwtch1 < siwtch2: one_edge = main_path[f + 1] + main_path[f + 2] one_edge1 = main_path[f + 2] + main_path[f + 1] else: one_edge = main_path[f + 2] + main_path[f + 1] one_edge1 = main_path[f + 1] + main_path[f + 2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) one_source_edge_order = [] one_source_node = [] one_source_edge = [] path = dijkstra2(g, source[sr], terminal[sr], main_path_all) main_path = path.get('path') for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) one_edge_order = main_path[f + 1] + main_path[f + 2] if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4: siwtch2 = siwtch2 * 100 + int(main_path[f + 2][2]) * 10 + int( main_path[f + 2][3]) if siwtch1 < siwtch2: one_edge = main_path[f + 1] + main_path[f + 2] one_edge1 = main_path[f + 2] + main_path[f + 1] else: one_edge = main_path[f + 2] + main_path[f + 1] one_edge1 = main_path[f + 1] + main_path[f + 2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) one_source_node = [] one_source_edge = [] one_source_edge_order = [] path = dijkstra3(g, source[sr], terminal[sr], main_path_all) main_path = path.get('path') for i in range(0, len(main_path)): main_path[i] = main_path[i].encode(CODEC) main_path_all.append(main_path[i]) for f in range(0, len(main_path) - 3): siwtch1 = int(main_path[f + 1][1]) siwtch2 = int(main_path[f + 2][1]) one_edge_order = main_path[f + 1] + main_path[f + 2] if len(main_path[f + 1]) == 3: siwtch1 = siwtch1 * 10 + int(main_path[f + 1][2]) if len(main_path[f + 1]) == 4: siwtch1 = siwtch1 * 100 + int(main_path[f + 1][2]) * 10 + int( main_path[f + 1][3]) if len(main_path[f + 2]) == 3: siwtch2 = siwtch2 * 10 + int(main_path[f + 2][2]) if len(main_path[f + 2]) == 4: siwtch2 = siwtch2 * 100 + int(main_path[f + 2][2]) * 10 + int( main_path[f + 2][3]) if siwtch1 < siwtch2: one_edge = main_path[f + 1] + main_path[f + 2] one_edge1 = main_path[f + 2] + main_path[f + 1] else: one_edge = main_path[f + 2] + main_path[f + 1] one_edge1 = main_path[f + 1] + main_path[f + 2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0, len(main_path) - 2): one_node = main_path[f + 1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) weight[sr] = int(weight[sr]) for flow_traffic in range(1, 2): alg0 = 0 alg1 = 0 alg2 = 0 alg3 = 0 for randome_time in range(0, 1): for entries in range(31, 32): #300-4200 entries = entries * 10000 ce = 10000 #print entries for alg in range(0, 1): if alg == 0: x_e = defaultdict(lambda: (defaultdict(lambda: ( defaultdict(lambda: None))))) x_v = defaultdict(lambda: (defaultdict(lambda: ( defaultdict(lambda: None))))) x = [ [0 for col in range(3)] for row in range(flow_number) ] #3 is the number of path that each flow can choose for i in range(0, flow_number): for j in range( 0, 3 ): #3 is the number of path that each flow can choose x[i][j] = "x" if flow_number < 100000: if i < 10: x[i][j] = x[i][j] + "0000" elif i < 100: x[i][j] = x[i][j] + "000" elif i < 1000: x[i][j] = x[i][j] + "00" elif i < 10000: x[i][j] = x[i][j] + "0" x[i][j] = x[i][j] + str(i) + str(j) z = [] for i in range(0, flow_number): z.append("z" + str(i)) temp = [] for i in range(0, flow_number): for j in range( 0, 3 ): #3 si the number of path that each flow can choose temp.append(x[i][j]) for i in range(0, flow_number): temp.append(z[i]) prob = LpProblem('lptest', LpMinimize) r = LpVariable('r', lowBound=0) xx = LpVariable.dicts("", temp, lowBound=0, upBound=1) prob += r for i in range(0, flow_number): prob += lpSum([xx[j] for j in x[i]]) == xx[z[ i]] #that means, x[i][0]+x[i][1]....+x[i][len(x[i])-1] = 1 prob += xx[z[i]] == 1 print "len(all_edge_alone)", len(all_edge_alone) for i in range(0, len(all_edge_alone)): for j in range( 0, len(all_edge_alone[i]) ): #for example, all_edge_alone[i][j] = v1v2 length_x_e = len(x_e[all_edge_alone[i][j]]) #print "length_x_e",length_x_e,all_edge_alone[i],all_edge_alone[i][j],i,j x_e[all_edge_alone[i][j]][length_x_e][0] = x[ i / 3][i % 3] x_e[all_edge_alone[i] [j]][length_x_e][1] = weight[i / 3] print "222" for h in all_edge: prob += lpSum(x_e[h][i][1] * xx[x_e[h][i][0]] for i in x_e[h]) <= ce * r for i in range(0, len(all_node_alone)): for j in range(0, len(all_node_alone[i])): length_x_v = len(x_v[all_node_alone[i][j]]) if i % 3 == 0: x_v[all_node_alone[i][j]][length_x_v][ 1] = 0 #choose dijstra path do not need extra entries x_v[all_node_alone[i] [j]][length_x_v][0] = x[i / 3][i % 3] else: x_v[all_node_alone[i] [j]][length_x_v][0] = x[i / 3][i % 3] if j == len(all_node_alone[i]) - 1: x_v[all_node_alone[i][j]][length_x_v][ 1] = 0 #the last hop do not need extra entries else: x_v[all_node_alone[i] [j]][length_x_v][1] = 1 print "333" for v in all_node: prob += lpSum(x_v[v][i][1] * xx[x_v[v][i][0]] for i in x_v[v]) <= entries GLPK().solve(prob) print 'objective_sr =', value(prob.objective) p1 = file_path + "/alg0.json" fd = open(p1, 'w') fd.write(json.dumps(value(prob.objective))) fd.close() i = 0 j = 0 l = 0 for v in prob.variables(): l = l + 1 if l < flow_number * 3 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 2: j = j + 1 else: i = i + 1 j = 0 lambda1 = 0 used_ce = defaultdict(lambda: None) for e in all_edge: used_ce[e] = 0 flows_add = {} for i in range(0, flow_number): k = 0 choose = 0 choosed_path = [] for j in range(0, 3): if x[i][j] >= k: choose = j k = x[i][j] for n in range(0, len(all_edge_alone[3 * i + choose])): used_ce[all_edge_alone[3 * i + choose] [n]] = used_ce[all_edge_alone[ 3 * i + choose][n]] + weight[i] if used_ce[all_edge_alone[3 * i + choose] [n]] / float(ce) > lambda1: lambda1 = used_ce[all_edge_alone[ 3 * i + choose][n]] / float(ce) choosed_path.append(source[i]) for j in range(0, len(all_node_alone[3 * i + choose])): choosed_path.append(all_node_alone[3 * i + choose][j]) choosed_path.append(terminal[i]) choosed_path.append(str(weight[i])) fd = open(file_path + '/alg1_path_segment.txt', 'a') fd.write(str(choosed_path) + '\n') fd.close() num_add_flow = 0 for key in range( 0, len(all_edge_alone_order[3 * i + choose])): if all_edge_alone_order[3 * i + choose][ key] not in all_edge_alone_order[3 * i]: #['v2v9', 'v9v7', 'v7v1']->['', '2', '9']['', '9', '7']['', '7', '1'] add_flow_key = str(all_edge_alone_order[ 3 * i + choose][key]).split('s') for num_i in range(0, 3): add_flow_key[num_i] = 's' + str( add_flow_key[num_i]) #print add_flow_key[1] #If the node is not in the dictionary, vi=1, if the dictionary is in the dictionary, flows_add[vi] = flows_add[vi]+1 if add_flow_key[1] not in flows_add: flows_add[add_flow_key[1]] = 1 else: flows_add[add_flow_key[1]] = flows_add[ add_flow_key[1]] + 1 #print add_flow_key #print "-------------->",all_node for key_all in all_node: if key_all not in flows_add: flows_add[key_all] = 0 p11 = file_path + "/alg1_entries.json" # rounding fd = open(p11, 'w') fd.write(json.dumps(flows_add, indent=1)) fd.close() alg1_max_entries = 0 for j in flows_add: if alg1_max_entries < flows_add[j]: alg1_max_entries = flows_add[j] p11 = file_path + "/alg1_max_entries.json" # rounding fd = open(p11, 'w') fd.write(json.dumps(alg1_max_entries)) fd.close() file_object = open(file_path + '/alg1_path_segment.txt') try: data = file_object.read() finally: file_object.close() print "data", data data = data.replace("]", "],") data_len = len(data) - 2 data = '[' + data[:data_len] + ']' data_list = list(eval(data)) print "data_list", data_list, len(data_list) stack = [] new_path = [] for i in range(0, len(data_list)): if len(stack) == 0: stack.append(data_list[i]) else: if 'h' not in data_list[i][len(data_list[i]) - 2]: stack.append(data_list[i]) else: #If the last host is found, merge this link this_path = [] for len_stack in range(0, len(stack)): a = stack.pop() this_path.append(a) one_path = [] #print this_path for t in range(0, len(this_path)): index = len(this_path) - t - 1 #If it is the last list of this path, take the first four values if index == (len(this_path) - 1): for w in range( 0, (len(this_path[index]) - 1)): one_path.append( this_path[index][w]) else: for w in range( 1, (len(this_path[index]) - 1)): one_path.append( this_path[index][w]) for w in range(1, len(data_list[i])): one_path.append(data_list[i][w]) print "one_path", one_path fd = open(file_path + '/alg1_path.txt', 'a') fd.write(str(one_path) + '\n') fd.close() p1 = file_path + "/alg1_ce.json" #rounding fd = open(p1, 'w') alg1_ce = {} for e in all_edge: alg1_ce[e] = float(used_ce[e]) / float(ce) fd.write(json.dumps(alg1_ce, indent=1)) fd.close() print lambda1 p1 = file_path + "/alg1_lambda.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.close() if alg == 2: #heuristic algorithm ,ospf need entries used_ce = defaultdict(lambda: None) used_entries = defaultdict(lambda: None) for v in all_node: used_entries[v] = 0 for e in all_edge: used_ce[e] = 0 lambda1 = 0 temp2 = 0 for i in range(0, flow_number): temp = [[0 for col in range(2)] for row in range(3)] for row in range(0, 3): for col in range(0, 2): temp[row][col] = 0 for j in range(0, 3): for n in range(0, len(all_node_alone[3 * i + j])): if used_entries[all_node_alone[3 * i + j] [n]] + 1 > entries * 10000: temp[j][0] = 1 for n in range(0, len(all_edge_alone[3 * i + j])): if temp[j][1] < (used_ce[all_edge_alone[ 3 * i + j][n]] + weight[i]) / float(ce): temp[j][1] = (used_ce[all_edge_alone[ 3 * i + j][n]] + weight[i]) / float(ce) temp1 = 10000 k = 0 #Indicate which of several feasible paths is selected for j in range(0, 3): if temp[j][0] != 1: if temp[j][ 1] < temp1: #Select the feasible path that meets the flow table constraints and has the lightest load. k = j temp1 = temp[j][1] for n in range( 0, len(all_edge_alone[3 * i + k]) ): #Select one of the feasible paths (Article k), and use link capacity and flow entries. used_ce[all_edge_alone[3 * i + k] [n]] = used_ce[all_edge_alone[ 3 * i + k][n]] + weight[i] if used_ce[all_edge_alone[3 * i + k] [n]] / float(ce) > lambda1: lambda1 = used_ce[ all_edge_alone[3 * i + k][n]] / float( ce) #Record the largest lambda for n in range(0, len(all_node_alone[3 * i + k])): used_entries[all_node_alone[ 3 * i + k][n]] = used_entries[ all_node_alone[3 * i + k][n]] + 1 #print lambda1,all_edge_alone[3*i+k] alg2_choosed_path = [] alg2_choosed_path.append(source[i]) for j in range(0, len(all_node_alone[3 * i + k])): alg2_choosed_path.append(all_node_alone[3 * i + k][j]) alg2_choosed_path.append(terminal[i]) alg2_choosed_path.append(weight[i]) fd = open(file_path + '/alg2_path_segment.txt', 'a') fd.write(str(alg2_choosed_path) + '\n') fd.close() file_object = open(file_path + '/alg2_path_segment.txt') try: data = file_object.read() finally: file_object.close() data = data.replace("]", "],") data_len = len(data) - 2 data = '[' + data[:data_len] + ']' data_list = list(eval(data)) print data_list stack = [] new_path = [] for i in range(0, len(data_list)): if len(stack) == 0: stack.append(data_list[i]) else: if 'h' not in data_list[i][len(data_list[i]) - 2]: stack.append(data_list[i]) else: this_path = [] for len_stack in range(0, len(stack)): a = stack.pop() this_path.append(a) one_path = [] print this_path for t in range(0, len(this_path)): index = len(this_path) - t - 1 if index == (len(this_path) - 1): for w in range( 0, (len(this_path[index]) - 1)): one_path.append( this_path[index][w]) else: for w in range( 1, (len(this_path[index]) - 1)): one_path.append( this_path[index][w]) for w in range(1, len(data_list[i])): one_path.append(data_list[i][w]) print one_path fd = open(file_path + '/alg2_path.txt', 'a') fd.write(str(one_path) + '\n') fd.close() p1 = file_path + "/alg2_entries.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(used_entries, indent=1)) fd.close() p1 = file_path + "/alg2_ce.json" #rounding fd = open(p1, 'w') alg2_ce = {} for e in all_edge: alg2_ce[e] = float(used_ce[e]) / float(ce) fd.write(json.dumps(alg2_ce, indent=1)) fd.close() print "greedy", lambda1 p1 = file_path + "/alg2_lambda.json" fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.write(json.dumps("/")) fd.close() alg2 = alg2 + lambda1 if alg == 3: #ospf lambda1 = 0 used_ce = defaultdict(lambda: None) used_entries = defaultdict(lambda: None) for v in all_node: used_entries[v] = 0 for e in all_edge: used_ce[e] = 0 for i in range(0, flow_number): for n in range(0, len(all_edge_alone[3 * i])): #print n used_ce[all_edge_alone[3 * i][n]] = used_ce[ all_edge_alone[3 * i][n]] + weight[i] if used_ce[all_edge_alone[3 * i][n]] / float( ce) > lambda1: #print "hhhhh" lambda1 = used_ce[all_edge_alone[3 * i] [n]] / float(ce) #print weight[i],source[i],terminal[i],lambda1,all_edge_alone[3*i+choose] print "ospf", lambda1 p1 = file_path + "/alg3_lambda.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.write(json.dumps("/")) fd.close() alg3 = alg3 + lambda1 p1 = file_path + "/alg3_ce.json" #rounding fd = open(p1, 'w') alg3_ce = {} for e in all_edge: alg3_ce[e] = float(used_ce[e]) / float(ce) fd.write(json.dumps(alg3_ce, indent=1)) fd.close() print "Alg1 Finished"
def get_targets(g, s, ranks): _, distances = dijkstra(g, s) distances = distances.items() distances.sort(key=lambda (x,y): y) targets = [ (distances[r][0], r) for r in ranks] return targets
for row in rows: GMaster.add_edge(row[0], row[1], row[2]) for irow in interrows: GMaster.add_edge(irow[0], irow[1], irow[2]) print "Calculate Routes..." # Holds all routes in memory to aid filtering fromToDict = {} for stopFrom in fullStops: print "Currently working on:", stopFrom stA, t = spsl(stopFrom) G = copy.deepcopy(GMaster) distances, previous = algorithms.dijkstra(G, stopFrom) for stopTo in fullStops: stB, t = spsl(stopTo) weight = distances[stopTo] dKey = (stA, stB) if (dKey not in fromToDict or fromToDict[dKey] > weight): fromToDict[dKey] = weight print "Saving Routes..." with conn: insStr = """INSERT INTO FULLROUTES (STATIONA, STATIONB, WEIGHT) VALUES (?, ?, ?) """ for stations, weightBB in fromToDict.iteritems():
def get_nfv_distribution(ratio, host_num, file_path): CODEC = 'utf-8' mynet = "8s9vnf.json" g = DiGraph(mynet) #自定义VNF的处理能力(1-10) capacity = { 'F1': 10, 'F2': 10, 'F3': 10, 'I1': 10, 'I2': 10, 'I3': 10, 'P1': 10, 'P2': 10, 'P3': 10 } each_NF_num = 3 #the number of 80 hosts flowDic = get_flows(host_num, ratio) flow_number = len(flowDic) print "flow_number", flow_number #Find out which streams of each type of NF have passed flow = {} flow['F'] = [] flow['I'] = [] flow['P'] = [] for key in flowDic: if 'F' in str(flowDic[key][3]): flow['F'].append(key) if 'I' in str(flowDic[key][3]): flow['I'].append(key) if 'P' in str(flowDic[key][3]): flow['P'].append(key) #j stands for 9 processors, i stands for task (flow) p = [([0] * len(flow) * 3) for i in range(len(flowDic))] for j in capacity.keys(): #compute Pij= brand*distance(S-VNF-D) / processors if 'F' in j: if '1' in j: row = 0 elif '2' in j: row = 1 elif '3' in j: row = 2 for i in flow['F']: #src,vnf,dst # print flowDic[i][0],j,flowDic[i][1] path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] #print power p[i - 1][row] = bandwidth * distance / power #print i #print p[i-1][row] elif 'I' in j: if '1' in j: row = 3 elif '2' in j: row = 4 elif '3' in j: row = 5 for i in flow['I']: #print flowDic[i][0],j,flowDic[i][1] path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] #print power p[i - 1][row] = bandwidth * distance / power #print i #print p[i-1][row] elif 'P' in j: if '1' in j: row = 6 elif '2' in j: row = 7 elif '3' in j: row = 8 for i in flow['P']: #print flowDic[i][0],j,flowDic[i][1] path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] #print power p[i - 1][row] = bandwidth * distance / power #print i #print p[i-1][row] print "p", p ''' Solving linear equation ''' #x[i][j] :Indicates that the ith stream passes through the jth vnf, and j(0-8) represents [F1, F2, F3, I1, I2, I3, P1, P2, P3] '''Which F passed''' x = [[0 for col in range(3)] for row in range(len(flowDic))] #print x for i in range(0, len(flowDic)): for j in range(0, 3): x[i][j] = "x" if len(flowDic) < 100000: if i < 10: x[i][j] = x[i][j] + "0000" elif i < 100: x[i][j] = x[i][j] + "000" elif i < 1000: x[i][j] = x[i][j] + "00" elif i < 10000: x[i][j] = x[i][j] + "0" x[i][j] = x[i][j] + str(i) + str(j) z = [] for i in range(0, len(flowDic)): z.append("z" + str(i)) temp = [] for i in range(0, len(flowDic)): for j in range(0, 3): # 3 si the number of path that each flow can choose temp.append(x[i][j]) for i in range(0, len(flowDic)): temp.append(z[i]) prob = LpProblem('lptest', LpMinimize) r = LpVariable('r', lowBound=0) xx = LpVariable.dicts("", temp, lowBound=0, upBound=1) #,cat = pulp.LpInteger print x[0] #Add target equation prob += r #0->F(0,1,2);1->I(3,4,5);2->P(6,7,8) x_e = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(3): for j in range(len(flowDic)): x_e[i][j][0] = x[j][i] x_e[i][j][1] = p[j][i] for i in range(0, 3): prob += lpSum(xx[x_e[i][j][0]] * x_e[i][j][1] for j in x_e[i]) <= r #Add constraints for i in range(0, len(flowDic)): prob += lpSum([xx[j] for j in x[i]]) == 1 #solve the question GLPK().solve(prob) print 'objective_sr =', value(prob.objective) i = 0 j = 0 l = 0 for v in prob.variables(): l = l + 1 if l < flow_number * 3 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 2: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 3): if x[i][j] >= k: choose = j k = x[i][j] for j in range(0, 3): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('F' in str(flowDic[i + 1][3])): for j in range(0, 3): if x[i][j] == 1: flowDic[i + 1].append(j) '''which I''' p_1 = [([0] * 3) for i in range(len(flowDic))] for i in range(len(flowDic)): for j in range(3): p_1[i][j] = p[i][j + 3] x1 = [[0 for col in range(3)] for row in range(len(flowDic))] for i in range(0, len(flowDic)): for j in range(0, 3): x1[i][j] = "x" if len(flowDic) < 100000: if i < 10: x1[i][j] = x1[i][j] + "0000" elif i < 100: x1[i][j] = x1[i][j] + "000" elif i < 1000: x1[i][j] = x1[i][j] + "00" elif i < 10000: x1[i][j] = x1[i][j] + "0" x1[i][j] = x1[i][j] + str(i) + str(j) z1 = [] for i in range(0, len(flowDic)): z1.append("z" + str(i)) temp1 = [] for i in range(0, len(flowDic)): for j in range(0, 3): # 3 si the number of path that each flow can choose temp1.append(x1[i][j]) for i in range(0, len(flowDic)): temp1.append(z1[i]) prob1 = LpProblem('lptest1', LpMinimize) r1 = LpVariable('r1', lowBound=0) xx1 = LpVariable.dicts("", temp1, lowBound=0, upBound=1) #, cat = pulp.LpInteger prob1 += r1 #0->F(0,1,2);1->I(3,4,5);2->P(6,7,8) x_e1 = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(3): for j in range(len(flowDic)): x_e1[i][j][0] = x1[j][i] x_e1[i][j][1] = p_1[j][i] for i in range(0, 3): prob1 += lpSum(xx1[x_e1[i][j][0]] * x_e1[i][j][1] for j in x_e1[i]) <= r1 for i in range(0, len(flowDic)): prob1 += lpSum([xx1[j] for j in x1[i]]) == 1 GLPK().solve(prob1) print 'objective_sr =', value(prob1.objective) i = 0 j = 0 l = 0 for v in prob1.variables(): l = l + 1 if l < flow_number * 3 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 2: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 3): if x[i][j] >= k: choose = j k = x[i][j] for j in range(0, 3): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('I' in str(flowDic[i + 1][3])): for j in range(0, 3): if x[i][j] == 1: flowDic[i + 1].append(j) '''which P''' p_3 = [([0] * 3) for i in range(len(flowDic))] for i in range(len(flowDic)): for j in range(3): p_3[i][j] = p[i][j + 6] print p_3 x3 = [[0 for col in range(3)] for row in range(len(flowDic))] for i in range(0, len(flowDic)): for j in range(0, 3): x3[i][j] = "x" if len(flowDic) < 100000: if i < 10: x3[i][j] = x3[i][j] + "0000" elif i < 100: x3[i][j] = x3[i][j] + "000" elif i < 1000: x3[i][j] = x3[i][j] + "00" elif i < 10000: x3[i][j] = x3[i][j] + "0" x3[i][j] = x3[i][j] + str(i) + str(j) z3 = [] for i in range(0, len(flowDic)): z3.append("z3" + str(i)) temp3 = [] for i in range(0, len(flowDic)): for j in range(0, 3): # 3 si the number of path that each flow can choose temp3.append(x3[i][j]) for i in range(0, len(flowDic)): temp3.append(z3[i]) prob3 = LpProblem('lptest3', LpMinimize) r3 = LpVariable('r3', lowBound=0) xx3 = LpVariable.dicts("", temp3, lowBound=0, upBound=1) #, cat = pulp.LpInteger prob3 += r3 x_e3 = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(3): for j in range(len(flowDic)): x_e3[i][j][0] = x3[j][i] x_e3[i][j][1] = p_3[j][i] for i in range(0, 3): prob3 += lpSum(xx3[x_e3[i][j][0]] * x_e3[i][j][1] for j in x_e3[i]) <= r3 for i in range(0, len(flowDic)): prob3 += lpSum([xx3[j] for j in x3[i]]) == 1 GLPK().solve(prob3) print 'objective_sr =', value(prob3.objective) i = 0 j = 0 l = 0 for v in prob3.variables(): l = l + 1 if l < flow_number * 3 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 2: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 3): if x[i][j] >= k: choose = j k = x[i][j] for j in range(0, 3): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('P' in str(flowDic[i + 1][3])): for j in range(0, 3): if x[i][j] == 1: flowDic[i + 1].append(j) #print flowDic[i+1] flow_vnf = file_path + '/flow_vnf.txt' file = open(flow_vnf, 'w') NF_load = (defaultdict(lambda: None)) for i in capacity: NF_load[i] = 0 for t in flowDic: line = str(flowDic[t]) newsDate = txt_wrap_by("'", "'", line, 0) flow_rate = txt_wrap_by2(",", ",", line)[1] #print "flow-rate",flow_rate nf_number = [] #Save the serial number of each passed NF h = txt_wrap_by2(",", ",", line) for i in range(3, len(h)): nf_number.append(h[i]) ff = find_n_sub_str(line, ",", 3, 0) sfc_len = (len(newsDate[2]) + 1) / 2 k = find_n_sub_str(line, ",", 2 + sfc_len, 0) # Find the index where the last comma is located nf_number.append(txt_wrap_by(",", "]", line, k)[0]) flow_path = [] flow_path.append(newsDate[0]) for i in range(0, sfc_len): flow_path.append( str(newsDate[2][2 * i]) + str(int(nf_number[i]) + 1)) flow_path.append(newsDate[1]) flow_path.append(flow_rate) # print flow_path,flow_path[0],len(flow_path) file.write(str(flow_path) + '\n') for i in range(1, sfc_len + 1): #print flow_path[len(flow_path)-1] NF_load[flow_path[i]] = int(NF_load[flow_path[i]]) + int( flow_path[len(flow_path) - 1]) file.closed flow_vnf = file_path + '/alg1_NF_load.txt' file = open(flow_vnf, 'w') file.write(json.dumps(NF_load, indent=1)) file.closed alg1_max_NF = 0 for i in NF_load: if alg1_max_NF < NF_load[i]: alg1_max_NF = NF_load[i] fd = open(file_path + '/alg1_max_NF.txt', 'w') fd.write(json.dumps(alg1_max_NF)) fd.closed file = open(file_path + '/flow_feasible_vnf.txt', 'w') for t in flowDic: for n in range(0, 3): for i in range(4, len(flowDic[t])): flowDic[t][i] = random.randint(0, each_NF_num - 1) line = str(flowDic[t]) newsDate = txt_wrap_by("'", "'", line, 0) flow_rate = txt_wrap_by2(",", ",", line)[1] #print "flow-rate",flow_rate nf_number = [] #Save the serial number of each passed NF h = txt_wrap_by2(",", ",", line) for i in range(3, len(h)): nf_number.append(h[i]) ff = find_n_sub_str(line, ",", 3, 0) sfc_len = (len(newsDate[2]) + 1) / 2 k = find_n_sub_str(line, ",", 2 + sfc_len, 0) nf_number.append(txt_wrap_by(",", "]", line, k)[0]) flow_path = [] flow_path.append(newsDate[0]) for i in range(0, sfc_len): flow_path.append( str(newsDate[2][2 * i]) + str(int(nf_number[i]) + 1)) flow_path.append(newsDate[1]) flow_path.append(flow_rate) # print flow_path,flow_path[0],len(flow_path) file.write(str(flow_path) + '\n') file.closed print "NFV Distribution Finished"
def start(): """menu func """ distance = 0 flag = False g = None path = None shortest_path = None file_path = None temp = utils.load_graph() #DataFrame #default csv file # temp = utils.load_graph(file_path = 'data/topo3.csv', # start_position = 1, end_position = 2, weight_positon = 3) while True: print("welcome to tourism management system") print("plz enter a number:") print("1. input Scenic Spots graph or change graph") print("2. show Scenic Spots graph") print("3. show Tour guide Graph") print("4. show circle road in guide Graph") print("5. calculate shortest path and distance") print("6. show road constraction plan") print("7. search and sort Scenic Spots") print("8. parking system") print("9. algorithm compare") print("0. exit") line = raw_input("enter sth, seperate by space, 0 for exit\n") if line.strip() == "1": file_path = raw_input( "enter file name, 0 for creating a new csv file to store your graph\n" ) if file_path.strip() == "0": director.input_TourSortGraph() #create csv file flag = True temp = utils.load_graph(file_path='data/graph1.csv', start_position=0, end_position=1, weight_positon=2) else: try: temp = utils.load_graph(file_path='data/' + file_path.strip(), start_position=1, end_position=2, weight_positon=3) g = nxutil.load_csv_nx(file_path='data/' + file_path.strip()) except: print("file do not exist, try again") continue elif line.strip() == "2": if flag: g = nxutil.load_csv_nx(file_path='data/graph1.csv') elif g == None: if file_path != None: g = nxutil.load_csv_nx(file_path='data/' + file_path.strip()) else: g = nxutil.load_csv_nx() nxutil.show(g) elif line.strip() == "3": path, distance = director.CreatTourSortGraph(temp) print("#" * 40) for i in path: print(i) print("#" * 40) print(u"total length:") print(distance) if g == None: if file_path != None: g = nxutil.load_csv_nx(file_path='data/' + file_path.strip()) else: g = nxutil.load_csv_nx() nxutil.show(g) elif line.strip() == "4": if path == None: print("plz show Tour guide Graph first, now input 3") continue circle_path = algorithms_edgearr.TopoSort(path) if g == None: g = nxutil.load_csv_nx() nxutil.show(g, node_list=circle_path) elif line.strip() == "5": poi = raw_input( "enter startpoint|endpoint, seperate by |, input -1 for a default value" ) if len(poi.strip().split("|")) != 1: # print chardet.detect(poi.strip().split("|")[0]) start = unicode(poi.strip().split("|")[0], "GB2312") # for windows console end = unicode(poi.strip().split("|")[1], "GB2312") else: # print(len(poi.strip().split("|"))) print( u"wrong format! use default instead: start from 北门, end in 碧水亭" ) start = u'北门' end = u'碧水亭' # print(temp.columns.values[0]) if (type(temp.columns.values[0]) != unicode): start = int(start) end = int(end) # print type(end) dist, shortest_path = algorithms.dijkstra(temp, start=start, end=end) if shortest_path == None: print("point do not exsit") continue shortest_path.insert(0, start) shortest_path.append(end) for x in shortest_path: print(x) if g == None: g = nxutil.load_csv_nx() nxutil.show(g, path_list=shortest_path) elif line.strip() == "6": dis_list, distance = algorithms.MST(temp) print(u"------------修路------------------") for e in dis_list: print(e[0]), print(" --> "), print(e[1]) print(u"total length:") print(distance) if g == None: g = nxutil.load_csv_nx() nxutil.show(g, path_list=dis_list) elif line.strip() == "7": recommend.start(file_path=file_path) elif line.strip() == "8": parking.start() elif line.strip() == "9": compare() elif line.strip() == "0": break else: print("wrong input, try again") continue
def test_non_connected(self): with open(os.path.join(base_path, 'data/non_connected.json')) as graph_file: graph = Graph(graph_file) with self.assertRaises(InvalidPathError): dijkstra(graph, "1", "2")
def plan_route_from_graph(name: str, G, orig_coords, dest_coords, show: bool = None): """Diese Funktion wendet einen Algorithmus (name) auf einen Graphen G an, um den besten Weg von den Startkoordinaten (orig_coords) zu den Zielkoordinaten (dest_coords) zu ermitteln. Steht show auf True, wird der Graph geplottet, ansonsten (auch bei weglassen), werden nur die Daten ausgegeben. Zulässige name-Werte (Aufsteigend: ungefähre Laufzeit): * a-star * dijkstra * bellman-ford * floyd-warshall """ # fetch nodes and prepare fields orig_node = ox.get_nearest_node(G, orig_coords) dest_node = ox.get_nearest_node(G, dest_coords) dist = {} pred = {} route = [] if show is None: show = False print(f"===== START {name}, ({orig_coords[2]} → {dest_coords[2]})") if (name == "floyd-warshall"): # call algorithm dist, next, count = floyd_warshall(G) # prepare route u = orig_node v = dest_node while u != v: route += [u] u = next[u][v] else: # call algorithm if (name == "dijkstra"): dist, pred, count = dijkstra(G, orig_node) elif (name == "a-star"): dist, pred, count = a_star(G, orig_node, dest_node) elif (name == "bellman-ford"): dist, pred, count = bellman_ford(G, orig_node) # prepare route v = dest_node while v is not None: route = [v] + route v = pred[v] printInfos(G, dist, pred, count, route, dist[dest_node]) print(f"===== ENDE {name}") nc = ['r' if x in dist.keys() else 'black' for x in G.nodes] fig, ax = ox.plot_graph_route(G, route, node_size=24, node_color=nc, node_alpha=0.6, route_color='b', route_alpha=0.7, show=show)
def get_route_compare_algs(file_path): all_node = [] all_node_alone = [] all_edge = [] all_edge_alone = [] all_edge_alone_order = [] terminal_random = [] one_edge = [] one_edge1 = [] one_edge_order = [] one_node = [] source = [] terminal = [] weight = [] flow_sfc = [] capacity = {'F1':10,'F2':10,'F3':10,'F4':10,'F5':10,'F6':10,'F7':10,'F8':10,'F9':10,'F10':10,'I1':10,'I2':10,'I3':10,'I4':10,'I5':10,'I6':10,'I7':10,'I8':10,'I9':10,'I10':10,'P1':10,'P2':10,'P3':10,'P4':10,'P5':10,'P6':10,'P7':10,'P8':10,'P9':10,'P10':10,'D1':10,'D2':10,'D3':10,'D4':10,'D5':10,'D6':10,'D7':10,'D8':10,'D9':10,'D10':10,'W1':10,'W2':10,'W3':10,'W4':10,'W5':10,'W6':10,'W7':10,'W8':10,'W9':10,'W10':10} fd=file_path+'/SIMPLE_path.txt' if os.path.exists(fd): os.remove(fd) fd=file_path+'/PDA_path.txt' if os.path.exists(fd): os.remove(fd) k = 0 for line in open(file_path+"/flow_feasible_vnf.txt"): flow_path=txt_wrap_by("'","'",line,0) flow_sfc.append(flow_path) if k%3 == 0: source.append(flow_path[0]) terminal.append(flow_path[len(flow_path)-2]) weight.append(flow_path[len(flow_path)-1]) k=k+1 print len(source),len(flow_sfc),k flow_number = len(source) print "flow_number",flow_number for sr in range (0,flow_number): if sr%100 == 0: print"source is ",sr,source[sr], terminal[sr] main_path_all = [] one_source_edge = [] one_source_edge_order = [] one_source_node = [] main_path =[] for q in range(0,len(flow_sfc[3*sr])-2): path = dijkstra(g, str(flow_sfc[3*sr][q]),str(flow_sfc[3*sr][q+1])) main_path_seg = path.get('path') for i in range(0,len(main_path_seg)): main_path_seg[i] = main_path_seg[i].encode(CODEC) for i in range(0,len(main_path_seg)-1): main_path.append(main_path_seg[i]) if q == len(flow_sfc[3*sr])-3: main_path.append(main_path_seg[len(main_path_seg)-1]) for i in range(0,len(main_path)): main_path_all.append(main_path[i]) for f in range(0,len(main_path)-3): siwtch1 = int(main_path[f+1][1]) siwtch2 = int(main_path[f+2][1]) one_edge_order = main_path[f+1] + main_path[f+2] if len(main_path[f+1]) == 3: siwtch1 = siwtch1*10 + int(main_path[f+1][2]) if len(main_path[f+1]) == 4: siwtch1 = siwtch1*100 + int(main_path[f+1][2])*10 + int(main_path[f+1][3]) if len(main_path[f+2]) == 3: siwtch2 = siwtch2*10 + int(main_path[f+2][2]) if len(main_path[f+2]) == 4: siwtch2 = siwtch2*100 + int(main_path[f+2][2])*10 + int(main_path[f+2][3]) if siwtch1 < siwtch2: one_edge = main_path[f+1] + main_path[f+2] one_edge1 = main_path[f+2] + main_path[f+1] else: one_edge = main_path[f+2] + main_path[f+1] one_edge1 = main_path[f+1] + main_path[f+2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0,len(main_path)-2): one_node = main_path[f+1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) one_source_edge_order = [] one_source_node = [] one_source_edge = [] main_path =[] for q in range(0,len(flow_sfc[3*sr+1])-2): path = dijkstra2(g, str(flow_sfc[3*sr+1][q]),str(flow_sfc[3*sr+1][q+1]),main_path_all) main_path_seg = path.get('path') for i in range(0,len(main_path_seg)): main_path_seg[i] = main_path_seg[i].encode(CODEC) for i in range(0,len(main_path_seg)-1): main_path.append(main_path_seg[i]) if q == len(flow_sfc[3*sr+1])-3: main_path.append(main_path_seg[len(main_path_seg)-1]) for i in range(0,len(main_path)): main_path_all.append(main_path[i]) for f in range(0,len(main_path)-3): siwtch1 = int(main_path[f+1][1]) siwtch2 = int(main_path[f+2][1]) one_edge_order = main_path[f+1] + main_path[f+2] if len(main_path[f+1]) == 3: siwtch1 = siwtch1*10 + int(main_path[f+1][2]) if len(main_path[f+1]) == 4: siwtch1 = siwtch1*100 + int(main_path[f+1][2])*10 + int(main_path[f+1][3]) if len(main_path[f+2]) == 3: siwtch2 = siwtch2*10 + int(main_path[f+2][2]) if len(main_path[f+2]) == 4: siwtch2 = siwtch2*100 + int(main_path[f+2][2])*10 + int(main_path[f+2][3]) if siwtch1 < siwtch2: one_edge = main_path[f+1] + main_path[f+2] one_edge1 = main_path[f+2] + main_path[f+1] else: one_edge = main_path[f+2] + main_path[f+1] one_edge1 = main_path[f+1] + main_path[f+2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0,len(main_path)-2): one_node = main_path[f+1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) one_source_node = [] one_source_edge = [] one_source_edge_order = [] main_path =[] for q in range(0,len(flow_sfc[3*sr+2])-2): path = dijkstra3(g, str(flow_sfc[3*sr+2][q]),str(flow_sfc[3*sr+2][q+1]),main_path_all) main_path_seg = path.get('path') for i in range(0,len(main_path_seg)): main_path_seg[i] = main_path_seg[i].encode(CODEC) for i in range(0,len(main_path_seg)-1): main_path.append(main_path_seg[i]) if q == len(flow_sfc[3*sr+2])-3: main_path.append(main_path_seg[len(main_path_seg)-1]) for i in range(0,len(main_path)): main_path_all.append(main_path[i]) for f in range(0,len(main_path)-3): siwtch1 = int(main_path[f+1][1]) siwtch2 = int(main_path[f+2][1]) one_edge_order = main_path[f+1] + main_path[f+2] if len(main_path[f+1]) == 3: siwtch1 = siwtch1*10 + int(main_path[f+1][2]) if len(main_path[f+1]) == 4: siwtch1 = siwtch1*100 + int(main_path[f+1][2])*10 + int(main_path[f+1][3]) if len(main_path[f+2]) == 3: siwtch2 = siwtch2*10 + int(main_path[f+2][2]) if len(main_path[f+2]) == 4: siwtch2 = siwtch2*100 + int(main_path[f+2][2])*10 + int(main_path[f+2][3]) if siwtch1 < siwtch2: one_edge = main_path[f+1] + main_path[f+2] one_edge1 = main_path[f+2] + main_path[f+1] else: one_edge = main_path[f+2] + main_path[f+1] one_edge1 = main_path[f+1] + main_path[f+2] if one_edge not in all_edge: if one_edge1 not in all_edge: all_edge.append(one_edge) else: one_edge = one_edge1 one_source_edge.append(one_edge) one_source_edge_order.append(one_edge_order) all_edge_alone.append(one_source_edge) all_edge_alone_order.append(one_source_edge_order) for f in range(0,len(main_path)-2): one_node = main_path[f+1] if one_node not in all_node: all_node.append(one_node) one_source_node.append(one_node) all_node_alone.append(one_source_node) weight [sr] = int (weight[sr]) for flow_traffic in range(1,2): alg0=0 alg1=0 alg2=0 alg3=0 for randome_time in range(0,1): for entries in range(31,32): #300-4200 entries = entries *10000 ce = 10000 for alg in range(0,10): if alg == 2: #SIMPLE alg, linke load balancing used_ce = defaultdict(lambda:None) used_entries = defaultdict(lambda:None) for v in all_node: used_entries[v] = 0 for e in all_edge: used_ce[e] = 0 lambda1 = 0 max_entries = 0 temp2 = 0 for i in range(0,flow_number): temp = [[0 for col in range(2)] for row in range(3)] for row in range(0,3): for col in range(0,2): temp[row][col] = 0 for j in range(0,3): for n in range(0,len(all_edge_alone[3*i+j])): q=0 for m in range(0,len(all_edge_alone[3*i+j])): if all_edge_alone[3*i+j][n] == all_edge_alone[3*i+j][m]: q=q+1 if temp[j][1]<(used_ce[all_edge_alone[3*i+j][n]] + q*weight[i])/float(ce): temp[j][1] = (used_ce[all_edge_alone[3*i+j][n]] + q*weight[i])/float(ce) temp1 = 1000000 k = 0 for j in range(0,3): if temp[j][1] < temp1: k = j; temp1 = temp[j][1]; for n in range(0,len(all_edge_alone[3*i+k])): used_ce[all_edge_alone[3*i+k][n]] = used_ce[all_edge_alone[3*i+k][n]] + weight[i] for n in range(0,len(all_node_alone[3*i+k])): used_entries[all_node_alone[3*i+k][n]] = used_entries[all_node_alone[3*i+k][n]] + 1 alg2_choosed_path=[] alg2_choosed_path.append(source[i]) for j in range (0,len(all_node_alone[3*i+k])): alg2_choosed_path.append(all_node_alone[3*i+k][j]) alg2_choosed_path.append(terminal[i]) alg2_choosed_path.append(weight[i]) fd=open(file_path+'/SIMPLE_path.txt','a') fd.write(str(alg2_choosed_path) + '\n') fd.close() alg2_entries ={} for j in used_entries: if 's' in str(j): alg2_entries[j] = used_entries [j] if max_entries < used_entries[j]: max_entries = used_entries[j] p1 = file_path+"/SIMPLE_entries.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(alg2_entries, indent=1)) fd.close() p1 = file_path+"/SIMPLE_ce.json" #rounding p2 = file_path+"/SIMPLE_NF_load.json" # rounding fd1 = open(p1, 'w') fd2 = open(p2, 'w') alg2_ce_link = {} alg2_ce_middlebox = {} for e in all_edge: isMark = 0 for key in capacity: if key in e: alg2_ce_middlebox[key] = float(used_ce[e])/2 isMark = 1 if isMark == 0: alg2_ce_link[e] = float(used_ce[e]) / float(ce) fd1.write(json.dumps(alg2_ce_link, indent=1)) fd1.close() fd2.write(json.dumps(alg2_ce_middlebox, indent=1)) fd2.close() lambda1 = 0 for j in alg2_ce_link: if lambda1 < alg2_ce_link[j]: lambda1 = alg2_ce_link[j] SIMPLE_max_NF=0 for j in alg2_ce_middlebox: if SIMPLE_max_NF < alg2_ce_middlebox[j]: SIMPLE_max_NF = alg2_ce_middlebox[j] print "SIMPLE",lambda1,max_entries p1 = file_path+"/SIMPLE_lambda.json" fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.close() p1 = file_path+"/SIMPLE_max_entries.json" fd = open(p1, 'w') fd.write(json.dumps(max_entries)) fd.close() p1 = file_path+"/SIMPLE_max_NF.json" fd = open(p1, 'w') fd.write(json.dumps(SIMPLE_max_NF)) fd.close() if alg == 3: #PDA alg used_ce = defaultdict(lambda:None) used_entries = defaultdict(lambda:None) for v in all_node: used_entries[v] = 0 for e in all_edge: used_ce[e] = 0 lambda1 = 0 max_entries = 0 temp2 = 0 for i in range(0,flow_number): temp = [[0 for col in range(2)] for row in range(3)] for row in range(0,3): for col in range(0,2): temp[row][col] = 0 for j in range(0,3): for n in range(0,len(all_node_alone[3*i+j])): q=0 for m in range(0,len(all_node_alone[3*i+j])): if all_node_alone[3*i+j][n] == all_node_alone[3*i+j][m]: q=q+1 if temp[j][1]<used_entries[all_node_alone[3*i+j][n]] + q: temp[j][1] = used_entries[all_node_alone[3*i+j][n]] + q temp1 = 10000000 k = 0 #表示选几个可行路径中的哪一条 for j in range(0,3): if temp[j][1] < temp1: #选择满足流表约束条件下的,负载最轻的那条可行路径. k = j; temp1 = temp[j][1]; for n in range(0,len(all_edge_alone[3*i+k])): #从可行路径中选择一条(第k条),需要使用链路容量和流表项 used_ce[all_edge_alone[3*i+k][n]] = used_ce[all_edge_alone[3*i+k][n]] + weight[i] #if used_ce[all_edge_alone[3*i+k][n]]/float(ce) > lambda1: #lambda1 = used_ce[all_edge_alone[3*i+k][n]]/float(ce) #记录最大的lambda for n in range(0,len(all_node_alone[3*i+k])): used_entries[all_node_alone[3*i+k][n]] = used_entries[all_node_alone[3*i+k][n]] + 1 alg3_choosed_path=[] alg3_choosed_path.append(source[i]) for j in range (0,len(all_node_alone[3*i+k])): alg3_choosed_path.append(all_node_alone[3*i+k][j]) alg3_choosed_path.append(terminal[i]) alg3_choosed_path.append(weight[i]) fd=open(file_path+'/PDA_path.txt','a') fd.write(str(alg3_choosed_path) + '\n') fd.close() alg3_entries ={} for j in used_entries: if 's' in str(j): alg3_entries[j] = used_entries [j] if max_entries < used_entries[j]: max_entries = used_entries[j] p1 = file_path+"/PDA_entries.json" #rounding fd = open(p1, 'w') fd.write(json.dumps(alg3_entries, indent=1)) fd.close() p1 = file_path+"/PDA_ce.json" #rounding p2 = file_path+"/PDA_NF_load.json" # rounding fd1 = open(p1, 'w') fd2 = open(p2, 'w') alg3_ce_link = {} alg3_ce_middlebox = {} for e in all_edge: isMark = 0 #链路负载和midllebox负载 for key in capacity: if key in e: alg3_ce_middlebox[key] = float(used_ce[e])/2 isMark = 1 if isMark == 0: alg3_ce_link[e] = float(used_ce[e]) / float(ce) fd1.write(json.dumps(alg3_ce_link, indent=1)) fd1.close() fd2.write(json.dumps(alg3_ce_middlebox, indent=1)) fd2.close() lambda1 = 0 for j in alg3_ce_link: if lambda1 < alg3_ce_link[j]: lambda1 = alg3_ce_link[j] PDA_max_NF=0 for j in alg3_ce_middlebox: if PDA_max_NF < alg3_ce_middlebox[j]: PDA_max_NF = alg3_ce_middlebox[j] print "PDA",lambda1, max_entries p1 = file_path+"/PDA_lambda.json" fd = open(p1, 'w') fd.write(json.dumps(lambda1)) fd.close() p1 = file_path+"/PDA_max_entries.json" fd = open(p1, 'w') fd.write(json.dumps(max_entries)) fd.close() p1 = file_path+"/PDA_max_NF.json" fd = open(p1, 'w') fd.write(json.dumps(PDA_max_NF)) fd.close() print "Compare_algs Finished"
def get_nfv_distribution(ratio, host_num, file_path): CODEC = 'utf-8' mynet = "rocketfuel_87s174h50nf.json" g = DiGraph(mynet) #Custom VNF processing power (1-10) capacity = { 'F1': 10, 'F2': 10, 'F3': 10, 'F4': 10, 'F5': 10, 'F6': 10, 'F7': 10, 'F8': 10, 'F9': 10, 'F10': 10, 'I1': 10, 'I2': 10, 'I3': 10, 'I4': 10, 'I5': 10, 'I6': 10, 'I7': 10, 'I8': 10, 'I9': 10, 'I10': 10, 'P1': 10, 'P2': 10, 'P3': 10, 'P4': 10, 'P5': 10, 'P6': 10, 'P7': 10, 'P8': 10, 'P9': 10, 'P10': 10, 'D1': 10, 'D2': 10, 'D3': 10, 'D4': 10, 'D5': 10, 'D6': 10, 'D7': 10, 'D8': 10, 'D9': 10, 'D10': 10, 'W1': 10, 'W2': 10, 'W3': 10, 'W4': 10, 'W5': 10, 'W6': 10, 'W7': 10, 'W8': 10, 'W9': 10, 'W10': 10 } each_NF_num = 10 #Enter two functional chains #Number of streams between 80 hosts flowDic = get_flows(host_num, ratio) flow_number = len(flowDic) print "flow_number", flow_number #print flowDic #Find out which streams of each type of NF have passed flow = {} flow['F'] = [] flow['I'] = [] flow['P'] = [] flow['D'] = [] flow['W'] = [] for key in flowDic: if 'F' in str(flowDic[key][3]): flow['F'].append(key) if 'I' in str(flowDic[key][3]): flow['I'].append(key) if 'P' in str(flowDic[key][3]): flow['P'].append(key) if 'D' in str(flowDic[key][3]): flow['D'].append(key) if 'W' in str(flowDic[key][3]): flow['W'].append(key) #j stands for 9 processors, i stands for task (flow) p = [([0] * len(flow) * 10) for i in range(len(flowDic))] #print len(p),len(p[0]) for j in capacity.keys(): print j #Calculate the processing power of Pij = stream bandwidth * distance to the NF (S-VNF-D) / NF if 'F' in j: #print "F",j if '1' in j and '10' not in j: row = 0 elif '2' in j: row = 1 elif '3' in j: row = 2 elif '4' in j: row = 3 elif '5' in j: row = 4 elif '6' in j: row = 5 elif '7' in j: row = 6 elif '8' in j: row = 7 elif '9' in j: row = 8 elif '10' in j: #print "F10",j row = 9 for i in flow['F']: # print flowDic[i][0],j,flowDic[i][1] path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] #print "power",power #p[i-1][row] = bandwidth * distance / power distance = math.sqrt(math.sqrt(distance)) p[i - 1][row] = bandwidth * distance / power #p[i-1][row] = bandwidth #print i #print p[i-1][row] elif 'I' in j: if '1' in j and '10' not in j: row = 10 elif '2' in j: row = 11 elif '3' in j: row = 12 elif '4' in j: row = 13 elif '5' in j: row = 14 elif '6' in j: row = 15 elif '7' in j: row = 16 elif '8' in j: row = 17 elif '9' in j: row = 18 elif '10' in j: row = 19 for i in flow['I']: path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] distance = math.sqrt(math.sqrt(distance)) p[i - 1][row] = bandwidth * distance / power elif 'P' in j: if '1' in j and '10' not in j: row = 20 elif '2' in j: row = 21 elif '3' in j: row = 22 elif '4' in j: row = 23 elif '5' in j: row = 24 elif '6' in j: row = 25 elif '7' in j: row = 26 elif '8' in j: row = 27 elif '9' in j: row = 28 elif '10' in j: row = 29 for i in flow['P']: path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] distance = math.sqrt(math.sqrt(distance)) p[i - 1][row] = bandwidth * distance / power elif 'D' in j: if '1' in j and '10' not in j: row = 30 elif '2' in j: row = 31 elif '3' in j: row = 32 elif '4' in j: row = 33 elif '5' in j: row = 34 elif '6' in j: row = 35 elif '7' in j: row = 36 elif '8' in j: row = 37 elif '9' in j: row = 38 elif '10' in j: row = 39 for i in flow['D']: path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] distance = math.sqrt(math.sqrt(distance)) p[i - 1][row] = bandwidth * distance / power elif 'W' in j: if '1' in j and '10' not in j: row = 40 elif '2' in j: row = 41 elif '3' in j: row = 42 elif '4' in j: row = 43 elif '5' in j: row = 44 elif '6' in j: row = 45 elif '7' in j: row = 46 elif '8' in j: row = 47 elif '9' in j: row = 48 elif '10' in j: row = 49 for i in flow['W']: path1 = dijkstra(g, flowDic[i][0], j) path2 = dijkstra(g, j, flowDic[i][1]) distance = path1.get('cost') + path2.get('cost') bandwidth = flowDic[int(i)][2] power = capacity[j] distance = math.sqrt(math.sqrt(distance)) p[i - 1][row] = bandwidth * distance / power ''' Solving linear equation ''' #x = [[0 for col in range(len(flowDic))] for row in range(3)] #x[i][j] :Indicates that the ith stream passes through the jth vnf, and j(0-8) represents [F1, F2, F3, I1, I2, I3, P1, P2, P3] '''Which F is passed through?''' x = [[0 for col in range(10)] for row in range(len(flowDic))] #print x for i in range(0, len(flowDic)): for j in range(0, 10): x[i][j] = "x" if len(flowDic) < 100000: if i < 10: x[i][j] = x[i][j] + "0000" elif i < 100: x[i][j] = x[i][j] + "000" elif i < 1000: x[i][j] = x[i][j] + "00" elif i < 10000: x[i][j] = x[i][j] + "0" x[i][j] = x[i][j] + str(i) + str(j) z = [] for i in range(0, len(flowDic)): z.append("z" + str(i)) temp = [] for i in range(0, len(flowDic)): for j in range( 0, 10): # 3 si the number of path that each flow can choose temp.append(x[i][j]) for i in range(0, len(flowDic)): temp.append(z[i]) prob = LpProblem('lptest', LpMinimize) r = LpVariable('r', lowBound=0) xx = LpVariable.dicts("", temp, lowBound=0, upBound=1) #,cat = pulp.LpInteger #print temp print "-------2222222-----" #print x[0] #Add the target equation #0-28 flows prob += r #0->F(0,1,2);1->I(3,4,5);2->P(6,7,8) x_e = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(10): for j in range(len(flowDic)): x_e[i][j][0] = x[j][i] x_e[i][j][1] = p[j][i] for i in range(0, 10): prob += lpSum(xx[x_e[i][j][0]] * x_e[i][j][1] for j in x_e[i]) <= r #Add constraints for i in range(0, len(flowDic)): prob += lpSum([xx[j] for j in x[i]]) == 1 GLPK().solve(prob) print 'F_objective_sr =', value(prob.objective) i = 0 j = 0 l = 0 for v in prob.variables(): #print v, v.varValue l = l + 1 if l < flow_number * 10 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 9: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 10): if x[i][j] > k: choose = j k = x[i][j] for j in range(0, 10): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('F' in str(flowDic[i + 1][3])): for j in range(0, 10): if x[i][j] == 1: flowDic[i + 1].append(j) #print flowDic[i+1] #sleep(600) '''Which I passed through''' #P_1 refers to the cost of I function p_1 = [([0] * 10) for i in range(len(flowDic))] #print "hh",len(p_1),len(p_1[0]) for i in range(len(flowDic)): for j in range(10): #print i,j, p_1[i][j] = p[i][j + 10] x1 = [[0 for col in range(10)] for row in range(len(flowDic))] for i in range(0, len(flowDic)): for j in range(0, 10): x1[i][j] = "x" if len(flowDic) < 100000: if i < 10: x1[i][j] = x1[i][j] + "0000" elif i < 100: x1[i][j] = x1[i][j] + "000" elif i < 1000: x1[i][j] = x1[i][j] + "00" elif i < 10000: x1[i][j] = x1[i][j] + "0" x1[i][j] = x1[i][j] + str(i) + str(j) z1 = [] for i in range(0, len(flowDic)): z1.append("z" + str(i)) temp1 = [] for i in range(0, len(flowDic)): for j in range( 0, 10): # 3 si the number of path that each flow can choose temp1.append(x1[i][j]) for i in range(0, len(flowDic)): temp1.append(z1[i]) prob1 = LpProblem('lptest1', LpMinimize) r1 = LpVariable('r1', lowBound=0) xx1 = LpVariable.dicts("", temp1, lowBound=0, upBound=1) #, cat = pulp.LpInteger #print xx1 prob1 += r1 #0->F(0,1,2);1->I(3,4,5);2->P(6,7,8) x_e1 = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(10): for j in range(len(flowDic)): x_e1[i][j][0] = x1[j][i] x_e1[i][j][1] = p_1[j][i] for i in range(0, 10): prob1 += lpSum(xx1[x_e1[i][j][0]] * x_e1[i][j][1] for j in x_e1[i]) <= r1 for i in range(0, len(flowDic)): prob1 += lpSum([xx1[j] for j in x1[i]]) == 1 GLPK().solve(prob1) print 'I_objective_sr =', value(prob1.objective) i = 0 j = 0 l = 0 for v in prob1.variables(): l = l + 1 if l < flow_number * 10 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 9: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 10): if x[i][j] > k: choose = j k = x[i][j] for j in range(0, 10): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('I' in str(flowDic[i + 1][3])): for j in range(0, 10): if x[i][j] == 1: flowDic[i + 1].append(j) #print flowDic[i+1] '''Which P passed?''' p_3 = [([0] * 10) for i in range(len(flowDic))] for i in range(len(flowDic)): for j in range(10): p_3[i][j] = p[i][j + 20] x3 = [[0 for col in range(10)] for row in range(len(flowDic))] for i in range(0, len(flowDic)): for j in range(0, 10): x3[i][j] = "x" if len(flowDic) < 100000: if i < 10: x3[i][j] = x3[i][j] + "0000" elif i < 100: x3[i][j] = x3[i][j] + "000" elif i < 1000: x3[i][j] = x3[i][j] + "00" elif i < 10000: x3[i][j] = x3[i][j] + "0" x3[i][j] = x3[i][j] + str(i) + str(j) z3 = [] for i in range(0, len(flowDic)): z3.append("z3" + str(i)) temp3 = [] for i in range(0, len(flowDic)): for j in range( 0, 10): # 3 si the number of path that each flow can choose temp3.append(x3[i][j]) for i in range(0, len(flowDic)): temp3.append(z3[i]) prob3 = LpProblem('lptest3', LpMinimize) r3 = LpVariable('r3', lowBound=0) xx3 = LpVariable.dicts("", temp3, lowBound=0, upBound=1) #, cat = pulp.LpInteger x_e3 = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(10): for j in range(len(flowDic)): x_e3[i][j][0] = x3[j][i] x_e3[i][j][1] = p_3[j][i] for i in range(0, 10): prob3 += lpSum(xx3[x_e3[i][j][0]] * x_e3[i][j][1] for j in x_e3[i]) <= r3 for i in range(0, len(flowDic)): prob3 += lpSum([xx3[j] for j in x3[i]]) == 1 GLPK().solve(prob3) print 'P_objective_sr =', value(prob3.objective) i = 0 j = 0 l = 0 for v in prob3.variables(): l = l + 1 if l < flow_number * 10 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 9: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 10): if x[i][j] > k: choose = j k = x[i][j] for j in range(0, 10): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('P' in str(flowDic[i + 1][3])): for j in range(0, 10): if x[i][j] == 1: flowDic[i + 1].append(j) #print flowDic[i+1] '''Which D is passed through?''' p_4 = [([0] * 10) for i in range(len(flowDic))] for i in range(len(flowDic)): for j in range(10): p_4[i][j] = p[i][j + 30] x4 = [[0 for col in range(10)] for row in range(len(flowDic))] for i in range(0, len(flowDic)): for j in range(0, 10): x4[i][j] = "x" if len(flowDic) < 100000: if i < 10: x4[i][j] = x4[i][j] + "0000" elif i < 100: x4[i][j] = x4[i][j] + "000" elif i < 1000: x4[i][j] = x4[i][j] + "00" elif i < 10000: x4[i][j] = x4[i][j] + "0" x4[i][j] = x4[i][j] + str(i) + str(j) z4 = [] for i in range(0, len(flowDic)): z4.append("z4" + str(i)) temp4 = [] for i in range(0, len(flowDic)): for j in range( 0, 10): # 3 si the number of path that each flow can choose temp4.append(x4[i][j]) for i in range(0, len(flowDic)): temp4.append(z4[i]) prob4 = LpProblem('lptest4', LpMinimize) r4 = LpVariable('r4', lowBound=0) xx4 = LpVariable.dicts("", temp4, lowBound=0, upBound=1) #, cat = pulp.LpInteger prob4 += r4 x_e4 = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(10): for j in range(len(flowDic)): x_e4[i][j][0] = x4[j][i] x_e4[i][j][1] = p_4[j][i] for i in range(0, 10): prob4 += lpSum(xx4[x_e4[i][j][0]] * x_e4[i][j][1] for j in x_e4[i]) <= r4 for i in range(0, len(flowDic)): prob4 += lpSum([xx4[j] for j in x4[i]]) == 1 GLPK().solve(prob4) print 'D_objective_sr =', value(prob4.objective) i = 0 j = 0 l = 0 for v in prob4.variables(): l = l + 1 if l < flow_number * 10 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 9: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 10): if x[i][j] > k: choose = j k = x[i][j] for j in range(0, 10): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('D' in str(flowDic[i + 1][3])): for j in range(0, 10): if x[i][j] == 1: flowDic[i + 1].append(j) #print flowDic[i+1] '''Which W to solve''' p_5 = [([0] * 10) for i in range(len(flowDic))] for i in range(len(flowDic)): for j in range(10): p_5[i][j] = p[i][j + 40] x5 = [[0 for col in range(10)] for row in range(len(flowDic))] for i in range(0, len(flowDic)): for j in range(0, 10): x5[i][j] = "x" if len(flowDic) < 100000: if i < 10: x5[i][j] = x5[i][j] + "0000" elif i < 100: x5[i][j] = x5[i][j] + "000" elif i < 1000: x5[i][j] = x5[i][j] + "00" elif i < 10000: x5[i][j] = x5[i][j] + "0" x5[i][j] = x5[i][j] + str(i) + str(j) z5 = [] for i in range(0, len(flowDic)): z5.append("z5" + str(i)) temp5 = [] for i in range(0, len(flowDic)): for j in range( 0, 10): # 3 si the number of path that each flow can choose temp5.append(x5[i][j]) for i in range(0, len(flowDic)): temp5.append(z5[i]) prob5 = LpProblem('lptest5', LpMinimize) r5 = LpVariable('r5', lowBound=0) xx5 = LpVariable.dicts("", temp5, lowBound=0, upBound=1) #, cat = pulp.LpInteger #print xx3 prob5 += r5 x_e5 = defaultdict(lambda: (defaultdict(lambda: (defaultdict(lambda: None))))) for i in range(10): for j in range(len(flowDic)): x_e5[i][j][0] = x5[j][i] x_e5[i][j][1] = p_5[j][i] for i in range(0, 10): prob5 += lpSum(xx5[x_e5[i][j][0]] * x_e5[i][j][1] for j in x_e5[i]) <= r5 for i in range(0, len(flowDic)): prob5 += lpSum([xx5[j] for j in x5[i]]) == 1 GLPK().solve(prob5) print 'W_objective_sr =', value(prob5.objective) i = 0 j = 0 l = 0 for v in prob5.variables(): l = l + 1 if l < flow_number * 10 + 1: #(the number of flows /cdot 3) +1 x[i][j] = v.varValue if j < 9: j = j + 1 else: i = i + 1 j = 0 for i in range(0, flow_number): k = 0 choose = 0 for j in range(0, 10): if x[i][j] > k: choose = j k = x[i][j] for j in range(0, 10): if j == choose: x[i][j] = 1 else: x[i][j] = 0 for i in range(0, flow_number): if ('W' in str(flowDic[i + 1][3])): for j in range(0, 10): if x[i][j] == 1: flowDic[i + 1].append(j) #print flowDic[i+1] flow_vnf = file_path + '/flow_vnf.txt' file = open(flow_vnf, 'w') NF_load = (defaultdict(lambda: None)) for i in capacity: NF_load[i] = 0 for t in flowDic: line = str(flowDic[t]) newsDate = txt_wrap_by("'", "'", line, 0) flow_rate = txt_wrap_by2(",", ",", line)[1] #print "flow-rate",flow_rate nf_number = [] #Save the serial number of each passed NF h = txt_wrap_by2(",", ",", line) for i in range(3, len(h)): nf_number.append(h[i]) ff = find_n_sub_str(line, ",", 3, 0) sfc_len = (len(newsDate[2]) + 1) / 2 k = find_n_sub_str(line, ",", 2 + sfc_len, 0) #print "2222222222",line,k, txt_wrap_by(",","]",line,k) nf_number.append(txt_wrap_by(",", "]", line, k)[0]) flow_path = [] flow_path.append(newsDate[0]) for i in range(0, sfc_len): flow_path.append( str(newsDate[2][2 * i]) + str(int(nf_number[i]) + 1)) flow_path.append(newsDate[1]) flow_path.append(flow_rate) # print flow_path,flow_path[0],len(flow_path) file.write(str(flow_path) + '\n') for i in range(1, sfc_len + 1): #print flow_path[len(flow_path)-1] NF_load[flow_path[i]] = int(NF_load[flow_path[i]]) + int( flow_path[len(flow_path) - 1]) file.closed flow_vnf = file_path + '/alg1_NF_load.txt' file = open(flow_vnf, 'w') file.write(json.dumps(NF_load, indent=1)) file.closed alg1_max_NF = 0 for i in NF_load: if alg1_max_NF < NF_load[i]: alg1_max_NF = NF_load[i] fd = open(file_path + '/alg1_max_NF.txt', 'w') fd.write(json.dumps(alg1_max_NF)) fd.closed file = open(file_path + '/flow_feasible_vnf.txt', 'w') for t in flowDic: for n in range(0, 3): for i in range(4, len(flowDic[t])): flowDic[t][i] = random.randint(0, each_NF_num - 1) line = str(flowDic[t]) newsDate = txt_wrap_by("'", "'", line, 0) flow_rate = txt_wrap_by2(",", ",", line)[1] #print "flow-rate",flow_rate nf_number = [] #Save the serial number of each passed NF h = txt_wrap_by2(",", ",", line) for i in range(3, len(h)): nf_number.append(h[i]) ff = find_n_sub_str(line, ",", 3, 0) sfc_len = (len(newsDate[2]) + 1) / 2 k = find_n_sub_str(line, ",", 2 + sfc_len, 0) nf_number.append(txt_wrap_by(",", "]", line, k)[0]) flow_path = [] flow_path.append(newsDate[0]) for i in range(0, sfc_len): flow_path.append( str(newsDate[2][2 * i]) + str(int(nf_number[i]) + 1)) flow_path.append(newsDate[1]) flow_path.append(flow_rate) # print flow_path,flow_path[0],len(flow_path) file.write(str(flow_path) + '\n') file.closed print "NFV Distribution Finished"