/Q-Learning

Teaching an agent to play a simple game using Q-Learning in Numpy

Primary LanguagePython

Q-Learning

Agent learns to play a simple game using Q-Learning in Numpy

Requirements

  • PIL >= 6.2.0
  • opencv-python >= 4.1.1
  • numpy >= 1.7.3
  • matplotlib >= 3.1.1
  • tqdm

Training the RL Agent

python q_learning.py

Note that if you want to train the agent from scratch (initiaalize the q-table randomly), then set q_table to None in q_learning.py, else set q_table to the path of the already saved q_table.

Results

Following is the plot for the moving average of the rewards. It's upward trend shows that the agent becomes smarter with more and more episodes of training.
reward_vs_episode.jpg

And here are some GIFs that show how the agent gets smarter with every episode of training.

Here is the thirsty agent looking for the bottle of beer with randomly initialized q-table. It means that the agent has no clue about the environment yet.
dumb_agent_gif.gif

After some training, the agent does a relatively better job of making sequential decisions. He is not very fast yet but he ends up finding the beer eventually.
moderately_smart_agent_gif.gif

Finally after thousands of episode of training, the agent gets really good at making sequential decisions and finds the beer in no time ! : D
smart_agent_gif.gif

Blog on Medium

https://medium.com/@animesh7pointer/using-q-learning-in-numpy-to-teach-an-agent-to-play-a-game-4fee32eb922a