Changeset 14430 for branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/Cluster.cs
- Timestamp:
- 11/30/16 09:24:40 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/Cluster.cs
r14424 r14430 7 7 using HeuristicLab.Core; 8 8 using HeuristicLab.Problems.VehicleRouting.Interfaces; 9 using HeuristicLab.Problems.VehicleRouting.Variants; 9 10 10 11 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 42 43 public int Id; 43 44 protected readonly IVRPProblemInstance instance; 44 public List<T> Objects { get; private set; }45 public List<T> Elements { get; private set; } 45 46 46 47 public T Mean { get; set; } 48 49 public double Variance { get; set; } 47 50 48 51 public Cluster(IVRPProblemInstance instance, int id) { 49 52 this.instance = instance; 50 53 Id = id; 51 Objects = new List<T>();54 Elements = new List<T>(); 52 55 53 56 } 54 57 public bool AddObject(T o) { 55 Objects.Add(o);58 Elements.Add(o); 56 59 57 60 bool clusterChanged = o.ClusterId != Id; … … 67 70 public abstract void CalculateMean(); 68 71 72 public abstract void CalculateVariance(); 73 69 74 public abstract double CalculateImpact(T o); 70 75 … … 76 81 public override void CalculateMean() { 77 82 int dimensions = instance.Coordinates.Columns; 78 SpatialDistanceClusterElement mean = new SpatialDistanceClusterElement(-1, Id, dimensions);79 foreach (SpatialDistanceClusterElement o in Objects) {83 SpatialDistanceClusterElement mean = new SpatialDistanceClusterElement(-1, dimensions, Id); 84 foreach (SpatialDistanceClusterElement o in Elements) { 80 85 double[] coordinates = instance.GetCoordinates(o.Id); 81 86 for (int i = 0; i < coordinates.Length; i++) { 82 mean.Coordinates[i] += (coordinates[i] / Objects.Count);87 mean.Coordinates[i] += (coordinates[i] / Elements.Count); 83 88 } 84 89 } 85 90 Mean = mean; 91 } 92 93 public override void CalculateVariance() { 94 if(Mean == null) 95 CalculateMean(); 96 97 Variance = 0.0; 98 foreach (SpatialDistanceClusterElement o in Elements) { 99 Variance += Math.Pow(Mean.GetDistance(o), 2); 100 } 101 Variance /= Elements.Count; 86 102 } 87 103 … … 90 106 CalculateMean(); 91 107 92 return Math.Pow(Mean.GetDistance(o), 2) / ((Objects.Count > 0) ? Objects.Count : 1); 108 double newVariance = (Variance*Elements.Count + Math.Pow(Mean.GetDistance(o), 2))/(Elements.Count + 1); 109 return newVariance - Variance; 110 } 111 } 112 113 public class TemporalDistanceCluster : Cluster<TemporalDistanceClusterElement> { 114 public TemporalDistanceCluster(ITimeWindowedProblemInstance instance, int id) : base(instance, id) { 115 } 116 117 118 public override void CalculateMean() { 119 TemporalDistanceClusterElement mean = new TemporalDistanceClusterElement(-1, Id); 120 foreach (TemporalDistanceClusterElement e in Elements) { 121 mean.ReadyTime += e.ReadyTime/Elements.Count; 122 mean.DueTime += e.DueTime/Elements.Count; 123 } 124 Mean = mean; 125 } 126 127 public override void CalculateVariance() { 128 if (Mean == null) 129 CalculateMean(); 130 131 Variance = 0.0; 132 foreach (TemporalDistanceClusterElement e in Elements) { 133 Variance += Math.Pow(Math.Abs(Mean.ReadyTime - e.ReadyTime) + Math.Abs(Mean.DueTime - e.DueTime), 2); 134 } 135 Variance /= Elements.Count; 136 } 137 138 public override double CalculateImpact(TemporalDistanceClusterElement o) { 139 if(Mean == null) 140 CalculateMean(); 141 142 double newVariance = (Variance * Elements.Count + Math.Pow(Math.Abs(Mean.ReadyTime - o.ReadyTime) + Math.Abs(Mean.DueTime - o.DueTime), 2)) / (Elements.Count + 1); 143 return newVariance - Variance; 93 144 } 94 145 } … … 131 182 var other = o as SpatialDistanceClusterElement; 132 183 if (other == null) { 133 throw new Ar rayTypeMismatchException(string.Format("Distance calculation between {0} and {1} is not possible.",184 throw new ArgumentException(string.Format("Distance calculation between {0} and {1} is not possible.", 134 185 typeof(SpatialDistanceClusterElement), o.GetType())); 135 186 } … … 143 194 } 144 195 } 196 197 public class TemporalDistanceClusterElement : ClusterElement { 198 public double ReadyTime { get; set; } 199 public double DueTime { get; set; } 200 201 public TemporalDistanceClusterElement(int id, int clusterId = -1) : base(id, clusterId) { 202 } 203 204 public TemporalDistanceClusterElement(int id, double readyTime, double dueTime, int clusterId = -1) : base(id, clusterId) { 205 ReadyTime = readyTime; 206 DueTime = dueTime; 207 } 208 209 public override double GetDistance(IClusterElement o) { 210 var other = o as TemporalDistanceClusterElement; 211 if (other == null) { 212 throw new ArgumentException(string.Format("Distance calculation between {0} and {1} is not possible.", 213 typeof(TemporalDistanceClusterElement), o.GetType())); 214 } 215 216 double distance = 0.0; 217 distance += Math.Abs(ReadyTime - other.ReadyTime); 218 distance += Math.Abs(DueTime - other.DueTime); 219 220 return distance; 221 222 //return GetDistanceEuklid(other); 223 } 224 225 public double GetDistanceEuklid(TemporalDistanceClusterElement other) { 226 double distance = 0.0; 227 distance += Math.Pow(ReadyTime - other.ReadyTime, 2); 228 distance += Math.Pow(DueTime - other.DueTime, 2); 229 230 return Math.Sqrt(distance); 231 } 232 233 } 234 145 235 #endregion ClusterElement 146 236 }
Note: See TracChangeset
for help on using the changeset viewer.