Пример #1
0
 public bool Equals(Node other)
 {
     if (c != other.c)
         return false;
     if (position.Length != other.position.Length)
         return false;
     for (int i = 0; i < position.Length; ++i)
         if (!position[i].Equals(other.position[i]))
             return false;
     return true;
 }
Пример #2
0
 public Node MostProving(Node v)
 {
     Node.InitiateVisiting ();
     while (v.children != null) {
         v = LeftmostNode (v);
         if (v.IsVisited ()) {
             //v.pn *= 2;
             //v.dn *= 2;
             break;
         }
     }
     return v;
 }
Пример #3
0
 public Node LeftmostNode(Node v)
 {
     v.SetVisit ();
     foreach (var child in v.children)
         if (v.c == 1) {
             if (child.pn == v.pn && !child.IsVisited ())
                 return child;
         } else {
             if (child.dn == v.dn && !child.IsVisited ())
                 return child;
         }
     return v;
 }
Пример #4
0
        public void UpdateTree(Node n, bool firstRun)
        {
            if (this.Equals (n)) {
                if (firstRun)
                {
                    n.pn = Int32.MaxValue;
                    n.dn = 0;
                }
                else
                {
                    n.pn = 0;
                    n.dn = Int32.MaxValue;
                }
                n.Update ();
                return;
            }

            UpdateNumbers();

            foreach(var parent in parents)
                parent.UpdateTree(n, firstRun);
        }
Пример #5
0
        public void ExpandTree(Game g, ref int nodeCount, ref int collisionCount, Hashtable transposition)
        {
            if(children != null)
                return;
            children = new List<Node>();
            var plies = g.children(position, c);
            foreach (var ply in plies)
            {
                var s = new Node(ply.Apply(position, g), (byte)(c ^ 1));
                s.Evaluate(g);
                nodeCount++;

                if (transposition.ContainsKey(s))
                    collisionCount++;
                else
                    transposition.Add(s, s);

                s.parents.Add(this);
                children.Add(s);
            }
        }
Пример #6
0
 public void Expand(Game g, Hashtable transposition)
 {
     if(children != null)
         return;
     children = new List<Node>();
     var plies = g.children(position, c);
     foreach (var ply in plies)
     {
         var s = new Node(ply.Apply(position, g), (byte)(c ^ 1));
         if (transposition.ContainsKey(s))
             s = (Node)transposition[s];
         else
         {
             s.Evaluate(g);
             transposition[s] = s;
         }
         s.parents.Add(this);
         children.Add(s);
     }
 }
Пример #7
0
        public void Prove(Game g)
        {
            root = new Node (g.startingPos, 1);
            root.Evaluate (g);
            transposition [root] = root;
            nodeCount++;
            Node lastExpanded = null;
            int lastpn = 0, lastdn = 0;

            int count = 0;

            Stopwatch sw = new Stopwatch ();
            sw.Start ();
            while (root.pn != 0 && root.dn != 0) {
                var mpn = MostProving (root);

                if (graph) {
                    if (System.Object.ReferenceEquals (mpn, lastExpanded) && lastpn == mpn.pn && lastdn == mpn.dn) {
                        sw.Stop ();
                        timeSpent = (int)sw.ElapsedMilliseconds;
                        throw new Exception ("Loop in finding most proving");
                    }
                    mpn.Expand (g, transposition);
                    mpn.StartUpdate ();

                    lastExpanded = mpn;
                    lastpn = mpn.pn;
                    lastdn = mpn.dn;
                } else {
                    mpn.ExpandTree (g, ref  nodeCount, ref collisionCount, transposition);
                    mpn.StartUpdateTree (firstRun);

                    if (count % 10000 == 0)
                    {
                        Console.WriteLine("PN:         {0}\nDN:         {1}", root.pn, root.dn);
                        Console.WriteLine("Collisions: " + collisionCount);
                        Console.WriteLine("Nodes:      " + nodeCount);
                    }

                    count++;
                }

                if (sw.ElapsedMilliseconds > timeLimit * 60000) {
                    sw.Stop ();
                    timeSpent = (int)sw.ElapsedMilliseconds;
                    throw new Exception ("Time limit exceeded");
                }
            }

            sw.Stop ();
            timeSpent = (int)sw.ElapsedMilliseconds;

            // If this is the first run of a graph search where black loses, run again to check for draw
            if (!graph && firstRun && root.dn == 0) {
                second = new PNSearch (false, timeLimit);
                second.firstRun = false;
                second.Prove (g);
            }
        }