Beispiel #1
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);
        }
Beispiel #2
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);
        }