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]); } }
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]); } }
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); }
/// <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(); } }
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); }