Monday, July 3, 2017

Lin-Kernighan algorithm basics – part 10

When using DLB and subsequent moves we need a variable and a pair of procs to save and restore DLB array:

var
  DontLook_copy: DLB_Array

proc Save_DLB() =
  DontLook_copy = DontLook

proc Restore_DLB() =
  DontLook = DontLook_copy

The last part contains some additional constants, variables and procs used to maintain subsequent moves:

var
  Max_Depth:int = 55
var
  SubseqLvl: int
type
  Link = array[0..1, City_Number]
var
  linksAdded: array[0 .. N-1, Link]
  linksAdded_Len: int = 0

proc LinksAddedClear() =
  linksAdded_Len = 0
  
proc AddToLinksAdded(city_A, city_B: City_Number) =
  var
    city_L, city_R: City_Number
  if city_A < city_B:
    city_L = city_A
    city_R = city_B
  else:
    city_L = city_B
    city_R = city_A
  linksAdded[linksAdded_Len] = [city_L, city_R]
  linksAdded_Len = linksAdded_Len + 1
  
proc isLinkAdded(city_A, city_B: City_Number): bool =
  var
    city_L, city_R: City_Number
  if city_A < city_B:
    city_L = city_A
    city_R = city_B
  else:
    city_L = city_B
    city_R = city_A
  result = false
  for idx in 0 .. linksAdded_Len-1:
    if (city_L == linksAdded[idx][0] and
        city_R == linksAdded[idx][1]):
       result = true
       break
  #end_loop idx

Note that procs for saving and restoring tour are not efficient. They can be replaced by procs for storing and "undoing" exchanges made, but this would reuquire additional means to make sure that recognized type of sequence does not change or is updated.

var
  t_copy: Tour_Array
  p_copy: City_Position_In_Tour
  l_copy: Length

proc Save_Tour() =
  t_copy = tour
  p_copy = position
  l_copy = tourLen
  
proc Restore_Tour() =
  tour     = t_copy
  position = p_copy
  tourLen  = l_copy

Some simple statistics

Relative time, best length, average length and worst length for all tours created by NN heuristic and improved by 3-opt with neighbor lists and simple Lin-Kernighan implementation. (Note that this 3-opt implementation uses slower method of applying moves than that in LK.) Neighbor lists size=24, LK breadth=(5, 5 ,3), LK depth=55. Random planar problems, N=100.
Problem # Method Time Best Avg Worst
1NN100100.00109.45117.82
3opt-ND(24)488185.9587.3290.41
LK 4+2224385.3886.2988.67
2NN100100.00108.00115.44
3opt-ND(24)458786.8888.3691.37
LK 4+2146886.8387.4090.19
3NN100100.00107.33117.17
3opt-ND(24)498184.1285.8789.60
LK 4+2234384.1285.0190.19
4NN100100.00106.75117.44
3opt-ND(24)458787.1089.5592.74
LK 4+2214987.1088.4791.22
5NN100100.00107.14123.30
3opt-ND(24)478189.7991.3393.25
LK 4+2214989.5590.7192.17
6NN100100.00109.40122.11
3opt-ND(24)507486.3687.9090.88
LK 4+2205085.8886.6488.74
7NN100100.00109.81121.38
3opt-ND(24)468791.8292.9296.31
LK 4+2194991.7192.3694.48
8NN100100.00107.21118.20
3opt-ND(24)478187.5788.8490.88
LK 4+2283787.2488.2590.63
9NN100100.00108.36117.82
3opt-ND(24)498188.1689.3391.96
LK 4+2126888.1688.5391.97
10NN100100.00108.14116.89
3opt-ND(24)517488.7690.3793.61
LK 4+2263788.5289.2491.08

No comments:

Post a Comment