Beispiel #1
0
 public ChanceAbstractedGameDefParams(FictitiousPlay_Test test, string name, string gameDefFile,
                                      IChanceAbstraction[] abstractions, double epsilon) :
     base(test, gameDefFile, epsilon)
 {
     ChanceTree = CreateChanceTreeByAbstraction.CreateS(GameDef, abstractions);
     Name       = name;
 }
Beispiel #2
0
 public ChanceAbstractedGameDefParams(EqLp_Test test, string name, string gameDefFile, IChanceAbstraction[] abstractions,
                                      double[] expectedResult, double epsilon) :
     base(test, gameDefFile, expectedResult, epsilon)
 {
     ChanceTree = CreateChanceTreeByAbstraction.CreateS(GameDef, abstractions);
     Name       = name;
 }
Beispiel #3
0
        /// <summary>
        /// This functions simulate a typical use case: create trees in many runs of MC sampling, write them, read again
        /// and merge into the master tree. The master tree is than verified.
        /// </summary>
        private void GenerateAndVerifyCT(string name, GameDefinition gd, IChanceAbstraction[] chanceAbstractions, bool areAbstractionsEqual, int samplesCount, int runsCount, double avRelProbabEps, double avPotShareEps, double eqValEps, bool visualize)
        {
            CtMcGen.Tree masterTree = new CtMcGen.Tree();

            int rngSeed = (int)DateTime.Now.Ticks;

            for (int run = 0; run < runsCount; ++run)
            {
                CtMcGen.Tree runTree  = CtMcGen.Generate(gd, chanceAbstractions, areAbstractionsEqual, samplesCount, rngSeed, null);
                string       fileName = Path.Combine(_outDir, String.Format("{0}-{1}-ct.dat", gd.Name, name));
                runTree.Write(fileName);
                masterTree.Read(fileName);

                // Do not use the timer anymore because the tests are too fast.
                rngSeed++;
            }

            ChanceTree actCt = masterTree.ConvertToChanceTree();

            VisChanceTree.Show(actCt, Path.Combine(_outDir, String.Format("{0}-{1}-ct.gv", gd.Name, name)));
            VerifyChanceTree.VerifyS(actCt);
            ChanceTree expCt = CreateChanceTreeByAbstraction.CreateS(gd, chanceAbstractions);

            Assert.AreEqual(expCt.PlayersCount, actCt.PlayersCount);
            CompareChanceTrees cmp = new CompareChanceTrees();

            cmp.IsVerbose = visualize;
            cmp.Output    = Console.Out;
            cmp.Compare(expCt, actCt);
            VisChanceTree.Show(expCt, Path.Combine(_outDir, String.Format("{0}-{1}-ct-exp.gv", gd.Name, name)));
            Assert.Less(cmp.AverageRelProbabDiff, avRelProbabEps);
            for (int p = 0; p < chanceAbstractions.Length; ++p)
            {
                Assert.Less(cmp.AveragePotShareDiff[p], avRelProbabEps);
            }

            ActionTree at = CreateActionTreeByGameDef.Create(gd);

            double [] actEqValues, expEqValues;
            EqLp.Solve(at, actCt, out actEqValues);
            EqLp.Solve(at, expCt, out expEqValues);
            for (int p = 0; p < chanceAbstractions.Length; ++p)
            {
                if (visualize)
                {
                    Console.WriteLine("Eq pos: {0} exp: {1} act: {2}", p, expEqValues[p], actEqValues[p]);
                }
                Assert.AreEqual(expEqValues[p], actEqValues[p], eqValEps);
            }
            if (visualize)
            {
                Console.WriteLine();
            }
        }
            public TestParams(FictitiousPlayMc_Test test, string gameDefFile,
                              IChanceAbstraction chanceAbstr, int [] cardCount, double epsilon)
            {
                Epsilon           = epsilon;
                GameDef           = XmlSerializerExt.Deserialize <GameDefinition>(Props.Global.Expand("${bds.DataDir}ai.pkr.metastrategy/${0}", gameDefFile));
                Name              = chanceAbstr.Name;
                ChanceAbstraction = chanceAbstr;

                ActionTree = CreateActionTreeByGameDef.Create(GameDef);
                Debug.Assert(GameDef.MinPlayers == 2);
                ChanceTree = CreateChanceTreeByAbstraction.CreateS(GameDef, new IChanceAbstraction[] { chanceAbstr, chanceAbstr });
                CardCount  = cardCount;
            }
Beispiel #5
0
            public OcfrParams(GameValue_Test test, string name, string gameDefFile, IChanceAbstraction[] abstractions, string[] ocfrFiles, double[] expectedResult, double epsilon)
            {
                Epsilon        = epsilon;
                ExpectedResult = expectedResult;

                GameDef = XmlSerializerExt.Deserialize <GameDefinition>(
                    Props.Global.Expand("${bds.DataDir}ai.pkr.metastrategy/${0}",
                                        gameDefFile));

                Name = name;

                OcfrFiles = new string[GameDef.MinPlayers].Fill(i => Path.Combine(test._testResDir, ocfrFiles[i]));
                if (abstractions == null)
                {
                    ChanceTree = CreateChanceTreeByGameDef.Create(GameDef);
                }
                else
                {
                    ChanceTree = CreateChanceTreeByAbstraction.CreateS(GameDef, abstractions);
                }

                //VisChanceTree.Show(ChanceTree, Path.Combine(test._outDir, String.Format("{0}-c.gv", GameDef.Name)));

                ActionTree = CreateActionTreeByGameDef.Create(GameDef);

                StrategyTrees = new StrategyTree[GameDef.MinPlayers];

                for (int p = 0; p < GameDef.MinPlayers; ++p)
                {
                    ChanceTree pct = ExtractPlayerChanceTree.ExtractS(ChanceTree, p);
                    StrategyTrees[p] = CreateStrategyTreeByChanceAndActionTrees.CreateS(pct, ActionTree);


                    OpenCfrStrategyConverter conv = new OpenCfrStrategyConverter
                    {
                        GameDef           = GameDef,
                        HeroPosition      = p,
                        SourceFile        = OcfrFiles[p],
                        Strategy          = StrategyTrees[p],
                        ChanceAbstraction = abstractions == null ? null : abstractions[p]
                    };
                    conv.Convert();

                    //VisStrategyTree.Show(StrategyTrees[p], Path.Combine(test._outDir, String.Format("{0}-str-{1}.gv", GameDef.Name, p)));
                }
            }
