Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/12 17:55:41 (13 years ago)
Author:
svonolfe
Message:

Added first working version of VRP path relinker (#1331)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.VehicleRouting/3.3/SimilarityCalculators/VRPSimilarityCalculator.cs

    r7793 r7815  
    2929using HeuristicLab.Problems.VehicleRouting.Encodings;
    3030using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
     31using HeuristicLab.Data;
     32using HeuristicLab.Parameters;
    3133
    3234namespace HeuristicLab.Problems.VehicleRouting {
     
    3739  [StorableClass]
    3840  public sealed class VRPSimilarityCalculator : SimilarityCalculator {
     41    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
     42      get {
     43        if (Parameters.ContainsKey("DistanceMatrix"))
     44          return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"];
     45        else
     46          return null;
     47      }
     48    }
     49
    3950    [StorableConstructor]
    4051    private VRPSimilarityCalculator(bool deserializing) : base(deserializing) { }
    4152    private VRPSimilarityCalculator(VRPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
    42     public VRPSimilarityCalculator() : base() { }
     53    public VRPSimilarityCalculator() : base() {
     54      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
     55    }
    4356
    4457    public override IDeepCloneable Clone(Cloner cloner) {
     
    4659    }
    4760
    48     protected override double CalculateSimilarity(IScope left, IScope right) {
    49       PotvinEncoding e1 = left.Variables[TargetParameter.ActualName].Value as PotvinEncoding;
    50       PotvinEncoding e2 = right.Variables[TargetParameter.ActualName].Value as PotvinEncoding;
    51 
     61    public static double CalculateSimilarity(PotvinEncoding e1, PotvinEncoding e2) {
    5262      // extract edges from first solution
    5363      var edges1 = new List<Tuple<int, int>>();
    5464      foreach (Tour tour in e1.Tours) {
    5565        edges1.Add(new Tuple<int, int>(0, tour.Cities[0]));
    56         edges1.Add(new Tuple<int, int>(0, tour.Cities[tour.Cities.Count - 1]));
    5766        for (int i = 0; i < tour.Cities.Count - 1; i++)
    5867          edges1.Add(new Tuple<int, int>(tour.Cities[i], tour.Cities[i + 1]));
     68        edges1.Add(new Tuple<int, int>(tour.Cities[tour.Cities.Count - 1], 0));
    5969      }
    6070
     
    6373      foreach (Tour tour in e2.Tours) {
    6474        edges2.Add(new Tuple<int, int>(0, tour.Cities[0]));
    65         edges2.Add(new Tuple<int, int>(0, tour.Cities[tour.Cities.Count - 1]));
    6675        for (int i = 0; i < tour.Cities.Count - 1; i++)
    6776          edges2.Add(new Tuple<int, int>(tour.Cities[i], tour.Cities[i + 1]));
     77        edges2.Add(new Tuple<int, int>(tour.Cities[tour.Cities.Count - 1], 0));
    6878      }
    6979
     
    7383      int identicalEdges = 0;
    7484      foreach (var edge in edges1) {
    75         if (edges2.Any(x => x.Equals(edge) || x.Item1 == edge.Item2 && x.Item2 == edge.Item1))
     85        if (edges2.Any(x => x.Equals(edge)))
    7686          identicalEdges++;
    7787      }
    7888
    79       return identicalEdges * 2.0 / edges1.Count + edges2.Count;
     89      return identicalEdges * 2.0 / (edges1.Count + edges2.Count);
     90    }
     91
     92    protected override double CalculateSimilarity(IScope left, IScope right) {
     93      IVRPEncoding e1 = left.Variables[TargetParameter.ActualName].Value as IVRPEncoding;
     94      if (!(e1 is PotvinEncoding))
     95        e1 = PotvinEncoding.ConvertFrom(e1 as IVRPEncoding, DistanceMatrixParameter);
     96
     97      IVRPEncoding e2 = right.Variables[TargetParameter.ActualName].Value as IVRPEncoding;
     98      if (!(e2 is PotvinEncoding))
     99        e2 = PotvinEncoding.ConvertFrom(e2 as IVRPEncoding, DistanceMatrixParameter);
     100
     101      return CalculateSimilarity(e1 as PotvinEncoding, e2 as PotvinEncoding);
    80102    }
    81103  }
Note: See TracChangeset for help on using the changeset viewer.