unvisibleman

Gen Alg

Mar 20th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.79 KB | None | 0 0
  1.             int dimension = Convert.ToInt32(dimensionSpin.Value);
  2.             int popSize = Convert.ToInt32(popSpin.Value);
  3.             int crossing = Convert.ToInt32(crossingSpin.Value);
  4.             int generations = Convert.ToInt32(genSpin.Value);
  5.  
  6.             double[,] popul = new double[popSize, dimension];
  7.             progressBar1.Visible = true;
  8.             progressBar1.Maximum = Convert.ToInt32(generations);
  9.             progressBar1.Step = 1;
  10.             char[] a = new char[11];
  11.             double[] fun = new double[popSize];
  12.             double[] funpot = new double[2*crossing];
  13.             int[, ,] children = new int[2*crossing, dimension, 12];
  14.             double[,] pot_kor = new double[2*crossing, dimension];
  15.             double[] verraz = new double[popSize];
  16.             double f_max = double.MinValue;
  17.             double f_min = double.MaxValue;
  18.             double sum_f = 0;
  19.            
  20.             double[] bit = new double[11];
  21.             double[] mutProp = new double[2];
  22.             mutProp[1] = Convert.ToDouble(mutationBox.Text);
  23.             mutProp[0] = 1 - mutProp[1];
  24.             double[] bit_mutProp = new double[11];
  25.             for (int i = 0; i < 11; i++)
  26.             {
  27.                 bit[i] = 1;
  28.                 bit_mutProp[i] = 1;
  29.             }
  30.             tempDataGrid.RowCount = popSize + 1;
  31.             string x;
  32.             Expression func = new Expression(fxBox.Text);
  33.             Random rand = new Random(DateTime.Now.Millisecond);
  34.             Stopwatch stopWatch = new Stopwatch();
  35.             stopWatch.Start();
  36.             Thread.Sleep(1);
  37.  
  38.             // 1. Создание исходной случайной популяции
  39.             for (int i = 0; i < popSize; i++)
  40.             {
  41.                 for (int j = 0; j < dimension; j++)
  42.                 {
  43.                     x = "x" + (j + 1).ToString();
  44.                     popul[i, j] = rand.NextDouble() * (Convert.ToDouble(xDiap.Rows[j].Cells[2].Value) - Convert.ToDouble(xDiap.Rows[j].Cells[0].Value)) + Convert.ToDouble(xDiap.Rows[j].Cells[0].Value);
  45.                     tempDataGrid.Rows[i].Cells[j + 1].Value = popul[i, j];
  46.                     func.Parameters[x] = popul[i, j];
  47.                 }
  48.                 fun[i] = Convert.ToDouble(func.Evaluate());
  49.                 tempDataGrid.Rows[i].Cells[0].Value = fun[i];
  50.             }
  51.  
  52.             // генериируем заданное количество поколений
  53.             for (int iter = 0; iter < generations; iter++)
  54.             {
  55.                 int[, ,] popul_int = new int[popSize, dimension, 12];
  56.                 for (int i = 0; i < popSize; i++) { // по представителям популяции
  57.                     for (int j = 0; j < dimension; j++)
  58.                     { // по аргументам функции
  59.                         x = "x" + (j + 1).ToString();
  60.                         popul[i, j] = Convert.ToDouble(tempDataGrid.Rows[i].Cells[j + 1].Value);
  61.  
  62.                         tempDataGrid.Rows[i].Cells[j + 1].Value = popul[i, j];
  63.                         func.Parameters[x] = popul[i, j];
  64.                         for (int k = 0; k < 2049; k++)
  65.                             if (popul[i, j] < (k * (Convert.ToDouble(xDiap.Rows[j].Cells[2].Value) - Convert.ToDouble(xDiap.Rows[j].Cells[0].Value)) / 2048 + Convert.ToDouble(xDiap.Rows[j].Cells[0].Value)))
  66.                             {
  67.                                 popul_int[i, j, 0] = k - 1;
  68.                                 k = 2090;
  69.                             }
  70.                         if (popul_int[i, j, 0] != 0) {
  71.                             a = Dec2Gray(popul_int[i, j, 0]);
  72.                         } else {
  73.                             a[0] = '0';
  74.                         }
  75.                         int temp = a.Length;
  76.                         int temp2 = 0;
  77.                         for (int ii = 1; ii < 12; ii++)
  78.                         {
  79.                             if (ii < 12 - temp) {
  80.                                 popul_int[i, j, ii] = 0;
  81.                             } else {
  82.                                 string tempString = Convert.ToString(a[temp2]);
  83.                                 popul_int[i, j, ii] = Convert.ToInt32(tempString);
  84.                                 temp2++;
  85.                             }
  86.                         }
  87.                         tempDataGrid.Rows[i].Cells[dimension + j + 2].Value = popul_int[i, j, 0];
  88.                     }
  89.                     fun[i] = Convert.ToDouble(func.Evaluate());
  90.                     tempDataGrid.Rows[i].Cells[0].Value = fun[i];
  91.                     sum_f = sum_f + fun[i];
  92.  
  93.                     if (f_max < fun[i])
  94.                         f_max = fun[i];
  95.                     if (f_min > fun[i])
  96.                         f_min = fun[i];
  97.                 }
  98.                 // запись значений функции для выбора родителей
  99.                 for (int i = 0; i < popSize; i++)
  100.                 {
  101.                     verraz[i] = (f_max - fun[i] + 1) / (Convert.ToDouble(popSize) * (f_max + 1) - sum_f);
  102.                     tempDataGrid.Rows[i].Cells[dimension + 1].Value = verraz[i];
  103.                 }
  104.  
  105.                 tempDataGrid.RowCount = Convert.ToInt32(tempDataGrid.RowCount) + 2*crossing;
  106.                 for (int i = 0; i < crossing; i++)
  107.                 {
  108.                     double grid = 0;
  109.                     int one = 0;
  110.                     int two = 0;
  111.                     int grbit = 0;
  112.  
  113.                     // выбираем двух разных родителей
  114.                     for (bool flag = false; flag != true; )
  115.                     {
  116.                         grid = rand.NextDouble();
  117.                         one = genver(verraz, grid, popSize);
  118.                         grid = rand.NextDouble();
  119.                         two = genver(verraz, grid, popSize);
  120.                         if (one != two)
  121.                             flag = true;
  122.                     }
  123.                     // скрещивание
  124.                     for (int kk = 0; kk < dimension; kk++)
  125.                     {
  126.                         grid = rand.NextDouble();
  127.                         grbit = genver(bit, grid, 11);
  128.  
  129.                         for (int ii = 1; ii < 12; ii++)
  130.                         {
  131.                             if (ii < grbit + 2)
  132.                             {
  133.                                 children[2*i, kk, ii] = popul_int[one, kk, ii];
  134.                                 children[2*i + 1, kk, ii] = popul_int[two, kk, ii];
  135.                             } else {
  136.                                 children[2*i + 1, kk, ii] = popul_int[one, kk, ii];
  137.                                 children[2*i, kk, ii] = popul_int[two, kk, ii];
  138.                             }
  139.                         }
  140.  
  141.                         // мутация - инвертирования бита (через ксор)
  142.                         for (int ll = 0; ll < 2; ll++)
  143.                         {
  144.                             grid = rand.NextDouble();
  145.                             int mutbit = genver(bit_mutProp, grid, 11);
  146.                             if (genver(mutProp, grid, 2) == 1)
  147.                                 children[2 * i + ll, kk, mutbit + 1] ^= 1;
  148.                         }
  149.  
  150.                     }
  151.                     // по геному из переменных
  152.                     for (int kk = 0; kk < dimension; kk++)
  153.                         for (int hh = 0; hh < 2; hh++)
  154.                             for (int ii = 1; ii < 12; ii++)
  155.                             {
  156.                                 // получаем ген по переменной
  157.                                 if (children[2*i + hh, kk, ii] == 1) {
  158.                                     string genome = "";
  159.                                     for (int ll = ii; ll < 12; ll++)
  160.                                         genome += Convert.ToString(children[2*i + hh, kk, ll]);
  161.                                     children[2*i + hh, kk, 0] = Gray2Dec(genome);
  162.                                     tempDataGrid.Rows[2*i + popSize + hh].Cells[dimension + kk + 2].Value = children[2*i + hh, kk, 0];
  163.  
  164.                                     double left = (children[2 * i + hh, kk, 0] * (Convert.ToDouble(xDiap.Rows[kk].Cells[2].Value) - Convert.ToDouble(xDiap.Rows[kk].Cells[0].Value)) / 2048 + Convert.ToDouble(xDiap.Rows[kk].Cells[0].Value));
  165.                                     double right = ((children[2 * i + hh, kk, 0] + 1) * (Convert.ToDouble(xDiap.Rows[kk].Cells[2].Value) - Convert.ToDouble(xDiap.Rows[kk].Cells[0].Value)) / 2048 + Convert.ToDouble(xDiap.Rows[kk].Cells[0].Value));
  166.                                     pot_kor[2*i + hh, kk] = (left + right) / 2;
  167.                                     tempDataGrid.Rows[2 * i + popSize + hh].Cells[kk + 1].Value = pot_kor[2 * i + hh, kk];
  168.                                     break;
  169.                                 }
  170.                             }
  171.                 }
  172.  
  173.                 for (int i = 0; i < 2*crossing; i++)
  174.                 {
  175.                     for (int j = 0; j < dimension; j++)
  176.                     {
  177.                         x = "x" + (j + 1).ToString();
  178.                         func.Parameters[x] = pot_kor[i, j];
  179.                     }
  180.                     funpot[i] = Convert.ToDouble(func.Evaluate());
  181.                     tempDataGrid.Rows[i + popSize].Cells[0].Value = funpot[i];
  182.                 }
  183.                 tempDataGrid.Sort(tempDataGrid.Columns[0], ListSortDirection.Ascending);
  184.                 tempDataGrid.RowCount = popSize + 1;
  185.                 list.Add(iter, Convert.ToDouble(tempDataGrid.Rows[0].Cells[0].Value));
  186.                 progressBar1.Value = iter;
  187.                 // обрабатываем следующее поколение
  188.  
  189.                 //MessageBox.Show("Next gen", "", MessageBoxButtons.OK);
  190.             }
Advertisement
Add Comment
Please, Sign In to add comment