guitarplayer616

2opt working! Conceptualization

Jan 13th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. -- [[ API file ]] --
  2. height,width,length = 8,15,16
  3. blocks = textutils.unserialize(fs.open("blocks","r").readAll())
  4.  
  5. function calculateDistance(tNode1,tNode2)
  6.     local y1 = tNode1[2]
  7.     local z1 = tNode1[3]
  8.     local y2 = tNode2[2]
  9.     local z2 = tNode2[3]
  10.     return math.sqrt( (z2-z1)^2 + (y2-y1)^2 )
  11.     --return math.abs( (z2-z1) + (y2-y1) )
  12. end
  13.  
  14. function twoOptSwap(route, i, k)
  15.     local new_route = {}
  16.     for c = 1,i-1 do
  17.         table.insert(new_route,route[c])
  18.     end
  19.     for c = k,i,-1 do
  20.         table.insert(new_route,route[c])
  21.     end
  22.     for c = k+1,#route do
  23.         table.insert(new_route,route[c])   
  24.     end
  25.     return new_route
  26. end
  27.  
  28. function calculateTotalDistance(route)
  29.     local addString = "0 + "
  30.     for i = 1,#route-1 do
  31.         addString = addString..tostring(calculateDistance(route[i],route[i+1])).." + "
  32.     end
  33.     addString = addString.."0"
  34.     loadstring("total_distance = "..addString)()
  35.     return total_distance
  36. end
  37.  
  38. function getBlockId(x,y,z)
  39.   return blocks[y + z*width + x*length*width + 1]
  40. end
  41.  
  42. function display(route,distance)
  43.     local l,h = term.getSize()
  44.     shell.run("clear")
  45.  
  46.     for i = 1,#route-1 do
  47.         paintutils.drawLine(route[i][2],route[i][3],route[i+1][2],route[i+1][3],colors.lime)
  48.     end
  49.     for i,coord in pairs(route) do
  50.         term.setBackgroundColor(colors.yellow)
  51.         term.setTextColor(colors.magenta)
  52.         term.setCursorPos(coord[2],coord[3])
  53.         term.write(string.char(i+64))
  54.     end
  55.     term.setBackgroundColor(colors.black)
  56.     term.setTextColor(colors.white)
  57.     term.setCursorPos(1,h)
  58.     term.write(distance)
  59. end
  60.  
  61. nodes = {}
  62. local x= 0
  63. for y = 0,width -1 do
  64.     for z = 0,length -1 do
  65.         local id = getBlockId(x,y,z)
  66.         if id and id > 0 then
  67.             table.insert(nodes,{x,y,z})
  68.         end
  69.     end
  70. end
  71.  
  72. -- [[ Main File ]] --
  73.  
  74. route = {}
  75. route[1] = {1,28,3}
  76. route[2] = {1,36,13}
  77. route[3] = {1,20,8}
  78. route[4] = {1,8,8}
  79. route[5] = {1,44,5}
  80. route[6] = {1,32,13}
  81. route[7] = {1,20,17}
  82.  
  83. local existing_route = route
  84. local best_distance = 0
  85.  
  86. function tsp_algorithm()
  87.     local improve = 0
  88.     while improve < 20 do
  89.         best_distance = calculateTotalDistance(existing_route)
  90.         for i = 1,#route-1 do
  91.             for k = i + 1, #route do
  92.                 new_route = twoOptSwap(existing_route, i, k)
  93.                 new_distance = calculateTotalDistance(new_route)
  94.                 if new_distance < best_distance then
  95.                     improve = 0
  96.                     existing_route = new_route
  97.                     best_distance = calculateTotalDistance(existing_route)
  98.                     display(existing_route,best_distance)
  99.                     sleep(.2)
  100.                 end
  101.             end
  102.         end
  103.         improve = improve + 1
  104.     end
  105.     return best_distance, existing_route
  106. end
  107.  
  108. local best_distance,best_route = tsp_algorithm()
  109.  
  110. --textutils.pagedPrint(textutils.serialize(best_route))
Add Comment
Please, Sign In to add comment