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/TwoOpt/ExhaustiveInversionMoveGenerator.cs

    r7259 r8338  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
     
    4042
    4143    public static InversionMove[] Apply(Permutation permutation) {
     44      return Generate(permutation).ToArray();
     45    }
     46
     47    public static IEnumerable<InversionMove> Generate(Permutation permutation) {
    4248      int length = permutation.Length;
    4349      if (length == 1) throw new ArgumentException("ExhaustiveInversionMoveGenerator: There cannot be an inversion move given a permutation of length 1.", "permutation");
    4450      int totalMoves = (length) * (length - 1) / 2;
    45       InversionMove[] moves = null;
    46       int count = 0;
    4751
    4852      if (permutation.PermutationType == PermutationTypes.RelativeUndirected) {
    4953        if (totalMoves - 3 > 0) {
    50           moves = new InversionMove[totalMoves - 3];
    5154          for (int i = 0; i < length - 1; i++) {
    5255            for (int j = i + 1; j < length; j++) {
    5356              // doesn't make sense to inverse the whole permutation or the whole but one in case of relative undirected permutations
    5457              if (j - i >= length - 2) continue;
    55               moves[count++] = new InversionMove(i, j);
     58              yield return new InversionMove(i, j);
    5659            }
    5760          }
    5861        } 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
    59           moves = new InversionMove[1];
    60           moves[0] = new InversionMove(0, 1);
     62          yield return new InversionMove(0, 1);
    6163        }
    6264      } else {
    63         moves = new InversionMove[totalMoves];
    6465        for (int i = 0; i < length - 1; i++)
    6566          for (int j = i + 1; j < length; j++) {
    66             moves[count++] = new InversionMove(i, j);
     67            yield return new InversionMove(i, j);
    6768          }
    6869      }
    69       return moves;
    7070    }
    7171
Note: See TracChangeset for help on using the changeset viewer.