Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/02/11 16:45:04 (14 years ago)
Author:
abeham
Message:

#1330

  • changes according to the review
Location:
branches/QAP/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/Swap2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/QAP/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/Swap2/ExhaustiveSwap2MoveGenerator.cs

    r5838 r5931  
    4343      if (length == 1) throw new ArgumentException("ExhaustiveSwap2MoveGenerator: There cannot be an Swap move given a permutation of length 1.", "permutation");
    4444      int totalMoves = (length) * (length - 1) / 2;
    45       Swap2Move[] moves = null;
     45      Swap2Move[] moves = new Swap2Move[totalMoves];
    4646      int count = 0;
    4747
    48       if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
    49         if (totalMoves - 3 > 0) {
    50           moves = new Swap2Move[totalMoves - 3];
    51           for (int i = 0; i < length - 1; i++) {
    52             for (int j = i + 1; j < length; j++) {
    53               if (j - i >= length - 2) continue;
    54               moves[count++] = new Swap2Move(i, j);
    55             }
    56           }
    57         } else { // when length is 3 or less, there's actually no difference, but for the sake of not crashing the algorithm create a dummy move
    58           moves = new Swap2Move[1];
    59           moves[0] = new Swap2Move(0, 1);
     48      for (int i = 0; i < length - 1; i++)
     49        for (int j = i + 1; j < length; j++) {
     50          moves[count++] = new Swap2Move(i, j);
    6051        }
    61       } else {
    62         moves = new Swap2Move[totalMoves];
    63         for (int i = 0; i < length - 1; i++)
    64           for (int j = i + 1; j < length; j++) {
    65             moves[count++] = new Swap2Move(i, j);
    66           }
    67       }
    6852      return moves;
    6953    }
  • branches/QAP/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/Swap2/StochasticSwap2SingleMoveGenerator.cs

    r5838 r5931  
    4949    public static Swap2Move Apply(Permutation permutation, IRandom random) {
    5050      int length = permutation.Length;
    51       if (length == 1) throw new ArgumentException("StochasticSwap2SingleMoveGenerator: There cannot be an Swap move given a permutation of length 1.", "permutation");
    52       int index1 = random.Next(length - 1);
    53       int index2 = random.Next(index1 + 1, length);
     51      if (length < 2) throw new ArgumentException("StochasticSwap2SingleMoveGenerator: There cannot be a swap-2 move given a permutation of length less than 2.", "permutation");
     52      int index1 = random.Next(length), index2 = 0;
     53      do {
     54        index2 = random.Next(length);
     55      } while (index1 == index2);
    5456      return new Swap2Move(index1, index2);
    5557    }
Note: See TracChangeset for help on using the changeset viewer.