Index: branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Moves/OneShift/PTSPEstimatedInsertionMoveEvaluator.cs
===================================================================
--- branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators/OneShift/PTSPEstimatedInsertionEvaluator.cs	(revision 12799)
+++ branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Moves/OneShift/PTSPEstimatedInsertionMoveEvaluator.cs	(revision 13412)
@@ -20,4 +20,5 @@
 #endregion
 
+using System;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
@@ -26,26 +27,18 @@
 using HeuristicLab.Parameters;
 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
-using System;
 
 namespace HeuristicLab.Problems.PTSP {
-  [Item("PTSPEstimatedInsertionEvaluator", "Evaluates an insertion move (1-shift)")]
+  [Item("PTSPEstimatedInsertionMoveEvaluator", "Evaluates an insertion move (1-shift)")]
   [StorableClass]
-  public class PTSPEstimatedInsertionEvaluator : PTSPMoveEvaluator, IPermutationTranslocationMoveOperator {
-
-    public override Type EvaluatorType {
-      get { return typeof(PTSPEstimatedInsertionEvaluator); }
-    }
+  public class PTSPEstimatedInsertionMoveEvaluator : EstimatedPTSPMoveEvaluator, IPermutationTranslocationMoveOperator {
+
     public ILookupParameter<TranslocationMove> TranslocationMoveParameter {
       get { return (ILookupParameter<TranslocationMove>)Parameters["TranslocationMove"]; }
     }
 
-    public IValueParameter<ItemList<ItemList<IntValue>>> RealizationsParameter {
-      get { return (IValueParameter<ItemList<ItemList<IntValue>>>)Parameters["Realizations"]; }
-    }
-
     [StorableConstructor]
-    protected PTSPEstimatedInsertionEvaluator(bool deserializing) : base(deserializing) { }
-    protected PTSPEstimatedInsertionEvaluator(PTSPEstimatedInsertionEvaluator original, Cloner cloner) : base(original, cloner) { }
-    public PTSPEstimatedInsertionEvaluator()
+    protected PTSPEstimatedInsertionMoveEvaluator(bool deserializing) : base(deserializing) { }
+    protected PTSPEstimatedInsertionMoveEvaluator(PTSPEstimatedInsertionMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
+    public PTSPEstimatedInsertionMoveEvaluator()
       : base() {
       Parameters.Add(new LookupParameter<TranslocationMove>("TranslocationMove", "The move to evaluate."));
@@ -53,35 +46,15 @@
 
     public override IDeepCloneable Clone(Cloner cloner) {
-      return new PTSPEstimatedInsertionEvaluator(this, cloner);
-    }
-
-    public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, PTSPEstimatedInsertionEvaluator evaluator) {
-      int edge1source = permutation.GetCircular(move.Index1 - 1);
-      int edge1target = permutation[move.Index1];
-      int edge2source = permutation[move.Index2];
-      int edge2target = permutation.GetCircular(move.Index2 + 1);
-      if (move.Index2 - move.Index1 >= permutation.Length - 2) return 0;
-      double moveQuality = 0;
-      // remove two edges
-      moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
-            coordinates[edge1target, 0], coordinates[edge1target, 1]);
-      moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
-        coordinates[edge2target, 0], coordinates[edge2target, 1]);
-      // add two edges
-      moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
-        coordinates[edge2source, 0], coordinates[edge2source, 1]);
-      moveQuality += evaluator.CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
-        coordinates[edge2target, 0], coordinates[edge2target, 1]);
-      return moveQuality;
-    }
-
-    public static double EvaluateByDistanceMatrix(Permutation permutation, TranslocationMove move, DistanceMatrix distanceMatrix, ItemList<ItemList<IntValue>> realizations) {
-      Permutation afterMove = new Permutation(PermutationTypes.RelativeUndirected,permutation);
+      return new PTSPEstimatedInsertionMoveEvaluator(this, cloner);
+    }
+
+    public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, DistanceCalculator distanceCalculator, ItemList<BoolArray> realizations) {
+      var afterMove = new Permutation(PermutationTypes.RelativeUndirected, permutation);
       TranslocationManipulator.Apply(afterMove, move.Index1, move.Index1, move.Index3);
       double moveQuality = 0;
-      int[] edges = new int[12];
-      int[] indices = new int[12];
+      var edges = new int[12];
+      var indices = new int[12];
       edges[0] = permutation.GetCircular(move.Index1 - 1);
-      indices[0] = decreaseCircularIndex(permutation.Length, move.Index1);
+      indices[0] = DecreaseCircularIndex(permutation.Length, move.Index1);
       edges[1] = permutation[move.Index1];
       indices[1] = move.Index1;
@@ -89,8 +62,8 @@
       indices[2] = move.Index1;
       edges[3] = permutation.GetCircular(move.Index1 + 1);
-      indices[3] = increaseCircularIndex(permutation.Length, move.Index1);
+      indices[3] = IncreaseCircularIndex(permutation.Length, move.Index1);
 
       edges[6] = afterMove.GetCircular(move.Index3 - 1);
-      indices[6] = decreaseCircularIndex(afterMove.Length, move.Index3);
+      indices[6] = DecreaseCircularIndex(afterMove.Length, move.Index3);
       edges[7] = afterMove[move.Index3];
       indices[7] = move.Index3;
@@ -98,5 +71,5 @@
       indices[8] = move.Index3;
       edges[9] = afterMove.GetCircular(move.Index3 + 1);
-      indices[9] = increaseCircularIndex(afterMove.Length, move.Index3);
+      indices[9] = IncreaseCircularIndex(afterMove.Length, move.Index3);
 
       if (move.Index3 > move.Index1) {
@@ -120,7 +93,7 @@
       }
       int[] aPosteriori = new int[12];
-      Permutation tempPermutation;
-      foreach (ItemList<IntValue> realization in realizations) {
+      foreach (var realization in realizations) {
         for (int i = 0; i < edges.Length; i++) {
+          Permutation tempPermutation;
           if (i < 6) {
             tempPermutation = permutation;
@@ -128,11 +101,11 @@
             tempPermutation = afterMove;
           }
-          if (realization[edges[i]].Value == 1) {
+          if (realization[edges[i]]) {
             aPosteriori[i] = edges[i];
           } else {
-            int j=1;
-            if (i%2==0) {
+            int j = 1;
+            if (i % 2 == 0) {
               // find nearest predecessor in realization if source edge
-              while (realization[tempPermutation.GetCircular(indices[i] - j)].Value == 0) {
+              while (!realization[tempPermutation.GetCircular(indices[i] - j)]) {
                 j++;
               }
@@ -140,5 +113,94 @@
             } else {
               // find nearest successor in realization if target edge
-              while (realization[tempPermutation.GetCircular(indices[i] + j)].Value == 0) {
+              while (!realization[tempPermutation.GetCircular(indices[i] + j)]) {
+                j++;
+              }
+              aPosteriori[i] = tempPermutation.GetCircular(indices[i] + j);
+            }
+          }
+        }
+        if (!(aPosteriori[0] == aPosteriori[2] && aPosteriori[1] == aPosteriori[3]) &&
+          !(aPosteriori[0] == aPosteriori[4] && aPosteriori[1] == aPosteriori[5]) &&
+          !(aPosteriori[2] == aPosteriori[4] && aPosteriori[3] == aPosteriori[5])) {
+          // compute cost difference between the two a posteriori solutions
+
+          moveQuality += distanceCalculator.Calculate(aPosteriori[6], aPosteriori[7], coordinates)
+                       + distanceCalculator.Calculate(aPosteriori[8], aPosteriori[9], coordinates)
+                       + distanceCalculator.Calculate(aPosteriori[10], aPosteriori[11], coordinates)
+                       - distanceCalculator.Calculate(aPosteriori[0], aPosteriori[1], coordinates)
+                       - distanceCalculator.Calculate(aPosteriori[2], aPosteriori[3], coordinates)
+                       - distanceCalculator.Calculate(aPosteriori[4], aPosteriori[5], coordinates);
+        }
+        Array.Clear(aPosteriori, 0, aPosteriori.Length);
+      }
+      // return average of cost differences
+      return moveQuality / realizations.Count;
+    }
+
+    public static double EvaluateByDistanceMatrix(Permutation permutation, TranslocationMove move, DistanceMatrix distanceMatrix, ItemList<BoolArray> realizations) {
+      var afterMove = new Permutation(PermutationTypes.RelativeUndirected, permutation);
+      TranslocationManipulator.Apply(afterMove, move.Index1, move.Index1, move.Index3);
+      double moveQuality = 0;
+      var edges = new int[12];
+      var indices = new int[12];
+      edges[0] = permutation.GetCircular(move.Index1 - 1);
+      indices[0] = DecreaseCircularIndex(permutation.Length, move.Index1);
+      edges[1] = permutation[move.Index1];
+      indices[1] = move.Index1;
+      edges[2] = permutation[move.Index1];
+      indices[2] = move.Index1;
+      edges[3] = permutation.GetCircular(move.Index1 + 1);
+      indices[3] = IncreaseCircularIndex(permutation.Length, move.Index1);
+
+      edges[6] = afterMove.GetCircular(move.Index3 - 1);
+      indices[6] = DecreaseCircularIndex(afterMove.Length, move.Index3);
+      edges[7] = afterMove[move.Index3];
+      indices[7] = move.Index3;
+      edges[8] = afterMove[move.Index3];
+      indices[8] = move.Index3;
+      edges[9] = afterMove.GetCircular(move.Index3 + 1);
+      indices[9] = IncreaseCircularIndex(afterMove.Length, move.Index3);
+
+      if (move.Index3 > move.Index1) {
+        edges[4] = permutation[move.Index3];
+        indices[4] = move.Index3;
+        edges[5] = permutation.GetCircular(move.Index3 + 1);
+        indices[5] = indices[9];
+        edges[10] = afterMove.GetCircular(move.Index1 - 1);
+        indices[10] = indices[0];
+        edges[11] = afterMove[move.Index1];
+        indices[11] = move.Index1;
+      } else {
+        edges[4] = permutation.GetCircular(move.Index3 - 1);
+        indices[4] = indices[6];
+        edges[5] = permutation[move.Index3];
+        indices[5] = move.Index3;
+        edges[10] = afterMove[move.Index1];
+        indices[10] = move.Index1;
+        edges[11] = afterMove.GetCircular(move.Index1 + 1);
+        indices[11] = indices[3];
+      }
+      int[] aPosteriori = new int[12];
+      foreach (var realization in realizations) {
+        for (int i = 0; i < edges.Length; i++) {
+          Permutation tempPermutation;
+          if (i < 6) {
+            tempPermutation = permutation;
+          } else {
+            tempPermutation = afterMove;
+          }
+          if (realization[edges[i]]) {
+            aPosteriori[i] = edges[i];
+          } else {
+            int j = 1;
+            if (i % 2 == 0) {
+              // find nearest predecessor in realization if source edge
+              while (!realization[tempPermutation.GetCircular(indices[i] - j)]) {
+                j++;
+              }
+              aPosteriori[i] = tempPermutation.GetCircular(indices[i] - j);
+            } else {
+              // find nearest successor in realization if target edge
+              while (!realization[tempPermutation.GetCircular(indices[i] + j)]) {
                 j++;
               }
@@ -160,6 +222,6 @@
     }
 
-    private static int decreaseCircularIndex(int length, int index) {
-      int result = index - 1;
+    private static int DecreaseCircularIndex(int length, int index) {
+      var result = index - 1;
       if (result == -1) {
         result = length - 1;
@@ -168,6 +230,6 @@
     }
 
-    private static int increaseCircularIndex(int length, int index) {
-      int result = index + 1;
+    private static int IncreaseCircularIndex(int length, int index) {
+      var result = index + 1;
       if (result == length + 1) {
         result = 0;
@@ -176,14 +238,10 @@
     }
 
-    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
-      return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, this);
+    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, DistanceCalculator distanceCalculator) {
+      return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, distanceCalculator, RealizationsParameter.ActualValue);
     }
 
     protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
-      return EvaluateByDistanceMatrix(permutation, TranslocationMoveParameter.ActualValue, distanceMatrix, RealizationsParameter.Value);
-    }
-
-    protected override double CalculateDistance(double x1, double y1, double x2, double y2) {
-      return Math.Round(Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
+      return EvaluateByDistanceMatrix(permutation, TranslocationMoveParameter.ActualValue, distanceMatrix, RealizationsParameter.ActualValue);
     }
   }
