/// <summary>
        /// Runs FictitiousPlay with the specified parameters.
        /// Some parameters are set by default (e.g. verbosity), the caller has a chance to overwrite them
        /// using Configure delegate.
        /// </summary>
        public StrategyTree[] Solve(ActionTree at, ChanceTree ct)
        {
            int playersCount = 2;

            DirectoryExt.Delete(BaseDir);
            Directory.CreateDirectory(BaseDir);

            string inputDir = Path.Combine(BaseDir, "input");

            Directory.CreateDirectory(inputDir);
            string traceDir = Path.Combine(BaseDir, "trace");

            string chanceTreeFile = Path.Combine(inputDir, "ct.dat");
            string actionTreeFile = Path.Combine(inputDir, "at.dat");

            ct.Write(chanceTreeFile);
            at.Write(actionTreeFile);

            if (VisualizeTrees)
            {
                VisActionTree.Show(at, actionTreeFile + ".gv");
                VisChanceTree.Show(ct, chanceTreeFile + ".gv");
            }

            Solver.ChanceTreeFile     = chanceTreeFile;
            Solver.EqualCa            = false;
            Solver.ActionTreeFile     = actionTreeFile;
            Solver.OutputPath         = BaseDir;
            Solver.SnapshotsCount     = 2;
            Solver.Epsilon            = Epsilon;
            Solver.ThreadsCount       = 6;
            Solver.IsVerbose          = true;
            Solver.IterationVerbosity = 10000;
            Solver.MaxIterationCount  = 1000000000;

            if (Configure != null)
            {
                Configure(Solver);
            }
            Solver.Solve();

            StrategyTree[] eqStrategies = new StrategyTree[playersCount];

            for (int p = 0; p < playersCount; ++p)
            {
                string fileName = Solver.CurrentSnapshotInfo.StrategyFile[p];
                eqStrategies[p] = StrategyTree.Read <StrategyTree>(fileName);
                if (VisualizeTrees)
                {
                    VisStrategyTree.Show(eqStrategies[p], fileName + ".gv");
                }
            }

            return(eqStrategies);
        }
Beispiel #2
0
        static int Main(string[] args)
        {
            if (!Parser.ParseArgumentsWithUsage(args, _cmdLine))
            {
                return(1);
            }

            if (_cmdLine.DebuggerLaunch)
            {
                Debugger.Launch();
            }

            StrategyTree st = StrategyTree.Read <StrategyTree>(_cmdLine.StrategyTree);

            if (_cmdLine.Verify)
            {
                string error = "";
                bool   isOk  = true;
                if (_cmdLine.IsAbsolute)
                {
                    Console.Write("Verifying absolute strategy ...");
                    isOk = VerifyAbsStrategy.Verify(st, _cmdLine.HeroPosition, 1e-7, out error);
                }
                else
                {
                    Console.Write("Verifying conditional strategy ...");
                    isOk = VerifyCondStrategy.Verify(st, _cmdLine.HeroPosition, 1e-7, out error);
                }
                if (isOk)
                {
                    Console.WriteLine(" OK");
                }
                else
                {
                    Console.WriteLine(" Verification failed: {0}", error);
                    return(1);
                }
            }

            AnalyzeStrategyTree.AnalyzeS(st, _cmdLine.IsAbsolute, _cmdLine.HeroPosition);

            return(0);
        }
Beispiel #3
0
        /// <summary>
        /// Runs the solver with the specified parameters..
        /// </summary>
        /// <param name="iterCounts">Number of iterations for each run, -1 - unlimited.</param>
        private StrategyTree RunSolver(TestParams testParams, bool visualize, bool trace, int [] iterCounts, ConfigureSolver configureSolver)
        {
            int playersCount = testParams.ChanceTree.PlayersCount;

            string baseDir = Path.Combine(_outDir, testParams.Name);

            DirectoryExt.Delete(baseDir);
            Directory.CreateDirectory(baseDir);

            string inputDir = Path.Combine(baseDir, "input");

            Directory.CreateDirectory(inputDir);
            string traceDir = Path.Combine(baseDir, "trace");

            if (trace)
            {
                Directory.CreateDirectory(traceDir);
            }

            string chanceTreeFile = Path.Combine(inputDir, "ct.dat");
            string actionTreeFile = Path.Combine(inputDir, "at.dat");

            testParams.ChanceTree.Write(chanceTreeFile);
            testParams.ActionTree.Write(actionTreeFile);

            if (visualize)
            {
                VisActionTree.Show(testParams.ActionTree, actionTreeFile + ".gv");
                VisChanceTree.Show(testParams.ChanceTree, chanceTreeFile + ".gv");
            }


            int  runs   = iterCounts.Length;
            Breb solver = null;

            for (int r = 0; r < runs; ++r)
            {
                // Create and configure a solver
                solver = new Breb
                {
                    GameDef        = testParams.GameDef,
                    ActionTreeFile = actionTreeFile,
                    ChanceTreeFile = chanceTreeFile,
                    OutputPath     = baseDir,
                    SnapshotsCount = 2,
                    Epsilon        = testParams.Epsilon,
                    ThreadsCount   = DEFAULT_THREADS_COUNT,
                };
                if (trace)
                {
                    solver.TraceDir = traceDir;
                }
                solver.MaxIterationCount = iterCounts[r];
                if (configureSolver != null)
                {
                    configureSolver(solver);
                }
                solver.Solve();
            }

            string       fileName   = solver.CurrentSnapshotInfo.StrategyFile;
            StrategyTree eqStrategy = StrategyTree.Read <StrategyTree>(fileName);

            if (visualize)
            {
                VisStrategyTree.Show(eqStrategy, fileName + ".gv");
            }
            return(eqStrategy);
        }
Beispiel #4
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);
        }