예제 #1
0
        public Gene(Gene a, Gene b)
        {
            int genePartIndex = Chromosome.random.Next(4);
            byte logicHelper = 0;

            for (int i = 0; i <= genePartIndex; i++)
            {
                logicHelper += (byte) Math.Pow(2, i);
            }

            byte vala = a.Value, valb = b.Value;
            byte valc = (byte) (valb & logicHelper);

            logicHelper = (byte) (~logicHelper - 240);
            valc += (byte)(vala & logicHelper);

            this.value = valc;
        }
        public Chromosome Recombine(Chromosome a, Chromosome b)
        {
            if (a == b)
                return null;

            // Chance to recombine means 2 fenotypes that meet
            // don't always create a new one.
            if (Chromosome.random.NextDouble() > crossoverProbability)
                return null;

            int partitionIndex = Chromosome.random.Next(Math.Min(a.Genes.Count, b.Genes.Count));
            Gene gene = new Gene((Gene) a.Genes[partitionIndex], (Gene) b.Genes[partitionIndex]);
            ArrayList list = new ArrayList();

            for (int i = 0; i < partitionIndex; i++)
            {
                list.Add(a.Genes[i]);
            }
            list.Add(gene);
            for (int i = partitionIndex + 1; i < b.Genes.Count; i++)
            {
                list.Add(b.Genes[i]);
            }

            Chromosome returnValue = new Chromosome(list);
            returnValue.Mutate();
            returnValue.ComputeFitness(target);

            return returnValue;
        }