Changeset 8086 for branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs
- Timestamp:
- 06/22/12 11:11:38 (12 years ago)
- Location:
- branches/ScatterSearch (trunk integration)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)
- Property svn:ignore
-
old new 20 20 bin 21 21 protoc.exe 22 _ReSharper.HeuristicLab 3.3 Tests
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs
r7793 r8086 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Encodings.PermutationEncoding; 25 26 using HeuristicLab.Optimization.Operators; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;27 27 28 28 namespace HeuristicLab.Problems.TravelingSalesman { … … 31 31 /// </summary> 32 32 [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions.")] 33 [StorableClass]34 33 public sealed class TSPSimilarityCalculator : SimilarityCalculator { 35 [StorableConstructor]36 34 private TSPSimilarityCalculator(bool deserializing) : base(deserializing) { } 37 35 private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { } 38 36 public TSPSimilarityCalculator() : base() { } 39 37 40 public override IDeepCloneable Clone(Cloner cloner) { 41 return new TSPSimilarityCalculator(this, cloner); 38 public static double CalculateSimilarity(Permutation left, Permutation right) { 39 if (left == null || right == null) 40 throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null."); 41 42 int[,] edges = new int[right.Length, 2]; 43 for (int i = 0; i < right.Length; i++) { 44 edges[right[i], 0] = right[(i + 1) % right.Length]; 45 edges[right[i], 1] = right[(i - 1 + right.Length) % right.Length]; 46 } 47 48 double similarity = 0.0; 49 for (int i = 0; i < left.Length; i++) { 50 int targetCity = left[(i + 1) % left.Length]; 51 if (targetCity == edges[left[i], 0] || targetCity == edges[left[i], 1]) 52 similarity++; 53 } 54 55 return similarity / left.Length; 42 56 } 43 57 44 58 protected override double CalculateSimilarity(IScope left, IScope right) { 45 Permutation v1 = left.Variables[TargetParameter.ActualName].Value as Permutation;46 Permutation v2 = right.Variables[TargetParameter.ActualName].Value as Permutation;59 Permutation sol1 = left.Variables[Target].Value as Permutation; 60 Permutation sol2 = right.Variables[Target].Value as Permutation; 47 61 48 int[,] edges = new int[v2.Length, 2]; 49 for (int i = 0; i < v2.Length; i++) { 50 edges[v2[i], 0] = v2[(i + 1) % v2.Length]; 51 edges[v2[i], 1] = v2[(i - 1 + v2.Length) % v2.Length]; 52 } 53 54 double similarity = 0.0; 55 for (int i = 0; i < v1.Length; i++) { 56 int targetCity = v1[(i + 1) % v1.Length]; 57 if (targetCity == edges[v1[i], 0] || targetCity == edges[v1[i], 1]) 58 similarity++; 59 } 60 61 return similarity / v1.Length; 62 return CalculateSimilarity(sol1, sol2); 62 63 } 63 64 }
Note: See TracChangeset
for help on using the changeset viewer.