Changeset 14454 for branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators
- Timestamp:
- 12/05/16 16:06:18 (8 years ago)
- Location:
- branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators
- Files:
-
- 8 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPMoveEvaluator.cs
r14185 r14454 36 36 [StorableClass] 37 37 public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator, IMoveOperator { 38 39 public abstract Type EvaluatorType { get; } 38 40 39 public override bool CanChangeName { 41 40 get { return false; } -
branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPPathMoveEvaluator.cs
r14185 r14454 47 47 get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; } 48 48 } 49 public ILookupParameter<EnumValue<TSPDistanceFunction>> DistanceFunctionParameter { 50 get { return (ILookupParameter<EnumValue<TSPDistanceFunction>>)Parameters["DistanceFunction"]; } 51 } 49 52 50 53 [StorableConstructor] … … 57 60 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 58 61 Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated (if it does not exist already) and used for evaluation, otherwise false.")); 59 } 60 61 [StorableHook(HookType.AfterDeserialization)] 62 private void AfterDeserialization() { 63 // BackwardsCompatibility3.3 64 #region Backwards compatible code (remove with 3.4) 65 LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["DistanceMatrix"] as LookupParameter<DoubleMatrix>; 66 if (oldDistanceMatrixParameter != null) { 67 Parameters.Remove(oldDistanceMatrixParameter); 68 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 69 DistanceMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName; 70 } 71 #endregion 62 Parameters.Add(new LookupParameter<EnumValue<TSPDistanceFunction>>("DistanceFunction", "The distance function to use when the distance matrix is not being used.")); 72 63 } 73 64 74 65 public override IOperation Apply() { 75 Permutation permutation = PermutationParameter.ActualValue; 76 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 66 var permutation = PermutationParameter.ActualValue; 67 var coordinates = CoordinatesParameter.ActualValue; 68 var distanceFunction = DistanceFunctionParameter.ActualValue.Value; 77 69 double relativeQualityDifference = 0; 78 70 if (UseDistanceMatrixParameter.ActualValue.Value) { 79 71 DistanceMatrix distanceMatrix = DistanceMatrixParameter.ActualValue; 80 72 if (distanceMatrix == null) { 81 if (coordinates == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given."); 82 distanceMatrix = CalculateDistanceMatrix(coordinates); 83 DistanceMatrixParameter.ActualValue = distanceMatrix; 73 throw new InvalidOperationException("Distance matrix is not given."); 84 74 } 85 75 relativeQualityDifference = EvaluateByDistanceMatrix(permutation, distanceMatrix); 86 76 } else { 87 77 if (coordinates == null) throw new InvalidOperationException("No coordinates were given."); 88 relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates );78 relativeQualityDifference = EvaluateByCoordinates(permutation, coordinates, distanceFunction); 89 79 } 90 80 DoubleValue moveQuality = MoveQualityParameter.ActualValue; … … 93 83 return base.Apply(); 94 84 } 95 96 protected abstract double CalculateDistance(double x1, double y1, double x2, double y2);97 85 protected abstract double EvaluateByDistanceMatrix(Permutation permutation, DistanceMatrix distanceMatrix); 98 protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates); 99 100 private DistanceMatrix CalculateDistanceMatrix(DoubleMatrix c) { 101 DistanceMatrix distanceMatrix = new DistanceMatrix(c.Rows, c.Rows); 102 for (int i = 0; i < distanceMatrix.Rows; i++) { 103 for (int j = 0; j < distanceMatrix.Columns; j++) 104 distanceMatrix[i, j] = CalculateDistance(c[i, 0], c[i, 1], c[j, 0], c[j, 1]); 105 } 106 return (DistanceMatrix)distanceMatrix.AsReadOnly(); 107 } 86 protected abstract double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction); 108 87 } 109 88 } -
branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/ThreeOpt/TSPTranslocationMovePathEvaluator.cs
r14185 r14454 46 46 } 47 47 48 public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSP TranslocationMovePathEvaluator evaluator) {48 public static double EvaluateByCoordinates(Permutation permutation, TranslocationMove move, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) { 49 49 if (move.Index1 == move.Index3 50 50 || move.Index2 == permutation.Length - 1 && move.Index3 == 0 … … 65 65 double moveQuality = 0; 66 66 // remove three edges 67 moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],67 moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1], 68 68 coordinates[edge1target, 0], coordinates[edge1target, 1]); 69 moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],69 moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1], 70 70 coordinates[edge2target, 0], coordinates[edge2target, 1]); 71 moveQuality -= evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],71 moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge3source, 0], coordinates[edge3source, 1], 72 72 coordinates[edge3target, 0], coordinates[edge3target, 1]); 73 73 // add three edges 74 moveQuality += evaluator.CalculateDistance(coordinates[edge3source, 0], coordinates[edge3source, 1],74 moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge3source, 0], coordinates[edge3source, 1], 75 75 coordinates[edge1target, 0], coordinates[edge1target, 1]); 76 moveQuality += evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],76 moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1], 77 77 coordinates[edge3target, 0], coordinates[edge3target, 1]); 78 moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],78 moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1], 79 79 coordinates[edge2target, 0], coordinates[edge2target, 1]); 80 80 return moveQuality; … … 110 110 } 111 111 112 protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates ) {113 return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, this);112 protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) { 113 return EvaluateByCoordinates(permutation, TranslocationMoveParameter.ActualValue, coordinates, distanceFunction); 114 114 } 115 115 -
branches/MemPRAlgorithm/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TwoOpt/TSPInversionMovePathEvaluator.cs
r14185 r14454 33 33 [Item("TSPInversionMovePathEvaluator", "Evaluates an inversion move (2-opt) by summing up the length of all added edges and subtracting the length of all deleted edges.")] 34 34 [StorableClass] 35 public abstractclass TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator {35 public class TSPInversionMovePathEvaluator : TSPPathMoveEvaluator, IPermutationInversionMoveOperator { 36 36 public ILookupParameter<InversionMove> InversionMoveParameter { 37 37 get { return (ILookupParameter<InversionMove>)Parameters["InversionMove"]; } … … 46 46 } 47 47 48 public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPInversionMovePathEvaluator evaluator) { 48 public override IDeepCloneable Clone(Cloner cloner) { 49 return new TSPInversionMovePathEvaluator(this, cloner); 50 } 51 52 public static double EvaluateByCoordinates(Permutation permutation, InversionMove move, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) { 49 53 int edge1source = permutation.GetCircular(move.Index1 - 1); 50 54 int edge1target = permutation[move.Index1]; … … 54 58 double moveQuality = 0; 55 59 // remove two edges 56 moveQuality -= evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],60 moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1], 57 61 coordinates[edge1target, 0], coordinates[edge1target, 1]); 58 moveQuality -= evaluator.CalculateDistance(coordinates[edge2source, 0], coordinates[edge2source, 1],62 moveQuality -= TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge2source, 0], coordinates[edge2source, 1], 59 63 coordinates[edge2target, 0], coordinates[edge2target, 1]); 60 64 // add two edges 61 moveQuality += evaluator.CalculateDistance(coordinates[edge1source, 0], coordinates[edge1source, 1],65 moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1source, 0], coordinates[edge1source, 1], 62 66 coordinates[edge2source, 0], coordinates[edge2source, 1]); 63 moveQuality += evaluator.CalculateDistance(coordinates[edge1target, 0], coordinates[edge1target, 1],67 moveQuality += TravelingSalesmanProblem.CalculateDistance(distanceFunction, coordinates[edge1target, 0], coordinates[edge1target, 1], 64 68 coordinates[edge2target, 0], coordinates[edge2target, 1]); 65 69 return moveQuality; … … 82 86 } 83 87 84 protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates ) {85 return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, this);88 protected override double EvaluateByCoordinates(Permutation permutation, DoubleMatrix coordinates, TSPDistanceFunction distanceFunction) { 89 return EvaluateByCoordinates(permutation, InversionMoveParameter.ActualValue, coordinates, distanceFunction); 86 90 } 87 91
Note: See TracChangeset
for help on using the changeset viewer.