Ejemplo n.º 1
0
 public override Task <LeafNode> BestNodeAsync(Tournament <T> tournament, IAsyncComparator <T> comparator, CancellationToken cancellationToken)
 {
     return(Task.FromResult(this));
 }
Ejemplo n.º 2
0
        static void PlotBrackets(IAsyncComparator <T> comparator, Node topMostRoot)
        {
            Graph graph = Graph.Directed;

            Dictionary <Node, NodeId> nodeIdCache = new Dictionary <Node, NodeId>();
            int bracketCounter = 0;

            string NodeToString(Node node)
            {
                if (node is LeafNode)
                {
                    return(((LeafNode)node).Item.ToString());
                }
                else
                {
                    return($"B{++bracketCounter}");
                }
            }

            NodeId NodeToNodeId(Node node)
            {
                if (!nodeIdCache.ContainsKey(node))
                {
                    nodeIdCache[node] = new NodeId(NodeToString(node));
                }
                return(nodeIdCache[node]);
            }

            void Recurse(Node root)
            {
                if (root is BracketNode)
                {
                    BracketNode node = root as BracketNode;

                    Debug.Assert(node.CompetitorA.Parent == node);
                    Debug.Assert(node.CompetitorB.Parent == node);

                    graph = graph.Add(EdgeStatement.For(
                                          NodeToNodeId(node),
                                          NodeToNodeId(node.CompetitorA)));
                    graph = graph.Add(EdgeStatement.For(
                                          NodeToNodeId(node),
                                          NodeToNodeId(node.CompetitorB)));

                    Recurse(node.CompetitorA);
                    Recurse(node.CompetitorB);
                }
            }

            Recurse(topMostRoot);

            IRenderer renderer = new Renderer(@"C:\Program Files (x86)\Graphviz2.38\bin");

            using (Stream file = File.Create("graph.png"))
            {
                AsyncHelper.RunSync(async() => await renderer.RunAsync(
                                        graph, file,
                                        RendererLayouts.Dot,
                                        RendererFormats.Png,
                                        CancellationToken.None));
            }
        }
Ejemplo n.º 3
0
 public abstract Task <LeafNode> BestNodeAsync(Tournament <T> tournament,
                                               IAsyncComparator <T> comparator, CancellationToken cancellationToken);
Ejemplo n.º 4
0
 public async override Task <LeafNode> BestNodeAsync(Tournament <T> tournament, IAsyncComparator <T> comparator, CancellationToken cancellationToken)
 {
     if (BestCompetitor == null)
     {
         Debug.Assert(tournament.NextWinnerStepsMax > 0);
         Debug.Assert(tournament.NextWinnerStepsMax - tournament.NextWinnerStepsDone > 0);
         cancellationToken.ThrowIfCancellationRequested();
         T itemA = (await CompetitorA.BestNodeAsync(tournament, comparator, cancellationToken)).Item;
         cancellationToken.ThrowIfCancellationRequested();
         T itemB = (await CompetitorB.BestNodeAsync(tournament, comparator, cancellationToken)).Item;
         cancellationToken.ThrowIfCancellationRequested();
         BestCompetitor = (await comparator.CompareAsync(itemA, itemB, cancellationToken)) > 0 ? CompetitorA : CompetitorB;
         tournament.NextWinnerStepsDone++;
         Debug.Assert(tournament.NextWinnerStepsDone <= tournament.NextWinnerStepsMax);
         tournament.GlobalStepsDone++;
         Debug.Assert(tournament.GlobalStepsDone <= tournament.GlobalStepsMax);
     }
     return(await BestCompetitor.BestNodeAsync(tournament, comparator, cancellationToken));
 }