[6056] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6056] | 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 System.Collections.Generic;
|
---|
[6507] | 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[6056] | 26 | using HeuristicLab.PluginInfrastructure;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Encodings.PermutationEncoding {
|
---|
| 29 | [NonDiscoverableType]
|
---|
[6507] | 30 | public class PermutationEqualityComparer : EqualityComparer<Permutation> {
|
---|
| 31 | public override bool Equals(Permutation x, Permutation y) {
|
---|
[6056] | 32 | if (x.PermutationType != y.PermutationType) return false;
|
---|
| 33 | if (x.Length != y.Length) return false;
|
---|
| 34 | switch (x.PermutationType) {
|
---|
| 35 | case PermutationTypes.Absolute:
|
---|
| 36 | return EqualsAbsolute(x, y);
|
---|
| 37 | case PermutationTypes.RelativeDirected:
|
---|
| 38 | return EqualsRelative(x, y, true);
|
---|
| 39 | case PermutationTypes.RelativeUndirected:
|
---|
| 40 | return EqualsRelative(x, y, false);
|
---|
| 41 | default:
|
---|
| 42 | throw new InvalidOperationException("unknown permutation type");
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | private bool EqualsAbsolute(Permutation x, Permutation y) {
|
---|
[6507] | 47 | return x.SequenceEqual(y);
|
---|
[6056] | 48 | }
|
---|
| 49 |
|
---|
| 50 | private bool EqualsRelative(Permutation x, Permutation y, bool directed) {
|
---|
| 51 | int[] edgesX = CalculateEdgesVector(x);
|
---|
| 52 | int[] edgesY = CalculateEdgesVector(y);
|
---|
| 53 | for (int i = 0; i < x.Length; i++)
|
---|
| 54 | if ((edgesX[i] != edgesY[i]) && (directed || edgesX[edgesY[i]] != i))
|
---|
| 55 | return false;
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private int[] CalculateEdgesVector(Permutation permutation) {
|
---|
| 60 | // transform path representation into adjacency representation
|
---|
| 61 | int[] edgesVector = new int[permutation.Length];
|
---|
| 62 | for (int i = 0; i < permutation.Length - 1; i++)
|
---|
| 63 | edgesVector[permutation[i]] = permutation[i + 1];
|
---|
| 64 | edgesVector[permutation[permutation.Length - 1]] = permutation[0];
|
---|
| 65 | return edgesVector;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[6507] | 68 | public override int GetHashCode(Permutation obj) {
|
---|
[6056] | 69 | if (obj == null) return 0;
|
---|
[6507] | 70 | return GenerateHashString(obj).GetHashCode();
|
---|
[6056] | 71 | }
|
---|
[6507] | 72 |
|
---|
| 73 | private string GenerateHashString(Permutation p) {
|
---|
| 74 | StringBuilder sb = new StringBuilder();
|
---|
| 75 | if (p.PermutationType == PermutationTypes.Absolute) {
|
---|
| 76 | for (int i = 0; i < p.Length; i++)
|
---|
| 77 | sb.Append(p[i].ToString() + ";");
|
---|
| 78 | } else {
|
---|
| 79 | int i = 0;
|
---|
| 80 | while (p[i] != 0) i++; // always start at element 0
|
---|
| 81 | if (p.PermutationType == PermutationTypes.RelativeDirected) {
|
---|
| 82 | for (int j = 0; j < p.Length; j++)
|
---|
| 83 | sb.Append(p.GetCircular(i + j).ToString() + ";");
|
---|
| 84 | } else {
|
---|
| 85 | bool goLeft = p.GetCircular(i - 1) < p.GetCircular(i + 1); // go in direction of the lowest edge so that the total inversion and its original return the same hash code
|
---|
| 86 | for (int j = 0; j < p.Length; j++) {
|
---|
| 87 | if (goLeft) sb.Append(p.GetCircular(i - j).ToString() + ";");
|
---|
| 88 | else sb.Append(p.GetCircular(i + j).ToString() + ";");
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | return sb.ToString();
|
---|
| 93 | }
|
---|
[6056] | 94 | }
|
---|
| 95 | }
|
---|