Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/DistanceCalculators/BinaryVectorDistanceCalculator.cs @ 7128

Last change on this file since 7128 was 7128, checked in by epitzer, 12 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

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