Advertisement
guitarplayer616

3opt api

Jan 14th, 2017
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. -- [[ API file ]] --
  2. local w,h = term.getSize()
  3.  
  4. function calculateDistance(tNode1,tNode2)
  5.     local turnCost = 0
  6.     local deltaY,deltaZ
  7.     local y1 = tNode1[2]
  8.     local z1 = tNode1[3]
  9.     local y2 = tNode2[2]
  10.     local z2 = tNode2[3]
  11.    
  12.     deltaZ = z2-z1
  13.     deltaY = y2-y1
  14.    
  15.     if deltaZ == 0 or deltaY == 0 then
  16.         turnCost = 0
  17.     else
  18.         turnCost = 1
  19.     end
  20.    
  21.     return math.abs(deltaZ) + math.abs(deltaY) + turnCost
  22.     --return math.sqrt( (z2-z1)^2 + (y2-y1)^2 )
  23. end
  24.  
  25. function twoOptSwap(route, i, k)
  26.     local new_route = {}
  27.     for c = 1,i-1 do
  28.         table.insert(new_route,route[c])
  29.     end
  30.     for c = k,i,-1 do
  31.         table.insert(new_route,route[c])
  32.     end
  33.     for c = k+1,#route do
  34.         table.insert(new_route,route[c])   
  35.     end
  36.     return new_route
  37. end
  38.  
  39. function calculateTotalDistance(route)
  40.     local total = 0
  41.     for i = 1,#route-1 do
  42.         total = total + calculateDistance(route[i],route[i+1])
  43.     end
  44.     return total
  45. end
  46.  
  47. function display(route,distance)
  48.     local l,h = term.getSize()
  49.     shell.run("clear")
  50.  
  51.     for i = 1,#route-1 do
  52.         paintutils.drawLine(route[i][2],route[i][3],route[i+1][2],route[i+1][3],colors.lime)
  53.     end
  54.     for i,coord in pairs(route) do
  55.         term.setBackgroundColor(colors.yellow)
  56.         term.setTextColor(colors.magenta)
  57.         term.setCursorPos(coord[2]+1,coord[3]+1)
  58.         term.write(string.char(i+64))
  59.     end
  60.     term.setBackgroundColor(colors.black)
  61.     term.setTextColor(colors.white)
  62.     term.setCursorPos(1,h)
  63.     term.write(distance)
  64. end
  65.  
  66.  
  67. --[[nodes = {}
  68. local x= 12
  69. for y = 0,finaly do
  70.     for z = 0,finalz do
  71.         local id = getBlockId(x,y,z)
  72.         if id and id > 0 then
  73.             table.insert(nodes,{x,y,z})
  74.         end
  75.     end
  76. end
  77. ]]
  78.  
  79.  
  80. -- [[ Main File ]] --
  81.  
  82. route = {}
  83. route[1] = {1,28,3}
  84. route[2] = {1,36,13}
  85. route[3] = {1,20,8}
  86. route[4] = {1,8,8}
  87. route[5] = {1,44,5}
  88. route[6] = {1,32,13}
  89. route[7] = {1,20,17}
  90. --route[8] = {1,28,3}
  91.  
  92. route2 = {
  93. {6,5,1},
  94. {6,1,2},
  95. {6,1,5},
  96. {6,2,1},
  97. {6,2,6},
  98. {6,3,1},
  99. {6,3,6},
  100. {6,4,1},
  101. {6,4,6},
  102. {6,5,6},
  103. {6,6,2},
  104. {6,6,5},
  105. }
  106.  
  107. for i = 1,#route2 do
  108.     route2[i][2] = (route2[i][2]/6)*w
  109.     route2[i][3] = (route2[i][3]/6)*h
  110. end
  111.  
  112. function nearest_neighbor(existing_route)
  113.     local best_distance = 10000
  114.     local best_route = { route[1] }
  115.    
  116.     for i = 2,#existing_route-1 do
  117.         for k = i+1,#existing_route do
  118.             local new_distance = calculateDistance(existing_route[i],existing_route[k])
  119.             if new_distance < best_distance then
  120.                 best_distance = new_distance
  121.                 best_route[i+1] = existing_route[k]
  122.             end
  123.         end
  124.     end
  125.    
  126.     return best_route,best_distance
  127. end
  128.  
  129.  
  130. function tsp_algorithm(existing_route)
  131.     local improve = 0
  132.     while improve < 3 do
  133.         local best_distance = calculateTotalDistance(existing_route)
  134.         for i = 2,#existing_route-1 do
  135.             for k = i + 1, #existing_route do
  136.                 new_route = twoOptSwap(existing_route, i, k)
  137.                 new_distance = calculateTotalDistance(new_route)
  138.                 if new_distance < best_distance then
  139.                     improve = 0
  140.                     existing_route = new_route
  141.                     best_distance = calculateTotalDistance(existing_route)
  142.                     display(existing_route,best_distance)
  143.                     sleep(0)
  144.                 end
  145.             end
  146.         end
  147.         improve = improve + 1
  148.     end
  149.     return existing_route, best_distance
  150. end
  151.  
  152. --local best_route,best_distance = tsp_algorithm(nodes)
  153.  
  154. --display(best_route,best_distance)
  155.  
  156. --term.setCursorPos(1,h-#best_route)
  157. --for i,v in pairs(best_route) do
  158.     --print(i," ",v[2]," ",v[3])
  159. --end
  160.  
  161.  
  162. --textutils.pagedPrint(textutils.serialize(best_route))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement