Ejemplo n.º 1
0
        public bool TestCloneSecretClone()
        {
            CardGame cg     = actualgameiterator.game.CloneSecret(0);
            CardGame clone  = cg.Clone();
            CardGame clone2 = clone.Clone();

            if (clone.Equals(cg))
            {
                Console.WriteLine("Clonesecret equals Cloned CloneSecret");
            }
            else
            {
                Console.WriteLine("Clonesecret Clone fail");
            }
            if (clone2.Equals(clone))
            {
                Console.WriteLine("CloneSecret's Clone Clone equals CloneSecret's Clone");
            }
            return(true);
        }
Ejemplo n.º 2
0
        public void RunSimulation()
        {
            // Each turn, need to check to see if we have enough information to make move using UCB
            // If we do (movelist.count() == choicenum), and we check the stats of each move
            // A predictable player is set for the currentplayers idx which wil chose the move determined by
            // Movelist should be tuple array with each entry a state and a who played it
            // Its key should be a state and the idx of the player in charge

            HashSet <Tuple <CardGame, int> > visitedstates = new HashSet <Tuple <CardGame, int> >();
            CardGame     cg           = privategame.Clone();
            GameIterator gameIterator = privateiterator.Clone(cg);

            for (int j = 0; j < numPlayers; j++)
            {
                cg.players[j].decision = new RandomPlayer(perspective);
            }
            int idxme = cg.currentPlayer.Peek().idx;

            bool expand = true;
            bool first  = true;

            // "Playing a simulated game"
            while (!gameIterator.AdvanceToChoice())
            {
                int idx = cg.currentPlayer.Peek().idx;
                if (idxme == idx)
                {
                    List <GameActionCollection> allOptions = gameIterator.BuildOptions();

                    Tuple <CardGame, int>[] movelist = null;
                    int c = 0;
                    if (expand)
                    {
                        int choicenum = allOptions.Count;
                        Tuple <CardGame, int> deliberator = Tuple.Create <CardGame, int>(cg.Clone(), idx);

                        if (!movestatetree.Keys.Contains(deliberator))
                        {
                            movestatetree[deliberator] = new Tuple <CardGame, int> [choicenum];
                        }
                        movelist = movestatetree[deliberator];

                        //Console.WriteLine("Choice num: " + choicenum + " Movelist Count: " + movelist.Count(s => s != null));
                        if (movelist.Count(s => s != null) == choicenum)
                        {
                            // USE UCB
                            double bestscore = 0;
                            c = 0;
                            double totalplays = 0;
                            foreach (Tuple <CardGame, int> stateandplay in (movelist))
                            {
                                totalplays += plays[stateandplay];
                            }
                            totalplays = Math.Log(totalplays);
                            for (int i = 0; i < movelist.Length; i++)
                            {
                                Tuple <CardGame, int> stateandplay = movelist[i];
                                double temp = wins[stateandplay] / plays[stateandplay];
                                temp += Math.Sqrt(2 * totalplays / plays[stateandplay]);
                                if (temp > bestscore)
                                {
                                    bestscore = temp;
                                    c         = i;
                                }
                            }
                            allOptions[c].ExecuteAll();
                            gameIterator.PopCurrentNode();
                        }
                        else
                        {
                            c = gameIterator.ProcessChoice();
                        }
                    }
                    else
                    {
                        c = gameIterator.ProcessChoice();
                    }

                    CardGame savestate = gameIterator.game.Clone();
                    // THIS HELPS FIND ORIGINAL MOVE CHOICES
                    if (first)
                    {
                        if (!movestates.Keys.Contains(c))
                        {
                            movestates[c] = savestate;
                        }
                        first = false;
                    }

                    // Stateandplayer is Tuple with state after move, and the idx of the player who made the move
                    Tuple <CardGame, int> stateandplayer = Tuple.Create <CardGame, int>(savestate, idx);

                    // IF THIS IS THE FIRST SIMULATION WHICH HAS ARRIVED AT THIS STATE::
                    if (expand && (!plays.Keys.Contains(stateandplayer)))
                    {
                        expand = false;
                        plays[stateandplayer] = 0;
                        wins[stateandplayer]  = 0;
                        movelist[c]           = stateandplayer;
                    }
                    visitedstates.Add(stateandplayer);
                }
                else
                {
                    gameIterator.ProcessChoice();
                }                                      // IF IT ISNT MY MOVE, DONT KEEP ANY DATA
            }

            // ProcessScore returns a sorted list
            // where the winner is rank 0 for either min/max games.
            var winners = gameIterator.ProcessScore();

            double[] inverseRankSum = new double[numPlayers];

            int p = 0;

            foreach (Tuple <int, int> scoreandidx in winners)
            {
                inverseRankSum[scoreandidx.Item2] = ((double)1) / (p + 1);
                p++;
            }
            // GO THROUGH VISITED STATES
            foreach (Tuple <CardGame, int> stateandplayer in visitedstates)
            {
                if (plays.Keys.Contains(stateandplayer))
                {
                    plays[stateandplayer] += 1;
                    wins[stateandplayer]  += inverseRankSum[stateandplayer.Item2];
                }
            }
        }
Ejemplo n.º 3
0
        public override int MakeAction(JObject possibles, Random rand, int idx)
        {
            var      items     = (JArray)possibles ["items"];
            CardGame preserved = CardEngine.CardGame.Instance;


            var results = new int[items.Count];

            for (int item = 0; item < items.Count; ++item)
            {
                results [item] = 0;
                for (int i = 0; i < 20; ++i)
                {
                    Debug.WriteLine("****Made Switch****");

                    CardGame cg   = preserved.Clone();
                    var      flag = true;
                    foreach (var player in cg.players)
                    {
                        if (flag)
                        {
                            flag            = false;
                            player.decision = new PredictablePlayer();
                            Debug.WriteLine("Predictable player choice set: " + item);
                            ((PredictablePlayer)player.decision).toChoose = item;
                        }
                        else
                        {
                            player.decision = new Players.GeneralPlayer();
                        }
                    }

                    var cloneContext = ParseEngine.currentIterator.Clone(cg);
                    while (!cloneContext.AdvanceToChoice())
                    {
                        cloneContext.ProcessChoice();
                    }
                    var winners = cloneContext.parseoop.ProcessScore(ParseEngine.currentTree.scoring());
                    for (int j = 0; j < winners.Count; ++j)
                    {
                        if (winners [j].Item2 == 0)
                        {
                            results [item] += j;
                        }
                    }
                }
            }
            //Debug.WriteLine ("***Switch Back***");
            CardEngine.CardGame.Instance = preserved;
            var typeOfGame = ParseEngine.currentTree.scoring().GetChild(2).GetText();

            if (typeOfGame == "min")
            {
                return(idxOfMinimum(results));
            }
            else
            {
                return(idxOfMaximum(results));
            }
            //return rand.Next (0,items.Count);
        }