Showing posts with label double bridge. Show all posts
Showing posts with label double bridge. Show all posts

Saturday, October 21, 2017

Non-sequential 4-opt moves – part 4 of 4

Double Bridge as an improving 4-opt move, part 2

In previous part we have found such cF1...cF4 that replacing links cF1-cF2 and cF3-cF4 with links cF2-cF3 and cF4-cF1 disjoins tour, but gives gain:

cF1 cF2 cF4 cF3 d1

We can expect that segment from cF2 to cF3 contains relatively small number of cities. Therefore we will look for d1 by examining one by one all cities between cF2 and cF3, and then look for d3 among these neighbors of given d1 that are in the other segment, that is between cF4 and cF1. If total gain from set of all eight cities obtained this way is positive, then we have found improving non-sequential move.

But what if segment from cF2 to cF3 contains more cities than the other one?

cF1 cF2 cF4 cF3 d1

Then we can simply "rename" cities before using the procedure described above:

cF3 cF4 cF2 cF1 d1

So we use:

if (2 * Segment_Size(cF2,cF3)) > N:
  (cF1, cF2, cF3, cF4) = (cF3, cF4, cF1, cF2)

where:

proc Segment_Size(cityStart, cityEnd: City_Number): int =
  ## Returns number of cities in segment given by the two cities.
  ## Note that order of parameters is important to distinguish
  ## between two possible segments
  result = (N + position[cityEnd] + 1 - position[cityStart]) mod N

Rest of the code implements the idea described above:

proc LK_Bridge_A1(c1, c2, c3, c4: City_Number;
                  G0: Length_Gain): bool =
  var
    improved: bool
    cF1, cF2, cF3, cF4: City_Number
    d1, d2: City_Number
    Ga, G1a: Length_Gain
    goodSuffix: Suffix
    goodSufficesList: seq[Suffix]
    breadth: int

  improved = false
  # order cities 'forward':
  if (c2 == t_succ(c1)):
    (cF1, cF2, cF3, cF4) = (c1, c2, c3, c4)
  else:
    (cF1, cF2, cF3, cF4) = (c2, c1, c4, c3)
  #end_if
  # we want segment [cF2,cF3] to be the shorter one
  if (2 * Segment_Size(cF2,cF3)) > N:
    (cF1, cF2, cF3, cF4) = (cF3, cF4, cF1, cF2)

  block find_promising_moves:
    goodSufficesList = @[]  # empty list (sequence)
    d1 = cF2
    d2 = t_succ(d1)
    while d2 != cF3:
      G1a = G0 + distance(d1, d2)
      goodSuffix = Suffix(c2iplus1: d1, c2iplus2: d2, Ga: G1a)
      goodSufficesList.add(goodSuffix)
      d1 = d2
      d2 = t_succ(d1)
    #end_loop while d2 != cF3
  #end_block find_promising_moves
    
  block evaluate_promising_moves:
    if len(goodSufficesList) == 0:
      break evaluate_promising_moves
    SortSufficesByGain(goodSufficesList)
    breadth = min(len(goodSufficesList), Max_Breadth_1)
    for mve in 0 .. breadth-1:
      goodSuffix = goodSufficesList[mve]
      d1 = goodSuffix.c2iplus1
      d2 = goodSuffix.c2iplus2     
      Ga = goodSuffix.Ga
      improved = LK_Bridge_A2(cF1, cF2, cF3, cF4, d1, d2, Ga)
      if improved:
        break evaluate_promising_moves
  #end_block evaluate_promising_moves

  result = improved

and:

