Ejemplo n.º 1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            GeneticAlgo geneticAlgo = new GeneticAlgo();

            geneticAlgo.progress += new Progress(updateProgress);
            //progressBar1.Maximum = (int)txtGen.Value;
            //progressBar1.Value = 0;


            if (Algo == 2)
            {
                List <Chromosome> initPopulation = GetInitialPopulation((int)txtPop.Value);
                //if(chkPorgress.Checked)

                geneticAlgo.DoMating(ref initPopulation, (int)txtGen.Value, (double)txtCrosProb.Value, (double)txtMutProb.Value);

                dgResults.Rows.Clear();
                for (int i = 0; i < initPopulation.Count - 1; i++)
                {
                    label8.Text = i.ToString();
                    String sol = "| ";
                    for (int j = 0; j < 8; j++)
                    {
                        sol = sol + initPopulation[i].genes[j] + " | ";
                    }
                    dgResults.Rows.Add(new Object[] { sol, initPopulation[i].fitness });
                }
                //int[] x = new int[]{ 1, 2 };
                board1.Genes = initPopulation[0].genes;
            }
        }
Ejemplo n.º 2
0
        private List <Chromosome> GetInitialPopulation(int population)
        {
            List <Chromosome> initPop   = new List <Chromosome>();
            GeneticAlgo       RandomGen = new GeneticAlgo();

            for (int i = 0; i < population; i++)
            {
                List <int> genes      = new List <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
                Chromosome chromosome = new Chromosome();
                chromosome.genes = new int[8];
                for (int j = 0; j < 8; j++)
                {
                    int geneIndex = (int)(RandomGen.GetRandomVal(0, genes.Count - 1) + 0.5);
                    chromosome.genes[j] = genes[geneIndex];
                    genes.RemoveAt(geneIndex);
                }

                initPop.Add(chromosome);
            }
            return(initPop);
        }