Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12547 was 7202, checked in by epitzer, 13 years ago

#1696: Add static item image according to #1651

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