Learn AI.
Build AI Systems · SOURCE LESSON 10

Learn from delayed rewards.

Teach an agent a route, inspect a Q-learning update, and uncover a reward rule that rewards the wrong behavior.

By the end: Explain discounted return and delayed credit, follow an action-value update, and distinguish reward from task success.

01 · TRY

Start with something concrete.

Imagine giving a robot points for cleaning a room. If it earns points for every piece of dirt picked up, could it improve its score by spilling dirt first? Before training an agent, decide what success means and whether the reward actually measures it.

Try the interactive model ↓
02 · PREDICT
Before you experiment

An agent earns +2 each time it re-enters a bonus square. Its real task is to reach a distant goal. What might learning produce?

An agent earns +2 each time it re-enters a bonus square. Its real task is to reach a distant goal. What might learning produce?
03 · EXPERIMENT

Change one thing. Watch what follows.

  1. Keep the default settings and train 100 episodes three times. Trace the frozen greedy policy. Does it reach G?
  2. Inspect a single update: reward, discounted next-state estimate, target, and new Q. Select squares to see how delayed reward travels through the table.
  3. Set γ to 0 and train again. Explain why a one-step learner cannot value a distant goal before it is next to that goal.
  4. Choose “Goal + repeatable bonus”, keeping ε = 0.20, α = 0.3 and γ = 0.90. Train 300 episodes and trace the policy. Compare reward with the independent goal check.

Loading this browser experiment…

04 · UNDERSTAND

Good actions may pay off later

A useful first move in a maze may receive no positive reward. Credit assignment asks which earlier choices contributed to the eventual outcome. Looking only at immediate rewards can miss the value of a sequence.

The return combines rewards over time. A discount γ between zero and one reduces the contribution of more distant rewards:

Gt=rt+1+γrt+2+γ2rt+3+⋯G_t=r_{t+1}+\gamma r_{t+2}+\gamma^2r_{t+3}+\cdots

For rewards 0, 0, 1 with γ = 0.9, the return is 0 + 0.9×0 + 0.9²×1 = 0.81. With γ = 0, only the next reward matters. Discounting is part of the chosen objective; it is not the same thing as the learning rate α, which controls the size of an update.

Let later value teach an earlier choice

Our Q-learning table stores four action values for every grid square. After an action, we observe its reward and next state. For a nonterminal next state, the target combines that reward with the largest estimated next-state value:

target=r+γmax⁡a′Q(s′,a′)\text{target}=r+\gamma\max_{a'}Q(s',a') Q(s,a)←Q(s,a)+α [target−Q(s,a)]Q(s,a)\leftarrow Q(s,a)+\alpha\,[\text{target}-Q(s,a)]

At the terminal goal, the target is just the reward: there are no later actions. Elsewhere the target uses an estimate. This is called bootstrapping. It can spread information about a distant goal backward through visited transitions over many updates; one successful episode does not instantly teach every route.

The exploratory behavior collects experience while the update’s maximum looks toward greedy next actions. Those are different roles. Changing γ changes the objective; changing ε changes data collection. Sufficient exploration and suitable learning-rate conditions matter for theoretical convergence. This bounded demonstration does not claim that a fixed number of episodes or a fixed learning rate always converges.

The 40-step cap is a practical reset for the demo. It truncates experience rather than representing a natural terminal grid state, so the last update still includes the next-state estimate. The displayed episode score is an undiscounted sum; it is not the discounted Q target.

A high score can be the wrong success

With the intended rule, ordinary moves cost 0.1 and reaching the goal earns 5. With the flawed rule, repeatedly entering the bonus square earns extra reward. A learned loop can then be attractive even though it never reaches G. This is reward hacking: exploiting the measurement in a way that fails the intended task.

The source’s cleaning robot illustrates the same problem. Rewarding dirt pickup can encourage creating more dirt if the environment allows it. Possible changes include measuring the room’s final cleanliness, preventing repeat credit for the same dirt, and testing unintended strategies. Every fix needs evaluation; changing one reward can introduce another loophole.

The grid’s independent check asks whether the frozen policy reaches G. It stops when a state repeats because a deterministic policy in this deterministic grid would repeat the same cycle. The displayed prefix reward therefore does not represent the cycle’s infinite return. Training goal counts also differ from greedy-policy performance because exploration can accidentally reach the goal.

Apply it to a card puzzle

The source asks you to define a reinforcement-learning task for FreeCell. Before opening the example, describe the state, legal actions, terminal reward, and credit-assignment difficulty.

Compare your task definition with an example

A state records the ordered cards in tableau columns, free cells, and foundations. An action is a legal card move according to the game’s rules; legality belongs to the environment, not to the policy’s guess. A terminal win can give +1, with 0 for an unsuccessful stopped attempt. You must specify how attempts stop and whether step costs or discounting are part of the objective.

A useful early move may produce no immediate reward and only enable a win many moves later. That makes delayed credit and exploration difficult. Rewarding moves to foundations might help but can change behavior, so compare eventual solve rates on reserved deals. The small grid table does not establish that the agent can solve FreeCell.

05 · APPLY
Check your understanding

A nonterminal move gives reward −0.1. γ = 0.9, the largest next-state Q is 2, old Q is 1, and α = 0.3. What is the updated Q?

A nonterminal move gives reward −0.1. γ = 0.9, the largest next-state Q is 2, old Q is 1, and α = 0.3. What is the updated Q?
06 · CONTINUE

Take this idea with you.

Delayed rewards can influence earlier actions through value updates. A high score is evidence about the reward rule; an independent task check tells you whether the intended goal was achieved.

My work
Learn AI · Understand by doing.Loading local progress…Copyright (c) 2026 Mobi Weave, Inc. All rights reserved