Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/24/12 15:04:37 (12 years ago)
Author:
jkarder
Message:

#1331:

  • applied some of the changes suggested by ascheibe in comment:32:ticket:1331
  • restructured path relinking and improvement operators and similarity calculators
  • fixed bug in TSPMultipleGuidesPathRelinker
File:
1 edited

Legend:

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

    r8304 r8319  
    3030  /// An operator that performs similarity calculation between two traveling salesman solutions.
    3131  /// </summary>
     32  /// <remarks>
     33  /// The operator calculates the similarity based on the number of edges the two solutions have in common.
     34  /// </remarks>
    3235  [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions.")]
    33   public sealed class TSPSimilarityCalculator : SimilarityCalculator {
     36  public sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
    3437    private TSPSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3538    private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
     
    4346      if (left == null || right == null)
    4447        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
    45       if (left == right) return 1.0;
     48      if (left.PermutationType != right.PermutationType)
     49        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different types.");
     50      if (left.Length != right.Length)
     51        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
     52      var comparer = new PermutationEqualityComparer();
     53      if (object.ReferenceEquals(left, right) || comparer.Equals(left, right)) return 1.0;
    4654
     55      switch (left.PermutationType) {
     56        case PermutationTypes.Absolute:
     57          return CalculateAbsolute(left, right);
     58        case PermutationTypes.RelativeDirected:
     59          return CalculateRelativeDirected(left, right);
     60        case PermutationTypes.RelativeUndirected:
     61          return CalculateRelativeUndirected(left, right);
     62        default:
     63          throw new InvalidOperationException("unknown permutation type");
     64      }
     65    }
     66
     67    private static double CalculateAbsolute(Permutation left, Permutation right) {
     68      double similarity = 0.0;
     69      for (int i = 0; i < left.Length && left[i] == right[i]; similarity = ++i) ;
     70      return similarity / left.Length;
     71    }
     72
     73    private static double CalculateRelativeDirected(Permutation left, Permutation right) {
     74      throw new NotImplementedException();
     75    }
     76
     77    private static double CalculateRelativeUndirected(Permutation left, Permutation right) {
    4778      int[,] edges = new int[right.Length, 2];
    4879      for (int i = 0; i < right.Length; i++) {
     
    6192    }
    6293
    63     public override double CalculateIndividualSimilarity(IScope left, IScope right) {
    64       var sol1 = left.Variables[Target].Value as Permutation;
    65       var sol2 = right.Variables[Target].Value as Permutation;
     94    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
     95      var sol1 = leftSolution.Variables[SolutionVariableName].Value as Permutation;
     96      var sol2 = rightSolution.Variables[SolutionVariableName].Value as Permutation;
    6697
    6798      return CalculateSimilarity(sol1, sol2);
Note: See TracChangeset for help on using the changeset viewer.