Skip to main content
Open In Colab The navigation MDP section wrote TurtleBot3 navigation as an MDP with 28 continuous state variables and a continuous action. It then solved a finite version by value iteration, which needed the robot’s true pose, a fixed goal and a table of transition probabilities built from the simulator. Q-learning needs no such table: it learns from the transitions the robot experiences. Q-learning in its tabular form needs a finite set of states and a finite set of actions, so that q(s,a)q(s, a) fits in a table. In this section you make the problem finite, train a tabular agent on it, and see where the coarse state stops being good enough. The simulator, reward and episode rules are the ones from that section.

A finite version of the problem

The action set keeps five commands. Each one turns at a fixed rate, and the sharper turns drive more slowly: The state keeps two pieces of the 28 numbers, each cut into a few bins:
  • The goal bearing βt\beta_t falls into one of 8 sectors of 45 degrees, centered on straight ahead, left, behind, right and the four diagonals.
  • The lidar is reduced to three sectors: front (±45\pm 45 degrees), left and right (45 to 135 degrees to each side). Each sector reports its shortest range as near (under 0.45 m), middle (0.45 to 0.9 m) or far.
That gives 8×33=2168 \times 3^3 = 216 states and a table of 216×5=1080216 \times 5 = 1080 action values. The goal distance, the beams behind the robot and the previous command are dropped.
Output from cell 5 The three lidar sectors around the robot. The darker inner band is “near” (under 0.45 m) and the lighter band is “middle” (under 0.9 m); beyond it a sector reads “far”. Only the shortest beam in each sector counts, and the beams behind the robot are ignored.

The Q-learning update

Q-learning keeps a table Q(s,a)Q(s, a) and moves one entry after every step toward a target built from the reward and the best action value in the next state: Q(St,At)←Q(St,At)+α[Rt+1+γmax⁡aQ(St+1,a)−Q(St,At)].Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \alpha \big[ R_{t+1} + \gamma \max_{a} Q(S_{t+1}, a) - Q(S_t, A_t) \big]. This is the update of TD prediction written for action values. The term in brackets is the TD error, δt=Rt+1+γmax⁡aQ(St+1,a)−Q(St,At),\delta_t = R_{t+1} + \gamma \max_{a} Q(S_{t+1}, a) - Q(S_t, A_t), the gap between the TD target and the current estimate. The update moves Q(St,At)Q(S_t, A_t) a fraction α\alpha of the way across that gap. The Monte Carlo estimate of the navigation MDP section did something else: it waited until an episode ended and averaged the actual returns GtG_t. TD bootstraps instead. It updates after every 0.2 s step, using the table’s own estimate of the next state in place of the rest of the return. An episode here lasts up to 200 steps, so TD makes up to 200 updates in the time Monte Carlo waits for one return. The comparison of TD and Monte Carlo shows the trade: the TD target has lower variance, and it is biased while the table is still wrong. Compare the target with the value-iteration backup of the navigation MDP section, written for action values: qk+1(s,a)=Rsa+γ∑s′Pss′amax⁡a′qk(s′,a′).q_{k+1}(s, a) = \mathcal R^a_s + \gamma \sum_{s'} \mathcal P^a_{ss'} \max_{a'} q_k(s', a'). Value iteration takes the expected reward and the sum over next states from the model. Q-learning replaces both with the one reward and the one next state that the robot actually experienced. One sample is a noisy estimate of the expectation. The step size α\alpha averages many of them, so over repeated visits Q(s,a)Q(s, a) moves toward the mean of the targets, which is the backup applied to the current table. That is why Q-learning needs no model: the robot’s own transitions are samples from Pss′a\mathcal P^a_{ss'}. After a goal or a collision the next state has no future, so the target is Rt+1R_{t+1} alone. After a timeout the robot still has a future, so the update bootstraps as usual. The agent explores with an ϵ\epsilon-greedy policy: with probability ϵ\epsilon it picks a random action, and otherwise the action with the largest Q(St,a)Q(S_t, a). Here ϵ\epsilon falls linearly from 1 to 0.05 over the first 60% of training, α=0.1\alpha = 0.1 and γ=0.99\gamma = 0.99. The update uses the greedy action in the target while the robot follows the ϵ\epsilon-greedy one, which makes Q-learning off-policy. SARSA would use the action the robot actually takes next: Q(St,At)←Q(St,At)+α[Rt+1+γQ(St+1,At+1)−Q(St,At)].Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \alpha \big[ R_{t+1} + \gamma Q(S_{t+1}, A_{t+1}) - Q(S_t, A_t) \big]. SARSA learns the value of the ϵ\epsilon-greedy policy itself. That value includes the random actions, some of which turn the robot into an obstacle. Q-learning learns the value of the greedy policy, whatever the exploration does. The Q-learning section shows the consequence on the cliff-walking gridworld: SARSA learns a safer path that stays away from the cliff, and Q-learning learns the shortest one. In a finite MDP, the table converges to q∗q_* if every state-action pair keeps being visited and the step size shrinks over time. Neither holds exactly here: α\alpha stays at 0.1, and the end of this section shows that the discrete state is not quite Markov. The ϵ\epsilon floor of 0.05 keeps every action in use.
Output from cell 7 Outcomes and discounted returns while the agent trains, averaged over windows of 200 episodes. The dotted line marks where ϵ\epsilon reaches its floor of 0.05. At the start the robot acts at random and collides in most episodes. As ϵ\epsilon falls, the agent acts more on its table and the goal rate climbs, to about 0.8 when ϵ\epsilon reaches its floor. It does not stay there. The goal rate falls back to about 0.7 for roughly 1,000 episodes, dips to 0.6 near episode 3,500, and ends near 0.85. To compare with the go-to-goal controller from the previous section, run the greedy policy, with no exploration, on the same 300 test episodes:
Output from cell 9 The four starts of the navigation MDP section, driven by the go-to-goal controller (left) and by the greedy Q-learning policy (right). The learned policy uses the lidar. When the front sector reads near, it turns away and drives around the obstacle, then turns back toward the goal once the goal bearing changes bin. That is the behavior the go-to-goal controller lacked, and it raises the success rate well above it. It is still not perfect. From the bottom left start it drives around the large obstacle and then collides with the small one at (3.0, 3.0), short of the goal. The next part explains why. The value-iteration policy of the navigation MDP section reached the goal in every test episode. The two results answer different problems. That policy was computed for one goal, from the true pose and a model of the dynamics. The Q-table works for any goal, from what the robot’s own sensors measure, and it never saw a model. The price is the state: 216 coarse bins in place of 16,229 pose cells.

