Curvingo · Made in India

Glossary

Select one of the keywords on the left…

Reinforcement LearningLine World

Reading time: ~15 min

After the mela, Sina walks home along a narrow lane. At one end is a muddy puddle; at the other end, the sweet shop. She starts in the middle and takes one step left or right at a time.

Now Sina's situation changes as she moves, so where she stands is her state. At the stalls every plate paid out at once; here the reward only comes at the end — +10 for reaching the sweet shop, −10 for splashing into the puddle — and every step on the way gives 0. One trip from the start until she reaches either end is an episode.

In the lane, Sina's state is , and her actions are .

Now or Later?

A reward several steps away is worth a little less than one right now — Sina might never get there, and a jalebi today beats a jalebi next week. RL captures this with the discount factor \gamma (gamma), a number from 0 to 1. A reward that arrives on step k is multiplied by \gamma^{k-1}, and the total is called the return:

G = r_1 + \gamma\, r_2 + \gamma^2 r_3 + \cdots

From cell 3, the sweet shop is three steps away, and the rewards on the way are 0, 0 and 10. With γ = 0.9 the return is 0 + 0.9 \times 0 + 0.9^2 \times 10 = .

So γ decides how patient Sina is. At the mela gate she can step left to a samosa stall (+4 at once) or walk four steps right to the sweet shop (+10, on step 4). The bars show what +10 is worth if it arrives on step 1, 2, 3 … — Sina picks the sweet shop only if the step-4 bar is taller than the samosa line. Complete both missions.

Discount factor γ = ${g}

The Q-Table

How can Sina learn which way to step when the reward only comes at the very end? She keeps a table of numbers called Q-values: Q(s, a) is her estimate of the return if she stands in cell s, takes action a, and plays well afterwards. All Q-values start at 0. In each cell, the arrow points to the action with the bigger Q-value — together, the arrows are her policy. While both values in a cell are equal, there is no arrow. The purple fill shows each cell's bigger Q-value: how much the cell is worth.

Sina explores with ε = 0.3 and learns with an update rule you will see next. Complete both missions.

After the first episode only one number changed: the move that actually reached the sweet shop or the puddle. On later trips, a cell next to a valuable cell becomes valuable too. The reward spreads backwards along the lane, one cell at a time, until every cell knows the way.

The Q Update

After every single step, Sina nudges the Q-value of the move she just made:

\begin{aligned} \text{new } Q &= \text{old } Q + \alpha \times (\text{target} - \text{old } Q) \\ \text{target} &= r + \gamma \times \text{best } Q \text{ in the next cell} \end{aligned}

It has the same shape as the jalebi estimate: old value + step × (target − old value). Here r is the reward she just got, and the target adds the discounted value of the best move from the cell she landed in. The learning rate \alpha sets the size of the step. Sina uses α = 0.5 and γ = 0.9.

Sina is in cell 5 and steps right into the sweet shop: r = 10, and the episode ends there, so the next cell's value counts as 0. Her new value is Q(5, →) = 0 + 0.5 × (10 + 0.9 × 0 − 0) = .

On a later trip she steps right from cell 4 into cell 5: now r = 0, and the best Q-value in cell 5 is 5. So Q(4, →) = 0 + 0.5 × (0 + 0.9 × 5 − 0) = .

Note

Repeat this often enough and Q(5, →) settles at 10, Q(4, →) at 0.9 × 10 = 9, Q(3, →) at 0.9 × 9 = 8.1, and so on. Each value becomes the return of the quickest walk to the sweet shop — the same 8.1 you worked out for cell 3.

Speed Round: What Is It Worth Now?

Get five right to move on.

To reveal more content, you have to complete all the activities and exercises above. 
Are you stuck? or reveal all steps

Next up:
Grid World
Sina