using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.BinaryVectorEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.NK { [Item("Median Bit Binary Vector Comparer", "Compares two binary vectors by the median positive bit location")] [StorableClass] public sealed class MedianBitBinaryVectorComparer : BinaryVectorComparer { [StorableConstructor] private MedianBitBinaryVectorComparer(bool deserializing) : base(deserializing) { } private MedianBitBinaryVectorComparer(MedianBitBinaryVectorComparer original, Cloner cloner) : base(original, cloner) { } public MedianBitBinaryVectorComparer() { } public override IDeepCloneable Clone(Cloner cloner) { return new MedianBitBinaryVectorComparer(this, cloner); } public static int MedianBit(BinaryVector x) { var activeIndices = x.Select((b, i) => new { b, i }).Where(v => v.b).ToList(); if (activeIndices.Count > 0) return activeIndices[activeIndices.Count / 2].i; else return 0; } public override int Compare(BinaryVector x, BinaryVector y) { return MedianBit(x) - MedianBit(y); } } }