Changeset 14447 for branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings
- Timestamp:
- 12/02/16 15:46:43 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/Cluster.cs
r14442 r14447 7 7 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { 8 8 9 public class ClusterAlgorithm<C,CO> where C : Cluster<CO> where CO : ClusterElement { 10 11 public static List<C> KMeans(IRandom random, List<CO> clusterElements, int k, double changeThreshold) { 9 public class ClusterAlgorithm<TCluster,TClusterElement> 10 where TCluster : Cluster<TClusterElement>, new() 11 where TClusterElement : ClusterElement, new() { 12 13 public static List<TCluster> KMeans(IRandom random, List<TClusterElement> clusterElements, 14 int k, double changeThreshold) { 12 15 HashSet<int> initMeans = new HashSet<int>(); 13 16 int nextMean = -1; 14 15 List<C> clusters = CreateCList(); 17 List<TCluster> clusters = CreateCList(); 16 18 17 19 // (1) initialize each cluster with a random element as mean 18 20 for (int i = 0; i < k && i < clusterElements.Count; i++) { 19 C cluster = (C)Activator.CreateInstance(typeof(C));21 TCluster cluster = new TCluster(); 20 22 cluster.Id = i; 21 23 … … 30 32 // (2) repeat clustering until change rate is below threshold 31 33 double changeRate = 0.0; 32 33 34 do { 34 35 int changes = KMeansRun(clusters, clusterElements); … … 39 40 // remove empty clusters 40 41 clusters.RemoveAll(c => c.Elements.Count.Equals(0)); 41 42 42 return clusters; 43 43 } 44 44 45 private static int KMeansRun(List< C> clusters, List<CO> clusterElements) {45 private static int KMeansRun(List<TCluster> clusters, List<TClusterElement> clusterElements) { 46 46 int changes = 0; 47 47 … … 62 62 } 63 63 } 64 if (clusters[optClusterIdx].Add Object(e)) {64 if (clusters[optClusterIdx].AddElement(e)) { 65 65 changes++; 66 66 } … … 76 76 } 77 77 78 private static List< C> CreateCList() {78 private static List<TCluster> CreateCList() { 79 79 var listType = typeof(List<>); 80 var constructedListType = listType.MakeGenericType(typeof( C));81 return (List< C>)Activator.CreateInstance(constructedListType);80 var constructedListType = listType.MakeGenericType(typeof(TCluster)); 81 return (List<TCluster>)Activator.CreateInstance(constructedListType); 82 82 } 83 83 } … … 86 86 public interface ICluster<T> where T : ClusterElement { 87 87 void SetMean(T o); 88 bool Add Object(T o);88 bool AddElement(T o); 89 89 void CalculateMean(); 90 90 void CalculateVariance(); 91 91 double CalculateImpact(T e); 92 92 double CalculateDistance(T e1, T e2); 93 94 } 95 public abstract class Cluster<T> : ICluster<T> where T : ClusterElement { 93 } 94 public abstract class Cluster<T> : ICluster<T> where T : ClusterElement, new() { 96 95 public int Id; 97 98 96 public List<T> Elements { get; private set; } 99 100 97 public T Mean { get; set; } 101 102 98 public double Variance { get; set; } 103 104 99 protected Cluster() { 105 100 Elements = new List<T>(); 106 101 } 107 108 102 protected Cluster(int id) { 109 103 Id = id; 110 104 Elements = new List<T>(); 111 112 } 113 114 public bool AddObject(T e) { 105 } 106 public bool AddElement(T e) { 115 107 Elements.Add(e); 116 108 … … 119 111 return clusterChanged; 120 112 } 121 122 113 public void SetMean(T e) { 123 114 Mean = e; 124 115 Mean.ClusterId = Id; 125 116 } 126 127 117 public abstract void CalculateMean(); 128 129 118 public abstract double CalculateDistance(T e1, T e2); 130 131 119 public virtual void CalculateVariance() { 132 120 if (Mean == null) … … 139 127 Variance /= Elements.Count; 140 128 } 141 142 129 public virtual double CalculateImpact(T e) { 143 130 if (Mean == null) … … 147 134 return newVariance - Variance; 148 135 } 149 150 136 } 151 137 public class SpatialDistanceCluster : Cluster<SpatialDistanceClusterElement> { … … 155 141 public SpatialDistanceCluster(int id) : base(id) { 156 142 } 157 158 143 public override void CalculateMean() { 159 144 int dimensions = (Mean != null) ? Mean.Coordinates.Length : (Elements.Count > 0) ? Elements[0].Coordinates.Length : 0; … … 166 151 Mean = mean; 167 152 } 168 169 153 public override double CalculateDistance(SpatialDistanceClusterElement e1, SpatialDistanceClusterElement e2) { 170 154 if (!e1.Coordinates.Length.Equals(e2.Coordinates.Length)) { … … 176 160 distance += Math.Pow(e1.Coordinates[i] - e2.Coordinates[i], 2); 177 161 } 178 179 162 return Math.Sqrt(distance); 180 163 } 181 164 } 182 183 165 public class TemporalDistanceCluster : Cluster<TemporalDistanceClusterElement> { 184 166 public TemporalDistanceCluster() : base() { 185 167 } 186 187 168 public TemporalDistanceCluster(int id) : base(id) { 188 169 } 189 190 170 public override void CalculateMean() { 191 171 TemporalDistanceClusterElement mean = new TemporalDistanceClusterElement(-1, Id); … … 196 176 Mean = mean; 197 177 } 198 199 178 public override double CalculateDistance(TemporalDistanceClusterElement e1, TemporalDistanceClusterElement e2) { 200 179 double distance = 0.0; … … 206 185 } 207 186 } 208 209 187 #endregion Cluster 210 188 … … 213 191 public int Id { get; set; } 214 192 public int ClusterId { get; set; } 215 216 193 protected ClusterElement() { 217 194 ClusterId = -1; 218 195 } 219 220 196 public ClusterElement(int id, int clusterId = -1) { 221 197 Id = id; … … 224 200 } 225 201 public class SpatialDistanceClusterElement : ClusterElement { 226 227 202 public double[] Coordinates { get; set; } 228 229 203 public SpatialDistanceClusterElement() : base() { 230 204 } 231 232 205 public SpatialDistanceClusterElement(int id, int clusterId = -1) : base(id, clusterId) { 233 206 } 234 235 207 public SpatialDistanceClusterElement(int id, double[] coordinates, int clusterId = -1) : base(id, clusterId) { 236 208 Coordinates = coordinates; 237 209 } 238 239 210 public SpatialDistanceClusterElement(int id, int dimensions, int clusterId = -1) : base(id, clusterId) { 240 211 Coordinates = new double[dimensions]; 241 212 } 242 243 } 244 213 } 245 214 public class TemporalDistanceClusterElement : ClusterElement { 246 215 public double ReadyTime { get; set; } 247 216 public double DueTime { get; set; } 248 249 217 public TemporalDistanceClusterElement() : base() { 250 218 } 251 252 219 public TemporalDistanceClusterElement(int id, int clusterId = -1) : base(id, clusterId) { 253 220 } 254 255 221 public TemporalDistanceClusterElement(int id, double readyTime, double dueTime, int clusterId = -1) : base(id, clusterId) { 256 222 ReadyTime = readyTime;
Note: See TracChangeset
for help on using the changeset viewer.