Esempio n. 1
0
        public IChromosome <BitGene> NewInstance(IImmutableSeq <BitGene> genes)
        {
            if (genes.IsEmpty)
            {
                throw new ArgumentException("The genes sequence must contain at least one gene.");
            }

            var chromosome = new BitChromosome(Bits.NewArray(genes.Length), genes.Length);
            var ones       = 0;

            if (genes is BitGeneImmutableSeq seq)
            {
                var iseq = seq;
                iseq.CopyTo(chromosome._genes);
                ones = Bits.Count(chromosome._genes);
            }
            else
            {
                for (var i = genes.Length; --i >= 0;)
                {
                    if (genes[i].BooleanValue())
                    {
                        Bits.Set(chromosome._genes, i);
                        ++ones;
                    }
                }
            }

            // TODO: do not match in copy, arrays do (see ChromosomeTester)
            chromosome._seq = BitGeneMutableSeq.Of(_genes, Length).ToImmutableSeq();

            chromosome._p = ones / (double)genes.Length;
            return(chromosome);
        }
Esempio n. 2
0
        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());
        }
Esempio n. 3
0
        public void ToCanonicalString()
        {
            var c     = BitChromosome.Of(new BigInteger(234902));
            var value = c.ToCanonicalString();
            var sc    = BitChromosome.Of(value);

            Assert.Equal(c, sc);
        }
Esempio n. 4
0
        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)));
        }
Esempio n. 5
0
        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)));
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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());
        }
Esempio n. 8
0
        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());
            }
        }
Esempio n. 9
0
        public void ChromosomeProbability()
        {
            var data = new byte[1234];

            RandomRegistry.GetRandom().NextBytes(data);

            var c = new BitChromosome(data);

            Assert.Equal(Bits.Count(data) / (double)(data.Length * 8), c.GetOneProbability());
        }
Esempio n. 10
0
        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));
            }
        }
Esempio n. 11
0
        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());
        }
Esempio n. 12
0
        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());
        }
Esempio n. 13
0
        public void ToByteArray()
        {
            var random = RandomRegistry.GetRandom();
            var data   = new byte[16];

            for (var i = 0; i < data.Length; ++i)
            {
                data[i] = (byte)(random.Next() * 256);
            }
            var bc = new BitChromosome(data);

            Assert.Equal(data, bc.ToByteArray());
        }
Esempio n. 14
0
        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);
            }
        }
Esempio n. 15
0
        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());
        }
Esempio n. 16
0
        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());
            }
        }
Esempio n. 17
0
        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);
        }
Esempio n. 18
0
        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());
            }
        }
Esempio n. 19
0
        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);
            }
        }
Esempio n. 20
0
 public int CompareTo(BitChromosome other)
 {
     return(ToBigInteger().CompareTo(other.ToBigInteger()));
 }
Esempio n. 21
0
 protected override Factory <IChromosome <BitGene> > Factory()
 {
     return(() => BitChromosome.Of(500, 0.3));
 }