Skip to content

Implementing Double DQN to train it on a custom pong environment in C++ from scratch

Notifications You must be signed in to change notification settings

vedvartak/PongDQN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

PongDQN

Introduction

Experimental implementaion of the Double DQN algorithm in C++ from scratch to train it on custom Pong environment. DQN agent learns to play Pong by playing against computer opponents using inputs from RAM. Pong environment is rendered using OpenGL with Win32 API for window and context creation. DQN library uses a simple neural network library implimented from scratch, a custom math library supporting vectors and matrices.

DQN agent learns to hold its own against chaser AI even if max vertical speed of paddle is less than max vertical speed of ball in about 400 episodes (approx 1 min on i5 11th gen at 3 GHz).

Results

Training LogTesting Log

Running demonstration

A x86-64 binary is provided in bin folder for demonstration purpose. After running this library a pre-trained DQN agent (left) will play with a simple chaser algorithm (right). User can use controls specified in this document to control demo.

Controls

  1. M
  2. To speed up the environment.
  3. L
  4. To log the model as cache.
  5. P
  6. Pause the application. To continue press enter on terminal. (Application uses blocking I/O call for pausing instead of busy waiting)

Config

Before compiling application their are some configurations that should be made in main.cpp according to the needs.

  1. OPPONENT AI
    1. BOT_CHASER
    2. A simple chaser AI as opponent on DQN agent. It alway tries to follow ball to hit at center of paddle.
    3. BOT_EDGE
    4. Slightly chalanging AI which has 50-50% probability to hit ball at center or hitting ball at edge.
    5. PLAYER
    6. Player can play as opponent using mouse.
    7. BOT_AGENT
    8. A second pre-trained DQN agent which can be used to play against agent on left.
  2. TRAIN
  3. If you want to train the model uncomment the define statement.
  4. MODEL_ID
  5. ID of model, which will be used in filename of model saved during traing,training log and testing log.

You can change model to be loaded for each agent in main.cpp. (Search for comment "Load model")

Building code

(This project only supports windows as UI and OpenGL initialization uses Windows APIs)

  • using vscode: After downloading repo simply open PongDQN folder in vscode. After adjusting configurations according to need simply run the task provided in .vscode/task.json. Binary will be created in bin folder.

Modules

This repository contains 6 libraries that are implemented from scratch in C++:

  1. Mathlib:
  2. A simple math library containing implementation of vectors and matrix which is used to implement neural networks.
  3. Neurallib:
  4. A simple neural network library containing implementation of deep neural networks, activation functions and loss functions which is used to implement DQN agent.
  5. Gymlab:
  6. A simple gym environment library for RL agents containing implementation of PONG environment which is used alongside DQN agent to train it.
  7. Uilib:
  8. A simple UI library(windows) for creating windows which can be used for rendering.
  9. Renderlib:
  10. A simple rendering library for rendering gym environment for human understanding. It provides way to draw simple 2d shapes using OpenGL API.
  11. Rllib:
  12. A simple reinforcement learning library containing implementation of DQN algorithm.

Screenshots

Pong environment
Training log
Test log

About

Implementing Double DQN to train it on a custom pong environment in C++ from scratch

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published