コード例 #1
0
        /// <summary>
        /// tester si un bloc appartient à la fenetre de recherche
        /// </summary>
        /// <param name="Br">bloc référence</param>
        /// <param name="Bc">bloc candidat</param>
        /// <returns>booleen qui indique si Bc appartient à la fenetre de Br</returns>
        public bool appartient_fen(bloc Br, bloc Bc)
        {
            bool b;

            if ((Bc.x >= Br.x - deplace_fen) && (Bc.x <= Br.x + deplace_fen))
            {
                if ((Bc.y >= Br.y - deplace_fen) && (Bc.y <= Br.y + deplace_fen))
                {
                    if (Bc.x + taille_bloc < wi && Bc.y + taille_bloc < he)
                    {
                        if (Bc.x >= 0 && Bc.y >= 0)
                        {
                            b = true;
                        }
                        else
                        {
                            b = false;
                        }
                    }
                    else
                    {
                        b = false;
                    }
                }
                else
                {
                    b = false;
                }
            }
            else
            {
                b = false;
            }
            return(b);
        }
コード例 #2
0
        public bloc algo_gen(bloc Br)
        {
            //bloc Br = new bloc();
            // bloc Br = (bloc)Bro;
            double[]    tab_sad = new double[nbchro];
            List <bloc> li_chro = new List <bloc>();
            bloc        result  = new bloc();
            int         ind     = 0;

            //double sad = 0;

            li_chro = initialisation(Br);

            for (int j = 0; j < nbgen; j++)
            {
                tab_sad = next_gen(Br, ref li_chro);
            }

            ind    = trouver_ind(tab_sad, tab_sad.Min());
            result = li_chro[ind];

            //resultfinal[indfinal] = result;
            //indfinal++;

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// critère de comparaison entre deux blocs
        /// </summary>
        /// <param name="bc">bloc candidat</param>
        /// <param name="br">bloc référence</param>
        /// <returns>l'erreur entre les deux blocs</returns>
        public double SAD(bloc bc, bloc br)
        {
            double    sad = 0;
            PixelData t1;
            PixelData t2;
            int       x1, x2, y1, y2;
            // PixelData t3 = new PixelData();
            //accés au pixel
            double val1, val2;

            for (int i = 0; i < taille_bloc; i++)
            {
                for (int j = 0; j < taille_bloc; j++)
                {
                    x1   = Math.Max(0, Math.Min(wi - 1, (br.x + i)));
                    y1   = Math.Max(0, Math.Min(he - 1, (br.y + j)));
                    x2   = Math.Max(0, Math.Min(wi - 1, (bc.x + i)));
                    y2   = Math.Max(0, Math.Min(he - 1, (bc.y + j)));
                    t1   = a.GetPixel(x1, y1);
                    t2   = b.GetPixel(x2, y2);
                    val1 = t1.blue * 0.1140 + t1.green * 0.5870 + t1.red * 0.2989;
                    val2 = t2.blue * 0.1140 + t2.green * 0.5870 + t2.red * 0.2989;

                    if (val2 - val1 < 0)
                    {
                        sad -= (val2 - val1);
                    }
                    else
                    {
                        sad += (val2 - val1);
                    }
                }
            }
            return(sad);
        }
コード例 #4
0
        public bool lst_contain(List <bloc> list, bloc b)
        {
            int  v  = 6;
            bool bo = false;
            int  i  = 0;

            while (i < list.Count && bo == false)
            {
                if (list[i].x == b.x && list[i].y == b.y)
                {
                    bo = true;
                }
                else
                {
                    if (b.x < list[i].x + v && b.x > list[i].x - v)
                    {
                        if (b.y < list[i].y + v && b.y > list[i].y - v)
                        {
                            bo = true;
                        }
                        else
                        {
                            bo = false;
                        }
                    }
                    else
                    {
                        bo = false;
                    }
                }
                i++;
            }
            return(bo);
        }
コード例 #5
0
        public List <bloc> analyse(List <bloc> liste_bloc1, List <bloc> liste_bloc2)
        {
            bloc        rst      = new bloc();
            List <bloc> resultat = new List <bloc>();
            DateTime    start    = DateTime.Now;

            //  //MessageBox.Show(liste_bloc1.Count.ToString());
            a.LockBitmap();
            b.LockBitmap();

            zmp(ref liste_bloc1, ref liste_bloc2);
            resultfinal = new bloc[liste_bloc1.Count];
            object ob = new object();

            AForge.Parallel.For(0, liste_bloc1.Count, delegate(int i)
                                //for (int i = 0;i< liste_bloc1.Count; i++)
            {
                //tabth[i].Start(liste_bloc1[i]);
                rst = algo_gen(liste_bloc1[i]);
                if (lst_contain(liste_bloc1, rst) == true)
                {
                    lock (ob)
                    {
                        if ((rst.x != liste_bloc1[i].x) || (rst.y != liste_bloc1[i].y))
                        {
                            resultat.Add(rst);
                        }
                    }
                }
            }
                                );
            a.UnlockBitmap();
            b.UnlockBitmap();
            return(resultat);
        }
コード例 #6
0
        private List <abeille> disperser_abeilles(bloc br, int nbr, int fen)
        {
            List <abeille> tmp  = new List <abeille>();
            Random         x    = new Random(DateTime.Now.Millisecond);
            abeille        ab   = new abeille();
            int            stop = 0;

            lock (proteger_abeille)
                for (int i = 0; i < nbr; i++)
                {
                    ab.distance.x = Math.Max(0, Math.Min(w, x.Next(br.x - fen, br.x + fen)));
                    ab.distance.y = Math.Max(0, Math.Min(h, x.Next(br.y - fen, br.y + fen)));
                    while (tmp.Contains(ab))
                    {
                        ab.distance.x = Math.Max(0, Math.Min(w, x.Next(br.x - fen, br.x + fen)));
                        ab.distance.y = Math.Max(0, Math.Min(h, x.Next(br.y - fen, br.y + fen)));
                        //MessageBox.Show(ab.distance.x.ToString() + "," + br.x.ToString());

                        stop++;
                        if (stop >= 20)
                        {
                            break;
                        }
                    }
                    stop = 0;

                    tmp.Add(ab);
                }
            return(tmp);
        }
コード例 #7
0
        /* dans cette fonction on initialise une liste de particule par rapport à un bloc donné
         * et on calcule le critere SAD entre ce bloc et le bloc généré aleatoirement*/
        private List <particule> initialiser(bloc BR)
        {
            Random           x     = new Random();
            List <particule> liste = new List <particule>();
            particule        tmp   = new particule();
            int cpt   = 0;
            int arret = 0;

            while (cpt < nbpar)
            {
                tmp.position.x = Math.Max(0, Math.Min(x.Next(BR.x - fenetre / 2, BR.x + fenetre / 2), w));
                tmp.position.y = Math.Max(0, Math.Min(x.Next(BR.y - fenetre / 2, BR.y + fenetre / 2), h));

                while (liste.Contains(tmp))
                {
                    tmp.position.x = Math.Max(0, Math.Min(x.Next(BR.x - fenetre / 2, BR.x + fenetre / 2), w));
                    tmp.position.y = Math.Max(0, Math.Min(x.Next(BR.y - fenetre / 2, BR.y + fenetre / 2), h));
                    arret++;
                    if (arret > 200)
                    {
                        break;
                    }
                }
                tmp.qualite = SAD(tmp.position, BR);
                // MessageBox.Show(tmp.position.x.ToString() + "," + tmp.position.y.ToString());

                liste.Add(tmp);
                cpt++;
            }
            //MessageBox.Show(cpt.ToString());
            return(liste);
        }
コード例 #8
0
        public double[] next_gen(bloc Br, ref List <bloc> li_chro)
        {
            List <bloc> temp = new List <bloc>();

            temp = li_chro.ToList();
            double[] tab_sad = new double[li_chro.Count];
            double   sadm    = 0;

            for (int i = 0; i < li_chro.Count; i++)
            // System.Threading.Tasks.Parallel.For(0, temp.Count, delegate(int i)
            {
                tab_sad[i] = SAD(li_chro[i], Br);
            }
            sadm = tab_sad.Average();
            //appliquer le parrallelisme ici
            //for(int i=0;i<li_chro.Count;i++)
            System.Threading.Tasks.Parallel.For(0, temp.Count, delegate(int i)
            {
                if (tab_sad[i] >= sadm)
                {
                    ////MessageBox.Show(i.ToString());
                    temp[i] = mutation(Br);
                }
            });
            li_chro = temp;
            return(tab_sad);
        }
コード例 #9
0
        public bloc algo_gen(bloc Br)
        {
            //bloc Br = new bloc();
               // bloc Br = (bloc)Bro;
            double[] tab_sad = new double[nbchro];
            List<bloc> li_chro = new List<bloc>();
            bloc result = new bloc();
            int ind = 0;
            //double sad = 0;

                li_chro = initialisation(Br);

                     for (int j = 0; j < nbgen; j++)
                    {
                        tab_sad = next_gen(Br, ref li_chro);
                    }

                ind = trouver_ind(tab_sad, tab_sad.Min());
                result = li_chro[ind];

                    //resultfinal[indfinal] = result;
                    //indfinal++;

            return result;
        }
コード例 #10
0
        public List <bloc> analyse(List <bloc> liste_bloc1, List <bloc> liste_bloc2)
        {
            a = new UnsafeBitmap(image1);
            b = new UnsafeBitmap(image2);
            bloc        rst      = new bloc();
            List <bloc> resultat = new List <bloc>();
            //   List<bloc> liste_bloc1= new List<bloc>();
            //   List<bloc> liste_bloc2= new List<bloc>();
            //   liste_bloc1= decoupage(image1);
            //  liste_bloc2= decoupage(image2);
            DateTime start = DateTime.Now;

            //  MessageBox.Show(liste_bloc1.Count.ToString());
            a.LockBitmap();
            b.LockBitmap();
            object o = new object();

            zmp(ref liste_bloc1, ref liste_bloc2);

            //   MessageBox.Show(liste_bloc1.Count.ToString());
            AForge.Parallel.For(0, liste_bloc1.Count, delegate(int i)
                                //   for(int i=0;i<liste_bloc1.Count;i++)
            {
                rst = diamond_search(liste_bloc1[i]);

                if (lst_contain(liste_bloc1, rst) == true)
                {
                    lock (o)
                    {
                        if ((rst.x != liste_bloc1[i].x) || (rst.y != liste_bloc1[i].y)) //MessageBox.Show("kifkif");
                        {
                            resultat.Add(rst);
                        }
                        //            sad_moyen += SAD(liste_bloc1[i], rst);
                        //          compteur_sad++;
                        // MessageBox.Show(SAD(liste_bloc1[i],rst).ToString());
                        //sad_moyen.Add(SAD(liste_bloc1[i], rst));
                    }
                }
            });


            //  sad_moyen = sad_moyen / compteur_sad;
            //);
            a.UnlockBitmap();
            b.UnlockBitmap();
            //fic_ecr.Close();
            //sady.Close();
            //sadt.Close();
            // MessageBox.Show(sad_moyen.Average().ToString());
            TimeSpan dur = DateTime.Now - start;

            //  MessageBox.Show(dur.ToString());
            return(resultat);
        }
コード例 #11
0
        private void mise_a_jour(ref List <abeille> l, bloc Br)
        {
            abeille ab = new abeille();

            for (int i = 0; i < l.Count; i++)
            {
                ab.distance  = l[i].distance;
                ab.direction = l[i].direction;
                ab.qualite   = SAD(l[i].distance, Br);
                l[i]         = ab;
            }
        }
コード例 #12
0
        public List <bloc> analyse(List <bloc> elts_candidats1, List <bloc> elts_candidats2)
        {
            List <bloc> temp  = new List <bloc>();
            bloc        rst   = new bloc();
            abeille     bctmp = new abeille();

            a.LockBitmap();
            b.LockBitmap();

            zmp(ref elts_candidats1, ref elts_candidats2);
            // MessageBox.Show("après zmp: " + elts_candidats1.Count.ToString());
            // verif = elts_candidats1;
            //  for (int i = 0; i < elts_candidats1.Count ; i++)
            //{
            object o = new object();

            AForge.Parallel.For(0, elts_candidats1.Count, delegate(int i)
                                //   for(int i=0; i< elts_candidats1.Count;i++)
            {
                //zm.SetPixel(elts_candidats1[i].x, elts_candidats1[i].y, Color.Red);
                bctmp = operation_abeille(elts_candidats1[i]);
                rst   = bctmp.distance;

                if (lst_contain(elts_candidats1, bctmp.distance))
                {
                    lock (o)
                    {
                        if (!((rst.x == elts_candidats1[i].x) && (rst.y == elts_candidats1[i].y)))
                        {
                            temp.Add(bctmp.distance);
                            //sad_moyen += bctmp.qualite;
                            //compteur_sad++;
                            //li_sad.Add(bctmp.qualite);
                        }
                    }


                    // MessageBox.Show(bctmp.qualite.ToString());
                }
            }
                                );
            //     sad_moyen = sad_moyen / compteur_sad;
            //s.ReadValue();

            //   MessageBox.Show(compteur_sad.ToString());
            //}
            a.UnlockBitmap();
            b.UnlockBitmap();

            return(temp);
        }
コード例 #13
0
        public bloc mutation(bloc Br, bloc B)
        {
            bloc chro_mute;

            chro_mute.x = -1;
            chro_mute.y = -1;
            string tmp1, tmp2;

            tmp1 = Convert.ToString(B.x, 2);
            tmp2 = Convert.ToString(B.y, 2);
            string sauv1 = tmp1;
            string sauv2 = tmp2;
            //intervertir un bit de tmp1 et un bit de tmp2
            StringBuilder str1 = new StringBuilder(tmp1);
            StringBuilder str2 = new StringBuilder(tmp2);
            char          c;
            Random        rd = new Random(DateTime.Now.Millisecond);

            while (!appartient_fen(Br, chro_mute))
            {
                str1 = new StringBuilder(sauv1);
                str2 = new StringBuilder(sauv2);
                int position = rd.Next(0, str1.Length - 1);
                if (str1[position] == '0')
                {
                    c = '1';
                }
                else
                {
                    c = '0';
                }
                str1[position] = c;
                position       = rd.Next(0, str2.Length - 1);
                if (str2[position] == '0')
                {
                    c = '1';
                }
                else
                {
                    c = '0';
                }
                str2[position] = c;
                tmp1           = str1.ToString();
                tmp2           = str2.ToString();
                //reconversion en entier
                chro_mute.x = Convert.ToInt32(tmp1, 2);
                chro_mute.y = Convert.ToInt32(tmp2, 2);
            }
            return(chro_mute);
        }
コード例 #14
0
        public bloc mutation(bloc Br)
        {
            Random rd   = new Random();
            bloc   alea = new bloc();

            alea.x = -1;
            alea.y = -1;
            while (!appartient_fen(Br, alea))
            {
                alea.x = rd.Next(Br.x - deplace_fen, Br.x + deplace_fen);
                alea.y = rd.Next(Br.y - deplace_fen, Br.y + deplace_fen);
            }
            return(alea);
        }
コード例 #15
0
        public List<bloc> analyse(List<bloc> elts_candidats1, List<bloc> elts_candidats2)
        {
            List<bloc> temp = new List<bloc>();
            bloc rst = new bloc();
            abeille bctmp = new abeille();
            a.LockBitmap();
            b.LockBitmap();

            zmp(ref elts_candidats1, ref elts_candidats2);
               // MessageBox.Show("après zmp: " + elts_candidats1.Count.ToString());
               // verif = elts_candidats1;
              //  for (int i = 0; i < elts_candidats1.Count ; i++)
            //{
            object o = new object();
               AForge.Parallel.For(0, elts_candidats1.Count, delegate(int i)
             //   for(int i=0; i< elts_candidats1.Count;i++)
            {
                //zm.SetPixel(elts_candidats1[i].x, elts_candidats1[i].y, Color.Red);
                bctmp = operation_abeille(elts_candidats1[i]);
                rst = bctmp.distance;

                if (lst_contain(elts_candidats1, bctmp.distance))
                {
                    lock (o)
                    {
                        if (!((rst.x == elts_candidats1[i].x) && (rst.y == elts_candidats1[i].y)))
                        {
                            temp.Add(bctmp.distance);
                            //sad_moyen += bctmp.qualite;
                            //compteur_sad++;
                            //li_sad.Add(bctmp.qualite);

                        }

                    }

                   // MessageBox.Show(bctmp.qualite.ToString());
                }
            }
              );
               //     sad_moyen = sad_moyen / compteur_sad;
            //s.ReadValue();

             //   MessageBox.Show(compteur_sad.ToString());
            //}
            a.UnlockBitmap();
            b.UnlockBitmap();

            return temp;
        }
コード例 #16
0
        private bloc opt_pso(bloc b, List <particule> pbest)
        {
            List <particule> li_par = new List <particule>();

            li_par = initialiser(b);
            System.Threading.Tasks.Parallel.For(0, nbgen, delegate(int i)
                                                //for (int i = 0; i < nbgen; i++)
            {
                mise_a_jour_pbset_gbset(li_par, ref pbest);

                calcule_nouvelle_position(ref li_par, b, pbest);
            }
                                                );
            mise_a_jour_pbset_gbset(li_par, ref pbest);
            return(gbest.position);
        }
コード例 #17
0
        public List<bloc> analyse(List<bloc> liste_bloc1,List<bloc>liste_bloc2)
        {
            a= new UnsafeBitmap(image1);
            b= new UnsafeBitmap(image2);
            bloc rst= new bloc();
            List<bloc> resultat= new List<bloc>();
             //   List<bloc> liste_bloc1= new List<bloc>();
             //   List<bloc> liste_bloc2= new List<bloc>();
             //   liste_bloc1= decoupage(image1);
              //  liste_bloc2= decoupage(image2);
            DateTime start = DateTime.Now;
              //  MessageBox.Show(liste_bloc1.Count.ToString());
            a.LockBitmap();
            b.LockBitmap();
            object o = new object();
            zmp(ref liste_bloc1,ref liste_bloc2);

             //   MessageBox.Show(liste_bloc1.Count.ToString());
            AForge.Parallel.For(0, liste_bloc1.Count, delegate(int i)
            //   for(int i=0;i<liste_bloc1.Count;i++)
            {
                rst = diamond_search(liste_bloc1[i]);

                if (lst_contain(liste_bloc1, rst) == true)
                    lock (o)
                    {
                        if ((rst.x != liste_bloc1[i].x) || (rst.y != liste_bloc1[i].y)) //MessageBox.Show("kifkif");
                            resultat.Add(rst);
            //            sad_moyen += SAD(liste_bloc1[i], rst);
              //          compteur_sad++;
                        // MessageBox.Show(SAD(liste_bloc1[i],rst).ToString());
                        //sad_moyen.Add(SAD(liste_bloc1[i], rst));
                    }
            });

              //  sad_moyen = sad_moyen / compteur_sad;
            //);
            a.UnlockBitmap();
            b.UnlockBitmap();
            //fic_ecr.Close();
            //sady.Close();
            //sadt.Close();
               // MessageBox.Show(sad_moyen.Average().ToString());
            TimeSpan dur = DateTime.Now - start;
              //  MessageBox.Show(dur.ToString());
               return resultat;
        }
コード例 #18
0
        /// <summary>
        /// constuire une liste de blocs selon le modèle LDSP
        /// </summary>
        /// <param name="Br">le bloc reference</param>
        /// <param name="B">le bloc central courant</param>
        /// <returns></returns>
        public bloc[] SDSP(bloc Br, bloc B)
        {
            int cpt = 0;

            bloc[] tab_sdsp = new bloc[5];
            for (int k = 0; k < 5; k++)
            {
                tab_sdsp[k].x = -1;
                tab_sdsp[k].y = -1;
            }

            //  List<bloc> lis_sdsp = new List<bloc>();
            bloc tmp;

            tab_sdsp[cpt] = B;
            cpt++;
            // lis_sdsp.Add(B);
            tmp.x = B.x + 1;
            tmp.y = B.y;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_sdsp.Add(tmp);*/
                tab_sdsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x - 1;
            // if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
            tmp.y = B.y;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_sdsp.Add(tmp);*/
                tab_sdsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x;
            tmp.y = B.y + 1;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_sdsp.Add(tmp);*/
                tab_sdsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x;
            tmp.y = B.y - 1;
            //  if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
            if (appartient_fen(Br, tmp) == true)
            { /* lis_sdsp.Add(tmp); */
                tab_sdsp[cpt] = tmp; cpt++;
            }
            // return lis_sdsp;
            return(tab_sdsp);
        }
コード例 #19
0
        public List <bloc> initialisation(bloc Br)
        {
            List <bloc> li_chro = new List <bloc>();
            bloc        chro    = new bloc();
            Random      rn      = new Random(DateTime.Now.Millisecond);

            while (li_chro.Count < nbchro)
            {
                //generer aléatoirement des blocs
                chro.x = rn.Next(Br.x - deplace_fen, Br.x + deplace_fen);
                chro.y = rn.Next(Br.y - deplace_fen, Br.y + deplace_fen);
                if (appartient_fen(Br, chro))
                {
                    li_chro.Add(chro);
                }
            }


            ////MessageBox.Show(li_chro.Count.ToString());
            return(li_chro);
        }
コード例 #20
0
        public List <bloc> analyser(List <bloc> li, List <bloc> li2)
        {
            // int compteur_sad = 0;
            List <bloc> res = new List <bloc>();
            bloc        tmp = new bloc();
            object      ob  = new object();

            a.LockBitmap();
            b.LockBitmap();
            zmp(ref li, ref li2);
            //MessageBox.Show(li.Count.ToString());
            // for (int i = 0; i < li.Count; i++)
            AForge.Parallel.For(0, li.Count, delegate(int i)
                                //for(int i=0;i<li.Count;i++)
            {
                // pbest.Clear();
                List <particule> pbest = new List <particule>();
                initialiser_gbset_pbest(ref pbest);
                // MessageBox.Show(pbest.Count.ToString());
                tmp = opt_pso(li[i], pbest);
                // MessageBox.Show(tmp.x.ToString() + "," + tmp.y.ToString());
                if (!lst_contain(li, tmp))
                {
                    lock (ob)
                    {
                        res.Add(tmp);
                        //sad_moyen += SAD(li[i],tmp);
                        //compteur_sad++;
                    }
                }
            }
                                );


            //  sad_moyen = sad_moyen / compteur_sad;
            b.UnlockBitmap();
            a.UnlockBitmap();

            return(res);
        }
コード例 #21
0
 /// <summary>
 /// tester si un bloc appartient à la fenetre de recherche
 /// </summary>
 /// <param name="Br">bloc référence</param>
 /// <param name="Bc">bloc candidat</param>
 /// <returns>booleen qui indique si Bc appartient à la fenetre de Br</returns>
 public bool appartient_fen(bloc Br, bloc Bc)
 {
     bool b;
     if ((Bc.x >= Br.x - deplace_fen) && (Bc.x <= Br.x + deplace_fen))
     {
         if ((Bc.y >= Br.y - deplace_fen) && (Bc.y <= Br.y + deplace_fen))
         {
             if (Bc.x + taille_bloc < wi && Bc.y + taille_bloc < he)
             {
                 if (Bc.x >= 0 && Bc.y >= 0)
                 {
                     b = true;
                 }
                 else b = false;
             }
             else b = false;
         }
         else b = false;
     }
     else b = false;
     return b;
 }
コード例 #22
0
        /// <summary>
        /// découper l'image en blocs de taille fixe
        /// </summary>
        /// <param name="im">l'image</param>
        /// <returns>une liste de coordonnées de blocs</returns>
        public static List <bloc> decoupage(Bitmap im)
        {
            List <bloc> liste = new List <bloc>();
            bloc        tmp   = new bloc();

            for (int i = 0; i < wi; i++)
            {
                for (int j = 0; j < he; j++)
                {
                    tmp   = new bloc();
                    tmp.x = i;
                    tmp.y = j;
                    if (i + taille_bloc < wi && j + taille_bloc < he)
                    {
                        liste.Add(tmp);
                    }
                    j = j + taille_bloc - 1;
                }
                i = i + taille_bloc - 1;
            }
            return(liste);
        }
コード例 #23
0
        /// <summary>
        /// découper l'image en blocs de taille fixe
        /// </summary>
        /// <param name="im">l'image</param>
        /// <returns>une liste de coordonnées de blocs</returns>
        public static List<bloc> decoupage(Bitmap im)
        {
            List<bloc> liste = new List<bloc>();
            bloc tmp = new bloc();
            for (int i = 0; i < wi; i++)
            {
                for (int j = 0; j < he; j++)
                {
                    tmp = new bloc();
                    tmp.x = i;
                    tmp.y = j;
                    if (i + taille_bloc < wi && j + taille_bloc < he)
                    {

                        liste.Add(tmp);
                    }
                    j = j + taille_bloc - 1;
                }
                i = i + taille_bloc - 1;
            }
            return liste;
        }
コード例 #24
0
        /*cette fonction calcule les nouvelles positions a partir du gbest et le pbest
         * et mis à jour les SAD de ces nlles positions*/
        private void calcule_nouvelle_position(ref List <particule> l, bloc br, List <particule> pbest)
        {
            //Random x = new Random();
            particule tmp = new particule();
            bloc      bl = new bloc();
            int       deplacement, nouvelle_pos;
            double    s;

            for (int i = 0; i < l.Count; i++)
            //  AForge.Parallel.For(0, l.Count, delegate(int i)
            {
                bl = l[i].position;
                // deplacement =(int)(2 * x.NextDouble() * (pbest[i].position.x - l[i].position.x) + 2 * x.NextDouble() * (pbest[i].position.x * gbest.position.x - l[i].position.x));
                deplacement = (int)(c1 * bl.x + c_max * (pbest[i].position.x - bl.x - 1) + c_max * (gbest.position.x - bl.x - 1));

                //bl.x= bl.x + deplacement;
                nouvelle_pos = Math.Min(l[i].position.x + fenetre / 2, bl.x + deplacement);
                nouvelle_pos = Math.Max(nouvelle_pos, l[i].position.x - fenetre / 2);
                nouvelle_pos = Math.Max(0, Math.Min(nouvelle_pos, w));
                bl.x         = nouvelle_pos;
                //deplacement = (int)(2 * x.NextDouble() * (pbest[i].position.y - l[i].position.y) + 2 * x.NextDouble() * (pbest[i].position.y * gbest.position.y - l[i].position.y));

                deplacement = (int)(c1 * bl.y + c_max * (pbest[i].position.y - bl.y - 1) + c_max * (gbest.position.y - bl.y - 1));

                //bl.y = bl.y + deplacement;
                //bl.y=Math.Max(0, Math.Min(bl.y + deplacement, h));
                nouvelle_pos = Math.Min(l[i].position.y + fenetre / 2, bl.y + deplacement);
                nouvelle_pos = Math.Max(nouvelle_pos, l[i].position.y - fenetre / 2);
                nouvelle_pos = Math.Max(0, Math.Min(nouvelle_pos, h));
                bl.y         = nouvelle_pos;
                //   MessageBox.Show(bl.x.ToString()+","+bl.y.ToString());
                s            = SAD(bl, br);
                tmp.position = bl;
                tmp.qualite  = s;
                l[i]         = tmp;
                //  MessageBox.Show(bl.x.ToString()+","+bl.y.ToString());
            }
            //);
        }
コード例 #25
0
        public List<bloc> analyser(List<bloc> li,List<bloc> li2)
        {
            // int compteur_sad = 0;
            List<bloc> res = new List<bloc>();
            bloc tmp = new bloc();
            object ob = new object();
            a.LockBitmap();
            b.LockBitmap();
            zmp(ref li,ref li2);
            //MessageBox.Show(li.Count.ToString());
               // for (int i = 0; i < li.Count; i++)
            AForge.Parallel.For(0, li.Count, delegate(int i)
            //for(int i=0;i<li.Count;i++)
            {
               // pbest.Clear();
                List<particule> pbest = new List<particule>();
               initialiser_gbset_pbest(ref pbest);
                // MessageBox.Show(pbest.Count.ToString());
                tmp = opt_pso(li[i],pbest);
                // MessageBox.Show(tmp.x.ToString() + "," + tmp.y.ToString());
                if (!lst_contain(li, tmp))
                    lock (ob)
                    {
                        res.Add(tmp);
                        //sad_moyen += SAD(li[i],tmp);
                        //compteur_sad++;
                    }
            }
            );

              //  sad_moyen = sad_moyen / compteur_sad;
            b.UnlockBitmap();
            a.UnlockBitmap();

            return res;
        }
コード例 #26
0
        public unsafe bloc diamond_search(bloc Br)
        {
            bloc Bcm =  Br;
            bloc B;
              //  bool b=false;
               // List<bloc> liste_ldsp;
               // List<bloc> liste_sdsp;
            bloc[] table_ldsp = new bloc[9];
            bloc[] table_sdsp = new bloc[5];
            bloc[] obj = new bloc[2];
            obj[0] = Br;
            double treshold=10;

            List<bloc> deja_visite = new List<bloc>();
            double[] sad_visite = new double[225];
            List<bloc> liste_alea=new List<bloc>();
            Random rndm = new Random(DateTime.Now.Millisecond);

            int ind;
            B.x = -1;
            B.y = -1;
            double comp=0;
            int cpt = 0;
            double min = int.MaxValue;
            double min1 = int.MaxValue;
               // liste_Bcm.Add(Bcm);
            while (((Bcm.x != B.x) ||(Bcm.y!=B.y)) && cpt<10)
            {
                int i = 0;
                cpt++;
                B = Bcm;
                liste_alea.Clear();
                table_ldsp = LDSP(Br, B);
                if (table_ldsp[0].x>0 )
                {
                    while(i<9 && table_ldsp[i].x>0  )
                    {
                        comp = SAD(table_ldsp[i], Br);
                        if (comp <= treshold)
                        {
                            return table_ldsp[i];
                        }

                        if (comp < min)
                        {
                            Bcm = table_ldsp[i];
                            min = comp;
                        }
                        else
                        {

                            if ((comp == min  && i != 0) )
                            {
                                liste_alea.Add(table_ldsp[i]); min1 = min;
                            }

                        }
                        i++;

                    }
                    if (liste_alea.Count > 0 && min1<=min )
                    {

                            ind = rndm.Next(liste_alea.Count);
                          //  fic_ecr.WriteLine("\nl'indice aléatoirement choisi est:" + ind.ToString());
                                Bcm = liste_alea[ind];
                               // liste_Bcm.Add(Bcm);
                           // fic_ecr.WriteLine("\nle bloc alétoirement choisi qui sera le prochaine centre est" + Bcm.x.ToString() + "," + Bcm.y.ToString());

                        }

                }

            }
               // MessageBox.Show(cpt.ToString());
               // comp = SAD(B, Br);
               // MessageBox.Show(comp.ToString());
            //min = 69854631325;
            table_sdsp = SDSP(Br, B);
            int k = 0;
            //MessageBox.Show("bloc minimisant est:" + B.x.ToString() + "," + B.y.ToString()+"mtn calcul des blocs sdsp");
            if (table_sdsp[0].x >0)
            {
                while(k<5 && table_sdsp[k].x>0  )
                {
                    comp=SAD(table_sdsp[k],Br);
                    //MessageBox.Show("blocs" + liste_sdsp[i].x.ToString() + "," + liste_sdsp[i].y.ToString() + "et" + Br.x.ToString() + "," + Br.y.ToString() + "sad=" + comp.ToString());
                    if (comp < min) { Bcm = table_sdsp[k]; min = comp;/* fic_ecr.WriteLine("\ncomp<min dans sdsp, Bcm=" + Bcm.x.ToString() + "," + Bcm.y.ToString());*/ }
                  //  MessageBox.Show("le min ldsp est:" + Bcm.x.ToString() + "," + Bcm.y.ToString());
                    k++;
                }
            }

            //MessageBox.Show("!!!");
               // fic_ecr.WriteLine("le bloc résultat final est:" + Bcm.x.ToString() + "," + Bcm.y.ToString());

              //  MessageBox.Show("cpt=" + cpt.ToString());
               // bloc v;
               // v.x=-1; v.y=-1;
               // if (min < treshold) return Bcm;
               // else return v;
            return Bcm;
        }
コード例 #27
0
        private bloc opt_pso(bloc b, List<particule> pbest)
        {
            List<particule> li_par = new List<particule>();
            li_par = initialiser(b);
            System.Threading.Tasks.Parallel.For(0, nbgen, delegate(int i)
            //for (int i = 0; i < nbgen; i++)
            {
                mise_a_jour_pbset_gbset(li_par, ref pbest);

                calcule_nouvelle_position(ref li_par, b, pbest);
            }
            );
            mise_a_jour_pbset_gbset(li_par, ref pbest);
            return gbest.position;
        }
コード例 #28
0
 public bloc mutation(bloc Br)
 {
     Random rd = new Random();
     bloc alea = new bloc();
     alea.x = -1;
     alea.y = -1;
     while (!appartient_fen(Br, alea))
     {
         alea.x = rd.Next(Br.x - deplace_fen, Br.x + deplace_fen);
         alea.y = rd.Next(Br.y - deplace_fen, Br.y + deplace_fen);
     }
     return alea;
 }
コード例 #29
0
        public List<bloc> initialisation(bloc Br)
        {
            List<bloc> li_chro = new List<bloc>();
            bloc chro = new bloc();
            Random rn = new Random(DateTime.Now.Millisecond);
            while (li_chro.Count < nbchro)
            {
                //generer aléatoirement des blocs
                chro.x = rn.Next(Br.x-deplace_fen,Br.x+deplace_fen);
                chro.y = rn.Next(Br.y-deplace_fen,Br.y+deplace_fen);
                if (appartient_fen(Br, chro)) { li_chro.Add(chro);  }

            }

            ////MessageBox.Show(li_chro.Count.ToString());
            return li_chro;
        }
コード例 #30
0
        private void mise_a_jour(ref List<abeille> l, bloc Br)
        {
            abeille ab = new abeille();
            for (int i = 0; i < l.Count; i++)
            {

                ab.distance = l[i].distance;
                ab.direction = l[i].direction;
                ab.qualite = SAD(l[i].distance, Br);
                l[i] = ab;
            }
        }
コード例 #31
0
        public double[] next_gen(bloc Br, ref List<bloc> li_chro)
        {
            List<bloc> temp = new List<bloc>();
            temp= li_chro.ToList();
            double[] tab_sad = new double[li_chro.Count];
            double sadm = 0;
            for(int i=0;i<li_chro.Count;i++)
               // System.Threading.Tasks.Parallel.For(0, temp.Count, delegate(int i)
              {

                  tab_sad[i] = SAD(li_chro[i], Br);

              }
            sadm = tab_sad.Average();
            //appliquer le parrallelisme ici
              //for(int i=0;i<li_chro.Count;i++)
            System.Threading.Tasks.Parallel.For(0, temp.Count, delegate(int i)
              {
                  if (tab_sad[i] >= sadm)
                  {
                      ////MessageBox.Show(i.ToString());
                      temp[i] = mutation(Br);

                  }
              });
               li_chro = temp;
            return tab_sad;
        }
コード例 #32
0
        /* dans cette fonction on initialise une liste de particule par rapport à un bloc donné
         * et on calcule le critere SAD entre ce bloc et le bloc généré aleatoirement*/
        private List<particule> initialiser(bloc BR)
        {
            Random x = new Random();
            List<particule> liste = new List<particule>();
            particule tmp = new particule();
            int cpt =0;
            int arret = 0;
            while(cpt<nbpar)
            {
                tmp.position.x = Math.Max(0, Math.Min(x.Next(BR.x - fenetre / 2, BR.x + fenetre / 2), w));
                tmp.position.y = Math.Max(0, Math.Min(x.Next(BR.y - fenetre / 2, BR.y + fenetre / 2), h));

                while (liste.Contains(tmp))
                {
                    tmp.position.x = Math.Max(0, Math.Min(x.Next(BR.x - fenetre / 2, BR.x + fenetre / 2), w));
                    tmp.position.y = Math.Max(0, Math.Min(x.Next(BR.y - fenetre / 2, BR.y + fenetre / 2), h));
                    arret++;
                    if (arret > 200) break;
                }
                tmp.qualite = SAD(tmp.position, BR);
               // MessageBox.Show(tmp.position.x.ToString() + "," + tmp.position.y.ToString());

                liste.Add(tmp);
                cpt++;
            }
            //MessageBox.Show(cpt.ToString());
            return liste;
        }
コード例 #33
0
        public unsafe bloc diamond_search(bloc Br)
        {
            bloc Bcm = Br;
            bloc B;

            //  bool b=false;
            // List<bloc> liste_ldsp;
            // List<bloc> liste_sdsp;
            bloc[] table_ldsp = new bloc[9];
            bloc[] table_sdsp = new bloc[5];
            bloc[] obj        = new bloc[2];
            obj[0] = Br;
            double treshold = 10;

            List <bloc> deja_visite = new List <bloc>();

            double[]    sad_visite = new double[225];
            List <bloc> liste_alea = new List <bloc>();
            Random      rndm       = new Random(DateTime.Now.Millisecond);


            int ind;

            B.x = -1;
            B.y = -1;
            double comp = 0;
            int    cpt  = 0;
            double min  = int.MaxValue;
            double min1 = int.MaxValue;

            // liste_Bcm.Add(Bcm);
            while (((Bcm.x != B.x) || (Bcm.y != B.y)) && cpt < 10)
            {
                int i = 0;
                cpt++;
                B = Bcm;
                liste_alea.Clear();
                table_ldsp = LDSP(Br, B);
                if (table_ldsp[0].x > 0)
                {
                    while (i < 9 && table_ldsp[i].x > 0)
                    {
                        comp = SAD(table_ldsp[i], Br);
                        if (comp <= treshold)
                        {
                            return(table_ldsp[i]);
                        }

                        if (comp < min)
                        {
                            Bcm = table_ldsp[i];
                            min = comp;
                        }
                        else
                        {
                            if ((comp == min && i != 0))
                            {
                                liste_alea.Add(table_ldsp[i]); min1 = min;
                            }
                        }
                        i++;
                    }
                    if (liste_alea.Count > 0 && min1 <= min)
                    {
                        ind = rndm.Next(liste_alea.Count);
                        //  fic_ecr.WriteLine("\nl'indice aléatoirement choisi est:" + ind.ToString());
                        Bcm = liste_alea[ind];
                        // liste_Bcm.Add(Bcm);
                        // fic_ecr.WriteLine("\nle bloc alétoirement choisi qui sera le prochaine centre est" + Bcm.x.ToString() + "," + Bcm.y.ToString());
                    }
                }
            }
            // MessageBox.Show(cpt.ToString());
            // comp = SAD(B, Br);
            // MessageBox.Show(comp.ToString());
            //min = 69854631325;
            table_sdsp = SDSP(Br, B);
            int k = 0;

            //MessageBox.Show("bloc minimisant est:" + B.x.ToString() + "," + B.y.ToString()+"mtn calcul des blocs sdsp");
            if (table_sdsp[0].x > 0)
            {
                while (k < 5 && table_sdsp[k].x > 0)
                {
                    comp = SAD(table_sdsp[k], Br);
                    //MessageBox.Show("blocs" + liste_sdsp[i].x.ToString() + "," + liste_sdsp[i].y.ToString() + "et" + Br.x.ToString() + "," + Br.y.ToString() + "sad=" + comp.ToString());
                    if (comp < min)
                    {
                        Bcm = table_sdsp[k]; min = comp;              /* fic_ecr.WriteLine("\ncomp<min dans sdsp, Bcm=" + Bcm.x.ToString() + "," + Bcm.y.ToString());*/
                    }
                    //  MessageBox.Show("le min ldsp est:" + Bcm.x.ToString() + "," + Bcm.y.ToString());
                    k++;
                }
            }

            //MessageBox.Show("!!!");
            // fic_ecr.WriteLine("le bloc résultat final est:" + Bcm.x.ToString() + "," + Bcm.y.ToString());

            //  MessageBox.Show("cpt=" + cpt.ToString());
            // bloc v;
            // v.x=-1; v.y=-1;
            // if (min < treshold) return Bcm;
            // else return v;
            return(Bcm);
        }
コード例 #34
0
/*
 *     public int selection_meilleur_abeille(List<abeille> l)
 *      {
 *          int pos=0;
 *          for (int i = 0; i < l.Count-1; i++)
 *          {
 *              if (l[i].qualite != l[i + 1].qualite)
 *              {
 *                  pos = i;
 *                  break;
 *              }
 *          }
 *
 *          return pos;
 *      }
 */
        private abeille operation_abeille(bloc br)
        {
            abeille ab = new abeille();
            //int cpt = 0;
            int            d         = 0;
            int            voisinage = fenetre / 2;
            List <abeille> list_n    = new List <abeille>();
            List <abeille> list_m    = new List <abeille>();
            List <abeille> list_e1   = new List <abeille>();
            List <abeille> list_e2   = new List <abeille>();
            List <abeille> bcm       = new List <abeille>();

            list_n = disperser_abeilles(br, n, fenetre);
            //if (list_n.Count == 0) MessageBox.Show("vide");
            //else MessageBox.Show("non vide");
            mise_a_jour(ref list_n, br);
            trier_selon_qualite(ref list_n, list_n.Count - 1);
            list_m = recuperer_abeilles(0, m, list_n);

            //   do
            for (int cpt = 0; cpt < nbgen; cpt++)
            //  System.Threading.Tasks.Parallel.For(0, nbgen, delegate(int cpt)
            {
                //bcm.Clear();
                list_e1.Clear();
                list_e2.Clear();
                if (list_m.Count == 0)
                {
                    //  System.Windows.Forms.MessageBox.Show("list_m = 0");
                    break;
                }
                else
                {
                    object o1 = new object();
                    object o2 = new object();
                    System.Threading.Tasks.Parallel.For(0, e, delegate(int i)
                                                        //  for (int i = 0; i < e; i++)
                    {
                        //if (list_m.Count == 0) MessageBox.Show("azeakzekazejazeazeazezae");
                        lock (o1)
                            list_e1 = disperser_abeilles(list_m[i].distance, nbe, voisinage);
                        mise_a_jour(ref list_e1, br);
                        trier_selon_qualite(ref list_e1, list_e1.Count - 1);
                        //     d = selection_meilleur_abeille(list_e1);
                        // if(d!=0)  MessageBox.Show(d.ToString());
                        lock (o2)
                            bcm.Add(list_e1[d]);
                    }
                                                        );

                    System.Threading.Tasks.Parallel.For(e, m, delegate(int i)
                                                        // for (int i = e; i < m; i++)
                    {
                        lock (o2)
                            list_e2 = disperser_abeilles(list_m[i].distance, nba, voisinage);
                        mise_a_jour(ref list_e2, br);
                        trier_selon_qualite(ref list_e2, list_e2.Count - 1);
                        //   d = selection_meilleur_abeille(list_e2);
                        //       if (d != 0) MessageBox.Show(d.ToString());
                        lock (o1)
                            bcm.Add(list_e2[d]);
                    }
                                                        );
                    //
                    list_m = bcm.ToList();
                    // mise_a_jour(ref list_m, br);
                    //      cpt++;
                }
            } //while (cpt <= nbgen);
            //);
            // mise_a_jour(ref bcm,br);
            trier_selon_qualite(ref bcm, bcm.Count - 1);
            //d = selection_meilleur_abeille(bcm);
            //     if (d != 0) MessageBox.Show(d.ToString());
            ab = bcm[0];



            return(ab);
        }
コード例 #35
0
 /// <summary>
 /// tester si ke bloc condidat appartient à la fenetre de recherche dub bloc référence
 /// </summary>
 /// <param name="Br">Bloc référence</param>
 /// <param name="Bc">Bloc condidat</param>
 /// <returns>b un booleen</returns>
 /*
 public bool appartient_fen(bloc Br, bloc Bc)
 {
     bool b;
     if ((Bc.x >= Br.x - deplacement_fenetre) && (Bc.x <= Br.x + deplacement_fenetre))
     {
         if ((Bc.y >= Br.y - deplacement_fenetre) && (Bc.y <= Br.y + deplacement_fenetre))
         {
            if (Bc.x+taille_bloc < w && Bc.y+taille_bloc < h)
             {
                 if (Bc.x >= 0 && Bc.y >= 0)
                {
                     b = true;
                 }
                else b = false;
            }
             else b = false;
         }
         else b = false;
     }
     else b = false;
     return b;
 }
 */
 /// <summary>
 /// constuire une liste de blocs selon le modèle LDSP
 /// </summary>
 /// <param name="Br">bloc référence</param>
 /// <param name="B">le bloc central courant</param>
 /// <returns></returns>
 public bloc[] LDSP(bloc Br,bloc B)
 {
     // List<bloc> lis_ldsp = new List<bloc>();
     int cpt = 0;
     bloc[] tab_ldsp = new bloc[9];
     for (int k = 0; k < 9; k++)
     {
         tab_ldsp[k].x = -1;
         tab_ldsp[k].y = -1;
     }
     bloc tmp;
        // lis_ldsp.Add(B);
     tab_ldsp[cpt] = B;
     cpt++;
     tmp.x = B.x + 2;
     tmp.y = B.y;
     if (appartient_fen(Br, tmp) == true)
       //  MessageBox.Show(tmp.x.ToString()+","+tmp.y.ToString());
     { /*lis_ldsp.Add(tmp); */tab_ldsp[cpt] = tmp; cpt++; }
     tmp.x = B.x - 2;
     tmp.y = B.y;
     if (appartient_fen(Br, tmp) == true)
     { /*lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++;}
     tmp.x = B.x;
     tmp.y = B.y + 2;
     if (appartient_fen(Br, tmp) == true)
     { /* lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++; }
     tmp.x = B.x;
     tmp.y = B.y - 2;
     if (appartient_fen(Br, tmp) == true)
     { /* lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++; }
     tmp.x = B.x + 1;
     tmp.y = B.y + 1;
     if (appartient_fen(Br, tmp) == true)
     { /*lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++; }
     tmp.x = B.x - 1;
     //if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
     tmp.y = B.y - 1;
     if (appartient_fen(Br, tmp) == true)
     { /*lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++; }
     tmp.x = B.x + 1;
     tmp.y = B.y - 1;
     if (appartient_fen(Br, tmp) == true)
     { /*lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++; }
     tmp.x = B.x - 1;
       //  if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
     tmp.y = B.y + 1;
     if (appartient_fen(Br, tmp) == true)
     { /* lis_ldsp.Add(tmp);*/ tab_ldsp[cpt] = tmp; cpt++; }
        // return lis_ldsp;
     //    if (cpt == 1) MessageBox.Show("cpt =1");
     return tab_ldsp;
 }
コード例 #36
0
        /// <summary>
        /// constuire une liste de blocs selon le modèle LDSP
        /// </summary>
        /// <param name="Br">le bloc reference</param>
        /// <param name="B">le bloc central courant</param>
        /// <returns></returns>
        public bloc[] SDSP(bloc Br, bloc B)
        {
            int cpt = 0;
            bloc[] tab_sdsp = new bloc[5];
            for (int k = 0; k < 5; k++)
            {
                tab_sdsp[k].x = -1;
                tab_sdsp[k].y = -1;
            }

              //  List<bloc> lis_sdsp = new List<bloc>();
            bloc tmp;
            tab_sdsp[cpt] = B;
            cpt++;
               // lis_sdsp.Add(B);
            tmp.x = B.x + 1;
            tmp.y = B.y;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_sdsp.Add(tmp);*/ tab_sdsp[cpt] = tmp; cpt++; }
            tmp.x = B.x - 1;
               // if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
            tmp.y = B.y;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_sdsp.Add(tmp);*/ tab_sdsp[cpt] = tmp; cpt++; }
            tmp.x = B.x;
            tmp.y = B.y + 1;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_sdsp.Add(tmp);*/ tab_sdsp[cpt] = tmp; cpt++; }
            tmp.x = B.x;
            tmp.y = B.y - 1;
              //  if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
            if (appartient_fen(Br, tmp) == true)
            { /* lis_sdsp.Add(tmp); */tab_sdsp[cpt] = tmp; cpt++; }
               // return lis_sdsp;
            return tab_sdsp;
        }
