Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/DistanceCalculators/BinaryVectorDistanceCalculator.cs @ 16591

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

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

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