Beispiel #1
0
        /// <summary>
        /// //
        /// </summary>
        /// <param name="Dad"></param>
        /// <param name="Mum"></param>
        /// <param name="child1"></param>
        private void GreedyCrossOver
        (
            GAChromosome Dad
            , GAChromosome Mum
            , ref GAChromosome child
        )
        {
            int length   = Dad.GeneLength;
            int MumIndex = -1;
            int DadIndex = RndObj.Next(0, length);

            GAGene DadGene = (GAGene)Dad[DadIndex];

            MumIndex = Mum.HasThisGene(DadGene);

            if (MumIndex < 0)
            {
                throw new Exception("Gene not found in mum");
            }

            child.Add(new GAGene(DadGene.Value));

            bool bDadAdded = true;
            bool bMumAdded = true;

            do
            {
                //As long as I can add from dad
                GAGene obMumGene = null;
                GAGene obDadGene = null;
                if (bDadAdded)
                {
                    if (DadIndex > 0)
                    {
                        DadIndex = DadIndex - 1;
                    }
                    else
                    {
                        DadIndex = length - 1;
                    }

                    obDadGene = (GAGene)Dad[DadIndex];
                }
                else
                {
                    bDadAdded = false;
                }
                //As long as I can add from mum
                if (bMumAdded)
                {
                    if (MumIndex < length - 1)
                    {
                        MumIndex = MumIndex + 1;
                    }
                    else
                    {
                        MumIndex = 0;
                    }
                    obMumGene = (GAGene)Mum[MumIndex];
                }
                else
                {
                    bMumAdded = false;
                }
                if (bDadAdded && child.HasThisGene(obDadGene) < 0)
                {
                    //Add to head Dad gene
                    child.Insert(0, obDadGene);
                }
                else
                {
                    bDadAdded = false;
                }

                if (bMumAdded && child.HasThisGene(obMumGene) < 0)
                {
                    //Add to Tail Mum gene
                    child.AddGene(obMumGene);
                }
                else
                {
                    bMumAdded = false;
                }
            }while(bDadAdded || bMumAdded);

            // Add rest of genes by Random Selection
            while (child.GeneLength < length)
            {
                bool bDone = false;
                do
                {
                    int iRandom = this.RndObj.Next(0, length);
                    if (child.HasThisGene(new GAGene(iRandom.ToString())) < 0)
                    {
                        child.Add(new GAGene(iRandom.ToString()));
                        bDone = true;
                    }
                }while(!bDone);
            }
        }
Beispiel #2
0
        /// <summary>
        /// // 
        /// </summary>
        /// <param name="Dad"></param>
        /// <param name="Mum"></param>
        /// <param name="child1"></param>
        private void GreedyCrossOver(
            GAChromosome Dad
            , GAChromosome Mum
            , ref GAChromosome child
            )
        {
            int length		= Dad.GeneLength;
            int MumIndex	= -1;
            int DadIndex	= RndObj.Next(0,length);

            GAGene DadGene = (GAGene)Dad[DadIndex];
            MumIndex = Mum.HasThisGene(DadGene);

            if (MumIndex < 0 )
                throw new Exception("Gene not found in mum");

            child.Add(new GAGene(DadGene.Value));

            bool bDadAdded = true;
            bool bMumAdded = true;
            do
            {
                //As long as I can add from dad
                GAGene obMumGene = null;
                GAGene obDadGene = null;
                if (bDadAdded )
                {
                    if(DadIndex > 0)
                        DadIndex = DadIndex - 1 ;
                    else
                        DadIndex = length - 1 ;

                    obDadGene = (GAGene)Dad[DadIndex];
                }
                else
                {
                    bDadAdded = false;
                }
                //As long as I can add from mum
                if (bMumAdded)
                {
                    if(MumIndex < length-1)
                        MumIndex  = MumIndex + 1 ;
                    else
                        MumIndex  = 0 ;
                    obMumGene = (GAGene)Mum[MumIndex];
                }
                else
                {
                    bMumAdded = false;
                }
                if(bDadAdded  && child.HasThisGene(obDadGene)< 0)
                {
                    //Add to head Dad gene
                    child.Insert(0,obDadGene);
                }
                else
                    bDadAdded = false;

                if (bMumAdded && child.HasThisGene(obMumGene) < 0)
                {
                    //Add to Tail Mum gene
                    child.AddGene(obMumGene);
                }
                else
                    bMumAdded = false;

            }while(bDadAdded || bMumAdded)	;

            // Add rest of genes by Random Selection
            while (child.GeneLength < length)
            {
                bool bDone = false;
                do
                {
                    int iRandom = this.RndObj.Next(0, length);
                    if (child.HasThisGene(new GAGene(iRandom.ToString()))< 0)
                    {
                        child.Add(new GAGene(iRandom.ToString()));
                        bDone = true;
                    }
                }while(!bDone);
            }
        }