This project compares several policy-gradient and actor-critic reinforcement learning algorithms on Atari Pong. The implemented methods are:
- REINFORCE
- Natural Policy Gradient (NPG)
- Trust Region Policy Optimization (TRPO)
- Proximal Policy Optimization (PPO)
- Soft Actor-Critic (SAC), discrete-action version
Unlike the standard pixel-based Atari setup, this project uses a simplified coordinate-based observation extracted from the game screen. Instead of giving the agent raw images, the environment detects the paddle and ball positions and feeds a short history of these coordinates to the policy. This makes the problem less visually heavy and allows us to focus on the optimization behavior of different policy-gradient methods.
| File | Description |
|---|---|
train_reinforce_pong.py |
REINFORCE training with Monte-Carlo returns |
train_naturalpg_pong.py |
Natural Policy Gradient training using Fisher-vector products |
train_trpo_pong.py |
TRPO training with conjugate gradient and KL-constrained update |
train_ppo_pong.py |
PPO training with clipped surrogate objective and GAE |
train_sac_pong.py |
Discrete SAC training with replay buffer and entropy regularization |
results_* |
Output folders with checkpoints, reward histories, losses, and timing logs |
The environment is based on Atari Pong, specifically: PongNoFrameskip-v4.
The agent controls one paddle and plays against the built-in Atari opponent. A full game ends when one side reaches the standard Pong terminal score. The reward is sparse:
The total episode return is therefore equal to the score difference:
A strong policy should obtain positive return, while a weak policy remains close to:
because it loses nearly every rally.
We model the task as a Markov Decision Process:
where:
-
$\mathcal{S}$ is the state space, -
$\mathcal{A}$ is the action space, -
$\mathcal{P}(s_{t+1}|s_t,a_t)$ is the transition function, -
$\mathcal{R}(s_t,a_t)$ is the reward function, -
$\gamma \in [0,1]$ is the discount factor.
The objective is to find a policy
where a trajectory is:
Instead of using raw Atari frames, we use a coordinate extractor. At each environment step, the RGB frame is scanned to detect:
- the agent paddle location,
- the ball location.
The extracted coordinates are normalized to
The basic coordinate vector is:
To preserve velocity-like information, the observation stacks the last four coordinate vectors:
Thus, the state dimension is:
This gives the policy enough information to infer approximate ball velocity without using a CNN.
Atari Pong exposes six discrete actions:
These correspond to the Atari action set, including actions such as no-op, fire, up, down, and redundant movement actions depending on the ALE interface.
The policy is categorical:
with:
where
The transition function is inherited from the Atari emulator:
Internally, the true emulator state contains the full Pong game state. Our wrapper observes only extracted coordinates.
The reward is the Atari Pong rally reward:
The return from time
We use
All methods use an MLP policy network on the coordinate observation. The common actor structure is:
The output logits parameterize a categorical distribution:
where:
For PPO, an additional value head is used:
For SAC, two Q-functions are used:
REINFORCE is the vanilla Monte-Carlo policy-gradient algorithm.
The policy-gradient theorem gives:
The implemented loss is:
The parameters are updated with Adam:
REINFORCE has high variance because every action in a trajectory receives credit only through the sampled return. In Pong, rewards are sparse and delayed, so this method improves slowly.
Natural Policy Gradient modifies the vanilla gradient step by accounting for the geometry of the policy distribution.
The standard gradient is:
The natural gradient direction is:
where
Equivalently, the Fisher matrix can be obtained from the local curvature of the KL divergence:
The update is:
In the implementation, the inverse Fisher product is computed approximately using conjugate gradient. The step size is scaled to satisfy an approximate KL bound:
so:
NPG improved much faster than REINFORCE in the experiments, but its run in the plot is shorter than the longest runs. It reached positive reward, showing that geometry-aware updates are useful for this problem.
TRPO improves policy-gradient training by explicitly constraining how much the new policy can move away from the old policy.
The surrogate objective is:
and
The constrained optimization problem is:
subject to:
The implementation computes:
without forming the full Fisher matrix. Then conjugate gradient approximates:
A backtracking line search accepts the update only if the KL constraint is satisfied and the surrogate objective improves.
TRPO was probably the strongest method in the final comparison. It reached the highest smoothed reward, although occasional sharp drops appeared during training.
PPO replaces the hard TRPO trust-region constraint with a simpler clipped objective.
The probability ratio is:
The clipped objective is:
The PPO loss is minimized as:
where:
and:
The advantage is computed with Generalized Advantage Estimation:
PPO achieved strong positive returns, although below TRPO in the final plot. It was easier to train than TRPO because it avoids explicit Fisher-vector computations and line search, giving stable objective growth.
SAC is an off-policy maximum-entropy actor-critic method. It optimizes both reward and entropy:
For discrete actions, the soft value is:
The critic target is:
The critic loss is:
The actor loss is:
The entropy coefficient
SAC performance highly depends on the gradient steps done per epoch. With 10 gradient steps per epoch, it trains faster in epochs than all other methods. However, it was computationally long, so it was not trained until full convergence. This is a powerful method for optimizing objective when trajectory sampling is costly, due to tunable gradient steps over the stored replay buffer.
Each script can be launched independently. The general training structure is the same across methods:
- create the Pong environment;
- initialize the policy or actor-critic model;
- collect trajectories or transitions;
- compute the corresponding objective;
- update the model parameters;
- save checkpoints and reward histories.
python train_reinforce_pong.py --epochs 10000 --episodes 20 --gamma 0.99 --lr 3e-4python train_naturalpg_pong.py --epochs 1000 --episodes 20 --gamma 0.99 --epsilon 1e-2 --lr 3e-4python train_trpo_pong.py --epochs 6000 --episodes 20 --gamma 0.99 --lr 3e-4python train_ppo_pong.py --epochs 5000 --episodes 20 --gamma 0.99 --lam 0.99 --lr 3e-4python train_sac_pong.py --total_steps 1000000 --learning_starts 5000 --batch_size 100 --lr 3e-4 --gradient_steps 10To resume training, pass a checkpoint path:
python train_ppo_pong.py \
--epochs 5000 \
--episodes 20 \
--checkpoint_path results_ppo/checkpoint_1000epoch_20ep.ptThe following plot compares smoothed rewards for all five methods.
The smoothed reward curves show the following behavior:
| Method | Result Summary |
|---|---|
| REINFORCE | Very slow improvement. It remains negative even after many epochs. |
| NPG | Learns much faster than REINFORCE and reaches positive rewards in fewer epochs. |
| TRPO |
Best-performing method. It reaches around |
| PPO |
Stable strong improvement, stabilizing around |
| SAC | Fastest in epochs due to multiple gradient steps per epoch. |
This project demonstrates the practical differences between several policy-gradient algorithms on a simplified Atari Pong task.
The main conclusions are:
- Vanilla REINFORCE is not enough for reliable Pong learning under sparse delayed rewards.
- Natural gradients significantly improve learning speed by using policy geometry.
- TRPO gives the strongest performance among the tested methods.
- PPO is the most stable strong practical compromise, achieving good results with simpler implementation than TRPO.
- SAC is fastest in epochs, and is a good candidate when trajectory sampling is costly.
