Use the Emulator for Reinforcement Learning
masterFor Reinforcement Learning (RL) workflows, use the Emulator class to simulate game states and outcomes.
Workflow:
- In
receive_game_start_message, initialize anEmulatorinstance. - Set game rules using
emulator.set_game_rule(player_num, max_round, small_blind_amount, ante_amount)andemulator.set_blind_structure(blind_structure). - Register player models using
emulator.register_player(uuid, model). - In
declare_action, userestore_game_state(round_state)to get a usable state, then use the emulator to simulate actions:emulator.apply_action(game_state, action): Applies a single action and returns(updated_state, events).emulator.run_until_round_finish(game_state): Simulates until the end of the current round.emulator.run_until_game_finish(game_state): Simulates until the end of the entire game.
from pypokerengine.players import BasePokerPlayer
from pypokerengine.api.emulator import Emulator
from pypokerengine.utils.game_state_utils import restore_game_state
class RLPlayer(BasePokerPlayer):
def receive_game_start_message(self, game_info):
# Setup Emulator
self.emulator = Emulator()
self.emulator.set_game_rule(
game_info["player_num"],
game_info["rule"]["max_round"],
game_info["rule"]["small_blind_amount"],
game_info["rule"]["ante"]
)
self.emulator.set_blind_structure(game_info["rule"]["blind_structure"])
for player_info in game_info["seats"]["players":
self.emulator.register_player(player_info["uuid"], SomePlayerModel())
def declare_action(self, valid_actions, hole_card, round_state):
game_state = restore_game_state(round_state)
# Simulate an action to decide
updated_state, events = self.emulator.apply_action(game_state, "fold")
# ... decision logic ...
return "fold", 0