Deep Reinforcement Learning · Course project, UC Riverside · 2022
Teaching an Agent to Play Ms. Pac-Man from Pixels
Two simple changes to DDQN and Dueling DDQN: a reward that tells the agent what actually hurts, and an extra task that makes the network notice whether the ghosts are scared. The best agent scores 68.8% more than the DDQN baseline.
At a glance
- Problem. Ms. Pac-Man is one of the Atari games where simple value-based agents stay far below human play. The agent sees only raw screen pixels.
- Idea 1: reward shaping. The default reward gives 0 both for standing still and for dying. I give −1 for a step with no progress and −200 for losing a life.
- Idea 2: an auxiliary task. The network also predicts whether the ghosts are scared and how many power pills are left. The labels come for free from the reward stream, so no extra labeling is needed.
- Outcome. The reward change lifts DDQN by 53.6%. The auxiliary task lifts both DDQN (+30.4%) and Dueling DDQN (+29.8%). The best agent reaches a mean score of 3,155.9.
The problem
The agent plays MsPacman-v0 in OpenAI Gym. Each state is the last 4 screen frames, turned to grayscale and resized to 84×84 (a 4×84×84 tensor). There are 9 possible moves. The agent must eat the pellets and avoid four ghosts. After it eats one of the four power pills, the ghosts turn blue (scared) for a short time and can be eaten for bonus points.
Two things make this hard for DDQN-style agents:
- The reward hides the most important event. Losing a life gives reward 0, the same as a step where nothing happens. The agent gets no clear signal that touching a dangerous ghost is the worst thing it can do.
- The grayscale state hides the ghost's mood. A scared ghost and a dangerous ghost differ mainly in color. Grayscale input weakens that cue. In training, the agent often ran toward dangerous ghosts and away from scared ones. Adding color back would cost 3× more memory and compute.
Method
Baselines: DDQN and Dueling DDQN
Double DQN uses the online network to pick the best next action and a slowly updated target network to score it. This cuts the over-estimation of plain DQN:
Dueling DDQN splits the last layers into a state value V(s) and an advantage A(s, a), then joins them back into Q(s, a). Both agents use the same CNN body and the same settings, so the comparison is fair.
Modification 1: a reward that separates "idle" from "dead"
The default reward is 0 both when Ms. Pac-Man makes no progress and when she runs into a dangerous ghost and loses a life. Losing a life is far worse, so it should get a far stronger signal. I changed only those two rows:
| Game event | Baseline | Mod 1 |
|---|---|---|
| No pellet eaten (stationary) | 0 | −1 |
| Hit a dangerous ghost (life lost) | 0 | −200 |
| Pellet | 10 | 10 |
| Power pill | 50 | 50 |
| Fruit | 100 | 100 |
| Eat a scared ghost | 200 | 200 |
Modification 2: make the network predict the ghost state
A compact, ideal state would say where the pills are and whether the ghosts are scared. Pixels do not give that directly. So I keep the pixel input and add a second training task that forces the CNN to learn those features. Two small heads sit on the shared 512-unit feature layer:
- Ghost state (2 classes): are the ghosts scared right now?
- Power pills left (5 classes, 0 to 4): this ties closely to the ghost state, because a power pill makes the ghosts scared.
The labels cost nothing. They come from the reward stream during play. A reward of 50 means a power pill was eaten, so the pill count drops by one and the ghosts count as scared for the next 40 steps. No human labeling and no game internals are needed.
Cross-entropy uses label smoothing 0.2, because the labels from rewards are a little noisy. α = 0.5 for DDQN and α = 0.25 for Dueling DDQN.
Training setup
| Episodes per agent | 5,000 |
|---|---|
| Batch size / learning rate | 64 / 1e-4 (Adam) |
| Discount γ / target-net copy | 0.99 / every 10,000 steps |
| Replay memory | 50,000 transitions |
| Exploration ε | 1.0 → 0.1 (decay 5e-6); 0.05 at evaluation |
| Hardware | One NVIDIA RTX 2080 (8 GB), PyTorch |
Results
Each trained agent played 100 games with ε = 0.05. The table shows the mean raw score.
| Algorithm | α | Mean score | vs. its baseline |
|---|---|---|---|
| DDQN (baseline) | – | 1,869.2 | – |
| DDQN + Mod 1 (reward) | – | 2,871.2 | +53.6% |
| DDQN + Mod 2 (ghost state) | 0.5 | 2,437.5 | +30.4% |
| Dueling DDQN (baseline) | – | 2,431.4 | – |
| Dueling DDQN + Mod 1 (reward) | – | 1,916.2 | −21.2% |
| Dueling DDQN + Mod 2 (ghost state) | 0.25 | 3,155.9 | +29.8% |
What I learned
- The reward function is part of the model. Changing two reward values lifted DDQN by 53.6% with no other change. The same values hurt Dueling DDQN (−21.2%), so reward shaping must be tuned for each algorithm.
- Auxiliary tasks shape the features. Asking the CNN to predict what matters (ghost mood, pills left) helped both algorithms. It costs almost nothing at run time: two tiny linear heads.
- Free labels are often already in the data. The reward stream was enough to build the auxiliary labels, with no human labels and no access to game memory.
- What did not work. I also tried an autoencoder to compress frames into a small state. Good reconstructions needed a large code, so it saved little memory or compute and made training slower.
Code
PyTorch code for all six agents is on GitHub. Trained weights are not included.
code/
DDQN/ network.py · ddqn.py · ddqn_mod2.py · run_ddqn.py · run_ddqn_mod2.py
buffer.py · utils.py (Gym wrappers, reward change, free labels) · get_plots.py
Dueling DDQN/ the same files for Dueling DDQN
requirements.txt · README.md
cd DDQN
python run_ddqn.py --name_exp DDQN_baseline # baseline
python run_ddqn.py --name_exp DDQN_Reward_Mod --reward_mod # modification 1
python run_ddqn_mod2.py --name_exp DDQN_Mod2 --alpha 0.5 # modification 2