1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2017 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Optimization.Operators;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
28 | [Item("Hamming Similarity Calculator for Permutation", "An operator that performs similarity calculation between two permutation-encoded solutions.")]
|
---|
29 | public sealed class HammingSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
|
---|
30 | protected override bool IsCommutative { get { return true; } }
|
---|
31 |
|
---|
32 | private HammingSimilarityCalculator(bool deserializing) : base(deserializing) { }
|
---|
33 | private HammingSimilarityCalculator(HammingSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
|
---|
34 | public HammingSimilarityCalculator() : base() { }
|
---|
35 |
|
---|
36 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
37 | return new HammingSimilarityCalculator(this, cloner);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static double CalculateSimilarity(Permutation left, Permutation right) {
|
---|
41 | if (left == null || right == null)
|
---|
42 | throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
|
---|
43 | if (left.PermutationType != right.PermutationType)
|
---|
44 | throw new ArgumentException("Cannot calculate similarity because the provided solutions have different types.");
|
---|
45 | if (left.Length != right.Length)
|
---|
46 | throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
|
---|
47 | if (object.ReferenceEquals(left, right)) return 1.0;
|
---|
48 |
|
---|
49 | switch (left.PermutationType) {
|
---|
50 | case PermutationTypes.Absolute:
|
---|
51 | return CalculateAbsolute(left, right);
|
---|
52 | case PermutationTypes.RelativeDirected:
|
---|
53 | return CalculateRelativeDirected(left, right);
|
---|
54 | case PermutationTypes.RelativeUndirected:
|
---|
55 | return CalculateRelativeUndirected(left, right);
|
---|
56 | default:
|
---|
57 | throw new InvalidOperationException("unknown permutation type");
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static double CalculateAbsolute(Permutation left, Permutation right) {
|
---|
62 | double similarity = 0.0;
|
---|
63 | for (int i = 0; i < left.Length; i++)
|
---|
64 | if (left[i] == right[i]) similarity++;
|
---|
65 |
|
---|
66 | return similarity / left.Length;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private static double CalculateRelativeDirected(Permutation left, Permutation right) {
|
---|
70 | int[] edgesR = CalculateEdgesVector(right);
|
---|
71 | int[] edgesL = CalculateEdgesVector(left);
|
---|
72 |
|
---|
73 | double similarity = 0.0;
|
---|
74 | for (int i = 0; i < left.Length; i++) {
|
---|
75 | if (edgesL[i] == edgesR[i]) similarity++;
|
---|
76 | }
|
---|
77 |
|
---|
78 | return similarity / left.Length;
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static double CalculateRelativeUndirected(Permutation left, Permutation right) {
|
---|
82 | int[] edgesR = CalculateEdgesVector(right);
|
---|
83 | int[] edgesL = CalculateEdgesVector(left);
|
---|
84 |
|
---|
85 | double similarity = 0.0;
|
---|
86 | for (int i = 0; i < left.Length; i++) {
|
---|
87 | if ((edgesL[i] == edgesR[i]) || (edgesL[edgesR[i]] == i))
|
---|
88 | similarity++;
|
---|
89 | }
|
---|
90 |
|
---|
91 | return similarity / left.Length;
|
---|
92 | }
|
---|
93 |
|
---|
94 | private static int[] CalculateEdgesVector(Permutation permutation) {
|
---|
95 | // transform path representation into adjacency representation
|
---|
96 | int[] edgesVector = new int[permutation.Length];
|
---|
97 | for (int i = 0; i < permutation.Length - 1; i++)
|
---|
98 | edgesVector[permutation[i]] = permutation[i + 1];
|
---|
99 | edgesVector[permutation[permutation.Length - 1]] = permutation[0];
|
---|
100 | return edgesVector;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
|
---|
104 | var sol1 = leftSolution.Variables[SolutionVariableName].Value as Permutation;
|
---|
105 | var sol2 = rightSolution.Variables[SolutionVariableName].Value as Permutation;
|
---|
106 |
|
---|
107 | return CalculateSimilarity(sol1, sol2);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|