Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/DistanceCalculators/BinaryVectorDistanceCalculator.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: 1.9 KB
Line 
1using System;
2using HeuristicLab.Core;
3using HeuristicLab.Common.Resources;
4using HeuristicLab.Common;
5using HeuristicLab.Encodings.BinaryVectorEncoding;
6using System.Drawing;
7using HEAL.Attic;
8
9namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators {
10
11  [Item("BinaryVectorDistanceCalculator", "Calculates the hamming distance of two binary vectors")]
12  [StorableType("BB6A8FD8-22B1-41CC-B38D-C8C9A2B79C83")]
13  public class BinaryVectorDistanceCalculator : NamedItem, IItemDistanceCalculator {
14
15    #region Properties
16
17    public override bool CanChangeName { get { return false; } }
18    public override bool CanChangeDescription { get { return false; } }
19    public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
20
21    #endregion
22
23    #region Construction & Cloning
24
25    [StorableConstructor]
26    protected BinaryVectorDistanceCalculator(StorableConstructorFlag _) : base(_) { }
27
28    protected BinaryVectorDistanceCalculator(BinaryVectorDistanceCalculator original, Cloner cloner)
29      : base(original, cloner) {
30    }
31
32    public BinaryVectorDistanceCalculator() {
33      name = ItemName;
34      description = ItemDescription;
35    }
36
37    public override IDeepCloneable Clone(Cloner cloner) {
38      return new BinaryVectorDistanceCalculator(this, cloner);
39    }
40
41    #endregion
42
43    #region IItemDistanceCalculator Members
44
45    public Type ItemType {
46      get { return typeof(BinaryVector); }
47    }
48
49    public double Distance(IItem x, IItem y) {
50      BinaryVector a = (BinaryVector)x;
51      BinaryVector b = (BinaryVector)y;
52      if (a.Length != b.Length)
53        throw new InvalidOperationException("Cannot compare vectors of different lengths");
54      double nEqualBits = 0;
55      for (int i = 0; i < a.Length; i++) {
56        if (a[i] == b[i])
57          nEqualBits++;
58      }
59      return Math.Max(a.Length, b.Length) - nEqualBits;
60    }
61
62    #endregion
63  }
64}
Note: See TracBrowser for help on using the repository browser.