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); }
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; }
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); }
public override void Add(Node node) { if (Heuristic == null) throw new InvalidOperationException("Invalid Heuristic!"); var h = node.Cost + Heuristic(node.State); Add(node, h); }
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(); }
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(); }
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; }
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; }
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; }
internal bool IsTerminal(Node node) { return (node.State as IAdversarialState).IsTerminal || node.Depth == Depth * 2; }
public void Add(Node node) { _list.Push(node); }
public abstract void Add(Node node);
public virtual Node Min(Node node) { return Find(node, double.PositiveInfinity, Math.Min, Max); }
public virtual Node Max(Node node) { return Find(node, double.NegativeInfinity, Math.Max, Min); }
public void AddChild(Node n) { if (State == null) throw new InvalidOperationException("Invalid node state!"); Children.Add(n); }
public void Add(Node node, double cost) { _queue.Enqueue(cost, node); }
public void Add(Node node) { _list.Enqueue(node); }
public void Add(Node node) { if (node.Depth <= _limit) _list.Push(node); }