|
| 1 | +# Add any missing reciprocal edges to graph and adjust successor_list |
| 2 | + |
| 3 | +# Solutions from the Simulator can be exported only to bidirectional networks generated from the same base as the unidirectional network |
| 4 | + |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# python convertToBidirec.py NETWORK_NAME_IN_DIRECTORY_STRUCTURE |
| 8 | +# |
| 9 | + |
| 10 | +import sys |
| 11 | +import os |
| 12 | +import json |
| 13 | +import networkx as nx |
| 14 | +import shutil |
| 15 | +import ast |
| 16 | + |
| 17 | +def parse_tuple(string): |
| 18 | + try: |
| 19 | + s = ast.literal_eval(str(string)) |
| 20 | + if type(s) == tuple: |
| 21 | + return s |
| 22 | + return |
| 23 | + except: |
| 24 | + return |
| 25 | + |
| 26 | + |
| 27 | +def readJson(path): |
| 28 | + with open(path,"r") as f: |
| 29 | + data = f.read() |
| 30 | + |
| 31 | + jsonObj = json.loads(data) |
| 32 | + return jsonObj |
| 33 | + |
| 34 | +def writeJson(obj, path): |
| 35 | + with open(path, 'w') as g: |
| 36 | + json.dump(obj, g, indent=4) |
| 37 | + |
| 38 | +origin_p = "./example-networks/" + sys.argv[1] |
| 39 | +destination_p = "./example-networks-sim-bidirec/" + sys.argv[1] |
| 40 | + |
| 41 | +print("Opening files ...") |
| 42 | +old_graph = nx.read_graphml(origin_p + "/network/tracks.graphml") |
| 43 | +graph = old_graph.copy() |
| 44 | + |
| 45 | +succ = readJson(origin_p + "/network/successors_cpp.json") |
| 46 | +stations = readJson(origin_p + "/timetable/stations.json") |
| 47 | +routes = readJson(origin_p + "/routes/routes.json") |
| 48 | + |
| 49 | +attb_mbl = nx.get_edge_attributes(graph, "min_block_length") |
| 50 | +attb_sp = nx.get_edge_attributes(graph, "max_speed") |
| 51 | +attb_le = nx.get_edge_attributes(graph, "length") |
| 52 | +attb_br = nx.get_edge_attributes(graph, "breakable") |
| 53 | + |
| 54 | +for (u, v) in old_graph.edges: |
| 55 | + # Add reciprocal |
| 56 | + if graph.has_edge(u, v) and not(graph.has_edge(v, u)): |
| 57 | + print("Graph: Adding reciprocal edge to ", [u, v], "because", [v, u], " does not exist") |
| 58 | + graph.add_edge(v, u, min_block_length=attb_mbl[u,v], max_speed=attb_sp[u,v], length=attb_le[u,v], breakable=attb_br[u,v]) |
| 59 | + |
| 60 | + ## Edit successor list |
| 61 | + key_old = "('"+ u + "', '" + v + "')" |
| 62 | + key_new = "('"+ v + "', '" + u + "')" |
| 63 | + |
| 64 | + # For each successor of the old edge |
| 65 | + for [o,d] in succ[key_old]: |
| 66 | + # The new edge is a successor of the reciprocal |
| 67 | + print("Successors: Adding new edge ", [v, u]," as a successor of ", [d, o]) |
| 68 | + key_succ_recip = "('"+ d + "', '" + o + "')" |
| 69 | + if key_succ_recip in succ: |
| 70 | + succ[key_succ_recip].append([v,u]) |
| 71 | + else: |
| 72 | + succ[key_succ_recip] = [[v,u]] |
| 73 | + |
| 74 | + succ_iter = succ.copy() |
| 75 | + for edge in succ_iter: |
| 76 | + edge_p = parse_tuple(edge) |
| 77 | + o = edge_p[0] |
| 78 | + d = edge_p[1] |
| 79 | + # For each predicessor of the old edge |
| 80 | + if [u, v] in succ_iter[edge]: |
| 81 | + # The new edge can reach the reciprocal |
| 82 | + print("Successors: Adding new edge ", [v, u]," as a predicessor of", [d, o]) |
| 83 | + key_pred_recip = "('"+ d + "', '" + o + "')" |
| 84 | + if key_new in succ: |
| 85 | + succ[key_new].append([d,o]) |
| 86 | + else: |
| 87 | + succ[key_new] = [[d,o]] |
| 88 | + |
| 89 | +os.makedirs(destination_p + "/network", exist_ok=True) |
| 90 | +os.makedirs(destination_p + "/routes", exist_ok=True) |
| 91 | +os.makedirs(destination_p + "/timetable", exist_ok=True) |
| 92 | + |
| 93 | +print("Saving modified files ...") |
| 94 | +nx.write_graphml(graph, destination_p + "/network/tracks.graphml") |
| 95 | +writeJson(succ, destination_p + "/network/successors_cpp.json") |
| 96 | +writeJson(stations, destination_p + "/timetable/stations.json") |
| 97 | +writeJson(routes, destination_p + "/routes/routes.json") |
| 98 | + |
| 99 | +shutil.copyfile(origin_p + "/timetable/schedules.json", destination_p + "/timetable/schedules.json") |
| 100 | +shutil.copyfile(origin_p + "/timetable/trains.json", destination_p + "/timetable/trains.json") |
0 commit comments