[7128] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Common.Resources;
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 7 | using System.Drawing;
|
---|
[16573] | 8 | using HEAL.Attic;
|
---|
[7128] | 9 |
|
---|
| 10 | namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators {
|
---|
| 11 |
|
---|
| 12 | [Item("PermutationDistanceCalculator", "Calculates the distance of two permutations")]
|
---|
[16573] | 13 | [StorableType("F2B93749-3059-4D37-91A5-FAE2DC9DB794")]
|
---|
[7128] | 14 | public class PermutationDistanceCalculator : NamedItem, IItemDistanceCalculator {
|
---|
| 15 |
|
---|
| 16 | #region Properties
|
---|
| 17 |
|
---|
| 18 | public override bool CanChangeName { get { return false; } }
|
---|
| 19 | public override bool CanChangeDescription { get { return false; } }
|
---|
[7202] | 20 | public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
|
---|
[7128] | 21 |
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | #region Construction & Cloning
|
---|
| 25 |
|
---|
| 26 | [StorableConstructor]
|
---|
[16573] | 27 | protected PermutationDistanceCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
[7128] | 28 |
|
---|
| 29 | protected PermutationDistanceCalculator(PermutationDistanceCalculator original, Cloner cloner)
|
---|
| 30 | : base(original, cloner) {
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public PermutationDistanceCalculator() {
|
---|
| 34 | name = ItemName;
|
---|
| 35 | description = ItemDescription;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 39 | return new PermutationDistanceCalculator(this, cloner);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | #region IItemDistanceCalculator Members
|
---|
| 45 |
|
---|
| 46 | public Type ItemType {
|
---|
| 47 | get { return typeof(Permutation); }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public double Distance(IItem x, IItem y) {
|
---|
| 51 | Permutation a = (Permutation)x;
|
---|
| 52 | Permutation b = (Permutation)y;
|
---|
| 53 | if (a.PermutationType != b.PermutationType)
|
---|
| 54 | throw new InvalidOperationException("Cannot compare two permutations of different type");
|
---|
| 55 | if (a.Length != b.Length)
|
---|
| 56 | throw new InvalidOperationException("Cannot compare vectors of different lengths");
|
---|
| 57 | switch (a.PermutationType) {
|
---|
| 58 | case PermutationTypes.Absolute: return AbsoluteDistance(a, b);
|
---|
| 59 | case PermutationTypes.RelativeDirected: return RelativeDistance(a, b);
|
---|
| 60 | case PermutationTypes.RelativeUndirected: return RelativeDistance(a, b);
|
---|
| 61 | default: throw new NotImplementedException("Unknown permutation type " + a.PermutationType);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private double AbsoluteDistance(Permutation a, Permutation b) {
|
---|
| 66 | int mismatches = 0;
|
---|
| 67 | for (int i = 0; i<a.Length; i++) {
|
---|
| 68 | if (a[i] != b[i])
|
---|
| 69 | mismatches++;
|
---|
| 70 | }
|
---|
| 71 | return (double)mismatches;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private double RelativeDistance(Permutation a, Permutation b) {
|
---|
| 75 | HashSet<Point> edges = new HashSet<Point>();
|
---|
| 76 | for (int i = 0; i < a.Length; i++)
|
---|
| 77 | edges.Add(GetEdge(a, i));
|
---|
| 78 | int nCommonEdges = 0;
|
---|
| 79 | for (int i = 0; i < b.Length; i++) {
|
---|
| 80 | if (edges.Contains(GetEdge(b, i)))
|
---|
| 81 | nCommonEdges++;
|
---|
| 82 | }
|
---|
| 83 | return Math.Max(a.Length, b.Length) - nCommonEdges;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private static Point GetEdge(Permutation a, int index) {
|
---|
| 87 | int start = a[index];
|
---|
| 88 | int end = a[(index + 1)%a.Length];
|
---|
| 89 | switch (a.PermutationType) {
|
---|
| 90 | case PermutationTypes.RelativeDirected: return new Point(start, end);
|
---|
| 91 | case PermutationTypes.RelativeUndirected: return new Point(Math.Min(start, end), Math.Max(start, end));
|
---|
| 92 | default: throw new ArgumentException("cannot derive edge from non-relative permutation type");
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | #endregion
|
---|
| 97 | }
|
---|
| 98 | }
|
---|