예제 #1
0
파일: Form1.cs 프로젝트: GiaiThuatGa/GA
        private void MaHoaToaDo(Tram tram)
        {
            string[] kinhdo = Convert.ToString(tram.Longitude).Split('.');
            int kd = Convert.ToInt32(kinhdo[1]);
            string bin = Convert.ToString(kd, 2).PadLeft(17, '0');
            tram.setLongitude(bin);

            string[] vido = Convert.ToString(tram.Latitude).Split('.');
            int vd = Convert.ToInt32(vido[1]);
            bin = Convert.ToString(vd, 2).PadLeft(17, '0');
            tram.setLatitude(bin);
        } 
예제 #2
0
파일: Form1.cs 프로젝트: GiaiThuatGa/GA
        private void btnTimTram_Click(object sender, EventArgs e)
        {
            //Them khoang cach cua moi 2 tram trong quan the
            int n = trams.Count;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                {
                    if (i!=j)
                    {
                        Pair temp = new Pair();
                        temp.tram1 = trams[i];
                        temp.tram2 = trams[j];
                        double khoangCach = KhoangCachHaiTram(trams[i],trams[j]) ;
                        temp.KhoangCach = khoangCach;
                        D.Add(temp);
                    }
                }
            n = D.Count;
            for (int i = 0; i < n; i++)
            {
                if (D[i].KhoangCach > BanKinhPhu(D[i].tram1) + BanKinhPhu(D[i].tram2)){
                    Pair temp = new Pair();
                    temp.tram1 = D[i].tram1;
                    temp.tram2 = D[i].tram2;
                    DuavaoQuanThe(temp);//dua vao quan the cap tram thich nghi de lai ghep va dot bien
                }
            }

            //Ma hoa toa do cua cac cap trong quan the
            n = quanthe.Count;
            for(int i = 0; i<n; i++)
            {
                MaHoaToaDo(quanthe[i].tram1);
                MaHoaToaDo(quanthe[i].tram2);
            }

            Boolean[] daxet = new Boolean[n];
            for(int i = 0; i < n; i++)
            {
                daxet[i] = false;
            }

            Boolean daTimduocConTotnhat = false;

            for(int k = 1; k <= n; k++)
            {
                //Khoi tao id khong trung lap
                Random random = new Random();
                int id ;
                do
                {
                    id = random.Next(0,n-1);
                } while (daxet[id] == true);
                daxet[id] = true;

                Tram con = new Tram();
                List<Tram> _temp = new List<Tram>();
                Boolean[] pos = new Boolean[n];
                for (int i = 0; i < n; i++)
                {
                    pos[i] = false;
                }
                int dosaulap = 1;
                do
                {
                    _temp.Clear();
                    Tram cha = quanthe[id].tram1;
                    Tram me = quanthe[id].tram2;
                    string kd_nst_cha = cha.getLongitude();
                    string vd_nst_cha = cha.getLatitude();
                    string kd_nst_me = me.getLongitude();
                    string vd_nst_me = me.getLatitude();

                char temp;
                char[] temp_kd_cha = kd_nst_cha.ToCharArray();
                char[] temp_vd_cha = vd_nst_cha.ToCharArray();
                char[] temp_kd_me = kd_nst_me.ToCharArray();
                char[] temp_vd_me = vd_nst_me.ToCharArray();

                int dosaulap = 1;
                List<Tram> _temp = new List<Tram>();
                do
                {
                    _temp.Clear();
                    Random r = new Random();
                    int _pos;
                    do
                    {
                        _pos = r.Next(0, 16);
                    } while (pos[_pos] == true);
                    pos[_pos] = true;

                    for (int i = _pos; i < 17; i++)
                    {
                        temp = temp_kd_me[i];
                        temp_kd_me[i] = temp_kd_cha[i];
                        temp_kd_cha[i] = temp;

                        temp = temp_vd_me[i];
                        temp_vd_me[i] = temp_vd_cha[i];
                        temp_vd_cha[i] = temp;
                    }
                    kd_nst_cha = "";
                    vd_nst_cha = "";
                    kd_nst_me = "";
                    vd_nst_me = "";
                    for (int i = 0; i < 17; i++)
                    {
                        kd_nst_cha += temp_kd_cha[i].ToString();
                        vd_nst_cha += temp_vd_cha[i].ToString();
                        kd_nst_me += temp_kd_me[i].ToString();
                        vd_nst_me += temp_vd_me[i].ToString();
                    }

                    Tram tc = new Tram();
                    tc.DoCao = 27;
                    tc.setLatitude(vd_nst_cha);
                    tc.setLongitude(kd_nst_cha);
                    if (!_temp.Contains(tc)) _temp.Add(tc); //Them con vao

                    Tram tm = new Tram();
                    tm.DoCao = 27;
                    tm.setLatitude(vd_nst_me);
                    tm.setLongitude(kd_nst_me);
                    if (!_temp.Contains(tm)) _temp.Add(tm);

                    double pm = 0.01;
                    for (int i = 0; i < 17; i++)
                    {
                        double pi = r.NextDouble() * 0.019;
                        if (pi < pm)
                        {
                            temp_kd_cha[i] = (char)((1 - (int)(temp_kd_cha[i] - 48)) + 48);
                            temp_vd_cha[i] = (char)((1 - (int)(temp_vd_cha[i] - 48)) + 48);
                        }
                    }
                    for (int i = 0; i < 17; i++)
                    {
                        double pi = r.NextDouble() * 0.019;
                        if (pi < pm)
                        {
                            temp_kd_me[i] = (char)((1 - (int)(temp_kd_me[i] - 48)) + 48);
                            temp_vd_me[i] = (char)((1 - (int)(temp_vd_me[i] - 48)) + 48);
                        }
                    }

                    kd_nst_cha = "";
                    vd_nst_cha = "";
                    kd_nst_me = "";
                    vd_nst_me = "";
                    for (int i = 0; i < 17; i++)
                    {
                        kd_nst_cha += temp_kd_cha[i].ToString();
                        vd_nst_cha += temp_vd_cha[i].ToString();
                        kd_nst_me += temp_kd_me[i].ToString();
                        vd_nst_me += temp_vd_me[i].ToString();
                    }

                    Tram tc_dotbien = new Tram();
                    tc_dotbien.DoCao = 27;
                    tc_dotbien.setLatitude(vd_nst_cha);
                    tc_dotbien.setLongitude(kd_nst_cha);
                    if (!_temp.Contains(tc_dotbien)) _temp.Add(tc_dotbien);

                    Tram tm_dotbien = new Tram();
                    tm_dotbien.DoCao = 27;
                    tm_dotbien.setLatitude(vd_nst_me);
                    tm_dotbien.setLongitude(kd_nst_me);
                    if (!_temp.Contains(tm_dotbien)) _temp.Add(tm_dotbien);

                    daTimduocConTotnhat = DieuKienDung(_temp);
                    dosaulap++;

                } while (daTimduocConTotnhat==false && dosaulap <=17);
                if (daTimduocConTotnhat) return;
            }
            //xu ly mang tams
            int vt = 0;
            int m = tams.Count;
            for (int i = 1; i < n; i++)
            {
                if (tams[i].sotramtoidaphuduoc > tams[vt].sotramtoidaphuduoc) vt = i;
            }
            if(!trams.Contains(tams[vt])) trams.Add(tams[vt]);

        }