From 464a863b99adc219b09667c4a3f2bfcb1bf27ee7 Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal Date: Fri, 25 Oct 2024 18:03:16 +0530 Subject: [PATCH 01/10] Added minimax algorithm --- machine_learning/minimax.py | 50 +++++++++++++++++ .../minimax_alpha_beta_pruning.py | 55 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 machine_learning/minimax.py create mode 100644 machine_learning/minimax_alpha_beta_pruning.py diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py new file mode 100644 index 000000000000..764f66563845 --- /dev/null +++ b/machine_learning/minimax.py @@ -0,0 +1,50 @@ +import math + +def minimax(depth, node_index, is_maximizing_player, scores, height): + """ + Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. + + Parameters: + - depth (int): Current depth in the game tree. Used to track recursion level. + - node_index (int): Index of the current node in the scores array, representing leaf nodes. + - is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise. + - scores (list): A list of integers representing the scores at the leaf nodes of the game tree. + - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure. + + Returns: + - int: The best score that the current player can achieve from this node. + """ + # Base case: If the maximum depth is reached, return the score at this node. + if depth == height: + return scores[node_index] + + # Maximizing player's move + if is_maximizing_player: + best_score = -math.inf # Start with the worst possible score for maximizer + # Simulate two possible moves (binary tree branches) + for i in range(2): + # Recursive call for the next level of depth + val = minimax(depth + 1, node_index * 2 + i, False, scores, height) + best_score = max(best_score, val) # Maximizer chooses the highest score available + return best_score + + # Minimizing player's move + else: + best_score = math.inf # Start with the worst possible score for minimizer + for i in range(2): + # Recursive call for the next level of depth + val = minimax(depth + 1, node_index * 2 + i, True, scores, height) + best_score = min(best_score, val) # Minimizer chooses the lowest score available + return best_score + +def main(): + # Scores array representing the leaf nodes of a binary tree (depth = 3) + scores = [3, 5, 2, 9, 12, 5, 23, 23] + # Calculate the height of the binary tree based on the number of leaf nodes + height = math.ceil(math.log2(len(scores))) + + # Print the optimal outcome for the maximizing player + print("Optimal value for the maximizing player:", minimax(0, 0, True, scores, height)) + +if __name__ == "__main__": + main() diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py new file mode 100644 index 000000000000..b41291de93cc --- /dev/null +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -0,0 +1,55 @@ +import math + +def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, height, alpha, beta): + """ + Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. + + Parameters: + - depth (int): Current depth in the game tree, used to track recursion level. + - node_index (int): Index of the current node in the scores array, representing leaf nodes. + - is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise. + - scores (list): A list of integers representing the scores at the leaf nodes of the game tree. + - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure. + - alpha (int): The best value that the maximizer can guarantee at the current level or above. + - beta (int): The best value that the minimizer can guarantee at the current level or above. + + Returns: + - int: The best score that the current player can achieve from this node. + """ + # Base case: If we reach the leaf node level, return its score + if depth == height: + return scores[node_index] + + # Maximizing player's move + if is_maximizing_player: + best_score = -math.inf # Start with the worst possible score for maximizer + for i in range(2): # Two branches at each level in binary tree + val = minimax_with_pruning(depth + 1, node_index * 2 + i, False, scores, height, alpha, beta) + best_score = max(best_score, val) # Maximizer selects the maximum value + alpha = max(alpha, best_score) # Update alpha (best option for maximizer) + if beta <= alpha: + break # Beta cut-off + return best_score + + # Minimizing player's move + else: + best_score = math.inf # Start with the worst possible score for minimizer + for i in range(2): + val = minimax_with_pruning(depth + 1, node_index * 2 + i, True, scores, height, alpha, beta) + best_score = min(best_score, val) # Minimizer selects the minimum value + beta = min(beta, best_score) # Update beta (best option for minimizer) + if beta <= alpha: + break # Alpha cut-off + return best_score + +def main(): + # Scores array representing the leaf nodes of a binary tree (depth = 3) + scores = [3, 5, 2, 9, 12, 5, 23, 23] + # Calculate the height of the binary tree based on the number of leaf nodes + height = math.ceil(math.log2(len(scores))) + + # Print the optimal outcome for the maximizing player with alpha-beta pruning + print("Optimal value for the maximizing player with pruning:", minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf)) + +if __name__ == "__main__": + main() From 0a1e9a4d6842c51d9443d03ed3d9d4c8be6e02f5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:35:27 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/minimax.py | 15 ++++++++++--- .../minimax_alpha_beta_pruning.py | 22 ++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index 764f66563845..5bcedc5dbd1e 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -1,5 +1,6 @@ import math + def minimax(depth, node_index, is_maximizing_player, scores, height): """ 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): for i in range(2): # Recursive call for the next level of depth val = minimax(depth + 1, node_index * 2 + i, False, scores, height) - best_score = max(best_score, val) # Maximizer chooses the highest score available + best_score = max( + best_score, val + ) # Maximizer chooses the highest score available return best_score # Minimizing player's move @@ -34,9 +37,12 @@ def minimax(depth, node_index, is_maximizing_player, scores, height): for i in range(2): # Recursive call for the next level of depth val = minimax(depth + 1, node_index * 2 + i, True, scores, height) - best_score = min(best_score, val) # Minimizer chooses the lowest score available + best_score = min( + best_score, val + ) # Minimizer chooses the lowest score available return best_score + def main(): # Scores array representing the leaf nodes of a binary tree (depth = 3) scores = [3, 5, 2, 9, 12, 5, 23, 23] @@ -44,7 +50,10 @@ def main(): height = math.ceil(math.log2(len(scores))) # Print the optimal outcome for the maximizing player - print("Optimal value for the maximizing player:", minimax(0, 0, True, scores, height)) + print( + "Optimal value for the maximizing player:", minimax(0, 0, True, scores, height) + ) + if __name__ == "__main__": main() diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index b41291de93cc..dcb0dcfdb318 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,9 +1,12 @@ import math -def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, height, alpha, beta): + +def minimax_with_pruning( + depth, node_index, is_maximizing_player, scores, height, alpha, beta +): """ Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. - + Parameters: - depth (int): Current depth in the game tree, used to track recursion level. - 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 if is_maximizing_player: best_score = -math.inf # Start with the worst possible score for maximizer for i in range(2): # Two branches at each level in binary tree - val = minimax_with_pruning(depth + 1, node_index * 2 + i, False, scores, height, alpha, beta) + val = minimax_with_pruning( + depth + 1, node_index * 2 + i, False, scores, height, alpha, beta + ) best_score = max(best_score, val) # Maximizer selects the maximum value alpha = max(alpha, best_score) # Update alpha (best option for maximizer) if beta <= alpha: @@ -35,13 +40,16 @@ def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, height else: best_score = math.inf # Start with the worst possible score for minimizer for i in range(2): - val = minimax_with_pruning(depth + 1, node_index * 2 + i, True, scores, height, alpha, beta) + val = minimax_with_pruning( + depth + 1, node_index * 2 + i, True, scores, height, alpha, beta + ) best_score = min(best_score, val) # Minimizer selects the minimum value beta = min(beta, best_score) # Update beta (best option for minimizer) if beta <= alpha: break # Alpha cut-off return best_score + def main(): # Scores array representing the leaf nodes of a binary tree (depth = 3) scores = [3, 5, 2, 9, 12, 5, 23, 23] @@ -49,7 +57,11 @@ def main(): height = math.ceil(math.log2(len(scores))) # Print the optimal outcome for the maximizing player with alpha-beta pruning - print("Optimal value for the maximizing player with pruning:", minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf)) + print( + "Optimal value for the maximizing player with pruning:", + minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf), + ) + if __name__ == "__main__": main() From 398412b8e3d643243f47cac7538f1c6d413ee514 Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal Date: Fri, 25 Oct 2024 18:15:08 +0530 Subject: [PATCH 03/10] Fixing tests --- machine_learning/minimax.py | 18 +++++--- .../minimax_alpha_beta_pruning.py | 46 +++++++++---------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index 5bcedc5dbd1e..9844c7e3cb53 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -3,14 +3,19 @@ def minimax(depth, node_index, is_maximizing_player, scores, height): """ - Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. + Minimax algorithm to determine the optimal move for a player in a two-player + zero-sum game. Parameters: - depth (int): Current depth in the game tree. Used to track recursion level. - - node_index (int): Index of the current node in the scores array, representing leaf nodes. - - is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise. - - scores (list): A list of integers representing the scores at the leaf nodes of the game tree. - - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure. + - node_index (int): Index of the current node in the scores array, representing + leaf nodes. + - is_maximizing_player (bool): True if the current player is the maximizing player + , False otherwise. + - scores (list): A list of integers representing the scores at the leaf nodes + of the game tree. + - height (int): The maximum depth of the game tree, based on the number of leaf + nodes in a binary structure. Returns: - int: The best score that the current player can achieve from this node. @@ -51,7 +56,8 @@ def main(): # Print the optimal outcome for the maximizing player print( - "Optimal value for the maximizing player:", minimax(0, 0, True, scores, height) + "Optimal value for the maximizing player:", + minimax(0, 0, True, scores, height) ) diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index dcb0dcfdb318..201b081f0165 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,20 +1,25 @@ import math - -def minimax_with_pruning( - depth, node_index, is_maximizing_player, scores, height, alpha, beta -): +def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, + height, alpha, beta): """ - Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. - + Minimax algorithm with alpha-beta pruning to determine the optimal + move with improved efficiency. + Parameters: - depth (int): Current depth in the game tree, used to track recursion level. - - node_index (int): Index of the current node in the scores array, representing leaf nodes. - - is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise. - - scores (list): A list of integers representing the scores at the leaf nodes of the game tree. - - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure. - - alpha (int): The best value that the maximizer can guarantee at the current level or above. - - beta (int): The best value that the minimizer can guarantee at the current level or above. + - node_index (int): Index of the current node in the scores array, representing + leaf nodes. + - is_maximizing_player (bool): True if the current player is the maximizing player + , False otherwise. + - scores (list): A list of integers representing the scores at the + leaf nodes of the game tree. + - height (int): The maximum depth of the game tree, based on the number of + leaf nodes in a binary structure. + - alpha (int): The best value that the maximizer can guarantee at the + current level or above. + - beta (int): The best value that the minimizer can guarantee at the + current level or above. Returns: - int: The best score that the current player can achieve from this node. @@ -27,9 +32,8 @@ def minimax_with_pruning( if is_maximizing_player: best_score = -math.inf # Start with the worst possible score for maximizer for i in range(2): # Two branches at each level in binary tree - val = minimax_with_pruning( - depth + 1, node_index * 2 + i, False, scores, height, alpha, beta - ) + val = minimax_with_pruning(depth + 1, node_index * 2 + i, + False, scores, height, alpha, beta) best_score = max(best_score, val) # Maximizer selects the maximum value alpha = max(alpha, best_score) # Update alpha (best option for maximizer) if beta <= alpha: @@ -40,9 +44,8 @@ def minimax_with_pruning( else: best_score = math.inf # Start with the worst possible score for minimizer for i in range(2): - val = minimax_with_pruning( - depth + 1, node_index * 2 + i, True, scores, height, alpha, beta - ) + val = minimax_with_pruning(depth + 1, node_index * 2 + i, True, + scores, height, alpha, beta) best_score = min(best_score, val) # Minimizer selects the minimum value beta = min(beta, best_score) # Update beta (best option for minimizer) if beta <= alpha: @@ -57,11 +60,8 @@ def main(): height = math.ceil(math.log2(len(scores))) # Print the optimal outcome for the maximizing player with alpha-beta pruning - print( - "Optimal value for the maximizing player with pruning:", - minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf), - ) - + print("Optimal value for the maximizing player with pruning:", + minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf)) if __name__ == "__main__": main() From 3debdbcefa0de6bec7e40689eebf065ca7cce067 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:46:37 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/minimax.py | 11 +++--- .../minimax_alpha_beta_pruning.py | 37 +++++++++++-------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index 9844c7e3cb53..691f5248f919 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -3,18 +3,18 @@ def minimax(depth, node_index, is_maximizing_player, scores, height): """ - Minimax algorithm to determine the optimal move for a player in a two-player + Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. Parameters: - depth (int): Current depth in the game tree. Used to track recursion level. - - node_index (int): Index of the current node in the scores array, representing + - node_index (int): Index of the current node in the scores array, representing leaf nodes. - is_maximizing_player (bool): True if the current player is the maximizing player , False otherwise. - - scores (list): A list of integers representing the scores at the leaf nodes + - scores (list): A list of integers representing the scores at the leaf nodes of the game tree. - - height (int): The maximum depth of the game tree, based on the number of leaf + - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure. Returns: @@ -56,8 +56,7 @@ def main(): # Print the optimal outcome for the maximizing player print( - "Optimal value for the maximizing player:", - minimax(0, 0, True, scores, height) + "Optimal value for the maximizing player:", minimax(0, 0, True, scores, height) ) diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index 201b081f0165..c5f3d526d85f 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,24 +1,26 @@ import math -def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, - height, alpha, beta): + +def minimax_with_pruning( + depth, node_index, is_maximizing_player, scores, height, alpha, beta +): """ - Minimax algorithm with alpha-beta pruning to determine the optimal + Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. - + Parameters: - depth (int): Current depth in the game tree, used to track recursion level. - - node_index (int): Index of the current node in the scores array, representing + - node_index (int): Index of the current node in the scores array, representing leaf nodes. - is_maximizing_player (bool): True if the current player is the maximizing player , False otherwise. - - scores (list): A list of integers representing the scores at the + - scores (list): A list of integers representing the scores at the leaf nodes of the game tree. - - height (int): The maximum depth of the game tree, based on the number of + - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure. - - alpha (int): The best value that the maximizer can guarantee at the + - alpha (int): The best value that the maximizer can guarantee at the current level or above. - - beta (int): The best value that the minimizer can guarantee at the + - beta (int): The best value that the minimizer can guarantee at the current level or above. Returns: @@ -32,8 +34,9 @@ def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, if is_maximizing_player: best_score = -math.inf # Start with the worst possible score for maximizer for i in range(2): # Two branches at each level in binary tree - val = minimax_with_pruning(depth + 1, node_index * 2 + i, - False, scores, height, alpha, beta) + val = minimax_with_pruning( + depth + 1, node_index * 2 + i, False, scores, height, alpha, beta + ) best_score = max(best_score, val) # Maximizer selects the maximum value alpha = max(alpha, best_score) # Update alpha (best option for maximizer) if beta <= alpha: @@ -44,8 +47,9 @@ def minimax_with_pruning(depth, node_index, is_maximizing_player, scores, else: best_score = math.inf # Start with the worst possible score for minimizer for i in range(2): - val = minimax_with_pruning(depth + 1, node_index * 2 + i, True, - scores, height, alpha, beta) + val = minimax_with_pruning( + depth + 1, node_index * 2 + i, True, scores, height, alpha, beta + ) best_score = min(best_score, val) # Minimizer selects the minimum value beta = min(beta, best_score) # Update beta (best option for minimizer) if beta <= alpha: @@ -60,8 +64,11 @@ def main(): height = math.ceil(math.log2(len(scores))) # Print the optimal outcome for the maximizing player with alpha-beta pruning - print("Optimal value for the maximizing player with pruning:", - minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf)) + print( + "Optimal value for the maximizing player with pruning:", + minimax_with_pruning(0, 0, True, scores, height, -math.inf, math.inf), + ) + if __name__ == "__main__": main() From af84b6c6bcaee2233c4aa538762ac1a0f1d19fdf Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal Date: Fri, 25 Oct 2024 18:22:41 +0530 Subject: [PATCH 05/10] Added type hints --- machine_learning/minimax.py | 5 +++-- machine_learning/minimax_alpha_beta_pruning.py | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index 691f5248f919..c3628abecdca 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -1,7 +1,8 @@ import math +from typing import List - -def minimax(depth, node_index, is_maximizing_player, scores, height): +def minimax(depth: int, node_index: int, is_maximizing_player: bool, + scores: List[int], height: int) -> int: """ Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index c5f3d526d85f..c8c4efb47313 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,9 +1,8 @@ import math +from typing import List - -def minimax_with_pruning( - depth, node_index, is_maximizing_player, scores, height, alpha, beta -): +def minimax_with_pruning(depth: int, node_index: int, is_maximizing_player: bool, + scores: List[int], height: int, alpha: int, beta: int) -> int: """ Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. From 52e91a86a820b80c70a6a99a6785ccc15a16b5c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:55:34 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/minimax.py | 10 ++++++++-- machine_learning/minimax_alpha_beta_pruning.py | 12 ++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index c3628abecdca..a95cd7fca391 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -1,8 +1,14 @@ import math from typing import List -def minimax(depth: int, node_index: int, is_maximizing_player: bool, - scores: List[int], height: int) -> int: + +def minimax( + depth: int, + node_index: int, + is_maximizing_player: bool, + scores: List[int], + height: int, +) -> int: """ Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index c8c4efb47313..0a22e5bda7b5 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,8 +1,16 @@ import math from typing import List -def minimax_with_pruning(depth: int, node_index: int, is_maximizing_player: bool, - scores: List[int], height: int, alpha: int, beta: int) -> int: + +def minimax_with_pruning( + depth: int, + node_index: int, + is_maximizing_player: bool, + scores: List[int], + height: int, + alpha: int, + beta: int, +) -> int: """ Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. From 1c3bff8491ac6455a713320fcd84ef1fd061b48a Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal Date: Fri, 25 Oct 2024 18:26:49 +0530 Subject: [PATCH 07/10] Fixing tests --- machine_learning/minimax.py | 12 +++--------- machine_learning/minimax_alpha_beta_pruning.py | 15 +++------------ 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index a95cd7fca391..2d6ba8802f8c 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -1,14 +1,8 @@ import math from typing import List - -def minimax( - depth: int, - node_index: int, - is_maximizing_player: bool, - scores: List[int], - height: int, -) -> int: +def minimax(depth: int, node_index: int, is_maximizing_player: bool, + scores: list, height: int) -> int: """ Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. @@ -55,7 +49,7 @@ def minimax( return best_score -def main(): +def main() -> None: # Scores array representing the leaf nodes of a binary tree (depth = 3) scores = [3, 5, 2, 9, 12, 5, 23, 23] # Calculate the height of the binary tree based on the number of leaf nodes diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index 0a22e5bda7b5..4c342dc8c1e1 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,16 +1,7 @@ import math -from typing import List - -def minimax_with_pruning( - depth: int, - node_index: int, - is_maximizing_player: bool, - scores: List[int], - height: int, - alpha: int, - beta: int, -) -> int: +def minimax_with_pruning(depth: int, node_index: int, is_maximizing_player: bool, + scores: list, height: int, alpha: int, beta: int) -> int: """ Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. @@ -64,7 +55,7 @@ def minimax_with_pruning( return best_score -def main(): +def main() -> None: # Scores array representing the leaf nodes of a binary tree (depth = 3) scores = [3, 5, 2, 9, 12, 5, 23, 23] # Calculate the height of the binary tree based on the number of leaf nodes From f04ef736ae962d62a0fd31a45c74e586cfe03b58 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:57:27 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- machine_learning/minimax.py | 6 ++++-- machine_learning/minimax_alpha_beta_pruning.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index 2d6ba8802f8c..433a33df410f 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -1,8 +1,10 @@ import math from typing import List -def minimax(depth: int, node_index: int, is_maximizing_player: bool, - scores: list, height: int) -> int: + +def minimax( + depth: int, node_index: int, is_maximizing_player: bool, scores: list, height: int +) -> int: """ Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index 4c342dc8c1e1..0841bb63b302 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -1,7 +1,15 @@ import math -def minimax_with_pruning(depth: int, node_index: int, is_maximizing_player: bool, - scores: list, height: int, alpha: int, beta: int) -> int: + +def minimax_with_pruning( + depth: int, + node_index: int, + is_maximizing_player: bool, + scores: list, + height: int, + alpha: int, + beta: int, +) -> int: """ Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency. From 9e9ec203256fbc5e43bfe5ffb1d12d25eec47a7a Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal Date: Fri, 25 Oct 2024 18:28:34 +0530 Subject: [PATCH 09/10] Using list insteaf of List --- machine_learning/minimax.py | 1 - 1 file changed, 1 deletion(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index 433a33df410f..e4f7fd7b7310 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -1,5 +1,4 @@ import math -from typing import List def minimax( From d9ef05aad6eea85a22f4081558c7bcddf70d240e Mon Sep 17 00:00:00 2001 From: Prateek Khandelwal Date: Fri, 25 Oct 2024 18:31:59 +0530 Subject: [PATCH 10/10] Correcting return types --- machine_learning/minimax.py | 2 +- machine_learning/minimax_alpha_beta_pruning.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/machine_learning/minimax.py b/machine_learning/minimax.py index e4f7fd7b7310..d661c81d3668 100644 --- a/machine_learning/minimax.py +++ b/machine_learning/minimax.py @@ -3,7 +3,7 @@ def minimax( depth: int, node_index: int, is_maximizing_player: bool, scores: list, height: int -) -> int: +) -> float: """ Minimax algorithm to determine the optimal move for a player in a two-player zero-sum game. diff --git a/machine_learning/minimax_alpha_beta_pruning.py b/machine_learning/minimax_alpha_beta_pruning.py index 0841bb63b302..519c960c1c05 100644 --- a/machine_learning/minimax_alpha_beta_pruning.py +++ b/machine_learning/minimax_alpha_beta_pruning.py @@ -7,9 +7,9 @@ def minimax_with_pruning( is_maximizing_player: bool, scores: list, height: int, - alpha: int, - beta: int, -) -> int: + alpha: float, + beta: float, +) -> float: """ Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency.