-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlpha_beta_pruning_using_array.py
More file actions
41 lines (34 loc) · 1.25 KB
/
Copy pathAlpha_beta_pruning_using_array.py
File metadata and controls
41 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import math
def minimax_alpha_beta(node, depth, alpha, beta, is_maximizing):
if depth == 0 or is_terminal(node):
return evaluate(node)
if is_maximizing:
value = -math.inf
for child in generate_children(node):
value = max(value, minimax_alpha_beta(child, depth - 1, alpha, beta, False))
alpha = max(alpha, value)
if beta <= alpha:
break # Prune the remaining branches
return value
else:
value = math.inf
for child in generate_children(node):
value = min(value, minimax_alpha_beta(child, depth - 1, alpha, beta, True))
beta = min(beta, value)
if beta <= alpha:
break # Prune the remaining branches
return value
def is_terminal(node):
# Define your terminal condition here
pass
def evaluate(node):
# Define your evaluation function here
pass
def generate_children(node):
# Generate the children nodes of the current node
pass
# Example usage
if __name__ == "__main__":
tree = [3, 5, 6, 9, 1, 2, 0, 8] # Replace with your tree structure
result = minimax_alpha_beta(tree, depth=3, alpha=-math.inf, beta=math.inf, is_maximizing=True)
print("Optimal value:", result)