Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int dimension = Convert.ToInt32(dimensionSpin.Value);
- int popSize = Convert.ToInt32(popSpin.Value);
- int crossing = Convert.ToInt32(crossingSpin.Value);
- int generations = Convert.ToInt32(genSpin.Value);
- double[,] popul = new double[popSize, dimension];
- progressBar1.Visible = true;
- progressBar1.Maximum = Convert.ToInt32(generations);
- progressBar1.Step = 1;
- char[] a = new char[11];
- double[] fun = new double[popSize];
- double[] funpot = new double[2*crossing];
- int[, ,] children = new int[2*crossing, dimension, 12];
- double[,] pot_kor = new double[2*crossing, dimension];
- double[] verraz = new double[popSize];
- double f_max = double.MinValue;
- double f_min = double.MaxValue;
- double sum_f = 0;
- double[] bit = new double[11];
- double[] mutProp = new double[2];
- mutProp[1] = Convert.ToDouble(mutationBox.Text);
- mutProp[0] = 1 - mutProp[1];
- double[] bit_mutProp = new double[11];
- for (int i = 0; i < 11; i++)
- {
- bit[i] = 1;
- bit_mutProp[i] = 1;
- }
- tempDataGrid.RowCount = popSize + 1;
- string x;
- Expression func = new Expression(fxBox.Text);
- Random rand = new Random(DateTime.Now.Millisecond);
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- Thread.Sleep(1);
- // 1. Создание исходной случайной популяции
- for (int i = 0; i < popSize; i++)
- {
- for (int j = 0; j < dimension; j++)
- {
- x = "x" + (j + 1).ToString();
- 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);
- tempDataGrid.Rows[i].Cells[j + 1].Value = popul[i, j];
- func.Parameters[x] = popul[i, j];
- }
- fun[i] = Convert.ToDouble(func.Evaluate());
- tempDataGrid.Rows[i].Cells[0].Value = fun[i];
- }
- // генериируем заданное количество поколений
- for (int iter = 0; iter < generations; iter++)
- {
- int[, ,] popul_int = new int[popSize, dimension, 12];
- for (int i = 0; i < popSize; i++) { // по представителям популяции
- for (int j = 0; j < dimension; j++)
- { // по аргументам функции
- x = "x" + (j + 1).ToString();
- popul[i, j] = Convert.ToDouble(tempDataGrid.Rows[i].Cells[j + 1].Value);
- tempDataGrid.Rows[i].Cells[j + 1].Value = popul[i, j];
- func.Parameters[x] = popul[i, j];
- for (int k = 0; k < 2049; k++)
- 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)))
- {
- popul_int[i, j, 0] = k - 1;
- k = 2090;
- }
- if (popul_int[i, j, 0] != 0) {
- a = Dec2Gray(popul_int[i, j, 0]);
- } else {
- a[0] = '0';
- }
- int temp = a.Length;
- int temp2 = 0;
- for (int ii = 1; ii < 12; ii++)
- {
- if (ii < 12 - temp) {
- popul_int[i, j, ii] = 0;
- } else {
- string tempString = Convert.ToString(a[temp2]);
- popul_int[i, j, ii] = Convert.ToInt32(tempString);
- temp2++;
- }
- }
- tempDataGrid.Rows[i].Cells[dimension + j + 2].Value = popul_int[i, j, 0];
- }
- fun[i] = Convert.ToDouble(func.Evaluate());
- tempDataGrid.Rows[i].Cells[0].Value = fun[i];
- sum_f = sum_f + fun[i];
- if (f_max < fun[i])
- f_max = fun[i];
- if (f_min > fun[i])
- f_min = fun[i];
- }
- // запись значений функции для выбора родителей
- for (int i = 0; i < popSize; i++)
- {
- verraz[i] = (f_max - fun[i] + 1) / (Convert.ToDouble(popSize) * (f_max + 1) - sum_f);
- tempDataGrid.Rows[i].Cells[dimension + 1].Value = verraz[i];
- }
- tempDataGrid.RowCount = Convert.ToInt32(tempDataGrid.RowCount) + 2*crossing;
- for (int i = 0; i < crossing; i++)
- {
- double grid = 0;
- int one = 0;
- int two = 0;
- int grbit = 0;
- // выбираем двух разных родителей
- for (bool flag = false; flag != true; )
- {
- grid = rand.NextDouble();
- one = genver(verraz, grid, popSize);
- grid = rand.NextDouble();
- two = genver(verraz, grid, popSize);
- if (one != two)
- flag = true;
- }
- // скрещивание
- for (int kk = 0; kk < dimension; kk++)
- {
- grid = rand.NextDouble();
- grbit = genver(bit, grid, 11);
- for (int ii = 1; ii < 12; ii++)
- {
- if (ii < grbit + 2)
- {
- children[2*i, kk, ii] = popul_int[one, kk, ii];
- children[2*i + 1, kk, ii] = popul_int[two, kk, ii];
- } else {
- children[2*i + 1, kk, ii] = popul_int[one, kk, ii];
- children[2*i, kk, ii] = popul_int[two, kk, ii];
- }
- }
- // мутация - инвертирования бита (через ксор)
- for (int ll = 0; ll < 2; ll++)
- {
- grid = rand.NextDouble();
- int mutbit = genver(bit_mutProp, grid, 11);
- if (genver(mutProp, grid, 2) == 1)
- children[2 * i + ll, kk, mutbit + 1] ^= 1;
- }
- }
- // по геному из переменных
- for (int kk = 0; kk < dimension; kk++)
- for (int hh = 0; hh < 2; hh++)
- for (int ii = 1; ii < 12; ii++)
- {
- // получаем ген по переменной
- if (children[2*i + hh, kk, ii] == 1) {
- string genome = "";
- for (int ll = ii; ll < 12; ll++)
- genome += Convert.ToString(children[2*i + hh, kk, ll]);
- children[2*i + hh, kk, 0] = Gray2Dec(genome);
- tempDataGrid.Rows[2*i + popSize + hh].Cells[dimension + kk + 2].Value = children[2*i + hh, kk, 0];
- 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));
- 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));
- pot_kor[2*i + hh, kk] = (left + right) / 2;
- tempDataGrid.Rows[2 * i + popSize + hh].Cells[kk + 1].Value = pot_kor[2 * i + hh, kk];
- break;
- }
- }
- }
- for (int i = 0; i < 2*crossing; i++)
- {
- for (int j = 0; j < dimension; j++)
- {
- x = "x" + (j + 1).ToString();
- func.Parameters[x] = pot_kor[i, j];
- }
- funpot[i] = Convert.ToDouble(func.Evaluate());
- tempDataGrid.Rows[i + popSize].Cells[0].Value = funpot[i];
- }
- tempDataGrid.Sort(tempDataGrid.Columns[0], ListSortDirection.Ascending);
- tempDataGrid.RowCount = popSize + 1;
- list.Add(iter, Convert.ToDouble(tempDataGrid.Rows[0].Cells[0].Value));
- progressBar1.Value = iter;
- // обрабатываем следующее поколение
- //MessageBox.Show("Next gen", "", MessageBoxButtons.OK);
- }
Advertisement
Add Comment
Please, Sign In to add comment