Skip to content

Added min_max algorithm in game_theory module #11999

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions game_theory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 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)

Empty file added game_theory/__init__.py
Empty file.
109 changes: 109 additions & 0 deletions game_theory/min_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import math
from typing import List

Check failure on line 2 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

game_theory/min_max.py:2:1: UP035 `typing.List` is deprecated, use `list` instead


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 game tree.

Methods:
-------
minimax(current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True) -> int:

Check failure on line 18 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

game_theory/min_max.py:18:89: E501 Line too long (90 > 88)
Recursive implementation of the minimax algorithm.
find_optimal_value() -> int:
Find and return the optimal value for the maximizing player.

Examples:
---------
>>> game = MinMax([3, 5, 2, 9, 12, 5, 23, 23])
>>> game.find_optimal_value()
12
"""

def __init__(self, scores: List[int]) -> None:

Check failure on line 30 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

game_theory/min_max.py:30:32: UP006 Use `list` instead of `List` for type annotation
"""
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(
self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True
) -> 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.

Examples:
---------
>>> game = MinMax([3, 5, 2, 9, 12, 5, 23, 23])
>>> game.minimax(0, 0, True)
12
"""

if current_depth == self.tree_depth:
return self.scores[node_index]

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.

Examples:
---------
>>> game = MinMax([3, 5, 2, 9, 12, 5, 23, 23])
>>> game.find_optimal_value()
12
"""
return self.minimax()


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}")
Loading