Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs

Last change on this file was 17434, checked in by bburlacu, 5 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

File size: 5.2 KB
RevLine 
[7789]1#region License Information
2/* HeuristicLab
[17434]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7789]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[8086]22using System;
[17434]23using HEAL.Attic;
[7789]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization.Operators;
[15351]28using HeuristicLab.PluginInfrastructure;
[7789]29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
32  /// An operator that performs similarity calculation between two traveling salesman solutions.
33  /// </summary>
[8319]34  /// <remarks>
35  /// The operator calculates the similarity based on the number of edges the two solutions have in common.
36  /// </remarks>
[8327]37  [Item("TSPSimilarityCalculator", "An operator that performs similarity calculation between two traveling salesman solutions. The operator calculates the similarity based on the number of edges the two solutions have in common.")]
[15351]38  [Obsolete("This operator is similar to the Hamming similarity calculator in the Encodings.PermutationEncoding namespace")]
39  [NonDiscoverableType]
[17434]40  [StorableType("0216BF57-995F-4F23-B422-F2C571314C5A")]
[15351]41  internal sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
[12155]42    protected override bool IsCommutative { get { return true; } }
43
[17434]44    [StorableConstructor]
45    private TSPSimilarityCalculator(StorableConstructorFlag _) : base(_) { }
[7789]46    private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
47    public TSPSimilarityCalculator() : base() { }
48
[8303]49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new TSPSimilarityCalculator(this, cloner);
51    }
52
[8086]53    public static double CalculateSimilarity(Permutation left, Permutation right) {
54      if (left == null || right == null)
55        throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
[8319]56      if (left.PermutationType != right.PermutationType)
57        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different types.");
58      if (left.Length != right.Length)
59        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
[9030]60      if (object.ReferenceEquals(left, right)) return 1.0;
[7789]61
[8319]62      switch (left.PermutationType) {
63        case PermutationTypes.Absolute:
64          return CalculateAbsolute(left, right);
65        case PermutationTypes.RelativeDirected:
66          return CalculateRelativeDirected(left, right);
67        case PermutationTypes.RelativeUndirected:
68          return CalculateRelativeUndirected(left, right);
69        default:
70          throw new InvalidOperationException("unknown permutation type");
71      }
72    }
73
74    private static double CalculateAbsolute(Permutation left, Permutation right) {
75      double similarity = 0.0;
[8322]76      for (int i = 0; i < left.Length; i++)
77        if (left[i] == right[i]) similarity++;
78
[8319]79      return similarity / left.Length;
80    }
81
82    private static double CalculateRelativeDirected(Permutation left, Permutation right) {
[9443]83      int[] edgesR = CalculateEdgesVector(right);
84      int[] edgesL = CalculateEdgesVector(left);
[8322]85
86      double similarity = 0.0;
[9443]87      for (int i = 0; i < left.Length; i++) {
88        if (edgesL[i] == edgesR[i]) similarity++;
89      }
[8322]90
91      return similarity / left.Length;
[8319]92    }
93
94    private static double CalculateRelativeUndirected(Permutation left, Permutation right) {
[9413]95      int[] edgesR = CalculateEdgesVector(right);
96      int[] edgesL = CalculateEdgesVector(left);
[7789]97
98      double similarity = 0.0;
[8086]99      for (int i = 0; i < left.Length; i++) {
[9413]100        if ((edgesL[i] == edgesR[i]) || (edgesL[edgesR[i]] == i))
[7789]101          similarity++;
102      }
103
[8086]104      return similarity / left.Length;
[7789]105    }
[8086]106
[9413]107    private static int[] CalculateEdgesVector(Permutation permutation) {
108      // transform path representation into adjacency representation
109      int[] edgesVector = new int[permutation.Length];
110      for (int i = 0; i < permutation.Length - 1; i++)
111        edgesVector[permutation[i]] = permutation[i + 1];
112      edgesVector[permutation[permutation.Length - 1]] = permutation[0];
113      return edgesVector;
114    }
115
[8319]116    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
117      var sol1 = leftSolution.Variables[SolutionVariableName].Value as Permutation;
118      var sol2 = rightSolution.Variables[SolutionVariableName].Value as Permutation;
[8086]119
120      return CalculateSimilarity(sol1, sol2);
121    }
[7789]122  }
123}
Note: See TracBrowser for help on using the repository browser.