Changeset 14430 for branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/TemporalDistanceClusterCreator.cs
- Timestamp:
- 11/30/16 09:24:40 (7 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/TemporalDistanceClusterCreator.cs
r14424 r14430 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 30 31 31 32 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { 32 [Item(" GeographicDistanceClusterCreator", "Creates a VRP solution by clustering customers first with a KMeans-algorithm based on their geographic position and building tours afterwards alternatevly in a random or a greedy fashion.")]33 [Item("TemporalDistanceClusterCreator", "Creates a VRP solution by clustering customers first with a KMeans-algorithm based on their geographic position and building tours afterwards alternatevly in a random or a greedy fashion.")] 33 34 [StorableClass] 34 public sealed class GeographicDistanceClusterCreator : ClusterCreator {35 public sealed class TemporalDistanceClusterCreator : ClusterCreator { 35 36 36 37 [StorableConstructor] 37 private GeographicDistanceClusterCreator(bool deserializing) : base(deserializing) { }38 private TemporalDistanceClusterCreator(bool deserializing) : base(deserializing) { } 38 39 39 public GeographicDistanceClusterCreator() : base() {40 public TemporalDistanceClusterCreator() : base() { 40 41 } 41 42 42 private GeographicDistanceClusterCreator(GeographicDistanceClusterCreator original, Cloner cloner)43 private TemporalDistanceClusterCreator(TemporalDistanceClusterCreator original, Cloner cloner) 43 44 : base(original, cloner) { 44 45 } 45 46 46 47 public override IDeepCloneable Clone(Cloner cloner) { 47 return new GeographicDistanceClusterCreator(this, cloner);48 return new TemporalDistanceClusterCreator(this, cloner); 48 49 } 49 50 50 public static List< SpatialDistanceClusterElement> CreateClusterObjects(IVRPProblemInstance instance) {51 public static List<TemporalDistanceClusterElement> CreateClusterObjects(ITimeWindowedProblemInstance instance) { 51 52 IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance; 52 53 … … 58 59 } 59 60 60 // wrap stops in SpatialDistanceClusterElement61 List< SpatialDistanceClusterElement> clusterObjects = new List<SpatialDistanceClusterElement>();61 // wrap stops in TemporalDistanceClusterElement 62 List<TemporalDistanceClusterElement> clusterObjects = new List<TemporalDistanceClusterElement>(); 62 63 foreach (int customer in customers) { 63 clusterObjects.Add(new SpatialDistanceClusterElement(customer, instance.GetCoordinates(customer)));64 clusterObjects.Add(new TemporalDistanceClusterElement(customer, instance.ReadyTime[customer], instance.DueTime[customer])); 64 65 } 65 66 return clusterObjects; 66 67 } 67 68 68 public static PotvinEncoding CreateSolution(I VRPProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) {69 public static PotvinEncoding CreateSolution(ITimeWindowedProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) { 69 70 PotvinEncoding result = new PotvinEncoding(instance); 70 71 71 List< SpatialDistanceClusterElement> clusterObjects = CreateClusterObjects(instance);72 List<TemporalDistanceClusterElement> clusterObjects = CreateClusterObjects(instance); 72 73 73 74 int k = random.Next(minK, maxK); 74 List< SpatialDistanceCluster> clusters = KMeans(instance, random, clusterObjects, k, clusterChangeThreshold);75 List<TemporalDistanceCluster> clusters = KMeans(instance, random, clusterObjects, k, clusterChangeThreshold); 75 76 76 77 // (3) build tours … … 83 84 if (creationOption == 0) { 84 85 // (a) shuffle 85 c. Objects.Shuffle(random);86 foreach (var o in c. Objects) {86 c.Elements.Shuffle(random); 87 foreach (var o in c.Elements) { 87 88 newTour.Stops.Add(o.Id); 88 89 } 89 90 } else { 90 91 // (b) greedy 91 foreach (var o in c. Objects) {92 foreach (var o in c.Elements) { 92 93 newTour.Stops.Add(o.Id); 93 94 } … … 99 100 } 100 101 101 private static List< SpatialDistanceCluster> KMeans(IVRPProblemInstance instance, IRandom random, List<SpatialDistanceClusterElement> objects, int k, double changeTreshold) {102 private static List<TemporalDistanceCluster> KMeans(ITimeWindowedProblemInstance instance, IRandom random, List<TemporalDistanceClusterElement> objects, int k, double changeTreshold) { 102 103 103 List< SpatialDistanceCluster> clusters = new List<SpatialDistanceCluster>();104 List<TemporalDistanceCluster> clusters = new List<TemporalDistanceCluster>(); 104 105 HashSet<int> initMeans = new HashSet<int>(); 105 106 int nextMean = -1; … … 107 108 // (1) initialize each cluster with a random first object (i.e. mean) 108 109 for (int i = 0; i < k && i < objects.Count; i++) { 109 SpatialDistanceCluster cluster = new SpatialDistanceCluster(instance, i);110 TemporalDistanceCluster cluster = new TemporalDistanceCluster(instance, i); 110 111 111 112 do { … … 128 129 129 130 // remove empty clusters 130 clusters.RemoveAll(c => c. Objects.Count.Equals(0));131 clusters.RemoveAll(c => c.Elements.Count.Equals(0)); 131 132 132 133 return clusters; 133 134 } 134 135 135 private static int KMeansRun(List< SpatialDistanceCluster> clusters, List<SpatialDistanceClusterElement> objects) {136 private static int KMeansRun(List<TemporalDistanceCluster> clusters, List<TemporalDistanceClusterElement> objects) { 136 137 int changes = 0; 137 138 138 139 foreach (var c in clusters) { 139 c. Objects.Clear();140 c.Elements.Clear(); 140 141 } 141 142 142 143 foreach (var o in objects) { 143 int bestClusterIdx = -1;144 double minImpact = double.MaxValue;145 for (int i = 0; i < clusters.Count; i++) {144 int bestClusterIdx = 0; 145 double minImpact = clusters[0].CalculateImpact(o); 146 for (int i = 1; i < clusters.Count; i++) { 146 147 double impact = clusters[i].CalculateImpact(o); 147 if (impact < minImpact) { 148 if ((minImpact > 0 && impact < minImpact) || (minImpact < 0 && impact > minImpact)) { // V1: min positive impact 149 //if(impact > minImpact) { // V2: max impact 148 150 minImpact = impact; 149 151 bestClusterIdx = i; … … 156 158 foreach (var c in clusters) { 157 159 c.CalculateMean(); 160 c.CalculateVariance(); 158 161 } 159 162 … … 181 184 int creationOption = creationOptions.SampleProportional(random, 1, probabilites, false, false).First(); 182 185 183 VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, random, minK, maxK, clusterChangeThreshold, creationOption); 186 var instance = ProblemInstance as ITimeWindowedProblemInstance; 187 if (instance == null) { 188 throw new ArgumentException(string.Format("Cannot initialize {0} with data from {1}", instance.GetType(), ProblemInstance.GetType())); 189 } 190 191 VRPToursParameter.ActualValue = CreateSolution(instance, random, minK, maxK, clusterChangeThreshold, creationOption); 184 192 return base.InstrumentedApply(); 185 193 }
Note: See TracChangeset
for help on using the changeset viewer.