Example #1
0
        public void Test_Eq()
        {
            GameDefinition gd = XmlSerializerExt.Deserialize <GameDefinition>("kuhn8.gamedef.xml");
            ChanceTree     ct = CreateChanceTreeByGameDef.Create(gd);

            VisChanceTree.Show(ct, "kuhn8-ct.gv");
            ActionTree at = CreateActionTreeByGameDef.Create(gd);

            VisActionTree.Show(at, "kuhn8-at.gv");



            double[]        values;
            StrategyTree [] strategies = EqLp.Solve(at, ct, out values);
            Console.WriteLine("Kuhn8 eq values: {0}, {1}", values[0], values[1]);
            VisStrategyTree.Show(strategies[0], "kuhn8-eq-0.gv");
            VisStrategyTree.Show(strategies[1], "kuhn8-eq-1.gv");

            // Make strategy for T same as for Q
            //strategies[0].Nodes[strategies[0].FindNode("0d0 0p0", null)].Probab = 0.5;
            //strategies[0].Nodes[strategies[0].FindNode("0d0 0p0 1p1 0p1", null)].Probab = 0.5;
            //strategies[0].Nodes[strategies[0].FindNode("0d0 0p1", null)].Probab = 0.5;


            // Make strategy for Q same as for T
            strategies[0].Nodes[strategies[0].FindNode("0d2 0p0", null)].Probab         = 0;
            strategies[0].Nodes[strategies[0].FindNode("0d2 0p0 1p1 0p1", null)].Probab = 0;
            strategies[0].Nodes[strategies[0].FindNode("0d2 0p1", null)].Probab         = 1;

            VisStrategyTree.Show(strategies[0], "kuhn8-eq-0-adj.gv");


            Br br = new Br {
                ActionTree = at, ChanceTree = ct, HeroPosition = 1
            };

            br.Strategies = new StrategyTree[] { strategies[0], null };
            br.Solve();
            StrategyTree br0 = br.Strategies[1];

            Console.WriteLine("Br against pos 0: {0}", br.Value);
            VisStrategyTree.Show(strategies[1], "kuhn8-eq-br-0.gv");
        }
Example #2
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);
        }
Example #3
0
        static int Main(string[] args)
        {
            if (!Parser.ParseArgumentsWithUsage(args, _cmdLine))
            {
                return(1);
            }

            if (_cmdLine.DebuggerLaunch)
            {
                Debugger.Launch();
            }
            if (_cmdLine.DiagUnmanagedMemory)
            {
                UnmanagedMemory.IsDiagOn = true;
                Console.WriteLine("Unmanaged memory diagnostics is on");
            }

            ActionTree at = ActionTree.Read <ActionTree>(_cmdLine.ActionTree);

            Console.WriteLine("Action tree: {0}", at.Version.ToString());

            ChanceTree ct = ChanceTree.Read <ChanceTree>(_cmdLine.ChanceTree);

            Console.WriteLine("Chance tree: {0}", ct.Version.ToString());

            StrategyTree oppSt = StrategyTree.Read <StrategyTree>(_cmdLine.OppStrategy);

            Console.WriteLine("Opp strategy: {0}", oppSt.Version.ToString());


            // Create and configure Br solver
            Br br = new Br
            {
                HeroPosition = _cmdLine.HeroPosition,
                ChanceTree   = ct,
                ActionTree   = at,
            };

            br.Strategies = new StrategyTree[ct.PlayersCount];
            for (int opp = 0; opp < ct.PlayersCount; ++opp)
            {
                if (opp == _cmdLine.HeroPosition)
                {
                    continue;
                }
                br.Strategies[opp] = oppSt;
            }

            DateTime startTime = DateTime.Now;

            // Solve Br
            br.Solve();

            double time = (DateTime.Now - startTime).TotalSeconds;

            Console.WriteLine("Done in {0:0.0} s, BR value for hero pos {1}: {2}", time, br.HeroPosition, br.Value);

            string outFile = Path.Combine(Path.GetDirectoryName(_cmdLine.OppStrategy), Path.GetFileNameWithoutExtension(_cmdLine.OppStrategy));

            outFile += "-br.dat";
            Console.WriteLine("Writing br to {0}", outFile);
            br.Strategies[_cmdLine.HeroPosition].Write(outFile);

            return(0);
        }
Example #4
0
        private void Solve(TestParams testParams, bool visualize, ConfigureSolver configureSolver)
        {
            if (visualize)
            {
                VisActionTree.Show(testParams.ActionTree,
                                   Path.Combine(_outDir, String.Format("{0}-at.gv", testParams.Name)));
                VisChanceTree.Show(testParams.ChanceTree,
                                   Path.Combine(_outDir, String.Format("{0}-ct.gv", testParams.Name)));
                for (int p = 0; p < testParams.StrategyTrees.Length; ++p)
                {
                    VisStrategyTree.Show(testParams.StrategyTrees[p],
                                         Path.Combine(_outDir, string.Format("{0}-st-{1}.gv", testParams.Name, p)));
                }
            }

            // Make sure input is correct.
            for (int p = 0; p < testParams.ChanceTree.Nodes[0].Position; ++p)
            {
                string errorText;
                Assert.IsTrue(VerifyAbsStrategy.Verify(testParams.StrategyTrees[p], p, 0.000001, out errorText), errorText);
            }

            for (int heroPos = 0; heroPos < testParams.ChanceTree.PlayersCount; ++heroPos)
            {
                // Create and configure Br solver
                Br br = new Br
                {
                    HeroPosition = heroPos,
                    ChanceTree   = testParams.ChanceTree,
                    ActionTree   = testParams.ActionTree,
                };
                br.Strategies = new StrategyTree[testParams.ChanceTree.PlayersCount];
                for (int opp = 0; opp < testParams.ChanceTree.PlayersCount; ++opp)
                {
                    if (opp == heroPos)
                    {
                        continue;
                    }
                    br.Strategies[opp] = testParams.StrategyTrees[opp];
                }
                br.PrepareVis = visualize;

                if (configureSolver != null)
                {
                    configureSolver(br);
                }

                // Solve Br
                br.Solve();

                if (visualize)
                {
                    Br.Vis.Show(br, Path.Combine(_outDir, String.Format("{0}-br-{1}.gv", testParams.Name, heroPos)));
                }

                // Verify the Br value and strategy

                Assert.AreEqual(testParams.ExpectedResult[heroPos], br.Value, testParams.Epsilon, "Wrong BR value");

                string error;
                Assert.IsTrue(VerifyAbsStrategy.Verify(br.Strategies[br.HeroPosition], br.HeroPosition, out error),
                              error);

                // Verify Br strategy has the expected value by running an independent GameValue algo on it.
                GameValue gv = new GameValue
                {
                    ActionTree = br.ActionTree,
                    ChanceTree = br.ChanceTree,
                    Strategies = br.Strategies
                };
                gv.Solve();
                Assert.AreEqual(testParams.ExpectedResult[heroPos], gv.Values[br.HeroPosition], testParams.Epsilon, "Wrong GameValue value");
            }
        }