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.

3,156
best mean score over 100 games (Dueling DDQN + ghost-state task)
+68.8%
vs. the DDQN baseline (1,869)
+53.6%
DDQN from the reward change alone
2 / 2
algorithms improved by the ghost-state task

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 agent with the ghost-state task, early in training (episode 64 of 5,000), recorded in the Gym environment.

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

Training pipeline with both modifications The game returns frames and a reward. Frames are preprocessed and stacked. The reward is reshaped (modification 1) and also used to make free labels for ghost state and power pills left. Transitions go to a replay buffer. A shared CNN feeds three heads: Q-values, ghost state, and power pills left. The loss is the DDQN loss plus alpha times the two cross-entropy losses (modification 2). 1 · COLLECT EXPERIENCE 2 · LEARN Ms. Pac-Man Gym MsPacman-v0 9 actions frames Preprocess gray · 84×84 · stack 4 · skip 4 reward r Mod 1 · reward shaping no progress −1 · life lost −200 Free labels from r ghost scared? · power pills left Replay buffer (s, a, r′, s′, done, ghost state, pills left) action: ε-greedy on the online network sample a batch of 64 Shared CNN 32 · 8×8 / 4 64 · 4×4 / 2 64 · 3×3 / 1 FC 512 features Q(s, a) · 9 values (Dueling: V(s) + A(s, a)) Ghost state · 2 classes scared or dangerous Power pills left · 5 classes 0, 1, 2, 3 or 4 TD loss vs. target net (Double DQN target, MSE) cross-entropy cross-entropy L = L_DDQN + α · L_GS new in this project shared network
Training pipeline. Red parts are the two modifications. The baselines use the same pipeline without them: the raw reward, and only the Q-head. On narrow screens, scroll the diagram sideways.

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:

y = r + γ · Qtarget( s′, argmaxa Qonline(s′, a) )

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 eventBaselineMod 1
No pellet eaten (stationary)0−1
Hit a dangerous ghost (life lost)0−200
Pellet1010
Power pill5050
Fruit100100
Eat a scared ghost200200

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.

LMOD = LDDQN + α · LGS,    LGS = CE(ŷpills, ypills) + CE(ŷghost, yghost)

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 agent5,000
Batch size / learning rate64 / 1e-4 (Adam)
Discount γ / target-net copy0.99 / every 10,000 steps
Replay memory50,000 transitions
Exploration ε1.0 → 0.1 (decay 5e-6); 0.05 at evaluation
HardwareOne NVIDIA RTX 2080 (8 GB), PyTorch

Results

Each trained agent played 100 games with ε = 0.05. The table shows the mean raw score.

BaselineMod 1: rewardMod 2: ghost-state task
DDQN · baseline
1,869
DDQN · Mod 1
2,871
DDQN · Mod 2
2,438

Dueling DDQN · baseline
2,431
Dueling DDQN · Mod 1
1,916
Dueling DDQN · Mod 2
3,156
AlgorithmαMean scorevs. its baseline
DDQN (baseline)–1,869.2–
DDQN + Mod 1 (reward)–2,871.2+53.6%
DDQN + Mod 2 (ghost state)0.52,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.253,155.9+29.8%
Running average of the last 200 scores over 5,000 episodes. Left: DDQN with the reward change stays well above plain DDQN. Right: Dueling DDQN with and without the reward change track each other.
Mod 1, training curves (running mean of the last 200 scores). The reward change lifts DDQN all through training. For Dueling DDQN the two curves stay close.
Running average of the last 200 scores over 5,000 episodes. Left: DDQN with ghost-state estimation is above DDQN throughout. Right: Dueling DDQN with ghost-state estimation starts slower, passes the baseline around episode 2,700 and ends near 2,400 versus about 1,800.
Mod 2, training curves. The ghost-state task helps both agents. Dueling DDQN starts slower, likely because the reward-derived labels are noisy early on. It passes its baseline near episode 2,700 and keeps a clear lead.

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