using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Encodings.BinaryVectorEncoding; 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] protected MedianBitBinaryVectorComparer(bool deserializing) : base(deserializing) { } protected 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); } } }