/// <summary>
    /// Calculates the quality of the move <paramref name="move"/> by evaluating the changes.
    /// The runtime complexity of this method is O(N) with N being the size of the permutation.
    /// </summary>
    /// <param name="assignment">The current permutation.</param>
    /// <param name="move">The move that is to be evaluated if it was applied to the current permutation.</param>
    /// <param name="weights">The weights matrix.</param>
    /// <param name="distances">The distances matrix.</param>
    /// <returns>The relative change in quality if <paramref name="move"/> was applied to <paramref name="assignment"/>.</returns>
    public static double Apply(Permutation assignment, ScrambleMove move, DoubleMatrix weights, DoubleMatrix distances) {
      double moveQuality = 0;
      int min = move.StartIndex;
      int max = min + move.ScrambledIndices.Length - 1;

      for (int i = min; i <= max; i++) {
        int locI = assignment[i];
        int newlocI = assignment[min + move.ScrambledIndices[i - min]];
        if (locI == newlocI) continue;

        for (int j = 0; j < assignment.Length; j++) {
          int locJ = assignment[j];
          if (j >= min && j <= max) {
            int newlocJ = assignment[min + move.ScrambledIndices[j - min]];
            moveQuality += weights[i, j] * (distances[newlocI, newlocJ] - distances[locI, locJ]);
            if (locJ == newlocJ)
              moveQuality += weights[j, i] * (distances[newlocJ, newlocI] - distances[locJ, locI]);
          } else {
            moveQuality += weights[i, j] * (distances[newlocI, locJ] - distances[locI, locJ]);
            moveQuality += weights[j, i] * (distances[locJ, newlocI] - distances[locJ, locI]);
          }
        }
      }
      return moveQuality;
    }
Example #2
0
 private ScrambleMove(ScrambleMove original, Cloner cloner)
     : base(original, cloner)
 {
     this.StartIndex = original.StartIndex;
     if (original.ScrambledIndices != null)
     {
         this.ScrambledIndices = (int[])original.ScrambledIndices.Clone();
     }
 }
        public static ScrambleMove[] Apply(Permutation permutation, IRandom random, int sampleSize)
        {
            int length = permutation.Length;

            ScrambleMove[] moves = new ScrambleMove[sampleSize];
            for (int i = 0; i < sampleSize; i++)
            {
                moves[i] = GenerateRandomMove(permutation, random);
            }
            return(moves);
        }
Example #4
0
        public override IOperation Apply()
        {
            ScrambleMove move        = ScrambleMoveParameter.ActualValue;
            Permutation  permutation = PermutationParameter.ActualValue;
            DoubleValue  moveQuality = MoveQualityParameter.ActualValue;
            DoubleValue  quality     = QualityParameter.ActualValue;

            ScrambleManipulator.Apply(permutation, move.StartIndex, move.ScrambledIndices);
            quality.Value = moveQuality.Value;

            return(base.Apply());
        }
Example #5
0
 private ScrambleMove(ScrambleMove original, Cloner cloner)
   : base(original, cloner) {
   this.StartIndex = original.StartIndex;
   if (original.ScrambledIndices != null)
     this.ScrambledIndices = (int[])original.ScrambledIndices.Clone();
 }
 public static ScrambleMove[] Apply(Permutation permutation, IRandom random, int sampleSize) {
   int length = permutation.Length;
   ScrambleMove[] moves = new ScrambleMove[sampleSize];
   for (int i = 0; i < sampleSize; i++) {
     moves[i] = GenerateRandomMove(permutation, random);
   }
   return moves;
 }