Changeset 7815 for branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.VehicleRouting/3.3/SimilarityCalculators
- Timestamp:
- 05/14/12 17:55:41 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.VehicleRouting/3.3/SimilarityCalculators/VRPSimilarityCalculator.cs
r7793 r7815 29 29 using HeuristicLab.Problems.VehicleRouting.Encodings; 30 30 using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin; 31 using HeuristicLab.Data; 32 using HeuristicLab.Parameters; 31 33 32 34 namespace HeuristicLab.Problems.VehicleRouting { … … 37 39 [StorableClass] 38 40 public sealed class VRPSimilarityCalculator : SimilarityCalculator { 41 public ILookupParameter<DoubleMatrix> DistanceMatrixParameter { 42 get { 43 if (Parameters.ContainsKey("DistanceMatrix")) 44 return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; 45 else 46 return null; 47 } 48 } 49 39 50 [StorableConstructor] 40 51 private VRPSimilarityCalculator(bool deserializing) : base(deserializing) { } 41 52 private VRPSimilarityCalculator(VRPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { } 42 public VRPSimilarityCalculator() : base() { } 53 public VRPSimilarityCalculator() : base() { 54 Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 55 } 43 56 44 57 public override IDeepCloneable Clone(Cloner cloner) { … … 46 59 } 47 60 48 protected override double CalculateSimilarity(IScope left, IScope right) { 49 PotvinEncoding e1 = left.Variables[TargetParameter.ActualName].Value as PotvinEncoding; 50 PotvinEncoding e2 = right.Variables[TargetParameter.ActualName].Value as PotvinEncoding; 51 61 public static double CalculateSimilarity(PotvinEncoding e1, PotvinEncoding e2) { 52 62 // extract edges from first solution 53 63 var edges1 = new List<Tuple<int, int>>(); 54 64 foreach (Tour tour in e1.Tours) { 55 65 edges1.Add(new Tuple<int, int>(0, tour.Cities[0])); 56 edges1.Add(new Tuple<int, int>(0, tour.Cities[tour.Cities.Count - 1]));57 66 for (int i = 0; i < tour.Cities.Count - 1; i++) 58 67 edges1.Add(new Tuple<int, int>(tour.Cities[i], tour.Cities[i + 1])); 68 edges1.Add(new Tuple<int, int>(tour.Cities[tour.Cities.Count - 1], 0)); 59 69 } 60 70 … … 63 73 foreach (Tour tour in e2.Tours) { 64 74 edges2.Add(new Tuple<int, int>(0, tour.Cities[0])); 65 edges2.Add(new Tuple<int, int>(0, tour.Cities[tour.Cities.Count - 1]));66 75 for (int i = 0; i < tour.Cities.Count - 1; i++) 67 76 edges2.Add(new Tuple<int, int>(tour.Cities[i], tour.Cities[i + 1])); 77 edges2.Add(new Tuple<int, int>(tour.Cities[tour.Cities.Count - 1], 0)); 68 78 } 69 79 … … 73 83 int identicalEdges = 0; 74 84 foreach (var edge in edges1) { 75 if (edges2.Any(x => x.Equals(edge) || x.Item1 == edge.Item2 && x.Item2 == edge.Item1))85 if (edges2.Any(x => x.Equals(edge))) 76 86 identicalEdges++; 77 87 } 78 88 79 return identicalEdges * 2.0 / edges1.Count + edges2.Count; 89 return identicalEdges * 2.0 / (edges1.Count + edges2.Count); 90 } 91 92 protected override double CalculateSimilarity(IScope left, IScope right) { 93 IVRPEncoding e1 = left.Variables[TargetParameter.ActualName].Value as IVRPEncoding; 94 if (!(e1 is PotvinEncoding)) 95 e1 = PotvinEncoding.ConvertFrom(e1 as IVRPEncoding, DistanceMatrixParameter); 96 97 IVRPEncoding e2 = right.Variables[TargetParameter.ActualName].Value as IVRPEncoding; 98 if (!(e2 is PotvinEncoding)) 99 e2 = PotvinEncoding.ConvertFrom(e2 as IVRPEncoding, DistanceMatrixParameter); 100 101 return CalculateSimilarity(e1 as PotvinEncoding, e2 as PotvinEncoding); 80 102 } 81 103 }
Note: See TracChangeset
for help on using the changeset viewer.