-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.py
120 lines (100 loc) · 2.92 KB
/
tsp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
Created on Mar 14, 2013
@Andrew ID: bramnani
@Title : Implementation of the A star algorithm used to solve TSP. Homework 5
@Author: Bhushan Ramnani
'''
import networkx as nx
import mst
import heapq
import copy
import sys
if __name__ == '__main__':
pass
class path:
p = []
g = 0
f = 0
def __cmp__(self, other):
"""Function used by heap in order to order the objects of type path"""
return cmp(self.f, other.f)
def heuristic(path, G):
"""Takes a path and the associated graph. Returns the MST based heuristic"""
H = copy.deepcopy(G)
l = len(path)
if l>2:
path = path[1:(l-1)]
H.remove_nodes_from(path)
elif l==2:
H.remove_edge(path[0],path[1])
M = mst.prim_mst(H)
weight = 0
for u,v in M.edges():
weight = weight + M[u][v]['weight']
return weight
def generatePaths(G,u):
""" Returns a list of possible paths by extending the path u by one more node in the graph G"""
start = u.p[-1]
result = []
List = G.neighbors(start)
for v in List:
if v not in u.p:
newP = path()
newP.p = copy.deepcopy(u.p)
newP.p.append(v)
edgeWt = G.edge[start][v]['weight']
newP.g = u.g + edgeWt
newP.f = newP.g + heuristic(newP.p, G)
result.append(newP)
return result
def generateLastPath(G,u):
"""In case the last node to be added in the path is just the source, add the last node and return the final path"""
start = u.p[-1]
List = G.neighbors(start)
newP = None
for v in List:
if v==u.p[0]:
newP = path()
newP.p = copy.deepcopy(u.p)
newP.p.append(v)
newP.g = u.g + G[start][v]['weight']
newP.f = newP.g
return newP
def aStarTSP(G,s):
"""Resolves the TSP problem using A Star algorithm"""
"""Returns the TSP path"""
numberOfNodes = G.number_of_nodes()
H = []
x = path()
x.p = [s]
while x is not None:
if len(x.p)== (numberOfNodes+1) and x.p[-1]==s:
return x
if len(x.p)==(numberOfNodes):
q = generateLastPath(G,x)
if q is None:
continue
else:
heapq.heappush(H,q)
else:
for q in generatePaths(G,x):
heapq.heappush(H, q)#add q to the heap
try:
x = heapq.heappop(H)#remove the minimum path from the heap
#print "Current path = ",x.p
except IndexError:
x = None
return None
def main(inputFile):
fileName = str(inputFile[1])
G = nx.read_gexf(fileName)
V = G.nodes()
s = min(V)
P = aStarTSP(G,s)
tour = P.p
print "Tour: ",
for i in range(len(tour)-1):
print tour[i],
print ""
print "Cost: ",P.f
main(sys.argv)