示例#1
0
        /*
         *
         * Burası sudokunu cozum işlemleri baslatan kısım
         *
         * **/
        public void solver(int GenerationSayisi)
        {
            Olustur g = null;
            int     tempUygunluk = 0;
            int     i = 0, yuzde;
            float   uygunlukKontrol      = 0.0f;
            int     baslangıcPopulasyonu = Convert.ToInt32(textBox1.Text);
            int     limit                = Convert.ToInt32(textBox2.Text);
            float   mutasyonOran         = float.Parse(textBox3.Text) / 100.0f;

            progressBar1.Maximum = GenerationSayisi;
            Populasyon populasyon = new Populasyon(this, baslangıcPopulasyonu, limit, mutasyonOran);

            //burada jenarasyon sayısı kadar gen olusturuyoruz ve olusan bu genlerin
            //işlemler sonucunda uygunluk degerini kontrol ediyoruz
            //bu şekilde oluşan genler arasındaki en uygun sudoku tahtasını ekranda gösteriyoruz
            while (i < GenerationSayisi && uygunlukKontrol != 1.0)
            {
                populasyon.NewGeneration();
                g = populasyon.getEnYuksekHesap();
                if ((int)(g.uygunlukSonuc * 100) > tempUygunluk)
                {
                    tempUygunluk = (int)(g.uygunlukSonuc * 100);
                }
                yuzde = (int)(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
                progressBar1.Value = i;
                progressBar1.PerformStep();
                progressBar1.CreateGraphics().DrawString("EN YUKSEK UYGUNLUK: " + g.uygunlukSonuc + "  " + yuzde.ToString() + "%  ADIM SAYISI " + i, new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 150, progressBar1.Height / 2 - 7));
                i++;
                uygunlukKontrol = g.uygunlukSonuc;
            }
            yazdir(g);

            label8.Text = ("EN YUKSEK UYGUNLUK: " + g.uygunlukSonuc + "  ADIM SAYISI " + i);
        }
示例#2
0
 //Mutasyon işlemini olasılıksal olarak yapıyoruz
 private void olsıMut(Olustur o)
 {
     if (rnd.Next(100) < (int)(mutasyonOran * 100.0))
     {
         o.mutasyon();
     }
 }
示例#3
0
        public int CompareTo(object obj)
        {
            Olustur gen1 = this;
            Olustur gen2 = (Olustur)obj;

            return(Math.Sign(gen2.uygunlukSonuc - gen1.uygunlukSonuc));
        }
示例#4
0
        /*
         * Bu method da genler arası caprazlama yapıyoruz.Bu caprazlama sonucunda olusturdugumuz
         * 2tane genden rasgele birini seçiyoruz
         * **/
        public Olustur caprazla(Olustur o)
        {
            Olustur olustur1          = new Olustur(f);
            Olustur olustur2          = new Olustur(f);
            Olustur caprazlamaOlustur = o;
            int     degisimYeri;

            //burada satirlar arası caprazlama yapıyoruz
            if (rnd.Next(2) == 1)
            {
                for (int j = 0; j < 9; j++)
                {
                    degisimYeri = rnd.Next(8) + 1;
                    for (int k = 0; k < degisimYeri; k++)
                    {
                        olustur1.sudokuMatrix[k, j] = caprazlamaOlustur.sudokuMatrix[k, j];
                        olustur2.sudokuMatrix[k, j] = sudokuMatrix[k, j];
                    }

                    for (int k = degisimYeri; k < 9; k++)
                    {
                        olustur2.sudokuMatrix[k, j] = caprazlamaOlustur.sudokuMatrix[k, j];
                        olustur1.sudokuMatrix[k, j] = sudokuMatrix[k, j];
                    }
                }
            }

            // burada sutunlar arası caprazlama yapıyoruz

            else
            {
                for (int j = 0; j < 9; j++)
                {
                    degisimYeri = rnd.Next(8) + 1;
                    for (int k = 0; k < degisimYeri; k++)
                    {
                        olustur1.sudokuMatrix[j, k] = caprazlamaOlustur.sudokuMatrix[j, k];
                        olustur2.sudokuMatrix[j, k] = sudokuMatrix[j, k];
                    }

                    for (int k = degisimYeri; k < 9; k++)
                    {
                        olustur2.sudokuMatrix[j, k] = caprazlamaOlustur.sudokuMatrix[j, k];
                        olustur1.sudokuMatrix[j, k] = sudokuMatrix[j, k];
                    }
                }
            }

            // olustur1 veya olustur2 den herhangi biri döndürülüyor.

            if (rnd.Next(2) == 1)
            {
                return(olustur1);
            }
            else
            {
                return(olustur2);
            }
        }
