Skip to main content

Reinforcement Learning Agent capble of playing 2048

Project description

Reinforcement Learning to Play 2048

This project trains agents to play the game 2048 using Q, SARSA, and TD0 learning agents.

Installation

This project is pip installable. On linux it can be installed by entering, pip3 install rl2048player into your terminal. For other operating systems, follow the pip instructions for that system.

Features

In order to use this package, you must first import it using

import rl2048player as rl

Now, I will review some of the main features of this package. For most of these features, there are addditional options not explained in this review. All options for any command can be found by reviewing the documentation in the code.

Masks

Pefore creating a learning agent, you must initialize a mask. Masks translate between the game board and the learning agent. This allows you to change the way the agent understands the game board without changing the agent itself. Currently, only one mask has been implemented. This mask breaks down the board into rows, columns, and 2x2 squares. This has the effect of decoupling parts of the board that do not interact strongly with each other. The mask can be initialized using the code

mask = rl.masks.Mask_rxcx4()

Agents

Once the mask has been initialized, you can initialize the agents. Currently, there are three agents that have been implemented: one using a Q learning algorithm, one using a SARSA learning algorithm, and one using a TD0 learning algorithm. These agents can be initialized by

agent = rl.agents.QAgent(mask)
agent = rl.agents.SARSAAgent(mask)
agent = rl.agents.TD0Agent(mask)

respectively. There are also options to change the agent's hyperparameters when initializing them. The default values are the hyperparameters I have found to work best.

Training

To train the agents over x number of games and record the scores use the code

scores = agent.train(x)

You can then look at a graph of those scores by

agent.makeGraph(scores)
plt.show()

There are also options to automatically save the scores and graph to a file.

Making Gif's

Once the agent has been trained, you can create a gif of the agent playing a game and save it to a file by using the command

agent.makeGif(gif_file)

Saving and Loading

You can also save trained agent by using

agent.save(agent_file)

This save function only saves the look-up table for this agent, not the whole object. In order to load the agent, you need to initialize an agent with the same mask and hyperparameters and then load the look-up table using

agent = rl.agents.AppropriateAgent(AppropriateMask)
agent.load(agent_file)

Examples

If you don't want to have to do all of this to use this package, there are some examples precoded in the package. Currently there are two examples that can be accessed using

rl.examples.example1()
rl.examples.example2()

Acknowledgments

The game logic for this project is based on code by georgwiese which can be found at https://github.com/georgwiese/2048-rl. The implementation of the learning agents is based on method described in the paper Temporal difference learning of n-tuple networks for the game 2048, by M.Szubertand W. Jaskowski. All code is my original work except when noted.

Future Work

In the future, I plan to work on implementing additional masks and learning agents.

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

rl2048player-0.0.3.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

rl2048player-0.0.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file rl2048player-0.0.3.tar.gz.

File metadata

  • Download URL: rl2048player-0.0.3.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for rl2048player-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d59425f86fec4a73d648e82faaafcb96aab9911e097d9b580e1d70a623609469
MD5 dd3df7144ecba66ef31903b78371ad15
BLAKE2b-256 aee374b3d0a67a87edb8c46b72a6888c65a985ae369e86c6e5db8853cd7c3c94

See more details on using hashes here.

File details

Details for the file rl2048player-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: rl2048player-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for rl2048player-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 77c2a8a8e09cae53ab9c9d41ef60fd7970ecf2a9fbe756d3b94a20aa8e4de45b
MD5 a9f1b7cc27cb12c6bca36a15acb3de42
BLAKE2b-256 5a468875933528cff82e1c07ce927a41bea0993e0fa1b77659341dca22cbaf5d

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