Esempio n. 1
0
        public void Test_Merge()
        {
            IChanceAbstraction[] chanceAbstractions = new IChanceAbstraction[]
            {
                new LeducHeChanceAbstraction(LeducHeChanceAbstraction.FractionalResult),
                new LeducHeChanceAbstraction(LeducHeChanceAbstraction.FractionalResult)
            };

            CtMcGen.Tree tree1 = CtMcGen.Generate(_leducHeGd, chanceAbstractions, false, 10000, 333, null);
            CtMcGen.Tree tree2 = CtMcGen.Generate(_leducHeGd, chanceAbstractions, false, 10000, 333, null);

            UInt64 expSamplesCount = tree1.SamplesCount + tree2.SamplesCount;

            string fileName = Path.Combine(_outDir, "ctmcgen-tree2.dat");

            tree2.Write(fileName);
            tree1.Read(fileName);

            Assert.AreEqual(expSamplesCount, tree1.SamplesCount);

            ChanceTree ct1 = tree1.ConvertToChanceTree();
            ChanceTree ct2 = tree2.ConvertToChanceTree();

            // Compare two chance trees, they must be exactly the same.
            CompareChanceTrees cmp = new CompareChanceTrees();

            cmp.Compare(ct1, ct2);
            Assert.AreEqual(0, cmp.SumProbabDiff);
            for (int p = 0; p < chanceAbstractions.Length; ++p)
            {
                Assert.AreEqual(0, cmp.SumPotShareDiff[p]);
            }
        }
Esempio n. 2
0
        public void Test_ReadWrite()
        {
            IChanceAbstraction[] chanceAbstractions = new IChanceAbstraction[]
            {
                new LeducHeChanceAbstraction(LeducHeChanceAbstraction.FractionalResult),
                new LeducHeChanceAbstraction(LeducHeChanceAbstraction.FractionalResult)
            };

            CtMcGen.Tree tree1 = CtMcGen.Generate(_leducHeGd, chanceAbstractions, false, 10000, (int)DateTime.Now.Ticks, null);
            ChanceTree   ct1   = tree1.ConvertToChanceTree();

            string fileName = Path.Combine(_outDir, "ctmcgen-tree.dat");

            tree1.Write(fileName);
            CtMcGen.Tree tree2 = new CtMcGen.Tree();
            tree2.Read(fileName);

            // Compare public data
            Assert.AreEqual(tree1.CalculateLeavesCount(), tree2.CalculateLeavesCount());
            Assert.AreEqual(tree1.SamplesCount, tree2.SamplesCount);
            Assert.AreEqual(tree1.Version, tree2.Version);

            ChanceTree ct2 = tree2.ConvertToChanceTree();

            // Compare two chance trees, they must be exactly the same.
            CompareChanceTrees cmp = new CompareChanceTrees();

            cmp.Compare(ct1, ct2);
            Assert.AreEqual(0, cmp.SumProbabDiff);
            for (int p = 0; p < chanceAbstractions.Length; ++p)
            {
                Assert.AreEqual(0, cmp.SumPotShareDiff[p]);
            }
        }
Esempio n. 3
0
        private static string SaveFile(CtMcGen.Tree tree, GameDefinition gd, IChanceAbstraction[] chanceAbstractions)
        {
            string dir = _cmdLine.Output.Get(Props.Global);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            string fileName = Path.Combine(dir, GetUniqueFileName(gd, chanceAbstractions));

            for (int attempt = 0; ; ++attempt)
            {
                try
                {
                    tree.Write(fileName);
                    // Write was successful - exit loop
                    break;
                }
                catch (IOException e)
                {
                    if (attempt >= 2)
                    {
                        throw e;
                    }
                    // Maybe it was an exception because another process created the file with the same name.
                    // Wait a little (this will change the file name) and try again.
                    Thread.Sleep(500);
                }
            }
            return(fileName);
        }
Esempio n. 4
0
        static int Main(string[] args)
        {
            if (!Parser.ParseArgumentsWithUsage(args, _cmdLine))
            {
                return(1);
            }

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

            CtMcGen.Tree input = new CtMcGen.Tree();
            input.Read(_cmdLine.Input);
            long leavesCount = input.CalculateLeavesCount();

            Console.WriteLine("Input file: leaves: {0:#,#}, samples: {1:#,#}, av. samples: {2:#,#}",
                              leavesCount, input.SamplesCount, input.SamplesCount / (ulong)leavesCount);

            ChanceTree ct = input.ConvertToChanceTree();

            ct.Write(_cmdLine.Output);

            return(0);
        }