コード例 #37
0
        private List<abeille> disperser_abeilles(bloc br, int nbr, int fen)
        {
            List<abeille> tmp = new List<abeille>();
            Random x = new Random(DateTime.Now.Millisecond);
            abeille ab = new abeille();
            int stop=0;
            lock(proteger_abeille)
            for (int i = 0; i < nbr; i++)
            {
                ab.distance.x =Math.Max(0,Math.Min(w, x.Next(br.x - fen , br.x + fen )));
                ab.distance.y =Math.Max(0,Math.Min(h, x.Next(br.y - fen , br.y + fen )));
                while (tmp.Contains(ab))
                {
                    ab.distance.x =Math.Max(0,Math.Min(w, x.Next(br.x - fen , br.x + fen )));
                    ab.distance.y = Math.Max(0, Math.Min(h, x.Next(br.y - fen , br.y + fen )));
                    //MessageBox.Show(ab.distance.x.ToString() + "," + br.x.ToString());

                        stop++;
                        if (stop >= 20)
                        {
                            break;
                        }
                    }
                stop = 0;

                tmp.Add(ab);
            }
            return tmp;
        }
コード例 #38
0
        public List<bloc> analyse(List<bloc> liste_bloc1, List<bloc> liste_bloc2)
        {
            bloc rst = new bloc();
            List<bloc> resultat = new List<bloc>();
            DateTime start = DateTime.Now;
            //  //MessageBox.Show(liste_bloc1.Count.ToString());
            a.LockBitmap();
            b.LockBitmap();

            zmp(ref liste_bloc1, ref liste_bloc2);
            resultfinal = new bloc[liste_bloc1.Count];
            object ob = new object();
                AForge.Parallel.For(0, liste_bloc1.Count, delegate(int i)
            //for (int i = 0;i< liste_bloc1.Count; i++)
             {
                 //tabth[i].Start(liste_bloc1[i]);
                 rst = algo_gen(liste_bloc1[i]);
                 if (lst_contain(liste_bloc1, rst) == true)
                     lock (ob)
                     {
                         if ((rst.x != liste_bloc1[i].x) || (rst.y != liste_bloc1[i].y))
                             resultat.Add(rst);
                     }

             }
            );
            a.UnlockBitmap();
            b.UnlockBitmap();
            return resultat;
        }