proc LK_Bridge_A2(c1, c2, c3, c4, d1, d2: City_Number;
                  G1a: Length_Gain): bool =
  var
    improved: bool
    d3, d4: City_Number
    d2_pred, d2_succ: City_Number
    d3_pred, d3_succ: City_Number
    G1, G2a, gainFromCloseUp: Length_Gain
    goodSuffix: Suffix
    goodSufficesList: seq[Suffix]
    tourOrder: int
    tried_c3: int = 0
    breadth: int

  improved = false
  
  # find d3 as a candidate for end of link (d2,d3) to add
  d2_succ = t_succ(d2)
  d2_pred = t_pred(d2)
  
  block find_promising_moves:
    goodSufficesList = @[]  # empty list (sequence)
    for d3 in neighbors(d2):
      if tried_c3 >= 2*Max_Breadth_1:
        break find_promising_moves
      if (d3 == d2_succ) or (d3 == d2_pred):
        continue
      if inOrder(c2, d3, c3, true):
        # d3 should not be between c2 and c3
        continue
        
      G1 = G1a - distance(d2, d3)
      # choose d4 as one of the two tour neighbors of d3:
      d3_succ = t_succ(d3)
      d3_pred = t_pred(d3)
      for d4 in [d3_succ, d3_pred]:
        if d4 == d2:
          continue
        if (d3 == c1) and (d4 == c2)  or
           (d3 == c2) and (d4 == c1):
          continue
        if (d3 == c3) and (d4 == c4)  or
           (d3 == c4) and (d4 == c3):
          continue

        G2a = G1 + distance(d3, d4)
        if (d4 == t_succ(d3)):
          tourOrder = TO_ADBC    # 'ADBC' (||=)
        else:
          tourOrder = TO_AxcxdB  # 'AcdB' (||X)
        #end_if
        goodSuffix = Suffix(c2iplus1: d3, c2iplus2: d4,
                            tourOrder: tourOrder, Ga: G2a)
        goodSufficesList.add(goodSuffix)
        tried_c3 = tried_c3 + 1
      #end_loop for d4
    #end_loop for d3
  #end_block find_promising_moves
        
  block evaluate_promising_moves:
    if len(goodSufficesList) == 0:
      break evaluate_promising_moves
    SortSufficesByGain(goodSufficesList)
    breadth = min(len(goodSufficesList), Max_Breadth_1)
    for mve in 0 .. breadth-1:
      goodSuffix = goodSufficesList[mve]
      d3 = goodSuffix.c2iplus1
      d4 = goodSuffix.c2iplus2     
      G2a = goodSuffix.Ga
      gainFromCloseUp = G2a - distance(d4, d1)
      if gainFromCloseUp > 0: # improving move found
        improved = true
        Make_4opt_NS_Move(c1, c2, c3, c4,
                          d1, d2, d3, d4, tourOrder)
        tourLen = tourLen - gainFromCloseUp
        Set_DLB_off(DontLook, [c1, c2, c3, c4,
                               d1, d2, d3, d4])
        break evaluate_promising_moves 
  #end_block evaluate_promising_moves

  result = improved

Monday, October 9, 2017

Non-sequential 4-opt moves – part 3 of 4

Double Bridge as an improving 4-opt move, part 1

Instead of treating double bridge as a mean of perturbation we could see it as one of the cases of valid 4-opt moves to consider. Then we should look for an improving double bridge move (or any other non-sequential 4-opt move).

So we want to find an improving 4-opt non-sequential move, where one of the links to remove is the link between given baseCity or c1 and one of its tour neighbors. Then we would need to find three more links to remove from remaining N-1 links of tour. We can avoid lot of searching when we notice that a move we are looking for starts with some 2-exchange:

c1 c2 d1 d2 d3 d4 c3 c4 c1 c2 d1 d2 d3 d4 c3 c4

Moreover, since we are looking for an improving move, we can try to use some techniques used in searching for sequential improving moves which make it fast. We would use neighbors as candidates and the idea of a promising move, that is a move with positive partial gain. The first steps are similar to the ones used for sequential moves:

proc LK_1NS_Move(c1: City_Number): bool =
  # returns true if improvement has been found and applied
  var
    improved: bool
    c2: City_Number
    c1_pred, c1_succ: City_Number
    G1a: Length_Gain

  improved = false
  c1_succ = t_succ(c1)
  c1_pred = t_pred(c1)
  for c2 in [c1_succ, c1_pred]:
    G1a = distance(c1, c2)
    improved = LK_2NS_Move(c1, c2, G1a)
    if improved:
      break
  #end_for loop
  result = improved

In the subsequent proc LK_2NS_Move() we gather all promising and "good" (see below) 2-exchanges, sort them by descending partial gain and then try to use them one by one as initial parts of a full move in the next step, in proc LK_3NS_Move(), until an improvement is found.

To simplify our task we narrow searching to these cases only when the first part of move we look for gives some gain (sum of lengths of two links removed so far is greater than sum of lengths of two links added so far).

del_Length = distance(c1, c2) + distance(c3, c4)
add_Length = distance(c2, c3) + distance(c4, c1)
gain = del_Length - add_Length
if gain > 0:
  # search for d1, d2, d3, d4 to build non-sequential move
