Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/DistanceCalculators/PermutationDistanceCalculator.cs @ 18068

Last change on this file since 18068 was 16995, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Core;
4using HeuristicLab.Common.Resources;
5using HeuristicLab.Common;
6using HeuristicLab.Encodings.PermutationEncoding;
7using System.Drawing;
8using HEAL.Attic;
9
10namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators {
11
12  [Item("PermutationDistanceCalculator", "Calculates the distance of two permutations")]
13  [StorableType("F2B93749-3059-4D37-91A5-FAE2DC9DB794")]
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; } }
20    public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
21
22    #endregion
23
24    #region Construction & Cloning
25
26    [StorableConstructor]
27    protected PermutationDistanceCalculator(StorableConstructorFlag _) : base(_) { }
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}
Note: See TracBrowser for help on using the repository browser.