Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/16 15:46:43 (8 years ago)
Author:
jzenisek
Message:

#2707 updated generic type instantiation in cluster algorithm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/Cluster.cs

    r14442 r14447  
    77namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
    88
    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) {
    1215      HashSet<int> initMeans = new HashSet<int>();
    1316      int nextMean = -1;
    14 
    15       List<C> clusters = CreateCList();
     17      List<TCluster> clusters = CreateCList();
    1618
    1719      // (1) initialize each cluster with a random element as mean
    1820      for (int i = 0; i < k && i < clusterElements.Count; i++) {
    19         C cluster = (C)Activator.CreateInstance(typeof(C));
     21        TCluster cluster = new TCluster();
    2022        cluster.Id = i;
    2123
     
    3032      // (2) repeat clustering until change rate is below threshold
    3133      double changeRate = 0.0;
    32 
    3334      do {
    3435        int changes = KMeansRun(clusters, clusterElements);
     
    3940      // remove empty clusters
    4041      clusters.RemoveAll(c => c.Elements.Count.Equals(0));
    41 
    4242      return clusters;
    4343    }
    4444
    45     private static int KMeansRun(List<C> clusters, List<CO> clusterElements) {
     45    private static int KMeansRun(List<TCluster> clusters, List<TClusterElement> clusterElements) {
    4646      int changes = 0;
    4747
     
    6262          }
    6363        }
    64         if (clusters[optClusterIdx].AddObject(e)) {
     64        if (clusters[optClusterIdx].AddElement(e)) {
    6565          changes++;
    6666        }
     
    7676    }
    7777
    78     private static List<C> CreateCList() {
     78    private static List<TCluster> CreateCList() {
    7979      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);
    8282    }
    8383  }
     
    8686  public interface ICluster<T> where T : ClusterElement {
    8787    void SetMean(T o);
    88     bool AddObject(T o);
     88    bool AddElement(T o);
    8989    void CalculateMean();
    9090    void CalculateVariance();
    9191    double CalculateImpact(T e);
    9292    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() {
    9695    public int Id;
    97 
    9896    public List<T> Elements { get; private set; }
    99 
    10097    public T Mean { get; set; }
    101 
    10298    public double Variance { get; set; }
    103 
    10499    protected Cluster() {
    105100      Elements = new List<T>();
    106101    }
    107 
    108102    protected Cluster(int id) {
    109103      Id = id;
    110104      Elements = new List<T>();
    111 
    112     }
    113 
    114     public bool AddObject(T e) {
     105    }
     106    public bool AddElement(T e) {
    115107      Elements.Add(e);
    116108
     
    119111      return clusterChanged;
    120112    }
    121 
    122113    public void SetMean(T e) {
    123114      Mean = e;
    124115      Mean.ClusterId = Id;
    125116    }
    126 
    127117    public abstract void CalculateMean();
    128 
    129118    public abstract double CalculateDistance(T e1, T e2);
    130 
    131119    public virtual void CalculateVariance() {
    132120      if (Mean == null)
     
    139127      Variance /= Elements.Count;
    140128    }
    141 
    142129    public virtual double CalculateImpact(T e) {
    143130      if (Mean == null)
     
    147134      return newVariance - Variance;
    148135    }
    149 
    150136  }
    151137  public class SpatialDistanceCluster : Cluster<SpatialDistanceClusterElement> {
     
    155141    public SpatialDistanceCluster(int id) : base(id) {
    156142    }
    157 
    158143    public override void CalculateMean() {
    159144      int dimensions = (Mean != null) ? Mean.Coordinates.Length : (Elements.Count > 0) ? Elements[0].Coordinates.Length : 0;
     
    166151      Mean = mean;
    167152    }
    168 
    169153    public override double CalculateDistance(SpatialDistanceClusterElement e1, SpatialDistanceClusterElement e2) {
    170154      if (!e1.Coordinates.Length.Equals(e2.Coordinates.Length)) {
     
    176160        distance += Math.Pow(e1.Coordinates[i] - e2.Coordinates[i], 2);
    177161      }
    178 
    179162      return Math.Sqrt(distance);
    180163    }
    181164  }
    182 
    183165  public class TemporalDistanceCluster : Cluster<TemporalDistanceClusterElement> {
    184166    public TemporalDistanceCluster() : base() {
    185167    }
    186 
    187168    public TemporalDistanceCluster(int id) : base(id) {
    188169    }
    189 
    190170    public override void CalculateMean() {
    191171      TemporalDistanceClusterElement mean = new TemporalDistanceClusterElement(-1, Id);
     
    196176      Mean = mean;
    197177    }
    198 
    199178    public override double CalculateDistance(TemporalDistanceClusterElement e1, TemporalDistanceClusterElement e2) {
    200179      double distance = 0.0;
     
    206185    }
    207186  }
    208 
    209187  #endregion Cluster
    210188
     
    213191    public int Id { get; set; }
    214192    public int ClusterId { get; set; }
    215 
    216193    protected ClusterElement() {
    217194      ClusterId = -1;
    218195    }
    219 
    220196    public ClusterElement(int id, int clusterId = -1) {
    221197      Id = id;
     
    224200  }
    225201  public class SpatialDistanceClusterElement : ClusterElement {
    226 
    227202    public double[] Coordinates { get; set; }
    228 
    229203    public SpatialDistanceClusterElement() : base() {
    230204    }
    231 
    232205    public SpatialDistanceClusterElement(int id, int clusterId = -1) : base(id, clusterId) {
    233206    }
    234 
    235207    public SpatialDistanceClusterElement(int id, double[] coordinates, int clusterId = -1) : base(id, clusterId) {
    236208      Coordinates = coordinates;
    237209    }
    238 
    239210    public SpatialDistanceClusterElement(int id, int dimensions, int clusterId = -1) : base(id, clusterId) {
    240211      Coordinates = new double[dimensions];
    241212    }
    242 
    243   }
    244 
     213  }
    245214  public class TemporalDistanceClusterElement : ClusterElement {
    246215    public double ReadyTime { get; set; }
    247216    public double DueTime { get; set; }
    248 
    249217    public TemporalDistanceClusterElement() : base() {
    250218    }
    251 
    252219    public TemporalDistanceClusterElement(int id, int clusterId = -1) : base(id, clusterId) {
    253220    }
    254 
    255221    public TemporalDistanceClusterElement(int id, double readyTime, double dueTime, int clusterId = -1) : base(id, clusterId) {
    256222      ReadyTime = readyTime;
Note: See TracChangeset for help on using the changeset viewer.