示例#1
0
        public TSPGeneticAlgorithm(City [] cities, int populationSize,
                                   double mutationPercent, double percentToMate,
                                   double matingPopulationPercent, int cutLength)
        {
            this.MutationPercent  = mutationPercent;
            this.MatingPopulation = matingPopulationPercent;
            this.PopulationSize   = populationSize;
            this.PercentToMate    = percentToMate;
            this.CutLength        = cutLength;
            this.PreventRepeat    = true;

            this.Chromosomes = new TSPChromosome[this.PopulationSize];
            for (int i = 0; i < this.Chromosomes.Length; i++)
            {
                TSPChromosome c = new TSPChromosome(this, cities);
                SetChromosome(i, c);
            }
            SortChromosomes();
        }
        private void WorldMapGenetic_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            int width  = this.Width;
            int height = this.Height;

            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            Pen        whitePen   = new Pen(Color.White);

            g.FillRectangle(blackBrush, 0, 0, width, height);

            for (int i = 0; i < WorldMapGenetic.CITY_COUNT; i++)
            {
                int xpos = this.cities[i].getx();
                int ypos = this.cities[i].gety();
                g.FillEllipse(greenBrush, xpos - 5, ypos - 5, 10, 10);
            }

            TSPChromosome top = this.getTopChromosome();

            for (int i = 0; i < WorldMapGenetic.CITY_COUNT - 1; i++)
            {
                int icity  = top.GetGene(i);
                int icity2 = top.GetGene(i + 1);

                g.DrawLine(whitePen, this.cities[icity].getx(),
                           this.cities[icity].gety(), this.cities[icity2]
                           .getx(), this.cities[icity2].gety());
            }

            // display status

            Font         drawFont   = new Font("Arial", 10);
            SolidBrush   drawBrush  = new SolidBrush(Color.White);
            float        x          = 0.0F;
            float        y          = this.Height - drawFont.Height - 35;
            StringFormat drawFormat = new StringFormat();

            e.Graphics.DrawString(status, drawFont, drawBrush, x, y, drawFormat);
        }