public void WholeTree()
        {
            var exit = new ExitConditions
            {
                Duration       = TimeSpan.FromSeconds(1),
                StopOnSolution = true,
                TotalNodes     = int.MaxValue,
                TotalDead      = int.MaxValue
            };
            var command = new SolverCommand
            {
                Puzzle = Puzzle.Builder.DefaultTestPuzzle(),

                ExitConditions = exit
            };

            // act
            var solver = new SingleThreadedForwardSolver(new SolverNodeFactoryTrivial());
            var result = solver.Init(command);

            solver.Solve(result);
            result.ThrowErrors();

            var allNodes = ((SolverBaseState)result).Root.Recurse().ToArray();

            var mem    = new MemoryStream();
            var writer = new BinaryNodeSerializer();

            using (var sw = new BinaryWriter(mem, Encoding.Unicode, true))
            {
                writer.Write(sw, allNodes);
            }

            outp.WriteLine($"Memory Stream Size = {allNodes.Length}nodes => {mem.Length}b");

            mem.Seek(0, SeekOrigin.Begin);

            using (var sr = new BinaryReader(mem))
            {
                var all = writer.ReadAll(sr).ToArray();

                Assert.Equal(allNodes.Length, all.Length);
            }
        }
Beispiel #2
0
        public static void Run(string file, string report)
        {
            System.Console.WriteLine($"<file> {file} --report {report}");


            var ser = new BinaryNodeSerializer();

            if (report == "depth")
            {
                using (var f = System.IO.File.OpenRead(file))
                {
                    using (var br = new BinaryReader(f))
                    {
                        System.Console.WriteLine($"Reading File...");
                        var root = ser.AssembleTree(br);



                        var repDepth = MapToReporting.Create <SolverHelper.DepthLineItem>()
                                       .AddColumn("Depth", x => x.Depth)
                                       .AddColumn("Total", x => x.Total)
                                       .AddColumn("Growth Rate", x => x.GrowthRate)
                                       .AddColumn("UnEval", x => x.UnEval)
                                       .AddColumn("Complete", x => (x.Total - x.UnEval) * 100 / x.Total, c => c.ColumnInfo.AsPercentage());
                        repDepth.RenderTo(SolverHelper.ReportDepth(root), new MapToReportingRendererText(), System.Console.Out);
                    }
                }
            }
            else if (report == "clash")
            {
                using (var f = File.OpenRead(file))
                {
                    var writer = new BinaryNodeSerializer();
                    var nodes  = writer.ReadAll(new BinaryReader(f));

                    Dictionary <int, ClashLineItem> hash = new Dictionary <int, ClashLineItem>();
                    foreach (var n in nodes)
                    {
                        if (hash.TryGetValue(n.HashCode, out var c))
                        {
                            c.Count++;

                            if (c.First.CrateMap.Equals(n.CrateMap) && c.First.MoveMap.Equals(n.MoveMap))
                            {
                                c.Dups++;
                            }
                        }
                        else
                        {
                            hash[n.HashCode] = new ClashLineItem()
                            {
                                Hash  = n.HashCode,
                                Count = 1,
                                First = n
                            };
                        }
                    }

                    var items = hash.Where(x => x.Value.Count > 1).OrderByDescending(x => x.Value.Dups).ThenByDescending(x => x.Value.Count).Take(50).Select(x => x.Value);
                    MapToReporting.Create <ClashLineItem>()
                    .AddColumn("Hash", x => x.Hash)
                    .AddColumn("Count", x => x.Count)
                    .AddColumn("Dups", x => x.Dups)
                    .RenderTo(items, new MapToReportingRendererText(), System.Console.Out);


                    System.Console.WriteLine($"Total Dups: {hash.Values.Sum(x=>x.Dups)}");
                }
            }
            else if (report == "dump")
            {
                using (var f = File.OpenRead(file))
                {
                    var writer = new BinaryNodeSerializer();
                    using (var br = new BinaryReader(f))
                    {
                        foreach (var node in writer.ReadAll(br).OrderBy(x => x.SolverNodeId).Take(20))
                        {
                            System.Console.WriteLine(node);
                        }
                    }
                }
            }
            else
            {
                throw new Exception($"Unknown Report: {report}");
            }
        }