using System; using HeuristicLab.Core; using HeuristicLab.Common.Resources; using HeuristicLab.Common; using HeuristicLab.Encodings.BinaryVectorEncoding; using System.Drawing; using HEAL.Attic; namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators { [Item("BinaryVectorDistanceCalculator", "Calculates the hamming distance of two binary vectors")] [StorableType("BB6A8FD8-22B1-41CC-B38D-C8C9A2B79C83")] public class BinaryVectorDistanceCalculator : NamedItem, IItemDistanceCalculator { #region Properties public override bool CanChangeName { get { return false; } } public override bool CanChangeDescription { get { return false; } } public static new Image StaticItemImage { get { return VSImageLibrary.Function; } } #endregion #region Construction & Cloning [StorableConstructor] protected BinaryVectorDistanceCalculator(StorableConstructorFlag _) : base(_) { } protected BinaryVectorDistanceCalculator(BinaryVectorDistanceCalculator original, Cloner cloner) : base(original, cloner) { } public BinaryVectorDistanceCalculator() { name = ItemName; description = ItemDescription; } public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorDistanceCalculator(this, cloner); } #endregion #region IItemDistanceCalculator Members public Type ItemType { get { return typeof(BinaryVector); } } public double Distance(IItem x, IItem y) { BinaryVector a = (BinaryVector)x; BinaryVector b = (BinaryVector)y; if (a.Length != b.Length) throw new InvalidOperationException("Cannot compare vectors of different lengths"); double nEqualBits = 0; for (int i = 0; i < a.Length; i++) { if (a[i] == b[i]) nEqualBits++; } return Math.Max(a.Length, b.Length) - nEqualBits; } #endregion } }