else:
  # do not consider

This would simplify the algorithm and make it faster, while still providing some flexibility, because the second part itself, exchange of links between d's cities, need not to be improving and can use up some of this gain.

On the other hand this has also other important consequence. If we look for improving non-sequential 4-opt moves after we have made all possible improving sequential moves starting from c1, it means that we know that now the tour cannot be improved by any sequential move starting from c1. However when we start from situation shown on the right side of diagram (which is the first part of ADbc move) and then additionally require that there must be a gain from this first part, then we in fact require that these c1, c2, c3, c4 must form an improving 2-opt move. Which cannot happen when the tour is already optimized by all possible 4-opt sequential moves, because 2-opt is a specific case of 4-opt. So using this approach means that searching for improving ADbc move would be useless.

Accepting this we see that it is sufficient to consider only disjoining moves for possible first part of our non-sequential move:

proc LK_2NS_Move(c1, c2: City_Number;
                 G1a: Length_Gain): bool =
  # returns true if improvement has been found and applied
  var
    improved: bool
    c3, c4: City_Number
    c2_pred, c2_succ: City_Number
    fwd: bool
    G1, G2a, G2M, Ga: Length_Gain
    goodSuffix: Suffix
    goodSufficesList: seq[Suffix]
    tried_c3: int = 0
    breadth: int

  improved = false

  fwd = (c2 == t_succ(c1))
  c2_succ = t_succ(c2)
  c2_pred = t_pred(c2)
  block find_promising_moves:
    goodSufficesList = @[]  # empty list (sequence)
    for c3 in neighbors(c2):
      if tried_c3 >= 2*Max_Breadth_1:
        break find_promising_moves
      if (c3 == c2_succ) or (c3 == c2_pred):
        continue
      G1 = G1a - distance(c2, c3)
      if G1 <= 0:
        break

      # if G1 > 0:
      if fwd:
        c4 = t_succ(c3)
      else:
        c4 = t_pred(c3)
      if c4 == c1:  # NOTE
        continue
      G2a = G1 + distance(c3, c4)
      G2M = G2a - distance(c4, c1)
      if G2M > 0:
        goodSuffix = Suffix(c2iplus1: c3, c2iplus2: c4, Ga: G2M)
        goodSufficesList.add(goodSuffix)
        tried_c3 = tried_c3 + 1
    #end_loop for neighbor_number
  #end_block find_promising_moves
  
  block evaluate_promising_moves:
    if len(goodSufficesList) == 0:
      break evaluate_promising_moves
    SortSufficesByGain(goodSufficesList)
    breadth = min(len(goodSufficesList), Max_Breadth_1)
    for mve in 0 .. breadth-1:
      goodSuffix = goodSufficesList[mve]
      c3 = goodSuffix.c2iplus1
      c4 = goodSuffix.c2iplus2     
      Ga = goodSuffix.Ga
      improved = LK_Bridge_A1(c1, c2, c3, c4, Ga)
      if improved:
        break evaluate_promising_moves
  #end_block evaluate_promising_moves
  result = improved

Now the key to efficient search for a double bridge is to find a good method of finding d1 and then d3 that together with c1...c4 make an improving move. Cities d2 and d4 are easy to find as tour neighbors of d1 and d3.

Important

We should not forget that c2 we considered so far can be a tour predecessor of c1 (then fwd is false). To avoid further annoyances we can start next step, in proc LK_Bridge_A1(), with "normalizing" the direction in which we would like to number the four cities obtained so far, and set it to "forward":

proc LK_Bridge_A1(c1, c2, c3, c4: City_Number;
                 G2a: Length_Gain): bool =
  var
    cF1, cF2, cF3, cF4: City_Number  # c1..c4 cities in forward direction
  ...
  if (c2 == t_succ(c1)): # forward
    (cF1, cF2, cF3, cF4) = (c1, c2, c3, c4)
  else:
    (cF1, cF2, cF3, cF4) = (c2, c1, c4, c3)
  #end_if

Now we are sure that the cities have labels exactly in order and direction shown on diagram above, that is: cF2 is a tour successor of cF1 and so on.

Wednesday, September 20, 2017

Non-sequential 4-opt moves – part 2 of 4

Double Bridge as a mean of perturbation

Sometimes double bridge move is used to perturb a tour. When local optimization method finds no more improving moves, it finishes its work and tour cannot be further improved by this method. But we can slightly perturb resulting tour to obtan a new, very similar, but not fully optimized tour and then try to optimize it in hope to find better result.

