| 1 |
from opencamlib import ocl |
| 2 |
import time |
| 3 |
import gzip |
| 4 |
import csv |
| 5 |
|
| 6 |
def TSPLIBReader( filename ): |
| 7 |
""" read a .tsp format TSPLIB file, and return list of points """ |
| 8 |
#infile = open(filename,"r") |
| 9 |
infile = gzip.open(filename, 'rb') |
| 10 |
points = [] |
| 11 |
found = 0 |
| 12 |
n = 1 |
| 13 |
for line in infile.readlines(): |
| 14 |
if not found: |
| 15 |
for word in line.split(): |
| 16 |
if word == "NODE_COORD_SECTION": |
| 17 |
found = 1 |
| 18 |
else: |
| 19 |
words = line.split() |
| 20 |
if len(words)!=0 and words[0]!="EOF" : |
| 21 |
points.append( ocl.Point( float( words[1] ), float( words[2] ) ) ) |
| 22 |
|
| 23 |
|
| 24 |
print("read ",len(points)," points from ", filename) |
| 25 |
return points |
| 26 |
|
| 27 |
|
| 28 |
if __name__ == "__main__": |
| 29 |
|
| 30 |
tsplist = [ ["a280" , 2579 ], |
| 31 |
["berlin52" , 7542 ], |
| 32 |
["bier127" , 118282 ], |
| 33 |
["brd14051" , 469385 ], |
| 34 |
["ch130" , 6110 ], |
| 35 |
["ch150" , 6528 ], |
| 36 |
["d198" , 15780 ], |
| 37 |
["d493" , 35002 ], |
| 38 |
["d657" , 48912 ], |
| 39 |
["d1291" , 50801], |
| 40 |
["d1655" , 62128], |
| 41 |
["d2103" , 80450], |
| 42 |
["d15112" , 1573084], |
| 43 |
["d18512" , 645238], |
| 44 |
["eil51" , 426 ], |
| 45 |
["eil76" , 538 ], |
| 46 |
["eil101" , 629 ], |
| 47 |
["fl417" , 11861 ], |
| 48 |
["fl1400" , 20127 ], |
| 49 |
["fl1577" , 22249 ], |
| 50 |
["fl3795" , 28772 ], |
| 51 |
["fnl4461" , 182566 ], |
| 52 |
["gil262" , 2378 ], |
| 53 |
["kroA100" , 21282 ], |
| 54 |
["kroB100" , 22141], |
| 55 |
["kroC100" , 20749], |
| 56 |
["kroD100" , 21294], |
| 57 |
["kroE100" , 22068], |
| 58 |
["kroA150" , 26524], |
| 59 |
["kroB150" , 26130], |
| 60 |
["kroA200" , 29368], |
| 61 |
["kroB200" , 29437], |
| 62 |
["lin105" , 14379], |
| 63 |
["lin318" , 42029], |
| 64 |
["linhp318" , 41345], |
| 65 |
["nrw1379" , 56638], |
| 66 |
["p654" , 34643], |
| 67 |
["pcb442" , 50778], |
| 68 |
["pcb1173" , 56892], |
| 69 |
["pcb3038" , 137694], |
| 70 |
["pr76" , 108159], |
| 71 |
["pr107" , 44303], |
| 72 |
["pr124" , 59030], |
| 73 |
["pr136" , 96772], |
| 74 |
["pr144" , 58537], |
| 75 |
["pr152" , 73682], |
| 76 |
["pr226" , 80369], |
| 77 |
["pr264" , 49135], |
| 78 |
["pr299" , 48191], |
| 79 |
["pr439" , 107217], |
| 80 |
["pr1002" , 259045], |
| 81 |
["pr2392" , 378032], |
| 82 |
["rat99" , 1211] , |
| 83 |
["rat195" , 2323], |
| 84 |
["rat575" , 6773], |
| 85 |
["rat783" , 8806], |
| 86 |
["rd100" , 7910], |
| 87 |
["rd400" , 15281], |
| 88 |
["rl1304" , 252948], |
| 89 |
["rl1323" , 270199], |
| 90 |
["rl1889" , 316536], |
| 91 |
["rl5915" , 565530], # 22 s |
| 92 |
["rl5934" , 556045], # 22 s |
| 93 |
["rl11849" , 923288], # 93 s |
| 94 |
["st70" , 675], |
| 95 |
["ts225" , 126643], |
| 96 |
["tsp225" , 3916], |
| 97 |
["u159" , 42080], |
| 98 |
["u574" , 36905], |
| 99 |
["u724" , 41910], |
| 100 |
["u1060" , 224094], |
| 101 |
["u1432" , 152970], |
| 102 |
["u1817" , 57201], |
| 103 |
["u2152" , 64253], |
| 104 |
["u2319" , 234256], |
| 105 |
["vm1084" , 239297], |
| 106 |
["usa13509" , 19982859], |
| 107 |
["vm1748" , 336556] |
| 108 |
] |
| 109 |
|
| 110 |
tsplist2=[] |
| 111 |
for t in tsplist: |
| 112 |
tspname = t[0] |
| 113 |
tspfile = "tsplib/"+tspname+".tsp.gz" |
| 114 |
pts = TSPLIBReader( tspfile ) |
| 115 |
inst=[] |
| 116 |
inst.append(len(pts)) # length |
| 117 |
inst.append(t[0]) # name |
| 118 |
inst.append(t[1]) # optimal |
| 119 |
inst.append(pts) # points |
| 120 |
tsplist2.append(inst) |
| 121 |
tsp_sorted = sorted(tsplist2) |
| 122 |
|
| 123 |
Nmax=65 #60(1577) 65(2103) |
| 124 |
results=[] |
| 125 |
#for n in range(0,Nmax): # len(tsp_sorted)): |
| 126 |
for n in range(0, len(tsp_sorted)): |
| 127 |
resultrow=[] |
| 128 |
tsp = ocl.TSPSolver() |
| 129 |
inst = tsp_sorted[n] |
| 130 |
for p in inst[3]: |
| 131 |
tsp.addPoint( p.x , p.y) |
| 132 |
start_time = time.time() |
| 133 |
tsp.run() |
| 134 |
run_time = time.time() - start_time |
| 135 |
l = tsp.getLength() |
| 136 |
ratio = float(l)/float(inst[2]) |
| 137 |
print(n," : ",inst[1],": ",ratio," l=",l," / ",inst[2], " / ",run_time, "seconds") |
| 138 |
resultrow.append( inst[0] ) # length |
| 139 |
resultrow.append( inst[2] ) # optimal |
| 140 |
resultrow.append( l ) # found tour length |
| 141 |
resultrow.append( ratio ) # found tour length |
| 142 |
resultrow.append( run_time ) |
| 143 |
results.append(resultrow) |
| 144 |
|
| 145 |
# write time and length results to file |
| 146 |
|
| 147 |
writer = csv.writer(open('tsp_results.csv', 'wb'), delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL) |
| 148 |
for row in results: |
| 149 |
writer.writerow( row ) |
| 150 |
|
| 151 |
|
| 152 |
|