def test_algo(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "temp_algo") if not os.path.exists(folder): os.mkdir(folder) edges = get_data(whereTo=folder, fLOG=fLOG) edges = edges[:1000] max_segment = max(e[-1] for e in edges) possibles = possible_edges(edges, max_segment / 8, fLOG=fLOG) init = bellman(edges, fLOG=fLOG, allow=lambda e: e in possibles) fLOG("---") init = bellman(edges, fLOG=fLOG, allow=lambda e: e in possibles, init=init) fLOG("---") added = kruskal(edges, init, fLOG=fLOG) d = graph_degree(edges + added) allow = sorted([k for k, v in d.items() if v % 2 == 1]) fLOG("degrees", allow) allow = set(allow) fLOG("---") init = bellman( edges, fLOG=fLOG, allow=lambda e: e in possibles or e[0] in allow or e[1] in allow, init=init) fLOG("---") added = kruskal(edges, init, fLOG=fLOG) d = graph_degree(edges + added) allow = sorted([k for k, v in d.items() if v % 2 == 1]) fLOG("degrees", allow)
def test_algo(self): fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") folder = os.path.join(os.path.abspath(os.path.dirname(__file__)),"temp_rues2") if not os.path.exists(folder) : os.mkdir(folder) edges = get_data(whereTo=folder) edges = edges[:1000] max_segment = max ( e[-1] for e in edges ) possibles = possible_edges(edges, max_segment/8, fLOG = fLOG) init = bellman(edges, fLOG = fLOG, allow = lambda e : e in possibles) fLOG("---") init = bellman(edges, fLOG = fLOG, allow = lambda e : e in possibles, init = init) fLOG("---") added = kruskall(edges, init, fLOG = fLOG) d = graph_degree(edges + added) allow = sorted([ k for k,v in d.items() if v%2 == 1 ]) fLOG("degrees", allow) allow = set(allow) fLOG("---") init = bellman(edges, fLOG = fLOG, allow = lambda e : e in possibles or e[0] in allow or e[1] in allow, init = init) fLOG("---") added = kruskall(edges, init, fLOG = fLOG) d = graph_degree(edges + added) allow = sorted([ k for k,v in d.items() if v%2 == 1 ]) fLOG("degrees", allow)
def test_algo_euler4(self): fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") folder = os.path.join(os.path.abspath(os.path.dirname(__file__)),"temp_rues5") if not os.path.exists(folder) : os.mkdir(folder) edges = get_data(whereTo=folder) edges = edges[:3] vertices = { } for e in edges : for i in range(0,2): _ = e[i] p = e[i+3] vertices[_] = p connex = connected_components(edges) v = [ v for k,v in connex.items() ] mi,ma = min(v), max(v) while mi != ma : edges.append( (mi, ma, 2, vertices[mi], vertices[ma], distance_haversine( * (vertices[mi] + vertices[ma]) ) ) ) connex = connected_components(edges) v = [ v for k,v in connex.items() ] mi,ma = min(v), max(v) if __name__ == "__main__": import matplotlib.pyplot as plt import networkx as nx fig = plt.figure() G = nx.Graph() for e in edges : a,b = e[:2] G.add_edge(a,b) pos = nx.spring_layout(G) nx.draw(G,pos,node_color='#A0CBE2') plt.savefig(os.path.join(folder, "graph1.png")) added = eulerien_extension( edges, fLOG=lambda *l : None, distance = distance_paris) if __name__ == "__main__": for e in added : a,b = e[:2] G.add_edge(a,b) fig = plt.figure() pos = nx.spring_layout(G) deg = graph_degree(edges + added) #labels={ v:"{0}".format(deg[v]) for v in G.nodes() } nx.draw(G,pos,node_color='#A0CBE2'#,labels=labels ) plt.savefig(os.path.join(folder, "graph2.png")) path = euler_path(edges, added) all = edges + added fLOG(len(all),len(path))
def test_algo_euler4(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "temp_algo_euler4") if not os.path.exists(folder): os.mkdir(folder) edges = get_data(whereTo=folder, fLOG=fLOG) edges = edges[:3] vertices = {} for e in edges: for i in range(0, 2): _ = e[i] p = e[i + 3] vertices[_] = p connex = connected_components(edges) v = [v for k, v in connex.items()] mi, ma = min(v), max(v) while mi != ma: edges.append((mi, ma, 2, vertices[mi], vertices[ma], distance_haversine(*(vertices[mi] + vertices[ma])))) connex = connected_components(edges) v = [v for k, v in connex.items()] mi, ma = min(v), max(v) fix_tkinter_issues_virtualenv() import matplotlib.pyplot as plt import networkx as nx plt.figure() G = nx.Graph() for e in edges: a, b = e[:2] G.add_edge(a, b) pos = nx.spring_layout(G) nx.draw(G, pos, node_color='#A0CBE2') plt.savefig(os.path.join(folder, "graph1.png")) plt.close('all') added = eulerien_extension(edges, fLOG=lambda *l: None, distance=distance_paris) for e in added: a, b = e[:2] G.add_edge(a, b) plt.figure() pos = nx.spring_layout(G) graph_degree(edges + added) #labels={ v:"{0}".format(deg[v]) for v in G.nodes() } nx.draw( G, pos, node_color='#A0CBE2' # ,labels=labels ) plt.savefig(os.path.join(folder, "graph2.png")) plt.close('all') path = euler_path(edges, added) alls = edges + added fLOG(len(alls), len(path))