示例#5
0
 void yazdir(Olustur o)
 {
     for (int i = 0; i < 9; i++)
     {
         for (int j = 0; j < 9; j++)
         {
             tablo2[i, j].Text = o.sudokuMatrix[i, j].ToString();
         }
     }
 }
示例#6
0
 /*
  * burada başlangıc populasyonu kadar gen oluşturutoruz
  * **/
 public Populasyon(Form1 f, int b, int l, float mo)
 {
     this.limit        = l;
     this.baslangicPo  = b;
     this.mutasyonOran = mo;
     this.f            = f;
     for (int i = 0; i < baslangicPo; i++)
     {
         Olustur gen = new Olustur(f);
         gen.hesapla();
         genler.Add(gen);
     }
 }
示例#7
0
        //burada genler arası caprazlama yapıyoruz
        public void caprazlamaYap(ArrayList genler)
        {
            ArrayList p1 = new ArrayList();
            ArrayList p2 = new ArrayList();

            //tüm genleri 2 parcaya arıyoruz.Bunun için rasgele urettigimiz sayının
            //2 ye gore modunu alıp cıkan sonuca gore p1 veya p2 arraylistine atıyoruz
            //Burada rasgele sayının mod2 sini almamızın sebebi çeşitliligi arttırmak
            //Ayrıca burada i nin tek veya çift olmasına görede ayrılabilirdi.Ama o zaman
            //çeşitliiligin az olucagını dusundugumuz random secmeyi tercih ettik.Bunun nedenide
            //işin içine sans faktorunun girmesi gerektigini dusunmemiz
            for (int i = 0; i < genler.Count; i++)
            {
                if (rnd.Next(100) % 2 == 0)
                {
                    p1.Add(genler[i]);
                }
                else
                {
                    p2.Add(genler[i]);
                }
            }

            //burada caprazlama yapabilmemiz için iki ye ayırdıgımız gen gruplarının sayılarının
            //eşit olması gerekiyor
            if (p1.Count > p2.Count)
            {
                while (p1.Count > p2.Count)
                {
                    p2.Add(p1[p1.Count - 1]);
                    p1.RemoveAt(p1.Count - 1);
                }
            }
            else
            {
                while (p2.Count > p1.Count)
                {
                    p1.Add(p2[p2.Count - 1]);
                    p2.RemoveAt(p2.Count - 1);
                }
            }

            int count = 0;

            if (p1.Count > p2.Count)
            {
                count = p2.Count;
            }
            else
            {
                count = p1.Count;
            }

            for (int i = 0; i < count; i++)
            {
                Olustur c1 = (((Olustur)p2[i]).caprazla((Olustur)p1[i]));
                Olustur c2 = (((Olustur)p1[i]).caprazla((Olustur)p2[i]));

                genAilesi.Clear();
                //bu kısımda olusturugumuz genleri genAilesi arraylistine ekleyip
                //daha sonra uygunlugunu hesaplayarak oluşan bu gen ailesindeki
                //en iyi uygunluga sahip ikitanesini alıyoruz
                genAilesi.Add(p2[i]);
                genAilesi.Add(p1[i]);
                genAilesi.Add(c1);
                genAilesi.Add(c2);

                uygunlukHesapla(genAilesi);
                genAilesi.Sort();
                //burada olusan en iyi iki geni alıyoruz
                genSonuc.Add(genAilesi[0]);
                genSonuc.Add(genAilesi[1]);
            }
        }