-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Add Minimax algorithm implementation in game_theory folder #12000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
143b434
8497aa5
4978277
fe7ea61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Game Theory Algorithms | ||
|
||
This repository contains implementations of various algorithms related to game theory. | ||
|
||
## Algorithms | ||
|
||
- **Minimax Algorithm** | ||
- [Minimax (Wikipedia)](https://en.wikipedia.org/wiki/Minimax) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import math | ||
|
||
|
||
class MinMax: | ||
""" | ||
A class to represent a game using the Minimax algorithm. | ||
|
||
Attributes: | ||
---------- | ||
scores : list[int] | ||
List of terminal node scores. | ||
tree_depth : int | ||
Depth of the minimax tree. | ||
""" | ||
|
||
def __init__(self, scores: list[int]): | ||
""" | ||
Initialize the MinMax game with a list of scores. | ||
|
||
Parameters: | ||
---------- | ||
scores : list[int] | ||
List of terminal node scores. | ||
""" | ||
self.scores = scores | ||
self.tree_depth = int(math.log2(len(scores))) | ||
|
||
def minimax( | ||
<<<<<<< HEAD | ||
Check failure on line 29 in game_theory/min_max.py
|
||
self, current_depth: int = 0, | ||
node_index: int = 0, is_max_turn: bool = True | ||
======= | ||
Check failure on line 32 in game_theory/min_max.py
|
||
self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True | ||
Check failure on line 33 in game_theory/min_max.py
|
||
>>>>>>> 8497aa531c8636d5735e3df3d7583d9c05a323b5 | ||
) -> int: | ||
""" | ||
Recursive implementation of the minimax algorithm. | ||
|
||
Parameters: | ||
---------- | ||
current_depth : int | ||
Current depth in the game tree. | ||
node_index : int | ||
Index of the current node in the tree. | ||
is_max_turn : bool | ||
Boolean indicating if it's the maximizing player's turn. | ||
|
||
Returns: | ||
------- | ||
int | ||
The optimal value for the current player. | ||
""" | ||
# Base case: we've reached a terminal node | ||
if current_depth == self.tree_depth: | ||
return self.scores[node_index] | ||
|
||
<<<<<<< HEAD | ||
# Recursive case | ||
======= | ||
>>>>>>> 8497aa531c8636d5735e3df3d7583d9c05a323b5 | ||
if is_max_turn: | ||
return max( | ||
self.minimax(current_depth + 1, node_index * 2, False), | ||
self.minimax(current_depth + 1, node_index * 2 + 1, False), | ||
) | ||
else: | ||
return min( | ||
self.minimax(current_depth + 1, node_index * 2, True), | ||
self.minimax(current_depth + 1, node_index * 2 + 1, True), | ||
) | ||
|
||
def find_optimal_value(self) -> int: | ||
""" | ||
Find and return the optimal value for the maximizing player. | ||
|
||
Returns: | ||
------- | ||
int | ||
The optimal value. | ||
""" | ||
return self.minimax() | ||
|
||
<<<<<<< HEAD | ||
======= | ||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
scores = [3, 5, 2, 9, 12, 5, 23, 23] | ||
game = MinMax(scores) | ||
optimal_value = game.find_optimal_value() | ||
print(f"The optimal value is: {optimal_value}") | ||
>>>>>>> 8497aa531c8636d5735e3df3d7583d9c05a323b5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
game_theory/min_max.py