I am creating a genetic algorithm to solve the traveling salesman problem using python and networkx. And I'm adding a condition to converge to a satisfactory solution: the path must not have crossing edges. I wonder if there's a quick function in networkx to verify if the graph has crossing edges or, at least, want to know if it's possible to create one.
The graph is created with a list of points (path
), each point has a coordinate in x, and a coordinate in y. The sequence of points index the path to tour. I created an object nx.Graph()
like below:
G = nx.Graph()
for i in range(len(path)):
G.add_node(i, pos=(path[i].x, path[i].y))
for i in range(len(path)-1):
G.add_edge(i, i+1)
G.add_edge(len(path)-1, 0)
One example of converging not optimal solution:
printing out the points with nx.get_node_attributes(G,'pos')
:
{0: (494, 680), 1: (431, 679), 2: (217, 565), 3: (197, 581), 4: (162, 586), 5: (90, 522), 6:(138, 508), 7: (217, 454), 8: (256, 275), 9: (118, 57), 10: (362, 139), 11: (673, 89), 12: (738, 153), 13: (884, 119), 14: (687, 542), 15: (720, 618), 16: (745, 737), 17: (895, 887), 18: (902, 574), 19: (910, 337), 20: (823, 371), 21: (601, 345), 22: (608, 302), 23: (436, 294), 24: (515, 384), 25: (646, 495)}
Here is an article supporting the condition of convergence: http://www.ams.org/publicoutreach/feature-column/fcarc-tsp