/// <summary>
        ///
        /// </summary>
        /// <param name="chromose"></param>
        private void Initializer(GAChromosome chromose)
        {
            bool [] PointSel = new bool[Points.Count];
            bool    bStop    = false;

            do
            {
                int iSel = RndObj.Next(0, Points.Count);
                if (!PointSel[iSel])
                {
                    PointSel[iSel] = true;
                    chromose.AddGene(new GAGene(iSel.ToString()));
                }

                for (int i = 0; i < Points.Count; i++)
                {
                    if (!PointSel[i])
                    {
                        break;
                    }
                    if (i == Points.Count - 1)
                    {
                        bStop = true;
                    }
                }
            }while(!bStop);
        }
        /// <summary>
        ///  Based On 2opt Algo
        /// </summary>
        /// <param name="chromose"></param>
        private void ChromoseCompraror(GAChromosome chromosome)
        {
            double [,] NighborMatrix = new double[Points.Count, Points.Count];
            for (int i = 0; i < Points.Count; i++)
            {
                NighborMatrix[i, i] = -1;                //distance diagonal
            }
            //start filling the Connection Matrix with cities and distances between each other
            for (int i = 0; i < Points.Count - 1; i++)
            {
                int City1Index = int.Parse(((GAGene)chromosome[i]).Value);
                for (int j = i + 1; j < Points.Count; j++)
                {
                    int    City2Index = int.Parse(((GAGene)chromosome[j]).Value);
                    double distance   = Distance((Point)Points[City1Index], (Point)Points[City2Index]);
                    NighborMatrix[City1Index, City2Index] = distance;
                    NighborMatrix[City2Index, City1Index] = distance;
                }
            }

            GAChromosome newChromosome = new GAChromosome();
            //Start By Random Selection
            int    iCurrentSel = RndObj.Next(0, Points.Count);
            GAGene Gene        = (GAGene)chromosome[iCurrentSel];

            newChromosome.AddGene(new GAGene(Gene.Value));
            //left cities for visiting
            int iLeftPoints     = Points.Count - 1;
            int iCurrentCitySel = int.Parse(Gene.Value);

            do
            {
                int iNearstNeighbor = GetNearstNeighbor(NighborMatrix, iCurrentCitySel);
                newChromosome.AddGene(new GAGene(iNearstNeighbor.ToString()));
                iLeftPoints--;
                iCurrentCitySel = iNearstNeighbor;
            }while(iLeftPoints > 0);
            chromosome.CopyChromosome(newChromosome);
        }
        /// <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);
            }
        }
Exemple #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="chromose"></param>
        private void Initializer(GAChromosome chromose)
        {
            bool []PointSel = new bool[Points.Count];
            bool bStop = false;
            do
            {
                int iSel = RndObj.Next(0,Points.Count);
                if (!PointSel[iSel])
                {
                    PointSel[iSel] = true;
                    chromose.AddGene(new GAGene(iSel.ToString()));

                }

                for(int i=0; i < Points.Count ;i++)
                {
                    if (!PointSel[i])
                        break;
                    if (i == Points.Count -1)
                        bStop = true;
                }
            }while(!bStop);
        }
Exemple #5
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);
            }
        }
Exemple #6
0
        /// <summary>
        ///  Based On 2opt Algo
        /// </summary>
        /// <param name="chromose"></param>
        private void ChromoseCompraror(GAChromosome chromosome)
        {
            double [,] NighborMatrix = new double[Points.Count , Points.Count];
            for (int i = 0; i < Points.Count ; i++)
                NighborMatrix[i,i] = -1; //distance diagonal

            //start filling the Connection Matrix with cities and distances between each other
            for (int i = 0 ; i < Points.Count - 1 ; i++)
            {
                int City1Index = int.Parse(((GAGene)chromosome[i]).Value);
                for (int j = i + 1; j < Points.Count ; j++)
                {
                    int City2Index  = int.Parse(((GAGene)chromosome[j]).Value);
                    double distance = Distance((Point)Points[City1Index],(Point)Points[City2Index]);
                    NighborMatrix[City1Index, City2Index] = distance ;
                    NighborMatrix[City2Index, City1Index] = distance ;
                }
            }

            GAChromosome newChromosome = new GAChromosome();
            //Start By Random Selection
            int iCurrentSel		=  RndObj.Next(0, Points.Count);
            GAGene Gene			= (GAGene)chromosome[iCurrentSel];
            newChromosome.AddGene(new GAGene(Gene.Value));
            //left cities for visiting
            int iLeftPoints		= Points.Count - 1;
            int iCurrentCitySel = int.Parse(Gene.Value);
            do
            {
                int iNearstNeighbor = GetNearstNeighbor(NighborMatrix, iCurrentCitySel);
                newChromosome.AddGene(new GAGene(iNearstNeighbor.ToString()));
                iLeftPoints--;
                iCurrentCitySel = iNearstNeighbor;

            }while(iLeftPoints > 0);
             chromosome.CopyChromosome(newChromosome);
        }