Skip to main content

RL environments for poker games

Project description

PokeRL

Build Status

pokerl is a poker Reinforcement Learning (RL) environment.

Currently the only game supported is No Limit Texas Hold'em.

Usage

agent@pokerl:~$ pip install pokerl

A game is created by creating a new instance of the Game class:

from pokerl import Game
game = Game()

The constructor accepts various parameters, such as the number of players and the initial credits:

game = Game(num_players=4, start_credits=100)
game.reset() # Init game state

At each step, there is an active player and an active state, which captures the state of the game as seen by the active player. The active player performs actions by passing a valid integer value to game.step(action):

from pokerl.enums import PokerMoves
game.step(PokerMoves.CALL)
game.step(PokerMoves.FOLD)
game.step(PokerMoves.ALL_IN)

The type of action must be int or np.integer

The game.active_state property is an object of type Game.StateView that has various informations that can be used by the agent to make informed decisions. In particular, valid_actions is a one-hot encoded np.ndarray of valid actions:

# Choose random action
valids = game.active_state.valid_actions
action = np.random.choice(len(valids), p=valids / np.sum(valids))
game.step(action)

If an invalid action is passed to game.step (e.g. if you try to call a bet but you don't have enough credit) an AssertionError is thrown.

game.step also returns a 3-element tuple indicating whether the game is over, the hand is over and/or the round of bets is over:

done, _, _ = game.step(action)
if done: print('game is over')

After each hand, the net profit of each player is saved in game.payoffs.

For more info see the Wiki page.

Environments

If you are familiar with OpenAI Gym, the racerl.envs environments expose an API similar to that of gym.Env. The PokerGameEnv simulates an entire game and returns a non-zero reward after each hand, depending on the player's payoffs and bets. To use the PokerGameEnv create a new instance by passing a list of opponents:

from pokerl import Game
from pokerl.envs import PokerGameEnv

def random_agent(state: Game.StateView) -> int:

	actions = state.valid_action_indices
	return np.random.choice(list(actions))

env = PokerGameEnv([random_agent, random_agent, random_agent], num_players=4)

An opponent must be a callable object which receives the game state as seen by the agent and returns the action to perform. The usual methods env.reset() and env.step(action) can be used to reset the state and perform actions on the environment:

# Evaluate agent on one run

R = []
state = env.reset()
done = False
while not done:
	action = predict(state)
	state, rwd, done = env.step(action)
	R.append(rwd)

print('score', np.sum(R))

The reward is the sum of the payoffs.

Network

The network branch is an experimental branch where it is possible to play online games. Many functionalities, however, are also available in the main branch.

To create a server, create a new instance of the pokerl.network.PokerGameServer class:

from pokerl.network import PokerGameServer
server = PokerGameServer(num_players=3)

The constructor accepts the same parameters of Game. To start the server and play a game, use server.setup(host, port) followed by server.run():

server.setup('localhost', 25560)
#server.setup() Listen on default host and port
server.run()

A client is an instance of pokerl.network.PokerGameClient. The contructor accepts a strategy policy agent, which ideally is a subclass of pokerl.agents.PokerAgent:

from pokerl.agents import RandomAgent
client = PokerGameClient(agent=RandomAgent)

To connect to a server, use client.connect(host, port):

client.connect('localhost', 25560)
client.connect() # Use default host and port

Once all the players have connected, the game begins.

At the moment, it is not possible to follow the actions of other clients.

Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pokerl-0.0.4.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pokerl-0.0.4-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file pokerl-0.0.4.tar.gz.

File metadata

  • Download URL: pokerl-0.0.4.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for pokerl-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8a725d778faee9fbd0f30e6ddd6b476b0b1e8bfacb0602714e846a0f17e93a58
MD5 e16ed29509deee958d7f7d981281c629
BLAKE2b-256 b3740cdf677dc138b24e7231d8a58cda10e0ea3394c499ded396255709015097

See more details on using hashes here.

File details

Details for the file pokerl-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pokerl-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.10

File hashes

Hashes for pokerl-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a69f58375d4110182347097e6e384dc8d5f7fea78cee9e6bf368b712a496d3a1
MD5 9e2107574e2485feb20eb476d8c8389e
BLAKE2b-256 5aeab1d034fd5bdb8999ab7fa717b5e9d24f338f749e56c4d9d7c6ffb87a3d49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page