コード例 #39
0
        /*cette fonction calcule les nouvelles positions a partir du gbest et le pbest
         * et mis à jour les SAD de ces nlles positions*/
        private void calcule_nouvelle_position(ref List<particule> l, bloc br, List<particule> pbest)
        {
            //Random x = new Random();
            particule tmp = new particule();
            bloc bl = new bloc();
            int deplacement,nouvelle_pos;
            double s;
            for (int i = 0; i < l.Count; i++)
              //  AForge.Parallel.For(0, l.Count, delegate(int i)
            {
                 bl = l[i].position;
                // deplacement =(int)(2 * x.NextDouble() * (pbest[i].position.x - l[i].position.x) + 2 * x.NextDouble() * (pbest[i].position.x * gbest.position.x - l[i].position.x));
                     deplacement = (int)(c1 * bl.x + c_max * (pbest[i].position.x - bl.x - 1) + c_max * (gbest.position.x - bl.x - 1));

                //bl.x= bl.x + deplacement;
                nouvelle_pos = Math.Min(l[i].position.x + fenetre / 2, bl.x + deplacement);
                nouvelle_pos = Math.Max(nouvelle_pos, l[i].position.x - fenetre / 2);
                nouvelle_pos = Math.Max(0, Math.Min(nouvelle_pos, w));
                bl.x = nouvelle_pos;
                //deplacement = (int)(2 * x.NextDouble() * (pbest[i].position.y - l[i].position.y) + 2 * x.NextDouble() * (pbest[i].position.y * gbest.position.y - l[i].position.y));

                        deplacement = (int)(c1 * bl.y + c_max * (pbest[i].position.y - bl.y - 1) + c_max * (gbest.position.y - bl.y - 1));

                //bl.y = bl.y + deplacement;
                //bl.y=Math.Max(0, Math.Min(bl.y + deplacement, h));
                nouvelle_pos = Math.Min(l[i].position.y + fenetre / 2, bl.y + deplacement);
                nouvelle_pos = Math.Max(nouvelle_pos, l[i].position.y - fenetre / 2);
                nouvelle_pos = Math.Max(0, Math.Min(nouvelle_pos, h));
                bl.y = nouvelle_pos;
                //   MessageBox.Show(bl.x.ToString()+","+bl.y.ToString());
                s = SAD(bl, br);
                tmp.position = bl;
                tmp.qualite = s;
                l[i] = tmp;
                //  MessageBox.Show(bl.x.ToString()+","+bl.y.ToString());
            }
            //);
        }
