Пример #1
0
        static bool OnIterationDone(FictitiousPlayMc solver)
        {
            if ((DateTime.Now - _lastSnapshotTime).TotalMinutes >= _cmdLine.SnapshotTime)
            {
                // Do snapshot by timer
                Console.WriteLine("Make a snapshot by timer");
                _restart = true;
                return(false);
            }

            // Do some iterations to ensure stable epsilon.
            if (solver.CurrentIterationCount > 20)
            {
                if (!_epsilonsAdjusted)
                {
                    RemoveReachedEpsilons(solver.CurrentEpsilon);
                    _epsilonsAdjusted = true;
                }
                // The last epsilon is already set in the solver, so check
                // only if there are many.
                if (_epsilons.Count > 1 && solver.CurrentEpsilon <= _epsilons[0])
                {
                    Console.WriteLine("Epsilon {0} reached, make a snapshot", _epsilons[0]);
                    _reachedEpsilon = _epsilons[0];
                    _epsilons.RemoveAt(0);
                    _restart = true;
                    return(false);
                }
            }
            if (IsExitKeyPressed())
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
        static int Main(string[] args)
        {
            throw new NotImplementedException();
#if false
            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");
            }

            Console.WriteLine("Chance abstractions are {0}.", _cmdLine.EqualCa ? "equal" : "unequal");


            if (!ParseEpsilons())
            {
                return(1);
            }

            for (; ;)
            {
                FictitiousPlayMc solver = new FictitiousPlayMc
                {
                    ChanceTreeFile      = _cmdLine.ChanceTree,
                    EqualCa             = _cmdLine.EqualCa,
                    ActionTreeFile      = _cmdLine.ActionTree,
                    OutputPath          = _cmdLine.Output,
                    SnapshotsCount      = _cmdLine.SnapshotCount,
                    Epsilon             = _epsilons.LastOrDefault(),
                    EpsilonLogThreshold =
                        double.Parse(_cmdLine.EpsilonLogThreshold,
                                     CultureInfo.InvariantCulture),
                    OnIterationDone    = OnIterationDone,
                    IterationVerbosity = _cmdLine.IterationVerbosity,
                    ThreadsCount       = _cmdLine.ThreadCount,
                    IsVerbose          = true
                };

                _lastSnapshotTime = DateTime.Now;
                _restart          = false;
                solver.Solve();

                if (_reachedEpsilon > 0 || solver.CurrentEpsilon < solver.Epsilon)
                {
                    // Either an intermediate or the final epsilon is reached.
                    // Copy the snapshot to make sure it will not get lost.
                    string targetPath = Path.Combine(solver.OutputPath, string.Format("eps-{0:0.0000}", solver.CurrentEpsilon));
                    string sourcePath = solver.CurrentSnapshotInfo.BaseDir;
                    Console.WriteLine("Copy epsilon snapshot {0} to {1}", sourcePath, targetPath);
                    DirectoryExt.Copy(sourcePath, targetPath);
                    _reachedEpsilon = -1;
                }

                if (!_restart)
                {
                    break;
                }
            }
#endif
            return(0);
        }
        /// <summary>
        /// Runs FictitiousPlay with the specified parameters..
        /// </summary>
        /// <param name="iterCounts">Number of iterations for each run, -1 - unlimited.</param>
        private StrategyTree RunFictPlay(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;
            FictitiousPlayMc solver = null;

            for (int r = 0; r < runs; ++r)
            {
                // Create and configure a solver
                solver = new FictitiousPlayMc
                {
                    GameDef           = testParams.GameDef,
                    ChanceAbstraction = testParams.ChanceAbstraction,
                    ActionTreeFile    = actionTreeFile,
                    OutputPath        = baseDir,
                    SnapshotsCount    = 2,
                    Epsilon           = testParams.Epsilon,
                    ThreadsCount      = DEFAULT_THREADS_COUNT,
                    CardCount         = testParams.CardCount
                };
                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);
        }