Пример #1
0
 public NoeudP(NoeudP pere, Position p, int N)
 {
     this.pere = pere;
     this.p    = p;
     fils      = new NoeudP[this.p.NbCoups];
     cross     = new int[N];
     win       = new int[N];
 }
Пример #2
0
        public NoeudP MeilleurFils()
        {
            if (fils[indiceMeilleurFils] != null)
            {
                return(this.fils[indiceMeilleurFils]);
            }
            Position q = p.Clone();

            q.EffectuerCoup(indiceMeilleurFils);
            fils[indiceMeilleurFils] = new NoeudP(this, q, cross.Length);

            return(this.fils[indiceMeilleurFils]);
        }
Пример #3
0
        public override int Jouer(Position p)
        {
            Func <int, int, float> phi = (W, C) => (a + W) / (b + C);

            racine = null;
            int j = 0;

            while (racine == null)
            {
                if (memoire == null)
                {
                    racine = new NoeudP(null, p, NbrThreads); break;
                }
                if (memoire[j] == null)
                {
                    racine = new NoeudP(null, p, NbrThreads); break;
                }

                if (p.Equals((object)memoire[j].p))
                {
                    racine = memoire[j];
                }
                j++;
            }
            int iter = j;

            sw.Restart();

            Task <int>[] tblthread = new Task <int> [NbrThreads];
            for (int k = 0; k < NbrThreads; k++)
            {
                int indiceThread = k;
                tblthread[indiceThread] = Task.Run(() => Thread(phi, indiceThread));
                iter = tblthread[indiceThread].Result + iter;
            }
            Task.WaitAll(tblthread);
            racine.CalculMeilleurFils(phi);
            Console.WriteLine("{0} itérations", iter);
            //Console.WriteLine(racine.fils);
            memoire = racine.fils[racine.indiceMeilleurFils].fils;
            return(racine.indiceMeilleurFils);
        }
Пример #4
0
        public int Thread(Func <int, int, float> phi, int iff)
        {
            lock (verrou)
            {
                int iter = 0;
                while (sw.ElapsedMilliseconds < temps)
                {
                    NoeudP no            = racine;
                    int    SommeDesCross = 0;

                    do // Sélection
                    {
                        no.CalculMeilleurFils(phi);

                        no = no.MeilleurFils();

                        SommeDesCross = 0;
                        for (int j = 0; j < no.cross.Length; j++)
                        {
                            SommeDesCross = SommeDesCross + no.cross[j];
                        }
                    } while (SommeDesCross > 0 && no.fils.Length > 0);


                    int re = JeuHasard(no.p); // Simulation

                    while (no != null)        // Rétropropagation
                    {
                        no.cross[iff] += 2;
                        no.win[iff]   += re;
                        no             = no.pere;
                    }
                    iter++;
                }
                return(iter);
            }
        }