コード例 #40
0
 public bool lst_contain(List<bloc> list, bloc b)
 {
     int v = 2;
       bool bo = false;
       int i = 0;
       while (i < list.Count && bo == false)
       {
           if (list[i].x == b.x && list[i].y == b.y) bo = true;
           else
           {
               if (b.x < list[i].x + v && b.x > list[i].x - v)
               {
                   if (b.y < list[i].y + v && b.y > list[i].y - v) bo = true;
                   else bo = false;
               }
               else bo = false;
           }
           i++;
       }
       return bo;
 }
コード例 #41
0
        /*
           public int selection_meilleur_abeille(List<abeille> l)
        {
            int pos=0;
            for (int i = 0; i < l.Count-1; i++)
            {
                if (l[i].qualite != l[i + 1].qualite)
                {
                    pos = i;
                    break;
                }
            }

            return pos;
        }
        */
        private abeille operation_abeille(bloc br)
        {
            abeille ab = new abeille();
            //int cpt = 0;
            int d=0;
            int voisinage = fenetre / 2;
            List<abeille> list_n = new List<abeille>();
            List<abeille> list_m = new List<abeille>();
            List<abeille> list_e1 = new List<abeille>();
            List<abeille> list_e2 = new List<abeille>();
            List<abeille> bcm = new List<abeille>();
            list_n = disperser_abeilles(br, n, fenetre);
            //if (list_n.Count == 0) MessageBox.Show("vide");
            //else MessageBox.Show("non vide");
            mise_a_jour(ref list_n, br);
            trier_selon_qualite(ref list_n, list_n.Count - 1);
            list_m = recuperer_abeilles(0, m, list_n);

             //   do
            for(int cpt =0;cpt < nbgen;cpt++)
              //  System.Threading.Tasks.Parallel.For(0, nbgen, delegate(int cpt)
            {
                //bcm.Clear();
                list_e1.Clear();
                list_e2.Clear();
                if (list_m.Count == 0)
                {
                  //  System.Windows.Forms.MessageBox.Show("list_m = 0");
                    break;
                }
                else
                {
                    object o1 = new object();
                    object o2 = new object();
                    System.Threading.Tasks.Parallel.For(0, e, delegate(int i)
                  //  for (int i = 0; i < e; i++)
                    {
                        //if (list_m.Count == 0) MessageBox.Show("azeakzekazejazeazeazezae");
                        lock (o1)
                            list_e1 = disperser_abeilles(list_m[i].distance, nbe, voisinage);
                        mise_a_jour(ref list_e1, br);
                         trier_selon_qualite(ref list_e1, list_e1.Count - 1);
                        //     d = selection_meilleur_abeille(list_e1);
                        // if(d!=0)  MessageBox.Show(d.ToString());
                        lock (o2)
                            bcm.Add(list_e1[d]);
                    }
                     );

                    System.Threading.Tasks.Parallel.For(e, m, delegate(int i)
                    // for (int i = e; i < m; i++)
                    {
                        lock (o2)
                            list_e2 = disperser_abeilles(list_m[i].distance, nba, voisinage);
                        mise_a_jour(ref list_e2, br);
                        trier_selon_qualite(ref list_e2, list_e2.Count - 1);
                        //   d = selection_meilleur_abeille(list_e2);
                        //       if (d != 0) MessageBox.Show(d.ToString());
                        lock (o1)
                            bcm.Add(list_e2[d]);
                    }
                    );
              //
                    list_m = bcm.ToList();
                    // mise_a_jour(ref list_m, br);
                    //      cpt++;
                }

            } //while (cpt <= nbgen);
               //);
               // mise_a_jour(ref bcm,br);
            trier_selon_qualite(ref bcm, bcm.Count- 1);
            //d = selection_meilleur_abeille(bcm);
               //     if (d != 0) MessageBox.Show(d.ToString());
            ab = bcm[0];

            return ab;
        }
