| 1 |
from opencamlib import ocl, camvtk |
| 2 |
import vtk |
| 3 |
import math |
| 4 |
import gzip |
| 5 |
|
| 6 |
# Travelling Salesman Problem |
| 7 |
# a.k.a. drill-path optimization |
| 8 |
# |
| 9 |
# we have multiple points in the 2D plane to visit. |
| 10 |
# the task is to optimize the distance travelled by the tool while |
| 11 |
# visiting all points |
| 12 |
# |
| 13 |
# uses data from TSPLIB (not included in opencamlib) |
| 14 |
# |
| 15 |
# ocl.TSPSolver() is a wrapper for boost::metric_tsp_approx() |
| 16 |
# https://www.boost.org/doc/libs/1_67_0/libs/graph/doc/metric_tsp_approx.html |
| 17 |
|
| 18 |
|
| 19 |
def drawPoints(myscreen, pts): |
| 20 |
c=camvtk.PointCloud( pts ) |
| 21 |
c.SetPoints() |
| 22 |
myscreen.addActor(c ) |
| 23 |
|
| 24 |
def drawTour(myscreen, pts, tour, tourColor = camvtk.red): |
| 25 |
for n in range(0,len(tour)-1): |
| 26 |
idx0 = n |
| 27 |
idx1 = n+1 |
| 28 |
ip1 = pts[ tour[idx0] ] |
| 29 |
ip2 = pts[ tour[idx1] ] |
| 30 |
myscreen.addActor( camvtk.Line(p1=(ip1.x,ip1.y,ip1.z),p2=(ip2.x,ip2.y,ip2.z), color=tourColor) ) |
| 31 |
|
| 32 |
def TSPLIBReader( filename ): |
| 33 |
""" read a .tsp format TSPLIB file, and return list of points """ |
| 34 |
#infile = open(filename,"r") |
| 35 |
infile = gzip.open(filename, 'rb') |
| 36 |
points = [] |
| 37 |
found = 0 |
| 38 |
n = 1 |
| 39 |
for line in infile.readlines(): |
| 40 |
if not found: |
| 41 |
for word in line.split(): |
| 42 |
if word == "NODE_COORD_SECTION": |
| 43 |
found = 1 |
| 44 |
else: |
| 45 |
words = line.split() |
| 46 |
if len(words)!=0 and words[0]!="EOF" : |
| 47 |
points.append( ocl.Point( float( words[1] ), float( words[2] ) ) ) |
| 48 |
|
| 49 |
|
| 50 |
print("read ",len(points)," points from ", filename) |
| 51 |
return points |
| 52 |
|
| 53 |
def TSPTourReader( filename ): |
| 54 |
infile = gzip.open(filename, 'rb') |
| 55 |
points = [] |
| 56 |
found = 0 |
| 57 |
n = 1 |
| 58 |
for line in infile.readlines(): |
| 59 |
if not found: |
| 60 |
for word in line.split(): |
| 61 |
if word == "TOUR_SECTION": |
| 62 |
found = 1 |
| 63 |
else: |
| 64 |
words = line.split() |
| 65 |
if words[0]!="-1" and words[0]!="EOF" : |
| 66 |
points.append( int( words[0] )-1 ) # NOTE our indexes start from 0, thus "-1" |
| 67 |
print(" read length ",len(points)," tour from ",filename) |
| 68 |
points.append( points[0] ) # end at the same point we started |
| 69 |
return points |
| 70 |
|
| 71 |
def TSPTourLength(pts,tour): |
| 72 |
l = 0 |
| 73 |
for n in range(0,len(tour)-1): |
| 74 |
start = n |
| 75 |
stop = n + 1 |
| 76 |
#if stop == len(tour): |
| 77 |
# stop = 0 |
| 78 |
#print start," - ",stop," =: ",tour[start],"-",tour[stop] |
| 79 |
p1 = pts[ tour[start] ] |
| 80 |
p2 = pts[ tour[stop] ] |
| 81 |
d = round( math.sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) ) ) |
| 82 |
l = l+d |
| 83 |
|
| 84 |
return l |
| 85 |
if __name__ == "__main__": |
| 86 |
|
| 87 |
tspname = ["a280" , 2579 ] |
| 88 |
tspname = ["eil51" , 426 ] |
| 89 |
tspname = ["berlin52" , 7542 ] |
| 90 |
tspname = ["bier127" , 118282 ] |
| 91 |
tspname = ["brd14051" , 469385 ] # 90s |
| 92 |
tspname = ["ch130" , 6110 ] # |
| 93 |
tspname = ["ch150" , 6528 ] # |
| 94 |
tspname = ["d198" , 15780 ] # |
| 95 |
tspname = ["d493" , 35002 ] |
| 96 |
tspname = ["d657" , 48912 ] |
| 97 |
tspname = ["d1291" , 50801] # 0.7s |
| 98 |
tspname = ["d1655" , 62128] # 1.23s |
| 99 |
tspname = ["d2103" , 80450] # 1.98s |
| 100 |
tspname = ["d15112" , 1573084] # 110s |
| 101 |
tspname = ["d18512" , 645238] # 160s, 3.9-4.3 Gb |
| 102 |
tspname = ["eil51" , 426 ] |
| 103 |
tspname = ["eil76" , 538 ] |
| 104 |
tspname = ["eil101" , 629 ] |
| 105 |
tspname = ["fl417" , 11861 ] |
| 106 |
tspname = ["fl1400" , 20127 ] # 0.92s |
| 107 |
tspname = ["fl1577" , 22249 ] # 1.09s |
| 108 |
tspname = ["fl3795" , 28772 ] # 6.5s |
| 109 |
tspname = ["fnl4461" , 182566 ] # 9.2s |
| 110 |
tspname = ["gil262" , 2378 ] |
| 111 |
tspname = ["kroA100" , 21282 ] |
| 112 |
tspname = ["kroB100" , 22141] |
| 113 |
tspname = ["kroC100" , 20749] |
| 114 |
tspname = ["kroD100" , 21294] |
| 115 |
tspname = ["kroE100" , 22068] |
| 116 |
tspname = ["kroA150" , 26524] |
| 117 |
tspname = ["kroB150" , 26130] |
| 118 |
tspname = ["kroA200" , 29368] |
| 119 |
tspname = ["kroB200" , 29437] |
| 120 |
tspname = ["lin105" , 14379] |
| 121 |
tspname = ["lin318" , 42029] |
| 122 |
tspname = ["linhp318" , 41345] |
| 123 |
tspname = ["nrw1379" , 56638] # 0.87 |
| 124 |
tspname = ["p654" , 34643] |
| 125 |
tspname = ["pcb442" , 50778] |
| 126 |
tspname = ["pcb1173" , 56892] |
| 127 |
tspname = ["pcb3038" , 137694] # 4s |
| 128 |
tspname = ["pr76" , 108159] |
| 129 |
tspname = ["pr107" , 44303] |
| 130 |
tspname = ["pr124" , 59030] |
| 131 |
tspname = ["pr136" , 96772] |
| 132 |
tspname = ["pr144" , 58537] |
| 133 |
tspname = ["pr152" , 73682] |
| 134 |
tspname = ["pr226" , 80369] |
| 135 |
tspname = ["pr264" , 49135] |
| 136 |
tspname = ["pr299" , 48191] |
| 137 |
tspname = ["pr439" , 107217] |
| 138 |
tspname = ["pr1002" , 259045] |
| 139 |
tspname = ["pr2392" , 378032] |
| 140 |
tspname = ["pr2392" , 378032] |
| 141 |
tspname = ["rat99" , 1211] |
| 142 |
tspname = ["rat195" , 2323] |
| 143 |
tspname = ["rat575" , 6773] |
| 144 |
tspname = ["rat783" , 8806] |
| 145 |
tspname = ["rd100" , 7910] |
| 146 |
tspname = ["rd400" , 15281] |
| 147 |
tspname = ["rl1304" , 252948] |
| 148 |
tspname = ["rl1323" , 270199] |
| 149 |
tspname = ["rl1889" , 316536] # 1.64s |
| 150 |
tspname = ["rl5915" , 565530] # 15.95s |
| 151 |
tspname = ["rl5934" , 556045] # 16.05s |
| 152 |
tspname = ["rl11849" , 923288] # 68s |
| 153 |
tspname = ["st70" , 675] # |
| 154 |
tspname = ["ts225" , 126643] # |
| 155 |
tspname = ["tsp225" , 3916] # |
| 156 |
tspname = ["u159" , 42080] |
| 157 |
tspname = ["u574" , 36905] |
| 158 |
tspname = ["u724" , 41910] |
| 159 |
tspname = ["u1060" , 224094] |
| 160 |
tspname = ["u1432" , 152970] |
| 161 |
tspname = ["u1817" , 57201] |
| 162 |
|
| 163 |
tspname = ["u2319" , 234256] # grid |
| 164 |
tspname = ["vm1084" , 239297] |
| 165 |
|
| 166 |
|
| 167 |
tspname = ["vm1748" , 336556] |
| 168 |
tspname = ["u2152" , 64253] # pcb |
| 169 |
tspname = ["usa13509" , 19982859] # 86s |
| 170 |
opt_len = tspname[1] |
| 171 |
|
| 172 |
tspfile = "tsplib/"+tspname[0]+".tsp.gz" |
| 173 |
#tourfile = "tsplib/"+tspname+".opt.tour.gz" |
| 174 |
pts = TSPLIBReader( tspfile ) |
| 175 |
#tour = TSPTourReader( tourfile ) |
| 176 |
#print tour |
| 177 |
#opt_len = TSPTourLength(pts,tour) |
| 178 |
print(" optimal tour length = ",opt_len) |
| 179 |
|
| 180 |
myscreen = camvtk.VTKScreen() |
| 181 |
drawPoints(myscreen, pts) |
| 182 |
#drawTour(myscreen, pts, tour, camvtk.green) |
| 183 |
tsp = ocl.TSPSolver() |
| 184 |
n=0 |
| 185 |
for p in pts: |
| 186 |
tsp.addPoint( p.x , p.y) |
| 187 |
n=n+1 |
| 188 |
print(" added ",n," points") |
| 189 |
tsp.run() |
| 190 |
r = tsp.getOutput() |
| 191 |
l = tsp.getLength() |
| 192 |
print |
| 193 |
|
| 194 |
drawTour(myscreen, pts, r) |
| 195 |
#tsp.reset() |
| 196 |
print(" tour length= ",l, " ratio = ",float(l)/float(opt_len)) |
| 197 |
|
| 198 |
myscreen.camera.SetPosition(0.0, 0.0, 200) |
| 199 |
myscreen.camera.SetViewUp(0.0, 0.0, 0) |
| 200 |
myscreen.camera.SetFocalPoint(50, 50, 0) |
| 201 |
myscreen.render() |
| 202 |
|
| 203 |
w2if = vtk.vtkWindowToImageFilter() |
| 204 |
w2if.SetInput(myscreen.renWin) |
| 205 |
lwr = vtk.vtkPNGWriter() |
| 206 |
lwr.SetInput( w2if.GetOutput() ) |
| 207 |
|
| 208 |
myscreen.iren.Start() |