Esempio n. 5
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();
            }
        }
Esempio n. 6
0
        static int Main(string[] args)
        {
            if (!Parser.ParseArgumentsWithUsage(args, _cmdLine))
            {
                return(1);
            }

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

            _reIncludeFiles = new Regex(_cmdLine.IncludeFiles, RegexOptions.Compiled);

            if (string.IsNullOrEmpty(_cmdLine.Output))
            {
                Console.WriteLine("Output file name is missing");
                return(1);
            }
            _targetTree = new CtMcGen.Tree();
            if (File.Exists(_cmdLine.Output))
            {
                _targetTree.Read(_cmdLine.Output);
            }


            DateTime startTime = DateTime.Now;

            foreach (string path in _cmdLine.InputPaths)
            {
                ProcessPath(path);
            }

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

            _targetTree.Write(_cmdLine.Output);


            Console.WriteLine("TOTAL: samples: {0:#,#}, time: {1:0.0} s, {2:#,#} sm/s",
                              _targetTree.SamplesCount, time, _targetTree.SamplesCount / time);

            long leavesCount = _targetTree.CalculateLeavesCount();

            Console.WriteLine("Target file: leaves: {0:#,#}, samples: {1:#,#}, av. samples: {2:#,#}, path: {3}",
                              leavesCount, _targetTree.SamplesCount,
                              _targetTree.SamplesCount / (ulong)leavesCount,
                              _cmdLine.Output);

            return(0);
        }
Esempio n. 7
0
        static int Main(string[] args)
        {
            if (!Parser.ParseArgumentsWithUsage(args, _cmdLine))
            {
                return(1);
            }

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

            GameDefinition gd = XmlSerializerExt.Deserialize <GameDefinition>(_cmdLine.GameDef.Get(Props.Global));

            if (gd.MinPlayers != _cmdLine.ChanceAbstractionFiles.Length)
            {
                Console.WriteLine("Number of chance abstractions ({0}) does not match to the minimal number of players in game definition ({1})",
                                  _cmdLine.ChanceAbstractionFiles.Length, gd.MinPlayers);
                return(1);
            }

            IChanceAbstraction[] chanceAbstractions = new IChanceAbstraction[_cmdLine.ChanceAbstractionFiles.Length];
            bool   areAbstractionsEqual             = true;
            string fileName0 = _cmdLine.ChanceAbstractionFiles[0].Get().ToUpperInvariant();

            for (int p = 0; p < chanceAbstractions.Length; ++p)
            {
                string fileName = _cmdLine.ChanceAbstractionFiles[p];
                if (fileName.ToUpperInvariant() != fileName0)
                {
                    areAbstractionsEqual = false;
                }
                chanceAbstractions[p] = ChanceAbstractionHelper.CreateFromPropsFile(fileName);
                Console.WriteLine("CA pos {0}: {1}", p, chanceAbstractions[p].Name);
            }

            Console.WriteLine("Abstractions are {0}.", areAbstractionsEqual ? "equal" : "unequal");
            Console.Write("Samples: {0:#,#}. ", _cmdLine.SamplesCount);

            if (_cmdLine.RunsCount > 0)
            {
                Console.Write("Runs: {0:#,#}", _cmdLine.RunsCount);
            }
            else
            {
                Console.WriteLine("Runs: unlimited");
                Console.Write("Press 'q' to quit asap, 'f' to finish run.");
            }
            Console.WriteLine();

            for (int run = 0; ;)
            {
                int rngSeed = (int)DateTime.Now.Ticks;

                Console.Write("Run:{0}, seed:{1}. ", run, rngSeed);

                DateTime startTime = DateTime.Now;

                CtMcGen.Tree tree = CtMcGen.Generate(gd, chanceAbstractions, areAbstractionsEqual, _cmdLine.SamplesCount, rngSeed, Feedback);

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

                string fileName = SaveFile(tree, gd, chanceAbstractions);

                Int64 samplesDone = (Int64)tree.SamplesCount;

                Console.WriteLine("Samples: {0:#,#}, time: {1:0.0} s, {2:#,#} sm/s, file: {3}",
                                  samplesDone,
                                  genTime,
                                  samplesDone / genTime,
                                  fileName);

                ++run;
                if (_cmdLine.RunsCount > 0)
                {
                    if (run >= _cmdLine.RunsCount)
                    {
                        break;
                    }
                }
                else
                {
                    ProcessKeys();
                    if (_quitAsap || _finishRun)
                    {
                        Console.WriteLine("Exiting on user request");
                        break;
                    }
                }
            }
            return(0);
        }