Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/27/12 01:22:49 (12 years ago)
Author:
abeham
Message:

#1904: Added additional local improvement operators

File:
1 edited

Legend:

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

    r7259 r8338  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    3941    }
    4042
    41     public static TranslocationMove[] Apply(Permutation permutation) {
     43    public static IEnumerable<TranslocationMove> Generate(Permutation permutation) {
    4244      int length = permutation.Length;
    4345      if (length == 1) throw new ArgumentException("ExhaustiveInsertionMoveGenerator: There cannot be an insertion move given a permutation of length 1.", "permutation");
    44       TranslocationMove[] moves = null;
    45       int count = 0;
     46
    4647      if (permutation.PermutationType == PermutationTypes.Absolute) {
    47         moves = new TranslocationMove[length * (length - 1)];
    4848        for (int i = 0; i < length; i++) {
    4949          for (int j = 1; j <= length - 1; j++) {
    50             moves[count++] = new TranslocationMove(i, i, (i + j) % length);
     50            yield return new TranslocationMove(i, i, (i + j) % length);
    5151          }
    5252        }
    5353      } else {
    5454        if (length > 2) {
    55           moves = new TranslocationMove[length * (length - 1) - 2];
    5655          for (int i = 0; i < length; i++) {
    5756            for (int j = 1; j <= length - 1; j++) {
    5857              if (i == 0 && j == length - 1
    5958                || i == length - 1 && j == 1) continue;
    60               moves[count++] = new TranslocationMove(i, i, (i + j) % length);
     59              yield return new TranslocationMove(i, i, (i + j) % length);
    6160            }
    6261          }
    6362        } else { // doesn't make sense, but just create a dummy move to not crash the algorithms
    64           moves = new TranslocationMove[1];
    65           moves[0] = new TranslocationMove(0, 0, 1);
     63          yield return new TranslocationMove(0, 0, 1);
    6664        }
    6765      }
    68       System.Diagnostics.Debug.Assert(count == moves.Length);
    69       return moves;
     66    }
     67
     68    public static TranslocationMove[] Apply(Permutation permutation) {
     69      return Generate(permutation).ToArray();
    7070    }
    7171
Note: See TracChangeset for help on using the changeset viewer.