Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 398412b

Browse files
Prateek KhandelwalPrateek Khandelwal
authored andcommittedOct 25, 2024·
Fixing tests
1 parent 0a1e9a4 commit 398412b

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed
 

‎machine_learning/minimax.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
def minimax(depth, node_index, is_maximizing_player, scores, height):
55
"""
6-
Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game.
6+
Minimax algorithm to determine the optimal move for a player in a two-player
7+
zero-sum game.
78
89
Parameters:
910
- depth (int): Current depth in the game tree. Used to track recursion level.
10-
- node_index (int): Index of the current node in the scores array, representing leaf nodes.
11-
- is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise.
12-
- scores (list): A list of integers representing the scores at the leaf nodes of the game tree.
13-
- height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure.
11+
- node_index (int): Index of the current node in the scores array, representing
12+
leaf nodes.
13+
- is_maximizing_player (bool): True if the current player is the maximizing player
14+
, False otherwise.
15+
- scores (list): A list of integers representing the scores at the leaf nodes
16+
of the game tree.
17+
- height (int): The maximum depth of the game tree, based on the number of leaf
18+
nodes in a binary structure.
1419
1520
Returns:
1621
- int: The best score that the current player can achieve from this node.
@@ -51,7 +56,8 @@ def main():
5156

5257
# Print the optimal outcome for the maximizing player
5358
print(
54-
"Optimal value for the maximizing player:", minimax(0, 0, True, scores, height)
59+
"Optimal value for the maximizing player:",
60+
minimax(0, 0, True, scores, height)
5561
)
5662

5763

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import math
22

3-
4-
def minimax_with_pruning(
5-
depth, node_index, is_maximizing_player, scores, height, alpha, beta
6-
):
3+
def minimax_with_pruning(depth, node_index, is_maximizing_player, scores,
4+
height, alpha, beta):
75
"""
8-
Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency.
9-
6+
Minimax algorithm with alpha-beta pruning to determine the optimal
7+
move with improved efficiency.
8+
109
Parameters:
1110
- depth (int): Current depth in the game tree, used to track recursion level.
12-
- node_index (int): Index of the current node in the scores array, representing leaf nodes.
13-
- is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise.
14-
- scores (list): A list of integers representing the scores at the leaf nodes of the game tree.
15-
- height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure.
16-
- alpha (int): The best value that the maximizer can guarantee at the current level or above.
17-
- beta (int): The best value that the minimizer can guarantee at the current level or above.
11+
- node_index (int): Index of the current node in the scores array, representing
12+
leaf nodes.
13+
- is_maximizing_player (bool): True if the current player is the maximizing player
14+
, False otherwise.
15+
- scores (list): A list of integers representing the scores at the
16+
leaf nodes of the game tree.
17+
- height (int): The maximum depth of the game tree, based on the number of
18+
leaf nodes in a binary structure.
19+
- alpha (int): The best value that the maximizer can guarantee at the
20+
current level or above.
21+
- beta (int): The best value that the minimizer can guarantee at the
22+
current level or above.
1823
1924
Returns:
2025
- int: The best score that the current player can achieve from this node.
@@ -27,9 +32,8 @@ def minimax_with_pruning(
2732
if is_maximizing_player:
2833
best_score = -math.inf # Start with the worst possible score for maximizer
2934
for i in range(2): # Two branches at each level in binary tree
30-
val = minimax_with_pruning(
31-
depth + 1, node_index * 2 + i, False, scores, height, alpha, beta
32-
)
35+
val = minimax_with_pruning(depth + 1, node_index * 2 + i,
36+
False, scores, height, alpha, beta)
3337
best_score = max(best_score, val) # Maximizer selects the maximum value
3438
alpha = max(alpha, best_score) # Update alpha (best option for maximizer)
3539
if beta <= alpha:
@@ -40,9 +44,8 @@ def minimax_with_pruning(
4044
else:
4145
best_score = math.inf # Start with the worst possible score for minimizer
4246
for i in range(2):
43-
val = minimax_with_pruning(
44-
depth + 1, node_index * 2 + i, True, scores, height, alpha, beta
45-
)
47+
val = minimax_with_pruning(depth + 1, node_index * 2 + i, True,
48+
scores, height, alpha, beta)
4649
best_score = min(best_score, val) # Minimizer selects the minimum value
4750
beta = min(beta, best_score) # Update beta (best option for minimizer)
4851
if beta <= alpha:
@@ -57,11 +60,8 @@ def main():
5760
height = math.ceil(math.log2(len(scores)))
5861

5962
# Print the optimal outcome for the maximizing player with alpha-beta pruning
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-
63+
print("Optimal value for the maximizing player with pruning:",
64+
minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf))
6565

6666
if __name__ == "__main__":
6767
main()

0 commit comments

Comments
 (0)
Please sign in to comment.