Skip to content

rhalbersma/gym-blackjack-v1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI Gym blackjack environment (v1)

Language Standard License Lines of Code

Requirements

  • Python version 3.6 or higher

Installation

git clone https://github.com/rhalbersma/gym-blackjack-v1.git
cd gym-blackjack-v1
python3 -m venv .env
source .env/bin/activate
pip install --upgrade pip setuptools wheel
pip install -e .

Hello World

Let's simulate one millions blackjack hands using Sutton and Barto's blackjack rules and Thorp's basic strategy:

import gym
import gym_blackjack_v1 as bj
env = gym.make('Blackjack-v1')
agent = bj.BasicStrategyAgent(env)
stats = bj.simulate(agent, env)
dict(zip(stats.data, stats.weights / stats.sum_weights)), stats.mean, stats.tconfint_mean()

The above code will output the distribution of outcomes (win, loss, tie), the mean score per hand and its 95% confidence interval:

>>> import gym
>>> import gym_blackjack_v1 as bj
>>> env = gym.make('Blackjack-v1')
>>> agent = bj.BasicStrategyAgent(env)
>>> stats = bj.simulate(agent, env)
100%|██████████████████████████████| 1000000/1000000 [00:28<00:00, 35182.52it/s]
>>> dict(zip(stats.data, stats.weights / stats.sum_weights)), stats.mean, stats.tconfint_mean()
({0.0: 0.087323, -1.0: 0.479979, 1.0: 0.432698}, -0.047281, (-0.04915114318214309, -0.04541085681785691))

A player using the basic strategy will approximately lose 48.0%, win 43.3% and tie 8.7% of all hands, for a mean score per hand of -4.73%, give or take 0.19%.

Acknowledgement

Special thanks to Craig L. Zirbel for his illumunating blackjack paper.

License

Copyright Rein Halbersma 2020-2021.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Releases

No releases published

Packages

No packages published

Languages