Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/DistanceCalculators/PermutationDistanceCalculator.cs @ 17883

Last change on this file since 17883 was 16958, checked in by abeham, 6 years ago

#2457: adapted to trunk

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