From 5810196ab9d14e53cd6d844aded22a01d0f2fe34 Mon Sep 17 00:00:00 2001 From: Aditi Agarwal <31546143+aditiagarwal34550@users.noreply.github.com> Date: Wed, 3 Jul 2019 17:37:04 +0530 Subject: [PATCH 1/5] minimax.py minimax algorithm is used for game like tic tac toe. It traces the path and selects the optimal move. --- dynamic_programming/minimax.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 dynamic_programming/minimax.py diff --git a/dynamic_programming/minimax.py b/dynamic_programming/minimax.py new file mode 100644 index 000000000000..9578ce2b22b9 --- /dev/null +++ b/dynamic_programming/minimax.py @@ -0,0 +1,18 @@ +import math + +def minimax (Depth, nodeIndex, isMax, scores, height): + + if (Depth == height): + return scores[nodeIndex] + + if (isMax): + return max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)) + return min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)) + + +scores = [90, 23, 6, 33, 21, 65, 123, 34423] + +height = math.log(len(scores), 2) + +print("Optimal value : ", end = "") +print(minimax(0, 0, True, scores, height)) From d576a2bb8e11f399fab6762b3247cc3efa2afae0 Mon Sep 17 00:00:00 2001 From: Aditi Agarwal <31546143+aditiagarwal34550@users.noreply.github.com> Date: Wed, 3 Jul 2019 17:53:51 +0530 Subject: [PATCH 2/5] minimax.py Minimax is used in decision making and game theory to find the optimal move for a player, when your opponent also plays optimally. It is widely used in games like Tic-Tac-Toe, Chess. --- minimax.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 minimax.py diff --git a/minimax.py b/minimax.py new file mode 100644 index 000000000000..29c0e47be5d1 --- /dev/null +++ b/minimax.py @@ -0,0 +1,18 @@ +import math + +def minimax (Depth, nodeIndex, isMax, scores, height): + + if (Depth == height): + return scores[nodeIndex] + + if (isMax): + return max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)) + return min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)) + + +scores = [90, 23, 6, 33, 21, 65, 123, 34423] + +height = math.log(len(scores), 2) + +print("Optimal value : ", end = "") +print(minimax(0, 0, True, scores, height)) From 8c53b2249ee37b67c8f816626beb977088597ad4 Mon Sep 17 00:00:00 2001 From: Aditi Agarwal <31546143+aditiagarwal34550@users.noreply.github.com> Date: Fri, 5 Jul 2019 14:09:42 +0530 Subject: [PATCH 3/5] Delete minimax.py --- dynamic_programming/minimax.py | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 dynamic_programming/minimax.py diff --git a/dynamic_programming/minimax.py b/dynamic_programming/minimax.py deleted file mode 100644 index 9578ce2b22b9..000000000000 --- a/dynamic_programming/minimax.py +++ /dev/null @@ -1,18 +0,0 @@ -import math - -def minimax (Depth, nodeIndex, isMax, scores, height): - - if (Depth == height): - return scores[nodeIndex] - - if (isMax): - return max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)) - return min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)) - - -scores = [90, 23, 6, 33, 21, 65, 123, 34423] - -height = math.log(len(scores), 2) - -print("Optimal value : ", end = "") -print(minimax(0, 0, True, scores, height)) From 76f20c4a56a3ab258637962e163f2163169f7b83 Mon Sep 17 00:00:00 2001 From: Aditi Agarwal <31546143+aditiagarwal34550@users.noreply.github.com> Date: Fri, 5 Jul 2019 14:18:19 +0530 Subject: [PATCH 4/5] Update minimax.py --- minimax.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/minimax.py b/minimax.py index 29c0e47be5d1..51231b2aacde 100644 --- a/minimax.py +++ b/minimax.py @@ -1,5 +1,13 @@ import math +''' Minimax helps to achieve maximum score in a game by checking all possible moves + depth is current depth in game tree. + nodeIndex is index of current node in scores[]. + if move is of maximizer return true else false + leaves of game tree is stored in scores[] + height is maximum height of Game tree +''' + def minimax (Depth, nodeIndex, isMax, scores, height): if (Depth == height): From db773689a72d7fb3325a289174a7ab11031a96bd Mon Sep 17 00:00:00 2001 From: Aditi Agarwal <31546143+aditiagarwal34550@users.noreply.github.com> Date: Sun, 7 Jul 2019 08:35:19 +0530 Subject: [PATCH 5/5] Minimax is a backtracking algorithm that is used in game theory to find the optimal move for a player, assuming that your opponent also plays optimally --- backtracking/minimax.py | 28 ++++++++++++++++++++++++++++ minimax.py | 26 -------------------------- 2 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 backtracking/minimax.py delete mode 100644 minimax.py diff --git a/backtracking/minimax.py b/backtracking/minimax.py new file mode 100644 index 000000000000..5168306e71fc --- /dev/null +++ b/backtracking/minimax.py @@ -0,0 +1,28 @@ +import math + +''' Minimax helps to achieve maximum score in a game by checking all possible moves + depth is current depth in game tree. + nodeIndex is index of current node in scores[]. + if move is of maximizer return true else false + leaves of game tree is stored in scores[] + height is maximum height of Game tree +''' + +def minimax (Depth, nodeIndex, isMax, scores, height): + + if Depth == height: + return scores[nodeIndex] + + if isMax: + return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), + minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height))) + return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), + minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height))) + +if __name__ == "__main__": + + scores = [90, 23, 6, 33, 21, 65, 123, 34423] + height = math.log(len(scores), 2) + + print("Optimal value : ", end = "") + print(minimax(0, 0, True, scores, height)) diff --git a/minimax.py b/minimax.py deleted file mode 100644 index 51231b2aacde..000000000000 --- a/minimax.py +++ /dev/null @@ -1,26 +0,0 @@ -import math - -''' Minimax helps to achieve maximum score in a game by checking all possible moves - depth is current depth in game tree. - nodeIndex is index of current node in scores[]. - if move is of maximizer return true else false - leaves of game tree is stored in scores[] - height is maximum height of Game tree -''' - -def minimax (Depth, nodeIndex, isMax, scores, height): - - if (Depth == height): - return scores[nodeIndex] - - if (isMax): - return max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)) - return min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)) - - -scores = [90, 23, 6, 33, 21, 65, 123, 34423] - -height = math.log(len(scores), 2) - -print("Optimal value : ", end = "") -print(minimax(0, 0, True, scores, height))