コード例 #42
0
 public bloc mutation(bloc Br,bloc B)
 {
     bloc chro_mute;
     chro_mute.x = -1;
     chro_mute.y = -1;
     string tmp1, tmp2;
     tmp1 = Convert.ToString(B.x, 2);
     tmp2 = Convert.ToString(B.y, 2);
     string sauv1 = tmp1;
     string sauv2 = tmp2;
     //intervertir un bit de tmp1 et un bit de tmp2
     StringBuilder str1 = new StringBuilder(tmp1);
     StringBuilder str2 = new StringBuilder(tmp2);
     char c;
     Random rd = new Random(DateTime.Now.Millisecond);
     while (!appartient_fen(Br, chro_mute))
     {
         str1 = new StringBuilder(sauv1);
         str2 = new StringBuilder(sauv2);
         int position = rd.Next(0, str1.Length - 1);
         if (str1[position] == '0') c = '1';
         else c = '0';
         str1[position] = c;
         position = rd.Next(0, str2.Length - 1);
         if (str2[position] == '0') c = '1';
         else c = '0';
         str2[position] = c;
         tmp1 = str1.ToString();
         tmp2 = str2.ToString();
         //reconversion en entier
         chro_mute.x = Convert.ToInt32(tmp1, 2);
         chro_mute.y = Convert.ToInt32(tmp2, 2);
     }
     return chro_mute;
 }
コード例 #43
0
        /// <summary>
        /// critère de comparaison entre deux blocs
        /// </summary>
        /// <param name="bc">bloc candidat</param>
        /// <param name="br">bloc référence</param>
        /// <returns>l'erreur entre les deux blocs</returns>
        public double SAD(bloc bc, bloc br)
        {
            double sad = 0;
            PixelData t1;
            PixelData t2;
            int x1, x2, y1, y2;
            // PixelData t3 = new PixelData();
            //accés au pixel
            double val1, val2;

            for (int i = 0; i < taille_bloc; i++)
            {
                for (int j = 0; j < taille_bloc; j++)
                {
                     x1 = Math.Max(0, Math.Min(wi-1, (br.x + i)));
                     y1 = Math.Max(0, Math.Min(he-1, (br.y + j)));
                     x2 = Math.Max(0, Math.Min(wi-1, (bc.x + i)));
                     y2 = Math.Max(0, Math.Min(he-1, (bc.y + j)));
                    t1 = a.GetPixel(x1, y1);
                    t2 = b.GetPixel(x2, y2);
                    val1 = t1.blue * 0.1140 + t1.green * 0.5870 + t1.red * 0.2989;
                    val2 = t2.blue * 0.1140 + t2.green * 0.5870 + t2.red * 0.2989;

                    if (val2 - val1 < 0)
                    {
                        sad -= (val2 - val1);
                    }
                    else
                    {
                        sad += (val2 - val1);
                    }
                }

            }
            return sad;
        }
コード例 #44
0
        /// <summary>
        /// tester si ke bloc condidat appartient à la fenetre de recherche dub bloc référence
        /// </summary>
        /// <param name="Br">Bloc référence</param>
        /// <param name="Bc">Bloc condidat</param>
        /// <returns>b un booleen</returns>

        /*
         *    public bool appartient_fen(bloc Br, bloc Bc)
         *    {
         *        bool b;
         *        if ((Bc.x >= Br.x - deplacement_fenetre) && (Bc.x <= Br.x + deplacement_fenetre))
         *        {
         *            if ((Bc.y >= Br.y - deplacement_fenetre) && (Bc.y <= Br.y + deplacement_fenetre))
         *            {
         *               if (Bc.x+taille_bloc < w && Bc.y+taille_bloc < h)
         *                {
         *                    if (Bc.x >= 0 && Bc.y >= 0)
         *                   {
         *                        b = true;
         *                    }
         *                   else b = false;
         *               }
         *                else b = false;
         *            }
         *            else b = false;
         *        }
         *        else b = false;
         *        return b;
         *    }
         */
        /// <summary>
        /// constuire une liste de blocs selon le modèle LDSP
        /// </summary>
        /// <param name="Br">bloc référence</param>
        /// <param name="B">le bloc central courant</param>
        /// <returns></returns>
        public bloc[] LDSP(bloc Br, bloc B)
        {
            // List<bloc> lis_ldsp = new List<bloc>();
            int cpt = 0;

            bloc[] tab_ldsp = new bloc[9];
            for (int k = 0; k < 9; k++)
            {
                tab_ldsp[k].x = -1;
                tab_ldsp[k].y = -1;
            }
            bloc tmp;

            // lis_ldsp.Add(B);
            tab_ldsp[cpt] = B;
            cpt++;
            tmp.x = B.x + 2;
            tmp.y = B.y;
            if (appartient_fen(Br, tmp) == true)
            //  MessageBox.Show(tmp.x.ToString()+","+tmp.y.ToString());
            { /*lis_ldsp.Add(tmp); */
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x - 2;
            tmp.y = B.y;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x;
            tmp.y = B.y + 2;
            if (appartient_fen(Br, tmp) == true)
            { /* lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x;
            tmp.y = B.y - 2;
            if (appartient_fen(Br, tmp) == true)
            { /* lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x + 1;
            tmp.y = B.y + 1;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x - 1;
            //if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
            tmp.y = B.y - 1;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x + 1;
            tmp.y = B.y - 1;
            if (appartient_fen(Br, tmp) == true)
            { /*lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            tmp.x = B.x - 1;
            //  if (tmp.x < 0) MessageBox.Show("le bleme est presque ici");
            tmp.y = B.y + 1;
            if (appartient_fen(Br, tmp) == true)
            { /* lis_ldsp.Add(tmp);*/
                tab_ldsp[cpt] = tmp; cpt++;
            }
            // return lis_ldsp;
            //    if (cpt == 1) MessageBox.Show("cpt =1");
            return(tab_ldsp);
        }
コード例 #45
0
ファイル: CmdLisse.cs プロジェクト: larnin/McWorlds
        public void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type)
        {       //récupération des infos sur le dernier bloc cliqué
            p.ClearBlockchange();
            byte b = p.level.GetTile(x, y, z);

            p.SendBlockchange(x, y, z, b);
            CatchPos cpos = (CatchPos)p.blockchangeObject;

            int sizeX = Math.Abs(cpos.x - x) + 1, sizeY = Math.Abs(cpos.y - y) + 1, sizeZ = Math.Abs(cpos.z - z) + 1;
            int nbBlocs = sizeX * sizeY * sizeZ;

            if (nbBlocs > p.maxblocsbuild())
            {
                Player.SendMessage(p, "Vous essayer de lisser une zone de " + nbBlocs + " blocs.");
                Player.SendMessage(p, "Vous n'avez pas le droit a plus de " + p.maxblocsbuild() + ".");
                return;
            }
            List <byte> blocsLisse = new List <byte>(nbBlocs);

            List <bloc> blocsid = new List <bloc>();
            bloc        bMax    = new bloc();
            //bool blocFind = false;
            int  blocsIn = 0, nbValide = 0, tabX = 0, tabY = 0, tabZ = 0;
            byte bl = Block.Zero;


            //rechreche de tous les blocs et lissage
            for (int i = Math.Min(x, cpos.x); i <= Math.Max(x, cpos.x); i++)
            {
                for (int j = Math.Min(y, cpos.y); j <= Math.Max(y, cpos.y); j++)
                {
                    for (int k = Math.Min(z, cpos.z); k <= Math.Max(z, cpos.z); k++)
                    {
                        blocsIn = 0; nbValide = 0; blocsid.Clear(); //blocFind = false;

                        //calcul du lissage
                        for (int l = -2; l <= 2; l++)
                        {
                            for (int m = -2; m <= 2; m++)
                            {
                                for (int n = -2; n <= 2; n++)
                                {
                                    bl = p.level.GetTile((ushort)(i + l), (ushort)(j + m), (ushort)(k + n));

                                    if (bl == Block.Zero)
                                    {
                                        continue;
                                    }
                                    if (bl == Block.air)
                                    {
                                        blocsIn++; continue;
                                    }

                                    blocsIn++; nbValide++;

                                    bMax = blocsid.Find(bi => bi.id == bl);
                                    if (bMax == null)
                                    {
                                        bloc bc = new bloc(bl, 1); blocsid.Add(bc);
                                    }
                                    else
                                    {
                                        bMax.nbBlocs++;
                                    }

                                    /*for (int a = 0; a < blocsid.Count; a++)
                                     * {
                                     *  if (blocsid[a].id == bl)
                                     *  {
                                     *      bMax = blocsid[a];
                                     *      blocsid.Remove(bMax);
                                     *      bMax.nbBlocs++;
                                     *      blocsid.Add(bMax);
                                     *      blocFind = true;
                                     *  }
                                     * }
                                     * if (!blocFind)
                                     * { bMax.id = bl; bMax.nbBlocs = 1; blocsid.Add(bMax); }*/
                                }
                            }
                        }

                        // lissage (si 50% des blocs alentours sont air, le bloc devient air sinon il devient solide)
                        bl = p.level.GetTile((ushort)i, (ushort)j, (ushort)k);

                        if (bl == Block.Zero)
                        {
                            blocsLisse.Add(Block.Zero);
                        }

                        if (nbValide < blocsIn / 2)
                        {
                            blocsLisse.Add(Block.air);
                        }
                        else
                        {
                            if (bl != Block.air)
                            {
                                blocsLisse.Add(bl);
                            }
                            else
                            {
                                bMax.id = 1; bMax.nbBlocs = 0;

                                foreach (bloc bi in blocsid)
                                {
                                    if (bi.nbBlocs > bMax.nbBlocs)
                                    {
                                        bMax.id = bi.id; bMax.nbBlocs = bi.nbBlocs;
                                    }
                                }
                                blocsLisse.Add(bMax.id);
                            }
                        }
                    }
                }
            }

            blocsid.Clear();

            //Poussage de l'herbe et application du lissage
            byte bAct = Block.Zero;

            for (int i = Math.Min(x, cpos.x); i <= Math.Max(x, cpos.x); i++)
            {
                tabX = i - Math.Min(x, cpos.x);
                for (int j = Math.Min(y, cpos.y); j <= Math.Max(y, cpos.y); j++)
                {
                    tabY = j - Math.Min(y, cpos.y);
                    for (int k = Math.Min(z, cpos.z); k <= Math.Max(z, cpos.z); k++)
                    {
                        tabZ = k - Math.Min(z, cpos.z);
                        bAct = blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ];

                        if (j + 1 > Math.Max(y, cpos.y))
                        {
                            if (bAct == Block.dirt && p.level.GetTile((ushort)i, (ushort)(j + 1), (ushort)k) == Block.air)
                            {
                                blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ] = Block.grass;
                            }
                            if (bAct == Block.grass && p.level.GetTile((ushort)i, (ushort)(j + 1), (ushort)k) != Block.air)
                            {
                                blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ] = Block.dirt;
                            }
                        }
                        else
                        {
                            if (bAct == Block.dirt && blocsLisse[tabX * sizeY * sizeZ + (tabY + 1) * sizeZ + tabZ] == Block.air)
                            {
                                blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ] = Block.grass;
                            }
                            if (bAct == Block.grass && blocsLisse[tabX * sizeY * sizeZ + (tabY + 1) * sizeZ + tabZ] != Block.air)
                            {
                                blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ] = Block.dirt;
                            }
                        }

                        //Application
                        if (bAct != Block.Zero)
                        {
                            p.level.Blockchange(p, (ushort)i, (ushort)j, (ushort)k, bAct);
                        }
                    }
                }
            }

            /*//aplication du lissage
             * for (int i = Math.Min(x, cpos.x); i <= Math.Max(x, cpos.x); i++)
             * {
             *  tabX = i - Math.Min(x, cpos.x);
             *  for (int j = Math.Min(y, cpos.y); j <= Math.Max(y, cpos.y); j++)
             *  {
             *      tabY = j - Math.Min(y, cpos.y);
             *      for (int k = Math.Min(z, cpos.z); k <= Math.Max(z, cpos.z); k++)
             *      {
             *          tabZ = k - Math.Min(z, cpos.z);
             *
             *          if (blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ] != Block.Zero)
             *          { p.level.Blockchange(p, (ushort)i, (ushort)j, (ushort)k, blocsLisse[tabX * sizeY * sizeZ + tabY * sizeZ + tabZ]); }
             *      }
             *  }
             * }*/

            Player.SendMessage(p, "Lissage termine");

            blocsLisse.Clear();

            if (p.staticCommands)
            {
                p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);                   //si répétition commande activé
            }
        }