When double bridge (or some other non-sequential move) is to be used as a mean to perturb a tour, it does not matter whether it is improving move or not. It is sufficient to use any valid double bridge to obtain perturbed tour. So we can just take any set of cities that can form such move.

c1 c2 d1 d2 d3 d4 c3 c4

Since c2 is always the tour successor of c1, d2 is the successor of d1 and so on, the move is determinated by four cities. Then to make double bridge we can use a set of any four cities. The following proc generates four random city numbers, then sorts these cities by their ascending positions on tour and uses as parameters to make a double bridge move.

proc Double_Bridge_Perturbation() =
  var
    randCity: City_Number
    citiesDblBridge: array[0..3, City_Number]
    forbidden: bool
    i: int
    c1, c2, c3, c4: City_Number
    d1, d2, d3, d4: City_Number

  randomize()
  i = 0
  while i <= 3:
    randCity = random(N)  # returns number from 0 to N-1 inclusive
    forbidden = false
    for j in 0 .. i-1:    
      # check if city is already taken
      if randCity == citiesDblBridge[j]:
        forbidden = true
        continue
      # check if city is a tour neighbor of already taken
      if t_succ(randCity) == citiesDblBridge[j] or
         t_pred(randCity) == citiesDblBridge[j]:
        forbidden = true
        continue
    if not forbidden:
      citiesDblBridge[i] = randCity
      i = i + 1
  # sort by ascending position
  sort(citiesDblBridge) do (x,y: City_Number) -> int:
       result = cmp(position[x], position[y])

  c1 = citiesDblBridge[0]
  d1 = citiesDblBridge[1]
  c3 = citiesDblBridge[2]
  d3 = citiesDblBridge[3]
  c2 = t_succ(c1)
  c4 = t_succ(c3)  
  d2 = t_succ(d1)
  d4 = t_succ(d3)
  
  # make the move:
  Make_4opt_NS_Move(c1, c2, c3, c4, d1, d2, d3, d4, TO_ADBC)

Tuesday, September 12, 2017

Non-sequential 4-opt moves – part 1 of 4

Among all 48 types of 4-opt moves there are exactly five non-sequential pure 4-opt moves. They apparently form two distinct groups:

Moves AbCd and AdCb

AbCdAdCb
A b C d A d C b

While it may be suprising to count them as pure 4-opt non-sequential moves, in fact they are moves of this kind. They are 4-opt moves, because they remove four links and then add four links. They are both pure 4-opt moves, because none of them is equivalent of any single 2-opt nor 3-opt move. They are not sequential moves, which can be easily seen from the fact that we cannot show any closed polygonal chain of line segments connecting the consecutive cities. So by definition they are pure 4-opt non-sequential moves.

On the other hand, each of these moves consists of just two completely independent 2-opt moves, changing two non-overlaping ranges. Each 2-opt move of the pair can be improving or not improving and accordingly to this we would applied it or not. Therefore during optimization there is no need to treat these 4-opt non-sequential moves as practically distinct 4-opt moves, but rather as two separate 2-opts, then as two moves of type we consider anyway during the process of optimization by using sequential moves.

Moves ADbc, AcdB and ADCB

ADbcAcdBADCB
A D b c A c d B A D C B

This group includes non-sequential 4-opt moves, semeed to be specific combinations of two submoves, both being 2-exchanges, where one of them is disjoining (see parallel lines on schemes above) and the other is either disjoining or joining (2-opt, see x-crossed lines).

Including these moves into optimization procedure requires, as usual, resolving two issues: how to find (or generate) such moves and how to actually apply them. Let us start with solution of the latter issue.

The last case, so called "double bridge" move, is most known and instructive. It can be perceived as a kind of combination of two disjoining 2-exchanges. Then the first submove would be always disjoining, and on the other hand we cannot represent two disjoined segments when using array representation of tour (because this single array always contains consecutive city numbers of all cities, as there were links between each pair of consecutive cities). So we would not be able to implement even the first submove of double bridge move.

Second approach to this issue is based on observation that double bridge, which is ADCB, can be obtained from original tour ABCD by shifting two segments or swapping them. This would however require to introduce segment shift (or swap) as second basic transformation, distinct from link exchange we used so far as the only needed basic transformation.

