コード例 #1
0
        public void Select(Parametrs a)
        {
            VeshIndivid[] Ind_popul = new VeshIndivid[a.Popultion];
            VeshIndivid   f, g;

            VeshIndivid[] C = { new VeshIndivid(a), new VeshIndivid(a) };
            //  Individ[] q = new Individ[a.Tutu];

            for (int i = 0; i < (int)Math.Floor(a.Popultion / 2.0); i++)
            {
                f = Turnir(Rand_Turnir(a));
                g = Turnir(Rand_Turnir(a));


                C = VeshIndivid.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);
        }
コード例 #2
0
        public VeshIndivid Turnir(VeshIndivid[] Tur)
        {
            if (Tur.Length > 2)
            {
                int           i;
                VeshIndivid[] Tur2 = new VeshIndivid[Tur.Length - 1];
                for (i = 0; i < Tur2.Length - 1; i++)
                {
                    Tur2[i] = Tur[i];
                }

                if (Math.Abs(Tur[Tur.Length - 1].Fank_out) > Math.Abs(Tur[Tur.Length - 2].Fank_out))
                {
                    Tur2[i] = Tur[Tur.Length - 2];
                }
                else
                {
                    Tur2[i] = Tur[Tur.Length - 1];
                }

                return(Turnir(Tur2));
            }
            else
            {
                if (Math.Abs(Tur[1].Fank_out) > Math.Abs(Tur[0].Fank_out))
                {
                    return(Tur[0]);
                }
                else
                {
                    return(Tur[1]);
                }
            }
        }
