Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.TravelingSalesman/3.3/SimilarityCalculators/TSPSimilarityCalculator.cs @ 17398

Last change on this file since 17398 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Problems.TravelingSalesman {
31  /// <summary>
32  /// An operator that performs similarity calculation between two traveling salesman solutions.
33  /// </summary>
34  /// <remarks>
35  /// The operator calculates the similarity based on the number of edges the two solutions have in common.
36  /// </remarks>
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.")]
38  [Obsolete("This operator is similar to the Hamming similarity calculator in the Encodings.PermutationEncoding namespace")]
39  [NonDiscoverableType]
40  [StorableType("0216BF57-995F-4F23-B422-F2C571314C5A")]
41  internal sealed class TSPSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
42    protected override bool IsCommutative { get { return true; } }
43
44    [StorableConstructor]
45    private TSPSimilarityCalculator(StorableConstructorFlag _) : base(_) { }
46    private TSPSimilarityCalculator(TSPSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
47    public TSPSimilarityCalculator() : base() { }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new TSPSimilarityCalculator(this, cloner);
51    }
52
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.");
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.");
60      if (object.ReferenceEquals(left, right)) return 1.0;
61
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;
76      for (int i = 0; i < left.Length; i++)
77        if (left[i] == right[i]) similarity++;
78
79      return similarity / left.Length;
80    }
81
82    private static double CalculateRelativeDirected(Permutation left, Permutation right) {
83      int[] edgesR = CalculateEdgesVector(right);
84      int[] edgesL = CalculateEdgesVector(left);
85
86      double similarity = 0.0;
87      for (int i = 0; i < left.Length; i++) {
88        if (edgesL[i] == edgesR[i]) similarity++;
89      }
90
91      return similarity / left.Length;
92    }
93
94    private static double CalculateRelativeUndirected(Permutation left, Permutation right) {
95      int[] edgesR = CalculateEdgesVector(right);
96      int[] edgesL = CalculateEdgesVector(left);
97
98      double similarity = 0.0;
99      for (int i = 0; i < left.Length; i++) {
100        if ((edgesL[i] == edgesR[i]) || (edgesL[edgesR[i]] == i))
101          similarity++;
102      }
103
104      return similarity / left.Length;
105    }
106
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
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;
119
120      return CalculateSimilarity(sol1, sol2);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.