Пример #1
0
        public Node Min(Node node, double alpha, double beta)
        {
            if (IsTerminal(node)) return node;

            double v = double.PositiveInfinity;

            foreach (var successor in (node.State as IAdversarialState).GetSuccessors())
            {
                if (!ProcessEvent(node, successor)) return node;

                var s = successor.State as IAdversarialState;
                var child = new Node(node, successor) { Cost = s.Utility, Depth = node.Depth + 1 };
                node.AddChild(child);

                var g = Max(child, alpha, beta);
                v = Math.Min(v, g.Cost);
                child.Cost = g.Cost;
                node.Cost = v;

                // check to see if we can prune
                if (v <= alpha) return child;
                beta = Math.Min(beta, v);
            }

            if (node.Children.Count == 0)
                return node;
            else
                return GetBestChildNode(node, v);
        }
Пример #2
0
 internal bool ProcessEvent(Node parentNode, ISuccessor successor)
 {
     var state = parentNode.State as IAdversarialState;
     var args = new StateExpansionEventArgs(state, successor, parentNode.Cost, parentNode.Depth);
     OnSuccessorExpanded(this, args);
     return !args.CancelExpansion;
 }
Пример #3
0
        private Node Find(Node node, double initial, Func<double, double, double> limit, Func<Node, Node> f)
        {
            if (IsTerminal(node)) return node;

            double v = initial;

            foreach (var successor in (node.State as IAdversarialState).GetSuccessors())
            {
                if (!ProcessEvent(node, successor)) return node;

                var s = successor.State as IAdversarialState;
                var child = new Node(node, successor) { Cost = s.Utility, Depth = node.Depth + 1 };
                node.AddChild(child);

                var g = f(child);
                v = limit(v, g.Cost);
                child.Cost = g.Cost;
                node.Cost = v;
            }

            if (node.Children.Count == 0)
                return node;
            else
                return GetBestChildNode(node, v);
        }
Пример #4
0
        public override void Add(Node node)
        {
            if (Heuristic == null)
                throw new InvalidOperationException("Invalid Heuristic!");

            var h = node.Cost + Heuristic(node.State);
            Add(node, h);
        }
Пример #5
0
 private void CreateSolution(Node n)
 {
     if (Solution == null) Solution = new List<ISuccessor>();
     var node = n;
     while (!node.IsRoot)
     {
         Solution.Add(node.Successor);
         node = node.Parent;
     }
     Solution.Reverse();
 }
Пример #6
0
 public Node(Node parent, ISuccessor successor)
 {
     State = successor.State;
     Parent = parent;
     Successor = successor;
     Cost = parent.Cost + successor.Cost;
     Depth = parent.Depth + 1;
     Path = false;
     Children = new List<Node>();
     _stateComparer = new StateComparer();
 }
Пример #7
0
        public override ISuccessor Find(IAdversarialState state)
        {
            Root = new Node(state);
            Node a;

            if (state.Player)
                a = Max(Root, double.NegativeInfinity, double.PositiveInfinity);
            else
                a = Min(Root, double.NegativeInfinity, double.PositiveInfinity);

            return a.Successor;
        }
Пример #8
0
        public override ISuccessor Find(IAdversarialState state)
        {
            Root = new Node(state);
            Node a;

            if (state.Player)
                a = Max(Root);
            else
                a = Min(Root);

            return a.Successor;
        }
Пример #9
0
        internal Node GetBestChildNode(Node node, double v)
        {
            var q = node.Children
                        .Where(n => n.Cost == v && n.State.IsTerminal);

            Node r;
            if (q.Count() > 0) // favor terminal nodes first
                r = q.Rand();
            else
                r = node.Children
                            .Where(n => n.Cost == v)
                            .Rand();
            r.Cost = v;
            r.Path = true;
            return r;
        }
Пример #10
0
 internal bool IsTerminal(Node node)
 {
     return (node.State as IAdversarialState).IsTerminal ||
             node.Depth == Depth * 2;
 }
Пример #11
0
 public void Add(Node node)
 {
     _list.Push(node);
 }
Пример #12
0
 public abstract void Add(Node node);
Пример #13
0
 public virtual Node Min(Node node)
 {
     return Find(node, double.PositiveInfinity, Math.Min, Max);
 }
Пример #14
0
 public virtual Node Max(Node node)
 {
     return Find(node, double.NegativeInfinity, Math.Max, Min);
 }
Пример #15
0
        public void AddChild(Node n)
        {
            if (State == null)
                throw new InvalidOperationException("Invalid node state!");

            Children.Add(n);
        }
Пример #16
0
 public void Add(Node node, double cost)
 {
     _queue.Enqueue(cost, node);
 }
Пример #17
0
 public void Add(Node node)
 {
     _list.Enqueue(node);
 }
Пример #18
0
 public void Add(Node node)
 {
     if (node.Depth <= _limit)
         _list.Push(node);
 }