Changeset 8319 for branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators
- Timestamp:
- 07/24/12 15:04:37 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs
r8304 r8319 30 30 /// An operator that performs similarity calculation between two traveling salesman solutions. 31 31 /// </summary> 32 /// <remarks> 33 /// The operator calculates the similarity based on the number of edges the two solutions have in common. 34 /// </remarks> 32 35 [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions.")] 33 public sealed class TSPSimilarityCalculator : Si milarityCalculator {36 public sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator { 34 37 private TSPSimilarityCalculator(bool deserializing) : base(deserializing) { } 35 38 private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { } … … 43 46 if (left == null || right == null) 44 47 throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null."); 45 if (left == right) return 1.0; 48 if (left.PermutationType != right.PermutationType) 49 throw new ArgumentException("Cannot calculate similarity because the provided solutions have different types."); 50 if (left.Length != right.Length) 51 throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths."); 52 var comparer = new PermutationEqualityComparer(); 53 if (object.ReferenceEquals(left, right) || comparer.Equals(left, right)) return 1.0; 46 54 55 switch (left.PermutationType) { 56 case PermutationTypes.Absolute: 57 return CalculateAbsolute(left, right); 58 case PermutationTypes.RelativeDirected: 59 return CalculateRelativeDirected(left, right); 60 case PermutationTypes.RelativeUndirected: 61 return CalculateRelativeUndirected(left, right); 62 default: 63 throw new InvalidOperationException("unknown permutation type"); 64 } 65 } 66 67 private static double CalculateAbsolute(Permutation left, Permutation right) { 68 double similarity = 0.0; 69 for (int i = 0; i < left.Length && left[i] == right[i]; similarity = ++i) ; 70 return similarity / left.Length; 71 } 72 73 private static double CalculateRelativeDirected(Permutation left, Permutation right) { 74 throw new NotImplementedException(); 75 } 76 77 private static double CalculateRelativeUndirected(Permutation left, Permutation right) { 47 78 int[,] edges = new int[right.Length, 2]; 48 79 for (int i = 0; i < right.Length; i++) { … … 61 92 } 62 93 63 public override double Calculate IndividualSimilarity(IScope left, IScope right) {64 var sol1 = left .Variables[Target].Value as Permutation;65 var sol2 = right .Variables[Target].Value as Permutation;94 public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) { 95 var sol1 = leftSolution.Variables[SolutionVariableName].Value as Permutation; 96 var sol2 = rightSolution.Variables[SolutionVariableName].Value as Permutation; 66 97 67 98 return CalculateSimilarity(sol1, sol2);
Note: See TracChangeset
for help on using the changeset viewer.