Skip to main content
 _________________
< I like connect4 >
 -----------------
   \
   \   _//| .-~~~-.
     _/oo  }       }-@
    ('')_  }       |
     `--'| { }--{  }
          //_/  /_/

Image origination

Intro

Using reinforcement learning to train an agent to play Connect4 as well as it can. I use a recursive neural network as a value approximator which also outputs a policy of the likely best move in a position. This is combined with a Monte Carlo Marcov Chain tree search to plan the best move.

This repo is a self-project to learn about reinforcement learning. I hope that other people might also be able to learn from my work. I welcome constructive criticism on absolutely anything from how I could have improved the Machine Learning, python programming, project layout, etc.

Installation

pip install oinkoink

Alternatively checkout the repo and include the path to project base in your PYTHONPATH. If you checkout the repo you will also have all the misc scripts that I have used, in oinkoink/scripts. Be warned that these were Hacked (with a capital H) together. If you use this approach you will need to call the oinkoink/main.py script directly when running.

Usage

oinkoink -m [mode] [mode options]

There are two modes that can be used. To play a game vs the AI use:

oinkoink -m game [-n network_file -s simulations]

The default simulations is 800, this is the number of positions the AI will analyse before making a move. If a network file is not provided, a pre-trained one provided in oinkoink/data/example_net.pth is used. If you change any of the network parameters specified in oinkoink/neural/config.py:NetConfig you will need to train your own.

Gameplay

To run a self-play training loop use:

oinkoink -m training [-c config.py]

It is strongly recommended to create a config for yourself following the example of oinkoink/data/example_config.py. It is especially important to specify a working directory, everything else can use the default values found in oinkoink/neural/config.py.

Playing a game will run the network on the CPU - typically fast enough. The training loop will make use of a Cuda enabled GPU, and can use multiple processes. A generation of training (game creation + training) takes ~50min using the default settings on my machine Benchmarks. The game generation is quite CPU intensive, it is not required to have an amazing GPU. Though you will need to scale down the number of processes/threads if your GPU runs into memory issues.

Acknowledgements

This project is a re-implementation of Deepmind's paper A general reinforcement learning algorithm that masters chess, shogi and Go through self-play. This is a personal project, so the code isn't commented, etc. as it would be for a group project, and having an understanding of that paper will make things much clearer for a reader.

Another resource that has covered this that I found useful was an Oracle blog on medium. This is what I used as a rough guide to follow in selecting Connect4 as a reasonable game, and a few of the changes.

Other useful resources are:

Finally I should acknowledge the less glamarous but probably most imporant role that StackOverflow has played.

Conventions

A position is scored 1 if it is a win for the first player to move in a game of Connect4 ('player_o') and value 0 if the second player will win ('player_x'). Game tree nodes store the absolute position evaluation, but when querying the node for the value, it will return the relative value to the player querying (i.e. a value of 1 means the player to move will win).

Notes

I differ from Deepmind's paper in the following ways:

  • My training is synchronous, it runs after a generation of training games. All the games from the previous max (20, gen/2) generations are used.
  • I 'cheat' and augment my training data with positions reflected in the vertical axis, using the symmetrical nature of connect4.
  • I don't use parallel MCTS, relying instead on parallelising the game playing.
  • I select moves based upon value, rather than visit count. ** During the exploration moves at the start, I select proportional to the squared value of possible moves, rather than softmax of visit counts.
  • Lots of parameters are different from their suggested values (it is a different game after all).

User vs AI gameplay has not been optimised in any way; the point of this project is the training process. This functionality has been included for fun.

Verification of the neural network

As a test, I trained the network on the evaluation set to see if it was capable of learning the positions. Results are in this Jupyter Notebook

Training

The evaluation datasets are found in oinkoink/data. The 8ply file has all the positions from the UCI repo, converted into pytorch tensors. I use the RMSE of the position values outputted from my model compared to the theoretical values.

Because the network also outputs move probabilities, I generated a 7ply dataset where I solve for 'weak' winning moves using the 8ply dataset. Weak because any move the leads to a theoretically winning position is counted as correct, even if it is not the fastest win available. The evaluation loop also tests on these 7ply positions, and as well as the RMSE of the values, it finds the Cross Entropy Loss of the network policy.

Here are the results of an example training run with the default parameters from oinkoink/neural/config.py except where overridden by oinkoink/data/example_config.py and:

  • Learning Rate was 0.01 (5 training epochs) for 104 generations, then dropped to 0.001 (still 5 epochs per generation)

And the results of different generations playing each other, from all 49 possible 2 ply positions, playing both first and second player in each position (98 games). The results represent Row vs Column in W-D-L and return (1 * W + 0.5 * D + 0 * L). The final column shows the average the return over all matches a generation played (5 in total):

Generation 40 60 80 100 120 Mean Return
20 19-11-16 0.25 16-5-77 0.19 19-9-70 0.24 14-4-80 0.16 5-6-87 0.08 0.18
40 30-18-50 0.40 21-14-63 0.29 27-11-60 0.33 30-8-60 0.35 0.42
60 27-17-54 0.36 34-14-50 0.42 32-12-54 0.39 0.52
80 39-15-44 0.47 33-19-46 0.43 0.60
100 34-16-48 0.43 0.61
120 0.67

Metadata

Release files for oinkoink 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oinkoink 1.0.1
File Size Uploaded Provenance Actions
oinkoink-1.0.1.tar.gz 1.3 MB Without provenance Details for oinkoink-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for oinkoink 1.0.1
File Interpreter ABI Platform Actions
oinkoink-1.0.1-py3-none-any.whl Python 3 none any Details for oinkoink-1.0.1-py3-none-any.whl

Total release size: 3.1 MB

Release files / oinkoink-1.0.1.tar.gz

Download URL oinkoink-1.0.1.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
711c48b3594532d0eeb8f4482528d87e9386220e90b01e2409b1b1513de3fd43
BLAKE2b-256 checksum
How to use checksums
56ea22d1ab74b54686dce861e67efbd8b6084d0c2426a3e11e9a25bdbc3df2f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Release files / oinkoink-1.0.1-py3-none-any.whl

Download URL oinkoink-1.0.1-py3-none-any.whl
Size 1.8 MB
Tags Python 3
SHA-256 checksum
How to use checksums
e4b726c9c39e2fc7752b9c20fe712d7a1ca9acf81f7616748b2d22e5c5a6b2b9
BLAKE2b-256 checksum
How to use checksums
0360a99fc920b26a3735a29a412e94da10667cb1f382e92e13102b39a7d6a1f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Security for oinkoink 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page