Where the table falls short

Two things limit the tabular agent: how often it sees each state, and how much the state hides. Output from cell 10
Number of training steps spent in each discrete state, sorted, on a log scale. The visits are very uneven. The most common state, “goal ahead, middle range in all three sectors”, took more than 10,000 steps, while states such as “goal behind, obstacles near on both sides” occurred a few times or never. A rarely visited state has an action value estimated from few samples, and the policy there is close to a guess. Finer bins would make this worse: every extra bin multiplies the number of states, and each state is visited less. The larger problem is aliasing. Many physical situations share one discrete state, and they can need different actions. An obstacle at 0.5 m and one at 0.85 m both read “middle”. A goal 5 degrees to the left and one 40 degrees to the left fall in the same bearing bin. A pole just behind the left sector boundary is invisible to it. The table stores one value per state and action, so it learns the average return over all the situations that look alike, and in each of them the chosen action is right only on average. In MDP terms, the discrete state is not Markov: the next discrete state depends on where inside the bin the robot really is, and the table cannot see that. The pose grid of the navigation MDP section was close to Markov because it kept the position to within 11 cm and the heading to within 22.5 degrees. This state keeps neither. You cannot fix this with more bins. Ten range bins for each of the 24 beams would give 102410^{24} states, and nearly all of them would never be visited. The way out is to stop using a table: approximate q(s,a)q(s, a), or the policy itself, with a function of the full 28 numbers, so that nearby states share what is learned. Deep Q-networks do this for discrete actions. The next section goes one step further and also drops the five-command action set, learning continuous (v,ω)(v, \omega) with DDPG.
Found an error on this page? Report an issue.