Skip to content

Commit 0a1e9a4

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 464a863 commit 0a1e9a4

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

machine_learning/minimax.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22

3+
34
def minimax(depth, node_index, is_maximizing_player, scores, height):
45
"""
56
Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game.
@@ -25,7 +26,9 @@ def minimax(depth, node_index, is_maximizing_player, scores, height):
2526
for i in range(2):
2627
# Recursive call for the next level of depth
2728
val = minimax(depth + 1, node_index * 2 + i, False, scores, height)
28-
best_score = max(best_score, val) # Maximizer chooses the highest score available
29+
best_score = max(
30+
best_score, val
31+
) # Maximizer chooses the highest score available
2932
return best_score
3033

3134
# Minimizing player's move
@@ -34,17 +37,23 @@ def minimax(depth, node_index, is_maximizing_player, scores, height):
3437
for i in range(2):
3538
# Recursive call for the next level of depth
3639
val = minimax(depth + 1, node_index * 2 + i, True, scores, height)
37-
best_score = min(best_score, val) # Minimizer chooses the lowest score available
40+
best_score = min(
41+
best_score, val
42+
) # Minimizer chooses the lowest score available
3843
return best_score
3944

45+
4046
def main():
4147
# Scores array representing the leaf nodes of a binary tree (depth = 3)
4248
scores = [3, 5, 2, 9, 12, 5, 23, 23]
4349
# Calculate the height of the binary tree based on the number of leaf nodes
4450
height = math.ceil(math.log2(len(scores)))
4551

4652
# Print the optimal outcome for the maximizing player
47-
print("Optimal value for the maximizing player:", minimax(0, 0, True, scores, height))
53+
print(
54+
"Optimal value for the maximizing player:", minimax(0, 0, True, scores, height)
55+
)
56+
4857

4958
if __name__ == "__main__":
5059
main()

machine_learning/minimax_alpha_beta_pruning.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import math
22

3-
def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, height, alpha, beta):
3+
4+
def minimax_with_pruning(
5+
depth, node_index, is_maximizing_player, scores, height, alpha, beta
6+
):
47
"""
58
Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency.
6-
9+
710
Parameters:
811
- depth (int): Current depth in the game tree, used to track recursion level.
912
- node_index (int): Index of the current node in the scores array, representing leaf nodes.
@@ -24,7 +27,9 @@ def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, height
2427
if is_maximizing_player:
2528
best_score = -math.inf # Start with the worst possible score for maximizer
2629
for i in range(2): # Two branches at each level in binary tree
27-
val = minimax_with_pruning(depth + 1, node_index * 2 + i, False, scores, height, alpha, beta)
30+
val = minimax_with_pruning(
31+
depth + 1, node_index * 2 + i, False, scores, height, alpha, beta
32+
)
2833
best_score = max(best_score, val) # Maximizer selects the maximum value
2934
alpha = max(alpha, best_score) # Update alpha (best option for maximizer)
3035
if beta <= alpha:
@@ -35,21 +40,28 @@ def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, height
3540
else:
3641
best_score = math.inf # Start with the worst possible score for minimizer
3742
for i in range(2):
38-
val = minimax_with_pruning(depth + 1, node_index * 2 + i, True, scores, height, alpha, beta)
43+
val = minimax_with_pruning(
44+
depth + 1, node_index * 2 + i, True, scores, height, alpha, beta
45+
)
3946
best_score = min(best_score, val) # Minimizer selects the minimum value
4047
beta = min(beta, best_score) # Update beta (best option for minimizer)
4148
if beta <= alpha:
4249
break # Alpha cut-off
4350
return best_score
4451

52+
4553
def main():
4654
# Scores array representing the leaf nodes of a binary tree (depth = 3)
4755
scores = [3, 5, 2, 9, 12, 5, 23, 23]
4856
# Calculate the height of the binary tree based on the number of leaf nodes
4957
height = math.ceil(math.log2(len(scores)))
5058

5159
# Print the optimal outcome for the maximizing player with alpha-beta pruning
52-
print("Optimal value for the maximizing player with pruning:", minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf))
60+
print(
61+
"Optimal value for the maximizing player with pruning:",
62+
minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf),
63+
)
64+
5365

5466
if __name__ == "__main__":
5567
main()

0 commit comments

Comments
 (0)