While this may not be obvious, neither segment shifting nor swapping is necessary to obtain the same effect. Let symbol prime (') after a letter or letters in parenthesis means that we reverse this segment(s) and lower letter represent reversed segments:

0.  ABCD
1.  A(BC)'D = AcbD
2.  Ac(bD)' = AcdB
3.  A(cd)'B = ADCB

So double bdridge move is just a specific combination of three segment reversals (then three 2-opts), exactly as any pure 4-opt sequential move:

# 'ADBC' (||=), double bridge
Exchange_Links(d1, d2, d4, d3)
Exchange_Links(c1, c2, c4, c3)
Exchange_Links(d1, d3, d4, d2)
ABCD
A B C D c1 c2 d1 d2 d3 d4 c3 c4
AcbDAcdBADCB
A c b D c1 c2 d1 d2 d3 d4 c3 c4 A c d B c1 c2 d1 d2 d3 d4 c3 c4 A D C B c1 c2 d1 d2 d3 d4 c3 c4

Note that segment configuration before last exchange is AcdB, which is one of the remaining pure 4-opt non-sequential cases. So for this case we have:

# 'AcdB' (||X), crossed vertical bridge
Exchange_Links(d1, d2, d4, d3)
Exchange_Links(c1, c2, c4, c3)

The first case (ADbc) from this group can be implemented by analogy:

0.  ABCD
1.  AB(CD)' = ABdc
2.  A(Bd)'c = ADbc

We can make a general proc for making 4-opt non-sequential move:

const
  TO_ADBC   = -1  # 4-opt 'ADBC' (||=), double bridge
  TO_AxcxdB = -2  # 4-opt 'AcdB' (||X), crossed vertical bridge
  TO_ADxbxc = -3  # 4-opt 'ADbc' (X=), crossed horizontal bridge
proc Make_4opt_NS_Move(c1, c2, c3, c4, d1, d2, d3, d4: City_Number;
                       tourOrder: int) =

  case tourOrder

  of TO_ADBC:    # 'ADBC' (||=), double bridge
   # c12-d12-c34-d34
    Exchange_Links(d1, d2, d4, d3)
    Exchange_Links(c1, c2, c4, c3)
    Exchange_Links(d1, d3, d4, d2)

  of TO_AxcxdB:  # 'AcdB' (||X), crossed vertical bridge
    # c12-d12-c34-d43
    Exchange_Links(d1, d2, d4, d3)
    Exchange_Links(c1, c2, c4, c3)
    
  of TO_ADxbxc:  # 'ADbc' (X=), crossed horizontal bridge
    # c12-d12-c43-d34
    Exchange_Links(c1, c2, c4, c3)
    Exchange_Links(d1, d2, d3, d4)
  
  else:
    echo "Unknown 4-opt non-sequential move: ", tourOrder
    quit 1

Beware

Proc Exchange_Links() used here takes city numbers, not city positions in tour, as arguments. Confusing these two different ways leads to wrong results.

Saturday, April 22, 2017

4-opt and Double Bridge

Replacing 3-optic moves in the local optimization with 4-opt moves increases the time complexity the algorithm from O(N3) to O(N4), or, in the case of using lists of candidates (neighbors), from O(N * m2) to O (N * m3), where m is number of neighbors in the list.

However, the problem does not lie only in much greater time of execution. Applying 4-opt moves gives tours only slightly shorter than those from 3-optimization. Moreover, while for 3-opt there are 8 types (cases) of move, for 4-opt this number increases to 48, so considering all of them becomes much more complicated. As in 3-opt, one of them gives the tour identical with the tour before the move. Analogically to 3-opt, many of them are equal to a single 2-opt or a single 3-opt move; there are only 25 pure 4-opt moves. But something new appears in 4-opt moves: most known 4-opt move, called double bridge move.

The simplicity and symmetry of the double bridge scheme make it particularly easy to recognize and remember:

Double bridge, as the simplest non-sequential move (idea of so called sequenctial moves is described later), plays important role even in algorithms that do not use full 4-optimization. By definition given tour after full 3-optimization cannot be further improved by 3-opt, but it does not mean that there are no similar but shorter tours. Instead of trying to search or build such tours from scratch, we often use 3-optimized tour and perturb it to obtain a new good initial tour, that we can then optimize, perhaps getting better result. Double bridge is used as simple and effective perturbation.

Double bridge and other non-sequential 4-opt moves are discussed in detail later.