public void ToCanonicalString() { var c = BitChromosome.Of(new BigInteger(234902)); var value = c.ToCanonicalString(); var sc = BitChromosome.Of(value); Assert.Equal(c, sc); }
public void SeqTypes() { var c = BitChromosome.Of(100, 0.3); Assert.Equal(typeof(BitGeneImmutableSeq), c.ToSeq().GetType()); Assert.Equal(typeof(BitGeneMutableSeq), c.ToSeq().Copy().GetType()); Assert.Equal(typeof(BitGeneImmutableSeq), c.ToSeq().Copy().ToImmutableSeq().GetType()); }
public void Zeros() { var c = BitChromosome.Of(1000, 0.5); var zeros = c.Zeros().Count(); Assert.Equal(zeros, c.Length - c.BitCount()); Assert.True(c.Zeros().All(i => !c.Get(i))); }
public void Ones() { var c = BitChromosome.Of(1000, 0.5); var ones = c.Ones().Count(); Assert.Equal(ones, c.BitCount()); Assert.True(c.Ones().All(i => c.Get(i))); }
public void TestGenotypeGenotypeOfT() { var c1 = BitChromosome.Of(12); var c2 = BitChromosome.Of(12); var g2 = Genotype.Of(c1, c2, c2); var g4 = g2; Assert.Equal(g4, g2); Assert.Equal(g4.GetHashCode(), g2.GetHashCode()); }
public void ToBitSet() { var c1 = BitChromosome.Of(34); var c2 = BitChromosome.Of(c1.ToBitSet(), 34); for (var i = 0; i < c1.Length; ++i) { Assert.Equal(c1.GetGene(i).GetBit(), c2.GetGene(i).GetBit()); } }
public void ToBigInteger() { var data = new byte[1056]; RandomRegistry.GetRandom().NextBytes(data); var value = new BigInteger(data); var chromosome = BitChromosome.Of(value); Assert.Equal(chromosome.ToBigInteger(), value); }
public void BitSetBitCount(double p) { const int size = 1000; var @base = BitChromosome.Of(size, p); for (var i = 0; i < 1000; ++i) { var other = @base.NewInstance(); Assert.Equal(other.BitCount(), other.ToBitSet().OfType <bool>().Count(bit => bit)); } }
public void NumValue() { var c1 = BitChromosome.Of(10); var value = c1.IntValue(); Assert.Equal((short)value, c1.ShortValue()); Assert.Equal(value, c1.LongValue()); Assert.Equal(value, c1.FloatValue()); Assert.Equal((double)value, c1.DoubleValue()); }
public void FromByteArrayBitSet() { var random = RandomRegistry.GetRandom(); var bytes = new byte[234]; random.NextBytes(bytes); var bits = new BitArray(bytes); var c = BitChromosome.Of(bits); Assert.Equal(bytes, c.ToByteArray()); Assert.Equal(bytes, bits.ToByteArray()); }
public void NewInstance() { const int size = 50000; var @base = BitChromosome.Of(size, 0.5); for (var i = 0; i < 100; ++i) { var other = @base.NewInstance(); Assert.NotEqual(@base, other); Assert.True(Math.Abs(1 - other.BitCount() / (size / 2.0)) < 0.02); } }
public void BitCount(double p) { const int size = 1000; var @base = BitChromosome.Of(size, p); for (var i = 0; i < 1000; ++i) { var other = @base.NewInstance(); var bitCount = other.Count(gene => gene.BooleanValue()); Assert.Equal(bitCount, other.BitCount()); } }
public void FromBitSet() { var random = RandomRegistry.GetRandom(); var bits = new BitArray(2343); for (var i = 0; i < bits.Count; ++i) { bits[i] = random.NextBoolean(); } var c = BitChromosome.Of(bits); Assert.Equal(bits.ToByteArray(), c.ToByteArray()); }
public void Invert() { var c1 = BitChromosome.Of(100, 0.3); var c3 = c1.Invert(); for (var i = 0; i < c1.Length; ++i) { Assert.True(c1.GetGene(i).GetBit() != c3.GetGene(i).GetBit()); } var c4 = c3.Invert(); Assert.Equal(c1, c4); }
public void IntProbability() { var c = BitChromosome.Of(10, 0); foreach (var g in c) { Assert.False(g.GetBit()); } c = BitChromosome.Of(10, 1); foreach (var g in c) { Assert.True(g.GetBit()); } }
public void BitChromosomeBitSet() { var bits = new BitArray(10); for (var i = 0; i < 10; ++i) { bits[i] = i % 2 == 0; } var c = BitChromosome.Of(bits); for (var i = 0; i < bits.Length; ++i) { Assert.Equal(c.GetGene(i).GetBit(), i % 2 == 0); } }
protected override Factory <IChromosome <BitGene> > Factory() { return(() => BitChromosome.Of(500, 0.3)); }