Beispiel #6
0
            public TestParams(Breb_Test test, string gameDefFile,
                              IChanceAbstraction chanceAbstr, double epsilon)
            {
                Epsilon           = epsilon;
                GameDef           = XmlSerializerExt.Deserialize <GameDefinition>(Props.Global.Expand("${bds.DataDir}ai.pkr.metastrategy/${0}", gameDefFile));
                Name              = chanceAbstr == null ? GameDef.Name : chanceAbstr.Name;
                ChanceAbstraction = chanceAbstr;

                ActionTree = CreateActionTreeByGameDef.Create(GameDef);
                Debug.Assert(GameDef.MinPlayers == 2);
                if (chanceAbstr == null)
                {
                    ChanceTree = CreateChanceTreeByGameDef.Create(GameDef);
                }
                else
                {
                    ChanceTree = CreateChanceTreeByAbstraction.CreateS(GameDef,
                                                                       new IChanceAbstraction[]
                                                                       { chanceAbstr, chanceAbstr });
                }
            }
        public void Test_Kuhn()
        {
            GameDefinition gd = XmlSerializerExt.Deserialize <GameDefinition>(
                Props.Global.Expand("${bds.DataDir}ai.pkr.metastrategy/kuhn.gamedef.xml"));

            IChanceAbstraction[] abstractions = new IChanceAbstraction [] { new KuhnChanceAbstraction(), new KuhnChanceAbstraction() };

            ChanceTree ct  = CreateChanceTreeByGameDef.Create(gd);
            ChanceTree act = CreateChanceTreeByAbstraction.CreateS(gd, abstractions);

            VerifyChanceTree.VerifyS(act);

            VisChanceTree.Show(act, Path.Combine(_outDir, gd.Name + "-abct.gv"));

            UInt16[] activePlayersOne = ActivePlayers.Get(gd.MinPlayers, 1, 1);
            UInt16[] activePlayersAll = ActivePlayers.Get(gd.MinPlayers, 1, gd.MinPlayers);
            int      maxDepth         = gd.RoundsCount * gd.MinPlayers;

            Assert.AreEqual(act.PlayersCount, gd.MinPlayers);
            Assert.AreEqual(ct.NodesCount, act.NodesCount);
            double[] expPotShares = new double[gd.MinPlayers];
            double[] actPotShares = new double[gd.MinPlayers];
            for (int i = 0; i < ct.NodesCount; ++i)
            {
                Assert.AreEqual(ct.Nodes[i].Position, act.Nodes[i].Position);
                Assert.AreEqual(ct.Nodes[i].Card, act.Nodes[i].Card); // Kuhn abstraction has the same card
                Assert.AreEqual(ct.Nodes[i].Probab, act.Nodes[i].Probab);
                if (i == 0)
                {
                    continue;
                }
                UInt16[] activePlayers = ct.GetDepth(i) == maxDepth ? activePlayersAll : activePlayersOne;
                foreach (UInt16 ap in activePlayers)
                {
                    ct.Nodes[i].GetPotShare(ap, expPotShares);
                    act.Nodes[i].GetPotShare(ap, actPotShares);
                    Assert.AreEqual(expPotShares, actPotShares, String.Format("Node: {0}, ap: {1}", i, ap));
                }
            }
        }
Beispiel #8
0
        void SolveGame(string runDir, GameDefinition gd, IChanceAbstraction [] chanceAbstractions, double [] brValues)
        {
            ChanceTree ct = CreateChanceTreeByAbstraction.CreateS(gd, chanceAbstractions);
            ActionTree at = CreateActionTreeByGameDef.Create(gd);

            double[]       eqValues;
            StrategyTree[] eqStrategies = EqLp.Solve(at, ct, out eqValues);

            string error;

            for (int p = 0; p < gd.MinPlayers; ++p)
            {
                Assert.IsTrue(VerifyAbsStrategy.Verify(eqStrategies[p], p, 1e-7, out error), error);
            }
            // Verify eq
            Assert.IsTrue(VerifyEq.Verify(at, ct, eqStrategies, 1e-7, out error), error);

            StrategyTree[] brStrategies = new StrategyTree[gd.MinPlayers];

            // Find BR for each position
            for (int heroPos = 0; heroPos < gd.MinPlayers; ++heroPos)
            {
                Br br = new Br
                {
                    HeroPosition = heroPos,
                    ActionTree   = at,
                    ChanceTree   = ct,
                    Strategies   = (StrategyTree[])eqStrategies.Clone()
                };
                br.Solve();
                brStrategies[heroPos] = br.Strategies[heroPos];
                brValues[heroPos]     = br.Value;
            }

            MergeAndSaveStrategies(runDir, "", eqStrategies, chanceAbstractions);
            MergeAndSaveStrategies(runDir, "-br", brStrategies, chanceAbstractions);
        }