コード例 #3
0
ファイル: VeshIndivid.cs プロジェクト: M-Elena-A/University
        public static VeshIndivid[] PointCrossOver(Parametrs a, VeshIndivid A, VeshIndivid B)
        {
            VeshIndivid[] C = new VeshIndivid[] { new VeshIndivid(a), new VeshIndivid(a) };
            double[]      s = new double[2];
            if (a.Chom == 2)
            {
                s = Point_CrossOver(a, A.xrom[0], B.xrom[0]);
                C[0].xrom[0].chrom = s[0];
                C[1].xrom[0].chrom = s[1];

                s = Point_CrossOver(a, A.xrom[1], B.xrom[1]);
                C[0].xrom[1].chrom = s[0];
                C[1].xrom[1].chrom = s[1];
            }
            if (a.Chom == 4)
            {
                s = Point_CrossOver(a, A.xrom[0], B.xrom[0]);
                C[0].xrom[0].chrom = s[0];
                C[1].xrom[0].chrom = s[1];

                s = Point_CrossOver(a, A.xrom[1], B.xrom[1]);
                C[0].xrom[1].chrom = s[0];
                C[1].xrom[1].chrom = s[1];

                s = Point_CrossOver(a, A.xrom[2], B.xrom[2]);
                C[0].xrom[2].chrom = s[0];
                C[1].xrom[2].chrom = s[1];

                s = Point_CrossOver(a, A.xrom[3], B.xrom[3]);
                C[0].xrom[3].chrom = s[0];
                C[1].xrom[3].chrom = s[1];
            }

            return(C);
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: M-Elena-A/University
        //вещественная кодировка
        private void Test_VeshPopultion()
        {
            VeshPopulation test = new VeshPopulation(Aa);
            // создание популяции


            //int Namber0, int nIters0, double pMut0, double pInv0
            // (  (int L, float Mut, float Inv, int point, int point2, int Namber0, int nIters0)


            VeshIndivid test_Ind = test.Turnir(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;


            MtextBox.Text = MtextBox.Text + "------- + 0 + -------" + Environment.NewLine
                            //  + test.Print(Aa)  //;
                            + Environment.NewLine + "Min  " + test_Ind.Fank_out + Environment.NewLine;

            double minu;

            for (int i = 0; i < 50; i++) //Aa.Itertion
            {
                minu = test.TurnirMax(test.Popul_all).Fank_out - test.Turnir(test.Popul_all).Fank_out;
                if (minu > Aa.Itertion)
                {
                    test.Select(Aa);
                    test_Ind = test.Turnir(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;
                }
            }

            //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() + Environment.NewLine + test_Ind.xrom[1].Prin() + Environment.NewLine;

            /////для 4
            x1 = test_Ind.xrom[0].chrom;
            x2 = test_Ind.xrom[1].chrom;
        }
コード例 #5
0
 VeshIndivid[] Popul_0(Parametrs a)                          // новая популяция (прям новая первая)
 {
     VeshIndivid[] Ind_popul = new VeshIndivid[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 VeshIndivid(a);
         Ind_popul[i].Fank_out = Fank_(Ind_popul[i], a);
     }
     return(Ind_popul);
 }
コード例 #6
0
        public VeshIndivid[] Rand_Turnir(Parametrs a)
        {
            VeshIndivid[] Tur = new VeshIndivid[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);
        }
コード例 #7
0
        public double Fank_(VeshIndivid 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);
        }
コード例 #8
0
        private void Test_VeshInd()
        {
            VeshIndivid Te1, Te2;

            VeshIndivid[] Te = new VeshIndivid[2];
            Te[0] = new VeshIndivid(Aa);
            Te[1] = new VeshIndivid(Aa);

            Te1 = new VeshIndivid(Aa);
            Te2 = new VeshIndivid(Aa);

            MtextBox.Text = MtextBox.Text + "-------------" + Environment.NewLine +
                            Te1.Prin_Ind(Aa) + Environment.NewLine + Te2.Prin_Ind(Aa) + Environment.NewLine;

            Te = VeshIndivid.PointCrossOver(Aa, Te1, Te2);

            MtextBox.Text = MtextBox.Text +
                            Te[0].Prin_Ind(Aa) + Environment.NewLine + Te[1].Prin_Ind(Aa) + Environment.NewLine;
        }
コード例 #9
0
        private void XD()
        {
            //test_Ind;
            VeshIndivid t0, t1, t2, t3, t4;

            t0 = new VeshIndivid(Aa);
            t1 = new VeshIndivid(Aa);
            t2 = new VeshIndivid(Aa);
            t3 = new VeshIndivid(Aa);
            t4 = new VeshIndivid(Aa);
            double         f0, alf = 0.1;
            int            pr = 0;
            VeshPopulation F  = new VeshPopulation(Aa);

            // t0.xrom[0]= test_Ind
            /////для 4
            t0.xrom[0].chrom = x1;
            t0.xrom[1].chrom = x2;
            t0.xrom[2].chrom = x3;
            t0.xrom[3].chrom = x4;

            f0 = t0.Fank_out = F.Fank_(t0, Aa);
            //MtextBox.Text = MtextBox.Text + Environment.NewLine
            // + f0 + Environment.NewLine;
            ////



            for (int i = 0; i < 1000 && pr != 1; i++)
            {
                t1.xrom[0].chrom = t0.Fenotip_1(Aa) + alf;
                t1.xrom[1].chrom = t0.Fenotip_2(Aa);
                t1.Fank_out      = F.Fank_(t1, Aa);

                if (Math.Abs(t1.Fank_out) < Math.Abs(t0.Fank_out))
                {
                    t2.xrom[0].chrom = t0.xrom[0].chrom + alf;
                    t2.xrom[1].chrom = t0.xrom[1].chrom + alf;
                    t2.Fank_out      = F.Fank_(t2, Aa);

                    if (Math.Abs(t2.Fank_out) < Math.Abs(t1.Fank_out))
                    {
                        t0.xrom[0].chrom = t2.xrom[0].chrom;
                        t0.xrom[1].chrom = t2.xrom[1].chrom;

                        f0 = t0.Fank_out = t2.Fank_out;
                    }
                    else
                    {
                        t3.xrom[0].chrom = t0.xrom[0].chrom + alf;
                        t3.xrom[1].chrom = t0.xrom[1].chrom - alf;
                        t3.Fank_out      = F.Fank_(t3, Aa);

                        if (Math.Abs(t3.Fank_out) < Math.Abs(t1.Fank_out))
                        {
                            t0.xrom[0].chrom = t3.xrom[0].chrom;
                            t0.xrom[1].chrom = t3.xrom[1].chrom;

                            f0 = t0.Fank_out = t3.Fank_out;
                        }
                        else
                        {
                            t0.xrom[0].chrom = t1.xrom[0].chrom;
                            t0.xrom[1].chrom = t1.xrom[1].chrom;

                            f0 = t0.Fank_out = t1.Fank_out;
                        }
                    }
                }
                else
                {
                    t2.xrom[0].chrom = t0.xrom[0].chrom - alf;
                    t2.xrom[1].chrom = t0.xrom[1].chrom;
                    t2.Fank_out      = F.Fank_(t2, Aa);
                    if (Math.Abs(t2.Fank_out) < Math.Abs(t0.Fank_out))
                    {
                        t3.xrom[0].chrom = t0.xrom[0].chrom - alf;
                        t3.xrom[1].chrom = t0.xrom[1].chrom + alf;
                        t3.Fank_out      = F.Fank_(t3, Aa);

                        if (Math.Abs(t3.Fank_out) < Math.Abs(t2.Fank_out))
                        {
                            t0.xrom[0].chrom = t3.xrom[0].chrom;
                            t0.xrom[1].chrom = t3.xrom[1].chrom;

                            f0 = t0.Fank_out = t3.Fank_out;
                        }
                        else
                        {
                            t4.xrom[0].chrom = t0.xrom[0].chrom - alf;
                            t4.xrom[1].chrom = t0.xrom[1].chrom - alf;
                            t4.Fank_out      = F.Fank_(t4, Aa);

                            if (Math.Abs(t4.Fank_out) < Math.Abs(t2.Fank_out))
                            {
                                t0.xrom[0].chrom = t4.xrom[0].chrom;
                                t0.xrom[1].chrom = t4.xrom[1].chrom;

                                f0 = t0.Fank_out = t4.Fank_out;
                            }
                            else
                            {
                                t0.xrom[0].chrom = t2.xrom[0].chrom;
                                t0.xrom[1].chrom = t2.xrom[1].chrom;

                                f0 = t0.Fank_out = t2.Fank_out;
                            }
                        }
                    }    // 2+
                    else //2-
                    {
                        t3.xrom[0].chrom = t0.xrom[0].chrom;
                        t3.xrom[1].chrom = t0.xrom[1].chrom + alf;
                        t3.Fank_out      = F.Fank_(t3, Aa);
                        if (Math.Abs(t3.Fank_out) < Math.Abs(t0.Fank_out))
                        {
                            t0.xrom[0].chrom = t3.xrom[0].chrom;
                            t0.xrom[1].chrom = t3.xrom[1].chrom;

                            f0 = t0.Fank_out = t3.Fank_out;
                        }
                        else
                        {
                            t4.xrom[0].chrom = t0.xrom[0].chrom;
                            t4.xrom[1].chrom = t0.xrom[1].chrom - alf;
                            t4.Fank_out      = F.Fank_(t4, Aa);
                            if (Math.Abs(t4.Fank_out) < Math.Abs(t0.Fank_out))
                            {
                                t0.xrom[0].chrom = t4.xrom[0].chrom;
                                t0.xrom[1].chrom = t4.xrom[1].chrom;

                                f0 = t0.Fank_out = t4.Fank_out;
                            }
                            else
                            {
                                pr = 1; //
                            }
                        }
                    }
                    //els -1
                }

                if (pr == 1)
                {
                    alf = alf / 10;
                    if (alf > 0.000001)
                    {
                        pr = 0;
                    }
                }
            }

            MtextBox.Text = MtextBox.Text + Environment.NewLine + Environment.NewLine + "Метод ХД ";
            //MtextBox.Text = MtextBox.Text + Environment.NewLine + f0 + Environment.NewLine;

            MtextBox.Text = MtextBox.Text + Environment.NewLine +
                            t0.xrom[0].Prin() + Environment.NewLine + t0.xrom[1].Prin() + Environment.NewLine +
                            t0.xrom[2].Prin() + Environment.NewLine + t0.xrom[3].Prin() + Environment.NewLine;


            double Xx = t0.xrom[0].chrom * Aa.y1 * Aa.y1 + t0.xrom[1].chrom * Aa.y1 + t0.xrom[2].chrom;

            MtextBox.Text = MtextBox.Text +
                            "a1*" + Aa.y1 * Aa.y1 + "+a2*" + Aa.y1 + "+ a3=0" + Environment.NewLine +
                            t0.xrom[0].chrom + " * " + Aa.y1 * Aa.y1 + " + " + t0.xrom[1].chrom + " * " + Aa.y1 + " + " + t0.xrom[2].chrom + " = " + Environment.NewLine +
                            // x1 + " * " + Aa.y1 * Aa.y1 + " + " + x2 + " * " + Aa.y1 + " + " + x3 + " = " + Environment.NewLine +
                            Xx;
        }