Changeset 14559 for branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/GeographicDistanceClusterCreator.cs
- Timestamp:
- 01/12/17 16:42:50 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/GeographicDistanceClusterCreator.cs
r14442 r14559 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 48 49 } 49 50 50 public static List<SpatialDistanceClusterElement> CreateClusterElements(IVRPProblemInstance instance) {51 IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;52 53 // add all customers54 List<int> customers = new List<int>();55 for (int i = 1; i <= instance.Cities.Value; i++) {56 if (pdp == null || pdp.GetDemand(i) >= 0)57 customers.Add(i);58 }59 60 // wrap stops in SpatialDistanceClusterElement61 List<SpatialDistanceClusterElement> clusterElements = new List<SpatialDistanceClusterElement>();62 foreach (int customer in customers) {63 clusterElements.Add(new SpatialDistanceClusterElement(customer, instance.GetCoordinates(customer)));64 }65 return clusterElements;66 }67 51 68 52 public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) { 69 53 PotvinEncoding result = new PotvinEncoding(instance); 70 54 71 // (1) wrap stops in cluster elements 72 List<SpatialDistanceClusterElement> clusterElements = CreateClusterElements(instance); 55 // (1) init data 56 var coordinates = new List<double[]>(instance.Cities.Value); 57 var pdp = instance as IPickupAndDeliveryProblemInstance; 58 for (int i = 1; i <= instance.Cities.Value; i++) { 59 if (pdp == null || pdp.GetDemand(i) >= 0) 60 coordinates.Add(instance.GetCoordinates(i)); 61 } 73 62 74 63 // (2) create a random number k of clusters 75 64 int k = random.Next(minK, maxK); 76 List<SpatialDistanceCluster> clusters = ClusterAlgorithm<SpatialDistanceCluster, SpatialDistanceClusterElement>77 .KMeans(random, clusterElements, k, clusterChangeThreshold);78 65 var kMeans = new KMeansAlgorithm<double[]>(CalculateMeanHelper, CalculateDistanceHelper); 66 var clusters = kMeans.Run(coordinates, k, clusterChangeThreshold, random); 67 79 68 // (3) build tours with a (a) shuffling (b) greedy tour creation routine 80 foreach (var c in clusters) {69 foreach (var cluster in clusters) { 81 70 Tour newTour = new Tour(); 82 71 result.Tours.Add(newTour); … … 84 73 if (creationOption == 0) { 85 74 // (a) shuffle 86 c.Elements.Shuffle(random); 87 foreach (var o in c.Elements) { 88 newTour.Stops.Add(o.Id); 75 cluster.ShuffleInPlace(random); 76 newTour.Stops.AddRange(cluster); 77 foreach (var customer in cluster) { 78 newTour.Stops.Add(customer + 1); 89 79 } 90 80 } else { 91 81 // (b) greedy 92 foreach (var o in c.Elements) {93 newTour.Stops.Add( o.Id);82 foreach (var customer in cluster) { 83 newTour.Stops.Add(customer + 1); 94 84 } 95 85 GreedyTourCreation(instance, result, newTour, false); … … 98 88 99 89 return result; 90 } 91 92 private static double[] CalculateMeanHelper(List<double[]> coordinates) { 93 var mean = new double[coordinates[0].Length]; 94 foreach (double[] coord in coordinates) { 95 for (int i = 0; i < mean.Length; i++) { 96 mean[i] += coord[i] / coordinates.Count; 97 } 98 } 99 return mean; 100 } 101 private static double CalculateDistanceHelper(double[] coord1, double[] coord2) { 102 double distance = 0.0; 103 for (int i = 0; i < coord1.Length; i++) { 104 distance += Math.Pow(coord1[i] - coord2[i], 2); 105 } 106 return Math.Sqrt(distance); 100 107 } 101 108
Note: See TracChangeset
for help on using the changeset viewer.