Beispiel #1
0
 public static IEnumerable <ISwitchable <T> > MakeSwitchables <T>(this IRando random, int keyCount)
 {
     if (typeof(T) == typeof(uint))
     {
         return((IEnumerable <ISwitchable <T> >)random.ToUintEnumerator(((uint)1) << (keyCount - 1)).ToUintSwitchable(keyCount));
     }
     if (typeof(T) == typeof(ulong))
     {
         return((IEnumerable <ISwitchable <T> >)random.ToUlongEnumerator(((ulong)1) << (keyCount - 1)).ToUlongSwitchable(keyCount));
     }
     if (typeof(T) == typeof(bool[]))
     {
         return((IEnumerable <ISwitchable <T> >)random.ToBoolEnumerator(0.5).Chunk(keyCount).Select(b => b.ToSwitchableBitArray()));
     }
     if (typeof(T) == typeof(int[]))
     {
         return((IEnumerable <ISwitchable <T> >)random.ToRandomEnumerator().Select(r => r.ToSwitchableIntArray(keyCount)));
     }
     throw new Exception("unhandled switchable type");
 }
        public static ISwitchableGroupGenome ToSwitchableGroupGenome(
            this IRando rando,
            SwitchableGroupGenomeType switchableGroupGenomeType,
            int keyCount,
            int groupSize
            )
        {
            switch (switchableGroupGenomeType)
            {
            case SwitchableGroupGenomeType.UInt:
                return(new SwitchableGroupGenomeImpl
                       (
                           guid: rando.NextGuid(),
                           parentGuid: Guid.Empty,
                           keyCount: keyCount,
                           chromosome: rando.ToUintEnumerator(((uint)1) << (keyCount - 1))
                           .Take(groupSize)
                           .ToList()
                           .ToChromosomeUint(((uint)1) << keyCount),
                           groupCount: groupSize
                       ));

            case SwitchableGroupGenomeType.ULong:
                return(new SwitchableGroupGenomeImpl
                       (
                           guid: rando.NextGuid(),
                           parentGuid: Guid.Empty,
                           keyCount: keyCount,
                           chromosome: rando.ToUlongEnumerator(((ulong)1) << (keyCount - 1))
                           .Take(groupSize)
                           .ToUints()
                           .ToList()
                           .ToChromosomeUlongN(((ulong)1) << keyCount),
                           groupCount: groupSize
                       ));

            case SwitchableGroupGenomeType.BitArray:
                return(new SwitchableGroupGenomeImpl
                       (
                           guid: rando.NextGuid(),
                           parentGuid: Guid.Empty,
                           keyCount: keyCount,
                           chromosome: rando.ToUintEnumerator(2)
                           .Take(groupSize * keyCount)
                           .ToList()
                           .ToChromosomeBits(rando.NextGuid(), keyCount),
                           groupCount: groupSize
                       ));

            case SwitchableGroupGenomeType.IntArray:

                return(new SwitchableGroupGenomeImpl
                       (
                           guid: rando.NextGuid(),
                           parentGuid: Guid.Empty,
                           keyCount: keyCount,
                           chromosome: Enumerable
                           .Range(0, groupSize)
                           .SelectMany(i => Enumerable
                                       .Range(0, keyCount)
                                       .Select(t => (uint)t)
                                       .ToList()
                                       .FisherYatesShuffle(rando)
                                       )
                           .ToList()
                           .ToChromosomePermutation(rando.NextGuid(), keyCount, 0.2),

                           groupCount: groupSize
                       ));

            default:
                throw new Exception();
            }
        }