Ejemplo 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);
        }
Ejemplo n.º 2
0
 private BitChromosome(byte[] bits, int length, double p)
 {
     _genes = bits;
     Length = length;
     _p     = p;
     _seq   = BitGeneMutableSeq.Of(_genes, length).ToImmutableSeq();
 }
Ejemplo n.º 3
0
 public static IMutableSeq <BitGene> NewSeq(int length)
 {
     return(BitGeneMutableSeq.Of(Array.Of(BitGeneStore.OfLength(length))));
 }