예제 #1
0
파일: Genome.cs 프로젝트: zhoub/breeding
    static public Genome Breeding(Genome father, Genome mother)
    {
        Genome      child      = new Genome();
        List <Gene> childGenes = child.genes;

        //inherit genes from mother and father.
        foreach (Gene fatherGene in father.genes)
        {
            Gene newGene = fatherGene.Clone();
            childGenes.Add(newGene);
        }

        foreach (Gene motherGene in mother.genes)
        {
            Gene childGene = childGenes.Find(gene => gene.DNA == motherGene.DNA);
            if (childGene == null)
            {
                Gene newGene = motherGene.Clone();
                childGenes.Add(newGene);
            }
            else
            {
                childGene.Combine(motherGene);
            }
        }

        //bigger in front.
        childGenes.Sort((Gene a, Gene b) =>
        {
            if (a.Intensity > b.Intensity)
            {
                return(-1);
            }
            if (a.Intensity < b.Intensity)
            {
                return(1);
            }
            return(0);
        });

        //normalize.
        float       percent, frac = 0;
        List <Gene> removedGene = new List <Gene>();

        for (int i = 0; i < childGenes.Count; i++)
        {
            Gene gene = childGenes[i];
            percent = gene.Percent * 0.5f;
            if (!gene.SetPercent(percent, ref frac))
            {
                removedGene.Add(gene);
            }
        }

        removedGene.ForEach(rgene => childGenes.Remove(rgene));

        //gene mutation.
        //it is not a good solution.
        if (frac > 0.5)
        {
            childGenes[0].MutationOne();
        }


        child.FindStrongestGene();
        return(child);
    }