Personnal project to demonstrate how Artificial Intelligence (AI) can be applied to the simple 2048 game
Personal project to illustrate how some concepts from Artificial Intelligence (AI) can be applied to the 2048 video game.
If you want more details, please refer to the series of blog posts that I wrote on my personal blog:
2048AI/
│
└───ai/
│ │ Layer.py
│ │ NeuralNetwork.py
│
└───data/
│ └───replays/
│ └───train_logs/
│
└───model/
│ │ Game.py
│ │ Grid.py
│ │ History.py
│
└───ui/
│ │ TkConstants.py
│ │ Window.py
│
│ Constants.py
│ Main.py
The entry point of the program is the Main.py
file. To display the program usage help, simply type:
$ python3 Main.py -h
Result:
usage: Main.py [-h] [--game {HUMAN,RANDOM,NEURAL}] [--path PATH] {PLAY,STATS}
Play a new 2048 game or analyze a finished one
positional arguments:
{PLAY,STATS} whether to play or analyze a 2048 game
optional arguments:
-h, --help show this help message and exit
--game {HUMAN,RANDOM,NEURAL}
the kind of 2048 game to play
--path PATH relative path of the game log file to analyze in the
data folder (e.g., train_logs/human_2048_1.log)
For the sake of simplicity, some variables are defined in the Constants.py
file.
By editing this file, you can easily change:
4
);2048
);data
).Then, to play a new game in an interactive mode with GUI, simply type the following command in your terminal:
$ python3 Main.py PLAY --game HUMAN
By default, your game logs are saved in data/replays
.
For instance, all the game logs corresponding to a 4x4 grid size will be saved in data/replays/4_4
.
Replay mode is only available through the GUI. To start the GUI, enter the command:
$ python3 Main.py PLAY --game HUMAN
You can then import a previously saved 2048-game log file by clicking on File > Open game...
.
Then, you can navigate through the game with the directional arrows ( ← → ↑ ↓ ).
Main.py
and design your own Neural Network below the line:You can customize the number of layers (with
# TODO: customize your neural network below
add_layer
) as well as the number of neurons(Layer(X, Y)
).Finally, you can also customize the directory used for training (train_logs
by default), the learning rate (0.3
by default) as well as the number of
cycles that the training process should last (400
by default).
For convenience, all these parameters can be edited in the Constants.py
file:
TRAIN_DIR_NAME = 'train_logs'
NEURAL_NET_TRAINING_RATE = 0.3
NEURAL_NET_MAX_EPOCHS = 400
If you are new to AI and/or neural networks, I encourage you to read this excellent blog post that explains how to
implement a flexible neural network with backpropagation from scratch.
data/train_logs
directory.From your terminal, start the program with the following command:
$ python3 Main.py PLAY --game NEURAL
After training, your AI will play until it wins/looses (spoiler: it is most likely to
lose ).
When it does, some game stats are displayed before the program ends.
If you want to precisely see which moves your AI played (although it will not tell you why it decided
to play that way/move), the associated replay is available in data/replays/
.
OK - File parsed: data\train_logs\human_1024_1.log
OK - File parsed: data\train_logs\human_2048_1.log
OK - File parsed: data\train_logs\human_512_1.log
Epoch: #0, MSE: 0.000664
Epoch: #10, MSE: 0.000055
Epoch: #20, MSE: 0.000029
[...]
Sorry, you loose...
Final direction/state: States.LOOSE
Final score: 600
Number of rounds: 68
Max. tile: 64
Avg. points per round: 8.955223880597014
Choice frequencies: {0: 67.16417910447761, 1: 19.402985074626866, 2: 13.432835820895523}
Process finished with exit code 0
The question of designing an AI able to always win is, of course, out of the
scope of this project .
Nevertheless, you can read my blog post to understand better why your
AI is (almost) always loosing if you play with a high-target tile (such as 2048).
Simply type the following command in your terminal:
$ python3 Main.py PLAY --game RANDOM
Use the STATS
mode alongside with the filepath of the 2048 log that you want to analyze. For instance:
$ python3 Main.py STATS --path train_logs/human_2048_1.log
Result:
Final direction/state: States.WIN
Final score: 19984
Number of rounds: 753
Max. tile: 2048
Avg. points per round: 26.574468085106382
Choice frequencies: {0: 100.0}