Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files
simulators/connect_four_simulator.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simulator for Connect Four.
|
2 |
+
|
3 |
+
This module implements the ConnectFourSimulator class, which simulates games of
|
4 |
+
Connect Four using the OpenSpiel framework.
|
5 |
+
"""
|
6 |
+
|
7 |
+
from simulators.base_simulator import GameSimulator
|
8 |
+
|
9 |
+
class ConnectFourSimulator(GameSimulator):
|
10 |
+
"""Simulator for Connect Four."""
|
11 |
+
pass # All functionality is inherited from GameSimulator
|
simulators/kuhn_poker_simulator.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simulator for Kuhn Poker.
|
2 |
+
|
3 |
+
This module implements the KuhnPokerSimulator class, which simulates games of
|
4 |
+
Kuhn Poker using the OpenSpiel framework.
|
5 |
+
|
6 |
+
For Kuhn Poker, the game mechanics involve:
|
7 |
+
|
8 |
+
- Betting rounds where decisions depend on the game state and potential strategies.
|
9 |
+
- Chance nodes, which require specific handling (e.g., dealing cards).
|
10 |
+
"""
|
11 |
+
|
12 |
+
from simulators.base_simulator import GameSimulator
|
13 |
+
from utils.llm_utils import llm_decide_move
|
14 |
+
from typing import Any
|
15 |
+
import random
|
16 |
+
|
17 |
+
|
18 |
+
class KuhnPokerSimulator(GameSimulator):
|
19 |
+
"""Simulator for Kuhn Poker."""
|
20 |
+
|
21 |
+
def _handle_chance_node(self, state: Any):
|
22 |
+
"""Handle chance nodes for Kuhn Poker.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
state (pyspiel.State): The current state of the game.
|
26 |
+
"""
|
27 |
+
outcomes, probabilities = zip(*state.chance_outcomes())
|
28 |
+
sampled_outcome = random.choices(outcomes, probabilities)[0]
|
29 |
+
state.apply_action(sampled_outcome)
|
30 |
+
|
31 |
+
def _get_action(self, player: int, state: Any, legal_actions: list) -> int:
|
32 |
+
"""Gets the action for the current player.
|
33 |
+
|
34 |
+
Uses the dedicated Kuhn Poker prompt if the player type is LLM.
|
35 |
+
|
36 |
+
Args:
|
37 |
+
player: The index of the current player.
|
38 |
+
state: The current game state.
|
39 |
+
legal_actions: The legal actions available for the player.
|
40 |
+
|
41 |
+
Returns:
|
42 |
+
int: The action selected by the player.
|
43 |
+
"""
|
44 |
+
# If the player type is LLM, use the specialized Kuhn Poker prompt
|
45 |
+
if player < len(self.llms):
|
46 |
+
model_name = list(self.llms.keys())[player]
|
47 |
+
llm = self.llms[model_name]
|
48 |
+
prompt = self._generate_poker_prompt(state, legal_actions, player)
|
49 |
+
return llm_decide_move(llm, prompt, tuple(legal_actions)) # Convert to tuple
|
50 |
+
|
51 |
+
# For all other cases, defer to the parent class's implementation
|
52 |
+
return super()._get_action(player, state, legal_actions)
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
def _generate_poker_prompt(self,state: Any, legal_actions: list, player: int) -> str:
|
57 |
+
"""Generates a detailed prompt for Kuhn Poker using OpenSpiel's state.
|
58 |
+
|
59 |
+
Args:
|
60 |
+
state (pyspiel.State): The current game state.
|
61 |
+
legal_actions (list): Legal actions available to the player.
|
62 |
+
player (int): The index of the current player.
|
63 |
+
|
64 |
+
Returns:
|
65 |
+
str: A natural language prompt describing the game state and options.
|
66 |
+
"""
|
67 |
+
# Extract player-specific observation
|
68 |
+
observation = state.observation_string(player)
|
69 |
+
|
70 |
+
# Map actions to readable terms
|
71 |
+
action_map = {0: "PASS (no additional bet)", 1: "BET (add to the pot)"}
|
72 |
+
actions_str = "\n".join(f"{action}: {action_map[action]}" for action in legal_actions)
|
73 |
+
|
74 |
+
# Build the prompt
|
75 |
+
prompt = (
|
76 |
+
f"You are Player {player + 1} in a game of Kuhn Poker.\n"
|
77 |
+
f"Your private observation: {observation}\n"
|
78 |
+
f"The goal is to maximize your winnings based on your card and the pot.\n\n"
|
79 |
+
f"Available actions:\n{actions_str}\n\n"
|
80 |
+
"What action do you choose? Reply with the number corresponding to your action."
|
81 |
+
)
|
82 |
+
return prompt
|
simulators/matching_pennies_simulator.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simulator for Matching Pennies (3-player).
|
2 |
+
|
3 |
+
This module implements the MatchingPenniesSimulator class, which simulates games of
|
4 |
+
Matching Pennies with three players using the OpenSpiel framework.
|
5 |
+
"""
|
6 |
+
|
7 |
+
from simulators.base_simulator import GameSimulator
|
8 |
+
|
9 |
+
class MatchingPenniesSimulator(GameSimulator):
|
10 |
+
"""Simulator for Matching Pennies (3-player)."""
|
11 |
+
pass # All functionality is inherited from GameSimulator
|
simulators/matrix_game_simulator.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simulator for Matrix Games.
|
2 |
+
|
3 |
+
This module implements the MatrixGameSimulator class, which handles various
|
4 |
+
matrix games like Rock-Paper-Scissors and Prisoner's Dilemma using the OpenSpiel
|
5 |
+
framework.
|
6 |
+
"""
|
7 |
+
|
8 |
+
from simulators.base_simulator import GameSimulator
|
9 |
+
|
10 |
+
class MatrixGameSimulator(GameSimulator):
|
11 |
+
"""Simulator for Matrix Games."""
|
12 |
+
pass # All functionality is inherited from GameSimulator
|
simulators/prisoners_dilemma_simulator.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simulator for Iterated Prisoner's Dilemma.
|
2 |
+
|
3 |
+
This module implements the PrisonersDilemmaSimulator class, which simulates games of
|
4 |
+
the Iterated Prisoner's Dilemma using the OpenSpiel framework.
|
5 |
+
"""
|
6 |
+
from typing import Any, Dict
|
7 |
+
import random
|
8 |
+
|
9 |
+
from simulators.base_simulator import GameSimulator
|
10 |
+
|
11 |
+
|
12 |
+
class PrisonersDilemmaSimulator(GameSimulator):
|
13 |
+
"""Simulator for the Iterated Prisoner's Dilemma with stochastic termination.
|
14 |
+
|
15 |
+
This class extends the base GameSimulator to handle the specific logic
|
16 |
+
for chance nodes in the Iterated Prisoner's Dilemma, where the game
|
17 |
+
terminates probabilistically after each round.
|
18 |
+
"""
|
19 |
+
|
20 |
+
def _handle_chance_node(self, state: Any):
|
21 |
+
"""Handle the chance node for stochastic game termination.
|
22 |
+
|
23 |
+
At each chance node, the game decides whether to continue or stop
|
24 |
+
based on the termination probability defined in the game parameters.
|
25 |
+
|
26 |
+
Args:
|
27 |
+
state (pyspiel.State): The current state of the game.
|
28 |
+
|
29 |
+
Raises:
|
30 |
+
ValueError: If the chance outcomes are invalid.
|
31 |
+
"""
|
32 |
+
outcomes, probabilities = zip(*state.chance_outcomes())
|
33 |
+
sampled_outcome = random.choices(outcomes, probabilities)[0]
|
34 |
+
state.apply_action(sampled_outcome)
|
simulators/rock_paper_scissors_simulator.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Simulator for Rock-Paper-Scissors.
|
2 |
+
|
3 |
+
This module implements the RockPaperScissorsSimulator class, which simulates games of
|
4 |
+
Rock-Paper-Scissors using the OpenSpiel framework.
|
5 |
+
"""
|
6 |
+
|
7 |
+
from simulators.base_simulator import GameSimulator
|
8 |
+
|
9 |
+
class RockPaperScissorsSimulator(GameSimulator):
|
10 |
+
"""Simulator for Rock-Paper-Scissors."""
|
11 |
+
pass # All functionality is inherited from GameSimulator
|