Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/30/16 09:24:40 (7 years ago)
Author:
jzenisek
Message:

#2707 added temporal distance clustering

File:
1 edited

Legend:

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

    r14424 r14430  
    77using HeuristicLab.Core;
    88using HeuristicLab.Problems.VehicleRouting.Interfaces;
     9using HeuristicLab.Problems.VehicleRouting.Variants;
    910
    1011namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
     
    4243    public int Id;
    4344    protected readonly IVRPProblemInstance instance;
    44     public List<T> Objects { get; private set; }
     45    public List<T> Elements { get; private set; }
    4546
    4647    public T Mean { get; set; }
     48
     49    public double Variance { get; set; }
    4750
    4851    public Cluster(IVRPProblemInstance instance, int id) {
    4952      this.instance = instance;
    5053      Id = id;
    51       Objects = new List<T>();
     54      Elements = new List<T>();
    5255
    5356    }
    5457    public bool AddObject(T o) {
    55       Objects.Add(o);
     58      Elements.Add(o);
    5659
    5760      bool clusterChanged = o.ClusterId != Id;
     
    6770    public abstract void CalculateMean();
    6871
     72    public abstract void CalculateVariance();
     73
    6974    public abstract double CalculateImpact(T o);
    7075
     
    7681    public override void CalculateMean() {
    7782      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) {
    8085        double[] coordinates = instance.GetCoordinates(o.Id);
    8186        for (int i = 0; i < coordinates.Length; i++) {
    82           mean.Coordinates[i] += (coordinates[i] / Objects.Count);
     87          mean.Coordinates[i] += (coordinates[i] / Elements.Count);
    8388        }
    8489      }
    8590      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;
    86102    }
    87103
     
    90106        CalculateMean();
    91107
    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;
    93144    }
    94145  }
     
    131182      var other = o as SpatialDistanceClusterElement;
    132183      if (other == null) {
    133         throw new ArrayTypeMismatchException(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.",
    134185          typeof(SpatialDistanceClusterElement), o.GetType()));
    135186      }
     
    143194    }
    144195  }
     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
    145235  #endregion ClusterElement
    146236}
Note: See TracChangeset for help on using the changeset viewer.