Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/20/12 15:45:10 (13 years ago)
Author:
abeham
Message:

#1834

  • fixed the cases where the move quality would not change
  • added unit tests for translocation and inversion move evaluators
Location:
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs

    r7259 r7743  
    4646    }
    4747
    48     protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
    49       TranslocationMove move = TranslocationMoveParameter.ActualValue;
     48    public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSPTranslocationMovePathEvaluator evaluator) {
     49      if (move.Index1 == move.Index3
     50        || move.Index2 == permutation.Length - 1 && move.Index3 == 0
     51        || move.Index1 == 0 && move.Index3 == permutation.Length - 1 - move.Index2) return 0;
     52
    5053      int edge1source = permutation.GetCircular(move.Index1 - 1);
    5154      int edge1target = permutation[move.Index1];
     
    6063        edge3target = permutation[move.Index3];
    6164      }
    62       if (move.Index1 == move.Index3
    63         || move.Index2 - move.Index1 >= permutation.Length - 2
    64         || move.Index1 == permutation.Length - 1 && move.Index3 == 0
    65         || move.Index1 == 0 && move.Index3 == permutation.Length - 1) return 0;
    6665      double moveQuality = 0;
    6766      // remove three edges
    68       moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     67      moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
    6968        coordinates[edge1target, 0], coordinates[edge1target, 1]);
    70       moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
     69      moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
    7170        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    72       moveQuality -= CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
     71      moveQuality -= evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
    7372        coordinates[edge3target, 0], coordinates[edge3target, 1]);
    7473      // add three edges
    75       moveQuality += CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
     74      moveQuality += evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],
    7675        coordinates[edge1target, 0], coordinates[edge1target, 1]);
    77       moveQuality += CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
     76      moveQuality += evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
    7877        coordinates[edge3target, 0], coordinates[edge3target, 1]);
    79       moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     78      moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
    8079        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    8180      return moveQuality;
    8281    }
    8382
    84     protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
    85       TranslocationMove move = TranslocationMoveParameter.ActualValue;
     83    public static double EvaluateByDistanceMatrix(Permutation permutation, TranslocationMove move, DistanceMatrix distanceMatrix) {
     84      if (move.Index1 == move.Index3
     85        || move.Index2 == permutation.Length - 1 && move.Index3 == 0
     86        || move.Index1 == 0 && move.Index3 == permutation.Length - 1 - move.Index2) return 0;
     87
    8688      int edge1source = permutation.GetCircular(move.Index1 - 1);
    8789      int edge1target = permutation[move.Index1];
     
    9698        edge3target = permutation[move.Index3];
    9799      }
    98       if (move.Index1 == move.Index3
    99         || move.Index2 - move.Index1 >= permutation.Length - 2
    100         || move.Index1 == permutation.Length - 1 && move.Index3 == 0
    101         || move.Index1 == 0 && move.Index3 == permutation.Length - 1) return 0;
    102100      double moveQuality = 0;
    103101      // remove three edges
     
    111109      return moveQuality;
    112110    }
     111
     112    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
     113      return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, this);
     114    }
     115
     116    protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
     117      return EvaluateByDistanceMatrix(permutation, TranslocationMoveParameter.ActualValue, distanceMatrix);
     118    }
    113119  }
    114120}
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMovePathEvaluator.cs

    r7259 r7743  
    4646    }
    4747
    48     protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
    49       InversionMove move = InversionMoveParameter.ActualValue;
     48    public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPInversionMovePathEvaluator evaluator) {
    5049      int edge1source = permutation.GetCircular(move.Index1 - 1);
    5150      int edge1target = permutation[move.Index1];
     
    5554      double moveQuality = 0;
    5655      // remove two edges
    57       moveQuality -= CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     56      moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
    5857            coordinates[edge1target, 0], coordinates[edge1target, 1]);
    59       moveQuality -= CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
     58      moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],
    6059        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    6160      // add two edges
    62       moveQuality += CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
     61      moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],
    6362        coordinates[edge2source, 0], coordinates[edge2source, 1]);
    64       moveQuality += CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
     63      moveQuality += evaluator.CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],
    6564        coordinates[edge2target, 0], coordinates[edge2target, 1]);
    6665      return moveQuality;
    6766    }
    6867
    69     protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
    70       InversionMove move = InversionMoveParameter.ActualValue;
     68    public static double EvaluateByDistanceMatrix(Permutation permutation, InversionMove move, DistanceMatrix distanceMatrix) {
    7169      int edge1source = permutation.GetCircular(move.Index1 - 1);
    7270      int edge1target = permutation[move.Index1];
     
    8381      return moveQuality;
    8482    }
     83
     84    protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates) {
     85      return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, this);
     86    }
     87
     88    protected override double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix) {
     89      return EvaluateByDistanceMatrix(permutation, InversionMoveParameter.ActualValue, distanceMatrix);
     90    }
    8591  }
    8692}
Note: See TracChangeset for help on using the changeset viewer.