All projects

RL Dangerous Dave

A PPO agent with Random Network Distillation learns to play a PyGame re-creation of Dangerous Dave, driven by curiosity as well as score.

Built with PyTorch, Gymnasium, Stable-Baselines3, PyGame, NumPy

A reinforcement-learning agent playing a level of Dangerous Dave
The agent playing the PyGame re-creation of Dangerous Dave.

How it works

A playable PyGame re-creation of Dangerous Dave, the 1990 DOS platformer, wrapped as a Gymnasium environment and solved with PPO augmented by Random Network Distillation.

A frozen target network and a trainable predictor turn prediction error on novel frames into intrinsic curiosity, so the agent explores well beyond what the score reward alone would teach it.

Observations are 4-frame stacks of grayscale captures (with text and grid modes available), training runs on parallel vectorized environments, and every evaluation is automatically exported to video with ffmpeg.

Random Network Distillation: the same observation goes to a frozen target network and a trained predictor. The gap between their outputs is the curiosity reward, and the predictor is trained to close it.4 framesTarget networkrandom, frozenPredictortrainedCuriosityrewardfamiliar frames stop being rewarding
Random Network Distillation: the predictor’s error on a frame is the curiosity reward for reaching it.

Specs

Algorithm
PPO + Random Network Distillation (RND)
Curiosity reward
Prediction error against a frozen target network
Network
Shared CNN trunk, actor + extrinsic/intrinsic value heads
Action space
Discrete(7): movement, diagonal jumps, no-op
Observations
4 stacked grayscale frames; text and grid modes
Reward shaping
Score delta per step, -0.1 step penalty
Training setup
4 parallel envs, 1,000-step rollouts, annealed lr

Agent network

agent.py
from torch.distributions.categorical import Categorical
from algos.utils import layer_init

class Agent(nn.Module):
    def __init__(self, envs):
        super().__init__()
        self.network = nn.Sequential(
            layer_init(nn.Conv2d(4, 32, 8, stride=4)),
            nn.ReLU(),
            layer_init(nn.Conv2d(32, 64, 4, stride=2)),
            nn.ReLU(),
            layer_init(nn.Conv2d(64, 64, 3, stride=1)),
            nn.ReLU(),
            nn.Flatten(),
            layer_init(nn.Linear(64 * 8 * 4, 256)),
            nn.ReLU(),
            layer_init(nn.Linear(256, 448)),
            nn.ReLU(),
        )
        self.actor = nn.Sequential(
            layer_init(nn.Linear(448, 448), std=0.01),
            nn.ReLU(),
            layer_init(nn.Linear(448, envs.single_action_space.n), std=0.01),
        )
        # two critics: extrinsic (game score) and intrinsic (curiosity)
        self.critic_ext = layer_init(nn.Linear(448, 1), std=0.01)
        self.critic_int = layer_init(nn.Linear(448, 1), std=0.01)

    def get_action_and_value(self, x, action=None):
        hidden = self.network(x / 255.0)
        logits = self.actor(hidden)
        probs = Categorical(logits=logits)
        if action is None:
            action = probs.sample()
        return action, probs.log_prob(action), probs.entropy(), \
               self.critic_ext(hidden), self.critic_int(hidden)