Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16573 was 16573, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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