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); }
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 Node Max(Node node, double alpha, double beta) { if (IsTerminal(node)) { return(node); } double v = double.NegativeInfinity; 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 = Min(child, alpha, beta); v = Math.Max(v, g.Cost); child.Cost = g.Cost; node.Cost = v; // check to see if we can prune if (v >= beta) { return(child); } alpha = Math.Max(alpha, v); } if (node.Children.Count == 0) { return(node); } else { return(GetBestChildNode(node, v)); } }
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)); } }