Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3706


Ignore:
Timestamp:
05/07/10 16:29:25 (15 years ago)
Author:
abeham
Message:

#839

  • Added some checks to allow moves in permutation of length 2 or 3.
Location:
trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/ExhaustiveInsertionMoveGenerator.cs

    r3376 r3706  
    3535    public static TranslocationMove[] Apply(Permutation permutation) {
    3636      int length = permutation.Length;
     37      if (length == 1) throw new ArgumentException("ExhaustiveInsertionMoveGenerator: There cannot be an insertion move given a permutation of length 1.", "permutation");
    3738      TranslocationMove[] moves = null;
    3839      int count = 0;
     
    4546        }
    4647      } else {
    47         moves = new TranslocationMove[length * (length - 1) - 2];
    48         for (int i = 0; i < length; i++) {
    49           for (int j = 1; j <= length - 1; j++) {
    50             if (i == 0 && j == length - 1
    51               || i == length - 1 && j == 1) continue;
    52             moves[count++] = new TranslocationMove(i, i, (i + j) % length);
     48        if (length > 2) {
     49          moves = new TranslocationMove[length * (length - 1) - 2];
     50          for (int i = 0; i < length; i++) {
     51            for (int j = 1; j <= length - 1; j++) {
     52              if (i == 0 && j == length - 1
     53                || i == length - 1 && j == 1) continue;
     54              moves[count++] = new TranslocationMove(i, i, (i + j) % length);
     55            }
    5356          }
     57        } else { // doesn't make sense, but just create a dummy move to not crash the algorithms
     58          moves = new TranslocationMove[1];
     59          moves[0] = new TranslocationMove(0, 0, 1);
    5460        }
    5561      }
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/StochasticTranslocationMultiMoveGenerator.cs

    r3376 r3706  
    5454      TranslocationMove[] moves = new TranslocationMove[sampleSize];
    5555      for (int i = 0; i < sampleSize; i++) {
    56         moves[i] = StochasticThreeOptSingleMoveGenerator.Apply(permutation, random);
     56        moves[i] = StochasticTranslocationSingleMoveGenerator.Apply(permutation, random);
    5757      }
    5858      return moves;
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/StochasticTranslocationSingleMoveGenerator.cs

    r3376 r3706  
    3131  [Item("StochasticTranslocationSingleMoveGenerator", "Randomly samples one from all possible translocation and insertion moves (3-opt) from a given permutation.")]
    3232  [StorableClass]
    33   public class StochasticThreeOptSingleMoveGenerator : TranslocationMoveGenerator, IStochasticOperator, ISingleMoveGenerator {
     33  public class StochasticTranslocationSingleMoveGenerator : TranslocationMoveGenerator, IStochasticOperator, ISingleMoveGenerator {
    3434    public ILookupParameter<IRandom> RandomParameter {
    3535      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    3636    }
    3737
    38     public StochasticThreeOptSingleMoveGenerator()
     38    public StochasticTranslocationSingleMoveGenerator()
    3939      : base() {
    4040      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
     
    4343    public static TranslocationMove Apply(Permutation permutation, IRandom random) {
    4444      int length = permutation.Length;
     45      if (length == 1) throw new ArgumentException("StochasticThreeOptSingleMoveGenerator: There cannot be an insertion move given a permutation of length 1.", "permutation");
     46      if (permutation.PermutationType != PermutationTypes.Absolute && length == 2) return new TranslocationMove(0, 0, 1);
    4547      int index1, index2, index3;
    4648      do {
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/TwoOpt/ExhaustiveInversionMoveGenerator.cs

    r3376 r3706  
    3232    public static InversionMove[] Apply(Permutation permutation) {
    3333      int length = permutation.Length;
     34      if (length == 1) throw new ArgumentException("ExhaustiveInversionMoveGenerator: There cannot be an inversion move given a permutation of length 1.", "permutation");
    3435      int totalMoves = (length) * (length - 1) / 2;
    3536      InversionMove[] moves = null;
     
    3738
    3839      if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
    39         moves = new InversionMove[totalMoves - 3];
    40         for (int i = 0; i < length - 1; i++) {
    41           for (int j = i + 1; j < length; j++) {
    42             // doesn't make sense to inverse the whole permutation or the whole but one in case of relative undirected permutations
    43             if (j - i >= length - 2) continue;
    44             moves[count++] = new InversionMove(i, j);
     40        if (totalMoves - 3 > 0) {
     41          moves = new InversionMove[totalMoves - 3];
     42          for (int i = 0; i < length - 1; i++) {
     43            for (int j = i + 1; j < length; j++) {
     44              // doesn't make sense to inverse the whole permutation or the whole but one in case of relative undirected permutations
     45              if (j - i >= length - 2) continue;
     46              moves[count++] = new InversionMove(i, j);
     47            }
    4548          }
     49        } 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
     50          moves = new InversionMove[1];
     51          moves[0] = new InversionMove(0, 1);
    4652        }
    4753      } else {
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/TwoOpt/StochasticInversionSingleMoveGenerator.cs

    r3376 r3706  
    4343    public static InversionMove Apply(Permutation permutation, IRandom random) {
    4444      int length = permutation.Length;
     45      if (length == 1) throw new ArgumentException("StochasticInversionSingleMoveGenerator: There cannot be an inversion move given a permutation of length 1.", "permutation");
    4546      int index1 = random.Next(length - 1);
    4647      int index2 = random.Next(index1 + 1, length);
    4748      if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
    48         while (index2 - index1 >= length - 2)
    49           index2 = random.Next(index1 + 1, length);
     49        if (length > 3) {
     50          while (index2 - index1 >= length - 2)
     51            index2 = random.Next(index1 + 1, length);
     52        }
    5053      }
    5154      return new InversionMove(index1, index2);
Note: See TracChangeset for help on using the changeset viewer.