Materials available at: https://forejune.co/cuda/
The Pong Game
OpenGL Animation
glutVisibilityFunc(visHandle) registers a visibility callback for the current window When the window's visibility state changes, GLUT immediately calls the function visHandle GLUT_VISIBLE GLUT_NOT_VISIBLEglutTimerFunc registers a timer callback function to execute once after some milliseconds
void visHandle(int visible) { if (visible == GLUT_VISIBLE) timerHandle ( 0 ); else ; }
// Visibility callback void timerHandle (int value) { animate(); glutPostRedisplay(); // call timerHandle 25 ms later, // 0 is passed to timerHandle, not used here glutTimerFunc (25, timerHandle, 0); }
Complete C/C++ Program
|
|
Discussions
Training a Transformer to Play Pong Game
See also Training an AI Transformer to Play Tic-Tac-Toe in C/C++
State Sn ~ Action An a token ~ (S, A) State : (ballX, ballY, ballDX, ballDY, yl, yr) Action : (paddle y = y + Δy) During inference: Given (S0 A0), (S1 A1), ......, (Sn-1, An-1) The tranformer predicts the next state and action: (Sn, An)Gather Data:
Method 1: Rule-based expert:
if(ballY > paddleY + d)
Δy = D;
else if(ballY < paddleY - d)
Δy = -D;
else
Δy = 0;
action = Δy;
The transformer learns to imitate the expert.
Method 2: Playing the game by humans:
Play the game by humans and record sequences of
(state, action) ~ token
Method 3: Reinforcement Learning
The transformer interacts with the game and optimizes rewards:
+1 for hitting the ball
-1 for missing
Return: cumulative reward the agent aims to achieve State Action Example Score = 9 State0 Δy = D (UP) Score = 9 State1 Δy = 0 (STAY) Score = 10 State2 Δy = -D (DOWN) During training, we can mask the tokens so that the model only predicts the Action tokens