Beispiel #1
0
        public override int Jouer(Position p)
        {
            sw.Restart();
            Func <int, int, float> phi = (W, C) => (a + W) / (b + C);

            if (nouedMap.ContainsKey(p.GetHashCode()))
            {
                racine = (Noeud)nouedMap[p.GetHashCode()];
            }
            else
            {
                racine = new Noeud(null, p);
            }

            int iter = 0;

            while (sw.ElapsedMilliseconds < temps)
            {
                Noeud no = racine;

                do // Sélection
                {
                    no.CalculMeilleurFils(phi);
                    no = no.MeilleurFils();
                } while (no.cross > 0 && no.fils.Length > 0);


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

                while (no != null)        // Rétropropagation
                {
                    no.cross += 2;
                    no.win   += re;
                    no        = no.pere;
                }
                iter++;
            }
            racine.CalculMeilleurFils(phi);

            //Console.WriteLine("{0} itérations", iter);
            //Console.WriteLine(racine);
            //把本次算过得子节点都放进map去
            for (int i = 0; i < racine.fils.Length; i++)
            {
                if (racine.fils[i] != null)
                {
                    nouedMap.Add(racine.fils[i].p.GetHashCode(), racine.fils[i]);
                }
            }

            return(racine.indiceMeilleurFils);
        }
Beispiel #2
0
    public override int Jouer(Position p)
    {
        racine = new Noeud(null, p);
        Stopwatch t0 = Stopwatch.StartNew();

        for (int i = 0; i < n; i++)
        {
            // Sélection
            Noeud no = racine;
            //no.cross = 0;
            do
            {
                no.CalculMeilleurFils(a);
                no = no.MeilleurFils();
            } while (no.fils.Length > 0 && no.cross > 0);
            // Simulation
            Resultat res = JeuHasard(no.p);
            int      re  = p.j1aletrait ? 0 : 1;
            if (res == Resultat.j1gagne)
            {
                re = 1;
            }
            if (res == Resultat.j0gagne)
            {
                re = 0;
            }
            // Rétropropagation
            while (no != null)
            {
                no.cross += 1;
                no.win   += re;
                no        = no.pere;
            }
            Temps = (int)t0.ElapsedMilliseconds;
            if (Temps > 1000)
            {
                break;
            }
        }
        racine.CalculMeilleurFils(a);
        Console.WriteLine(racine);
        return(racine.indiceMeilleurFils);;
    }