Dyna Architecture
Learn from each real step once, then replay it many times inside a small learned model, so one trip to the goal teaches the whole path.
Why Does This Exist?
Real steps cost money and time. A robot that learns only from Q-learning needs many physical episodes for a reward at the end of a corridor to reach the start. Pure planning has the mirror flaw: it is fast but needs a perfect model up front, which you never have.
Dyna (Sutton, 1990) runs both on every step. Each real transition does two jobs: a direct Q update from the world, and a model update that stores for . Between real steps the agent replays simulated transitions sampled from visited pairs and applies the same Q update to them. With this is plain Q-learning; with each physical step fans out into many background updates. See planning and learning for the split and prioritized sweeping for smarter sampling than uniform replay.
Think of It Like This
Day shifts plus night rehearsals
A trainee pilot flies one real route by day. The flight recorder saves every turn. At night the trainee sits in a simulator loaded with that same recording and flies the tricky approach fifty more times, at zero fuel cost.
Day flying is direct RL: slow, honest, limited. Night rehearsal is planning: fast, cheap, limited by how well the simulator matches the day. Dyna is the school that schedules both, every day, with the same grading rubric for real and simulated landings.
The analogy stops at the model. A flight simulator ships with physics built in, while Dyna builds its model one observed transition at a time and must never rehearse routes it has never flown.
How It Actually Works
Keep a table and a model . Here is the state, the action, the reward, the next state, the learning rate, and the discount. Each real step runs direct RL, stores the model entry, then loops planning updates over previously visited pairs:
Real and simulated updates share this exact rule; only the source of differs. Search control samples from visited states and from actions tried there, never from unseen pairs.
Worlds change, so Dyna-Q+ adds a recency bonus during planning. Let count steps since the pair was tried in the world and be a small constant. Simulated reward becomes , which makes long untried moves look tempting in simulation and pulls the agent back to check them in the world.
Worked example
Corridor with goal reward at , otherwise . Set , , all Q at .
Real step into the goal from :
Plain Q-learning stops here, so after episode one. Dyna with keeps going, alternating samples:
- Sample : target , so .
- Sample : target , so .
- Sample : target , so .
- Sample : target , so .
- Sample : target , so .
After one physical episode, Dyna holds and against and without planning. The goal reward crossed two states in one episode.
Code
This snippet replays the worked episode with a fixed planning order, so the numbers land exactly as derived.
def q_update(q: float, r: float, gamma: float, best_next: float, alpha: float) -> float: return q + alpha * (r + gamma * best_next - q)
def main() -> None: gamma, alpha = 0.9, 0.5 q_s0, q_s1 = 0.0, 0.0
q_s1 = q_update(q_s1, 10.0, gamma, 0.0, alpha) print(f"direct Q(S1): {q_s1:.2f}") # -> direct Q(S1): 5.00
q_s0 = q_update(q_s0, 0.0, gamma, q_s1, alpha) q_s1 = q_update(q_s1, 10.0, gamma, 0.0, alpha) q_s0 = q_update(q_s0, 0.0, gamma, q_s1, alpha) q_s1 = q_update(q_s1, 10.0, gamma, 0.0, alpha) q_s0 = q_update(q_s0, 0.0, gamma, q_s1, alpha)
print(f"planned Q(S1): {q_s1:.2f}") # -> planned Q(S1): 8.75 print(f"planned Q(S0): {q_s0:.4f}") # -> planned Q(S0): 6.1875 print(f"plain Q(S0) stays: 0.0000") # -> plain Q(S0) stays: 0.0000
if __name__ == "__main__": main()Watch Out For
Planning on states you never visited
Uniform sampling over the full grid feels thorough, but the model has no entry for unseen pairs and returns a default. Planning then teaches Q from fiction, often zeros that wipe real values.
Sample only visited states and tried actions, keep modest early (5 to 20 works), and reach for the Dyna-Q+ bonus when the world may have changed. Fiction in, fiction out.
The Quick Version
- Dyna stores each real transition in a model and replays simulated transitions per real step.
- Real and simulated updates use the same Q rule; is plain Q-learning.
- Sampling stays on visited pairs, since unseen pairs have no model entry worth trusting.
- Dyna-Q+ adds in simulation so stale moves get rechecked in the world.
- In the worked corridor, one episode with reaches while plain learning sits at .