Debian Codegraph @

examples/python/tsp/tsp_04.py

raw
1 from opencamlib import ocl, camvtk
2 import time
3 import csv
4
5 def readPts():
6 spamReader = csv.reader(open('tsplib/graph.txt', 'rb'), delimiter=',', quotechar='|')
7 pts=[]
8 for row in spamReader:
9 pts.append( ocl.Point( float(row[0]), float(row[1]) ) )
10 return pts
11
12 def drawTour(myscreen, pts, tour, tourColor = camvtk.red):
13 for n in range(0,len(tour)-1):
14 idx0 = n
15 idx1 = n+1
16 ip1 = pts[ tour[idx0] ]
17 ip2 = pts[ tour[idx1] ]
18 myscreen.addActor( camvtk.Line(p1=(ip1.x,ip1.y,ip1.z),p2=(ip2.x,ip2.y,ip2.z), color=tourColor) )
19
20 def drawPoints(myscreen, pts):
21 c=camvtk.PointCloud( pts )
22 c.SetPoints()
23 myscreen.addActor(c )
24
25
26 if __name__ == "__main__":
27
28 myscreen = camvtk.VTKScreen()
29
30
31 tsp = ocl.TSPSolver()
32 pts = readPts()
33 drawPoints(myscreen, pts)
34 print("got ",len(pts)," points")
35 #exit()
36 for p in pts:
37 tsp.addPoint( p.x , p.y)
38 start_time = time.time()
39 tsp.run()
40 run_time = time.time() - start_time
41 l = tsp.getLength()
42 print("tour length ",l)
43 out = tsp.getOutput()
44 for o in out:
45 print(o,)
46
47 drawTour(myscreen, pts, out)
48
49 myscreen.camera.SetPosition(0.0, 0.0, 200)
50 myscreen.camera.SetViewUp(0.0, 0.0, 0)
51 myscreen.camera.SetFocalPoint(50, 50, 0)
52 myscreen.render()
53
54 myscreen.iren.Start()