public Individ TurnirMax(Individ[] Tur) { if (Tur.Length > 2) { int i; Individ[] Tur2 = new Individ[Tur.Length - 1]; for (i = 0; i < Tur2.Length - 1; i++) { Tur2[i] = Tur[i]; } if (Tur[Tur.Length - 1].Fank_out < Tur[Tur.Length - 2].Fank_out) { Tur2[i] = Tur[Tur.Length - 2]; } else { Tur2[i] = Tur[Tur.Length - 1]; } return(TurnirMax(Tur2)); } else { if (Tur[1].Fank_out < Tur[0].Fank_out) { return(Tur[0]); } else { return(Tur[1]); } } }
// целочисленная кодировка private void Test_Individ() { /*int LeGen = Convert.ToInt32(LengthGene.Text); * float Mut = float.Parse(Mutation.Text); * float Inv = float.Parse(Inversion.Text); * int point = (int)Math.Floor(LeGen * float.Parse(VerPoint.Text)); * int point2 = 0; // (int)Math.Floor(LeGen * (1 - float.Parse(VerPoint.Text))); * //Следить что бы р1 был меньше р2 */ Individ test_Ind1 = new Individ(Aa); Individ test_Ind2 = new Individ(Aa); MtextBox.Text = MtextBox.Text + "Особь 1" + Environment.NewLine + test_Ind1.Prin_Ind(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine; MtextBox.Text = MtextBox.Text + "Особь 2" + Environment.NewLine + test_Ind2.Prin_Ind(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine; MtextBox.Text = MtextBox.Text + "Особь 1" + Environment.NewLine + test_Ind1.Prin_Fen(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "Особь 2" + Environment.NewLine + test_Ind2.Prin_Fen(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine; //проверка на поинт2 и его наличие если 0 то 1 точечное Individ[] C = Individ.PointCrossOver(Aa, test_Ind1, test_Ind2); string ind = ""; for (int i = 0; i < C.Length; i++) { ind = ind + "Потомок " + (i + 1) + Environment.NewLine + C[i].Prin_Ind(Aa) + Environment.NewLine; ind = ind + "Потомок " + (i + 1) + Environment.NewLine + C[i].Prin_Fen(Aa) + Environment.NewLine; } MtextBox.Text = MtextBox.Text + ind + Environment.NewLine; }
public void Copi(Parametrs a, Population c) { for (int i = 0; i < a.Popultion; i++) { Popul_all[i] = Individ.CopiInd(a, c.Popul_all[i], Popul_all[i]); } }
public void Select(Parametrs a) { Individ[] Ind_popul = new Individ[a.Popultion]; Individ f, g; Individ[] C = { new Individ(a), new Individ(a) }; // Individ[] q = new Individ[a.Tutu]; for (int i = 0; i < (int)Math.Floor(a.Popultion / 2.0); i++) { f = Turnir2(Rand_Turnir(a)); g = Turnir2(Rand_Turnir(a)); C = Individ.PointCrossOver(a, f, g); C[0].MutateAll(a); C[1].MutateAll(a); Ind_popul[2 * i] = C[0]; Ind_popul[2 * i + 1] = C[1]; Ind_popul[2 * i].Fank_out = Fank_(Ind_popul[2 * i], a); Ind_popul[2 * i + 1].Fank_out = Fank_(Ind_popul[2 * i + 1], a); } Popul_all = Ind_popul; // Mut_all(Mut, Inv, 3); }
private void Pr_rez(Individ test_Ind) { double Xx; MtextBox.Text = MtextBox.Text + Environment.NewLine; if (Convert.ToInt32(fanction_Ch.Text) == 1) { Xx = test_Ind.Fenotip_1(Aa) * Aa.y1 * Aa.y1 + test_Ind.Fenotip_2(Aa) * Aa.y1 + test_Ind.Fenotip_3(Aa); MtextBox.Text = MtextBox.Text + "a1*" + Aa.y1 * Aa.y1 + "+a2*" + Aa.y1 + "+ a3=0" + Environment.NewLine + test_Ind.Fenotip_1(Aa) + " * " + Aa.y1 * Aa.y1 + " + " + test_Ind.Fenotip_2(Aa) + " * " + Aa.y1 + " + " + test_Ind.Fenotip_3(Aa) + " = " + Environment.NewLine + Xx + Environment.NewLine; } else if (Convert.ToInt32(fanction_Ch.Text) == 2) { Xx = test_Ind.Fenotip_1(Aa) * Aa.y1 * Aa.y1 + test_Ind.Fenotip_2(Aa) * Aa.y2 * Aa.y2; MtextBox.Text = MtextBox.Text + "a1*" + Aa.y1 * Aa.y1 + "+a2*" + Aa.y2 + "+ a3=0" + Environment.NewLine + test_Ind.Fenotip_1(Aa) + " * " + Aa.y1 + "^2 + " + test_Ind.Fenotip_2(Aa) + " * " + Aa.y2 + "^2 = " + Environment.NewLine + Xx + Environment.NewLine; } else { if (Convert.ToInt32(fanction_Ch.Text) == 3) { Xx = test_Ind.Fenotip_1(Aa) * Aa.y1 * Aa.y1 * Aa.y1 + test_Ind.Fenotip_2(Aa) * Aa.y2 * Aa.y2 + test_Ind.Fenotip_3(Aa) * Aa.y3; MtextBox.Text = MtextBox.Text + "a1 * " + Aa.y1 + "^3 + a2 * " + Aa.y2 + "^2 + a3 * " + Aa.y3 + "= 0" + Environment.NewLine + test_Ind.Fenotip_1(Aa) + " * " + Aa.y1 + "^3 + " + test_Ind.Fenotip_2(Aa) + " * " + Aa.y2 + "^2 + " + test_Ind.Fenotip_3(Aa) + " * " + Aa.y3 + "= " + Environment.NewLine + Xx + Environment.NewLine; } else if (Convert.ToInt32(fanction_Ch.Text) == 4) { Xx = test_Ind.Fenotip_1(Aa) * Aa.y1 * Aa.y4 + test_Ind.Fenotip_2(Aa) * Aa.y2 * Aa.y2 + test_Ind.Fenotip_3(Aa) * Aa.y3 + test_Ind.Fenotip_4(Aa) * Aa.y4 * Aa.y4; MtextBox.Text = MtextBox.Text + "a1 * " + Aa.y1 + " * " + Aa.y4 + " + a2 * " + Aa.y2 + "^2 +a3 * " + Aa.y3 + " +a4 * " + Aa.y4 + "^2 = 0" + Environment.NewLine + test_Ind.Fenotip_1(Aa) + " * " + Aa.y1 + " * " + Aa.y4 + +test_Ind.Fenotip_2(Aa) + " * " + Aa.y2 + "^2 +" + Environment.NewLine + test_Ind.Fenotip_3(Aa) + " * " + Aa.y3 + " +" + test_Ind.Fenotip_4(Aa) + " * " + Aa.y4 + "^2 =" + Environment.NewLine + Xx + Environment.NewLine; } else { textBox1.Text = "Введена не правильная функция!!!!!" + Environment.NewLine + " Не ломайте программу!!!"; } } MtextBox.Text = MtextBox.Text + Environment.NewLine; }
Individ[] Popul_0(Parametrs a) // новая популяция (прям новая первая) { Individ[] Ind_popul = new Individ[a.Popultion]; //(L, Mut, Inv, point, point2, inter); //Individual[] pools = new Individual[N]; for (int i = 0; i < a.Popultion; i++) { Ind_popul[i] = new Individ(a); Ind_popul[i].Fank_out = Fank_(Ind_popul[i], a); } return(Ind_popul); }
public double Fank_(Individ s, Parametrs a)//double Fenotip_01, double Fenotip_02) { double x1 = 0, x2 = 0, x3 = 0, x4 = 0; if (a.Chom == 4) { x1 = s.Fenotip_1(a); x2 = s.Fenotip_2(a); x3 = s.Fenotip_3(a); x4 = s.Fenotip_4(a); } else { x1 = s.Fenotip_1(a); x2 = s.Fenotip_2(a); } double ouT; switch (a.Fanc) { case 1: ouT = (x2 - x1 * x1) * (x2 - x1 * x1) + (1 - x1) * (1 - x1); break; case 4: ouT = 4 * (x1 - 5) * (x1 - 5) + (x2 - 6) * (x2 - 6); break; case 5: ouT = (x2 - x1 * x1) * (x2 - x1 * x1) + (1 - x1) * (1 - x1); break; case 12: ouT = 4 * x1 * x1 + 3 * x2 * x2 - 4 * x1 * x2 * x2 + x1; break; case 15: ouT = (1.5 - x1 * (1 - x2)) * (1.5 - x1 * (1 - x2)) + (2.25 - x1 * (1 - x2 * x2)) * (2.25 - x1 * (1 - x2 * x2)) + (2.625 - x1 * (1 - x2 * x2 * x2)) * (2.625 - x1 * (1 - x2 * x2 * x2)); break; case 17: ouT = 100 * (x2 - x1 * x1) * (x2 - x1 * x1) + (1 - x1) * (1 - x1) + 90 * (x4 - x3 * x3) * (x4 - x3 * x3) + (1 - x3) * (1 - x3) * (1 - x3) + 10.1 * ((x2 - 1) * (x2 - 1) + (x4 - 1) * (x4 - 1)) + 19.8 * (x2 - 1) * (x4 - 1); break; default: ouT = 4 * (x1 - 5) * (x1 - 5) + (x2 - 6) * (x2 - 6); break; } return(ouT); }
public Individ[] Rand_Turnir(Parametrs a) { // Random rnd11 = new Random(); Individ[] Tur = new Individ[a.Tutu]; int ii = 0; for (int i = 0; i < a.Tutu; i++) { ii = a.rnd1.Next(0, a.Popultion - 1); Tur[i] = Popul_all[ii]; } return(Tur); }
// целочисленная кодировка private void Test_Popultion() { Population test = new Population(Aa); // создание популяции //int Namber0, int nIters0, double pMut0, double pInv0 // ( (int L, float Mut, float Inv, int point, int point2, int Namber0, int nIters0) Individ test_Ind = test.Turnir2(test.Popul_all); // MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test.Print( point, point2) + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Fank_out + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen(Aa) + Environment.NewLine; Pr_param(); MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine // + test.Print(Aa) //; + Environment.NewLine + "Min " + test_Ind.Fank_out + Environment.NewLine; for (int i = 0; i < Aa.Itertion; i++) { test.Mut_all(Aa); test.Select(Aa); test_Ind = test.Turnir2(test.Popul_all); MtextBox.Text = MtextBox.Text + //"-------" + (i+1) + "-------" + Environment.NewLine + // test.Print(point, point2)+ Environment.NewLine + "-----" + Environment.NewLine + // test_Ind.Fank_out + Environment.NewLine; // test.Print(Aa) + Environment.NewLine + "Min " + (i + 1) + " " + test_Ind.Fank_out + Environment.NewLine; test.Mut_all(Aa); //MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; } //test_Ind = test.Turnir(test.Popul_all); MtextBox.Text = MtextBox.Text + "-------END-------" + Environment.NewLine + // test.Print(point, point2) + Environment.NewLine + "-----" + Environment.NewLine + test_Ind.Prin_Fen(Aa) + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine + test_Ind.xrom[0].Prin(Aa) + Environment.NewLine + test_Ind.xrom[1].Prin(Aa) + Environment.NewLine; /////для 4 x1 = test_Ind.Fenotip_1(Aa); x2 = test_Ind.Fenotip_2(Aa); }
public static Individ CopiInd(Parametrs a, Individ A, Individ B) { B = new Individ(a); if (a.Chom == 2) {//CopiCh(Parametrs A, Chromosom chromA, out Chromosom chromB) Chromosom.CopiCh(a, A.xrom[0], out B.xrom[0]); Chromosom.CopiCh(a, A.xrom[1], out B.xrom[1]); } if (a.Chom == 4) { Chromosom.CopiCh(a, A.xrom[0], out B.xrom[0]); Chromosom.CopiCh(a, A.xrom[1], out B.xrom[1]); Chromosom.CopiCh(a, A.xrom[2], out B.xrom[2]); Chromosom.CopiCh(a, A.xrom[3], out B.xrom[3]); } //Chromosom.PointCrossOver(a, A.xrom_1, B.xrom_1, out C[0].xrom_1, out C[1].xrom_1); //Chromosom.PointCrossOver(a, A.xrom_2, B.xrom_2, out C[0].xrom_2, out C[1].xrom_2); return(B); }
public static Individ[] PointCrossOver(Parametrs a, Individ A, Individ B) { Individ[] C = new Individ[] { new Individ(a), new Individ(a) }; if (a.Chom == 2) { Chromosom.PointCrossOver(a, A.xrom[0], B.xrom[0], out C[0].xrom[0], out C[1].xrom[0]); Chromosom.PointCrossOver(a, A.xrom[1], B.xrom[1], out C[0].xrom[1], out C[1].xrom[1]); } if (a.Chom == 4) { Chromosom.PointCrossOver(a, A.xrom[0], B.xrom[0], out C[0].xrom[0], out C[1].xrom[0]); Chromosom.PointCrossOver(a, A.xrom[1], B.xrom[1], out C[0].xrom[1], out C[1].xrom[1]); Chromosom.PointCrossOver(a, A.xrom[2], B.xrom[2], out C[0].xrom[2], out C[1].xrom[2]); Chromosom.PointCrossOver(a, A.xrom[3], B.xrom[3], out C[0].xrom[3], out C[1].xrom[3]); } //Chromosom.PointCrossOver(a, A.xrom_1, B.xrom_1, out C[0].xrom_1, out C[1].xrom_1); //Chromosom.PointCrossOver(a, A.xrom_2, B.xrom_2, out C[0].xrom_2, out C[1].xrom_2); return(C); }
// целочисленная кодировка private void Test_Popultion() { Population test = new Population(Aa); // создание популяции //int Namber0, int nIters0, double pMut0, double pInv0 // ( (int L, float Mut, float Inv, int point, int point2, int Namber0, int nIters0) Individ test_Ind = test.Turnir2(test.Popul_all); // MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test.Print( point, point2) + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Fank_out + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen(Aa) + Environment.NewLine; //Pr_param(); // MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine // + test.Print(Aa) //; // + Environment.NewLine+ "Min " + test_Ind.Fank_out+Environment.NewLine; for (int i = 0; i < Aa.Itertion; i++) { test.Mut_all(Aa); test.Select(Aa); test_Ind = test.Turnir2(test.Popul_all); /* * MtextBox.Text = MtextBox.Text + //"-------" + (i+1) + "-------" + Environment.NewLine + * // test.Print(point, point2)+ Environment.NewLine + "-----" + Environment.NewLine + * // test_Ind.Fank_out + Environment.NewLine; * // test.Print(Aa) + Environment.NewLine + * "Min " + (i + 1) +" " + test_Ind.Fank_out + Environment.NewLine; */ //test.Mut_all(Aa); //MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; } //test_Ind = test.Turnir(test.Popul_all); //MtextBox.Text = MtextBox.Text + "-------END-------" + Environment.NewLine;// + // test.Print(point, point2) + Environment.NewLine + "-----" + // Environment.NewLine + test_Ind.Prin_Fen(Aa) + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; /* * MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine + * test_Ind.xrom[0].Prin(Aa) + Environment.NewLine + test_Ind.xrom[1].Prin(Aa) + Environment.NewLine; * */ if (Convert.ToInt32(fanction_Ch.Text) == 1) { MtextBox.Text = MtextBox.Text + "-------1-------" + Environment.NewLine; MtextBox.Text = MtextBox.Text + "1) " + test_Ind.Fenotip_1(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "2) " + test_Ind.Fenotip_2(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "3) " + test_Ind.Fenotip_3(Aa) + Environment.NewLine; } else if (Convert.ToInt32(fanction_Ch.Text) == 2) { MtextBox.Text = MtextBox.Text + "-------2-------" + Environment.NewLine; MtextBox.Text = MtextBox.Text + "1) " + test_Ind.Fenotip_1(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "2) " + test_Ind.Fenotip_2(Aa) + Environment.NewLine; } else { if (Convert.ToInt32(fanction_Ch.Text) == 3) { MtextBox.Text = MtextBox.Text + "-------3-------" + Environment.NewLine; MtextBox.Text = MtextBox.Text + "1) " + test_Ind.Fenotip_1(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "2) " + test_Ind.Fenotip_2(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "3) " + test_Ind.Fenotip_3(Aa) + Environment.NewLine; } else if (Convert.ToInt32(fanction_Ch.Text) == 4) { MtextBox.Text = MtextBox.Text + "-------4-------" + Environment.NewLine; MtextBox.Text = MtextBox.Text + "1) " + test_Ind.Fenotip_1(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "2) " + test_Ind.Fenotip_2(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "3) " + test_Ind.Fenotip_3(Aa) + Environment.NewLine; MtextBox.Text = MtextBox.Text + "4) " + test_Ind.Fenotip_4(Aa) + Environment.NewLine; } else { MtextBox.Text = "Введена не правильная функция!!!!!" + Environment.NewLine + " Не ломайте программу!!!"; } } /////для 4 x1 = test_Ind.Fenotip_1(Aa); x2 = test_Ind.Fenotip_2(Aa); x3 = test_Ind.Fenotip_3(Aa); x4 = test_Ind.Fenotip_4(Aa); Pr_rez(test_Ind); /* double Xx= test_Ind.Fenotip_1(Aa) * Aa.y1 * Aa.y1 + test_Ind.Fenotip_2(Aa) * Aa.y1 + test_Ind.Fenotip_3(Aa); * * MtextBox.Text = MtextBox.Text+ * "a1*" + Aa.y1 * Aa.y1 + "+a2*" + Aa.y1 + "+ a3=0" +Environment.NewLine + * test_Ind.Fenotip_1(Aa)+ " * " + Aa.y1 * Aa.y1 + " + " + test_Ind.Fenotip_2(Aa) + " * " + Aa.y1 + " + " + test_Ind.Fenotip_3(Aa) + " = " + Environment.NewLine + * // x1 + " * " + Aa.y1 * Aa.y1 + " + " + x2 + " * " + Aa.y1 + " + " + x3 + " = " + Environment.NewLine + * Xx; */ }
// целочисленная кодировка с уплотнением сетки private void Test_Popultion2() { Population test = new Population(Aa); // создание популяции int i; //int Namber0, int nIters0, double pMut0, double pInv0 // ( (int L, float Mut, float Inv, int point, int point2, int Namber0, int nIters0) Individ test_Ind = test.Turnir2(test.Popul_all); // MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test.Print( point, point2) + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Fank_out + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen(Aa) + Environment.NewLine; Pr_param(); MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine // + test.Print(Aa) //; + Environment.NewLine + "Min " + test_Ind.Fank_out + Environment.NewLine; double minu; for (i = 0; i < Aa.DopIter; i++) //Aa.Itertion { minu = test.TurnirMax(test.Popul_all).Fank_out - test.Turnir2(test.Popul_all).Fank_out; if ((minu > Aa.Itertion)) { test.Mut_all(Aa); test.Select(Aa); test_Ind = test.Turnir2(test.Popul_all); MtextBox.Text = MtextBox.Text + //"-------" + (i+1) + "-------" + Environment.NewLine + // test.Print(point, point2)+ Environment.NewLine + "-----" + Environment.NewLine + // test_Ind.Fank_out + Environment.NewLine; // test.Print(Aa) + Environment.NewLine + "Min " + (i + 1) + " " + test_Ind.Fank_out + Environment.NewLine; test.Mut_all(Aa); //MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; } else { break; } } Aa.Length = Aa.Length * Aa.Setka; Population test2 = new Population(Aa); test2.Copi(Aa, test); Individ test_Ind2 = test2.Turnir2(test2.Popul_all); Aa.point1 = Aa.point1 * Aa.Setka; Aa.point2 = Aa.point2 * Aa.Setka; for (i = Aa.DopIter; i < 50; i++) { minu = test.TurnirMax(test.Popul_all).Fank_out - test.Turnir2(test.Popul_all).Fank_out; if ((minu > Aa.Itertion)) { test2.Mut_all(Aa); test2.Select(Aa); test_Ind2 = test2.Turnir2(test2.Popul_all); MtextBox.Text = MtextBox.Text + //"-------" + (i+1) + "-------" + Environment.NewLine + // test.Print(point, point2)+ Environment.NewLine + "-----" + Environment.NewLine + // test_Ind.Fank_out + Environment.NewLine; // test.Print(Aa) + Environment.NewLine + "Min " + (i + 1) + " " + test_Ind2.Fank_out + Environment.NewLine; test2.Mut_all(Aa); //MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; } else { break; } } //test_Ind = test.Turnir(test.Popul_all); MtextBox.Text = MtextBox.Text + "-------END-------" + Environment.NewLine + // test.Print(point, point2) + Environment.NewLine + "-----" + Environment.NewLine + test_Ind2.Prin_Fen(Aa) + Environment.NewLine; // MtextBox.Text = MtextBox.Text + test_Ind.Prin_Fen() + Environment.NewLine; MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine + test_Ind2.xrom[0].Prin(Aa) + Environment.NewLine + test_Ind2.xrom[1].Prin(Aa) + Environment.NewLine; /////для 4 x1 = test_Ind2.Fenotip_1(Aa); x2 = test_Ind2.Fenotip_2(Aa); }