Ejemplo n.º 1
0
        public void ConstructTree()
        {
            while (!(NodesStack.Count == 1 && NodesStack.Peek().AllNextVisited()) && Computing)
            {
                Node peek = NodesStack.Peek();
                if (!peek.Full)
                {
                    if (peek.NextNodes.Count == 0)  //means that not built yet
                    {
                        BuildNextNodes(peek);
                        if (MustBeat)
                        {
                            break;
                        }
                    }
                    else
                    {
                        Node next = peek.FindFirstEmpty();
                        if (next == null)  // just visited all next nodes
                        {
                            peek.Full = true;
                        }
                        else // built but not all is visited yet
                        {
                            NodesStack.Push(next);
                        }
                    }
                }
                else  // visited all next nodes -> go back
                {
                    NodesStack.Pop();
                    peek.ResetNextNodes();
                }
            }
            Root.ResetNextNodes();



            if (Computing)
            {
                if (MustBeat)
                {
                    Console.Write("Must beat step (depth 1)");
                    NodesStack.Peek().Benefit = FindMaximumBenefit(Root);
                }
                else
                {
                    ComputeAnswer();
                }

                KeyValuePair <Node, byte[]> pair = Root.NextNodes.Find(x => x.Key.Benefit == Root.Benefit);
                Answer = pair.Value;
            }
            //step.State.PrintBoard();
            //Root.PrintPretty("", true, "");
        }
        public void GoForward()
        {
            String nodePath = ForwardStack.Pop();

            if (!String.IsNullOrEmpty(nodePath))
            {
                if (_treeView.SelectedItem != null)
                {
                    BackStack.Push(_treeView.SelectedItem.Path);
                }

                ShouldAddToStack = false;
                _treeView.ShowNode(nodePath);
            }
        }
Ejemplo n.º 3
0
        private void ComputeAnswer()//getting the tree result comapring nodes
        {
            while (!(NodesStack.Count == 1 && NodesStack.Peek().AllNextVisited()))
            {
                Node peek = NodesStack.Peek();
                if (peek.AllNextVisited())
                {
                    peek.Benefit = peek.AgentIndex == 0 ? FindMaximumBenefit(peek) : FindMinimumBenefit(peek);

                    //_benefitsOnLevel.Add(NodesStack.Count, new List<sbyte>());//attempt to do alpha beta for minimax tree
                    //_benefitsOnLevel[NodesStack.Count].Add(peek.Benefit);
                    //if ((NodesStack.Count % 2 == 1 && _benefitsOnLevel[NodesStack.Count].Max() > peek.Benefit) || (NodesStack.Count % 2 == 0 && _benefitsOnLevel[NodesStack.Count].Min() < peek.Benefit))
                    //{
                    //    NodesStack.Pop();
                    //}
                    peek.Full = true;
                    NodesStack.Pop();
                    peek.ResetNextNodes();
                }
                else
                {
                    if (peek.NextNodes.Count == 0)
                    {
                        NodesStack.Pop();
                        peek.Full = true;
                    }
                    else
                    {
                        Node next = peek.FindFirstEmpty();
                        NodesStack.Push(next);
                    }
                }
            }
            NodesStack.Peek().Benefit = FindMaximumBenefit(Root);
            Root.ResetNextNodes();
            //logging tree depth
            Console.Write(String.Concat("Try depth: ", TreeDepth * 2 + 1, ", "));
        }