Code
Code
The robot and its room
The room is 4 m on a side, with four round obstacles at fixed places. Each episode puts the robot and the goal at random free positions at least 1.5 m apart, with a random heading. The robot is a TurtleBot3 Waffle, modeled as a disk of radius 0.2 m. It accepts a forward speed of at most 0.26 m/s and a turn rate of at most 1.82 rad/s, the limits the TurtleBot kinematics assignment uses. Its lidar returns 24 ranges, one every 15 degrees around the robot, each clipped to the sensor’s 0.12 to 3.5 m span. The code below builds this simulator. The next two sections use the same code.Code
Code
State
The agent never sees the pose . Its state is 28 numbers, each scaled to roughly , the same layout as the turtlebot3_drlnav state:- the 24 lidar ranges,
- the distance to the goal and its bearing relative to the heading,
- the previous command .
Actions
An action is a forward speed and a turn rate, held for one control period of s. The action set is the rectangle . The Waffle can reverse, but the task does not need it, and leaving it out keeps the action space smaller. This action space is continuous. Tabular methods need a finite set, so the Q-learning navigation section replaces it with five steering commands, and the DDPG section keeps the full rectangle.Transition model
Given the pose and the action, the robot moves by the unicycle model with a little noise on each command: where and , with . The new state then follows by ray casting from the new pose. Together these define the transition density . It is the continuous counterpart of the MDP chapter’s transition probabilities Nobody writes this density down. The simulator can only sample from it, and on the real robot even the simulator is gone. Reinforcement learning is built for that setting, because the agent learns from sampled transitions. The dynamic programming algorithms of the MDP chapter need as a table. Later in this section you build such a table for a gridded version of the room.Reward
The reward after each step is The reward depends on the pose before and after the step, so it is random given and . The Bellman equations below use its mean, the expected reward . Each term has a job. The goal bonus and the collision penalty define the task. If they were the only terms, the agent would see a nonzero reward only at the end of an episode, and a random policy would almost never reach the goal to learn from. The progress term gives a signal on every step: driving the full 5.2 cm of one step straight at the goal earns about 0.1. Over an episode these terms add up to , so they reward getting closer without preferring any particular path. Its weight is kept small so that the goal bonus, not the distance covered, decides which starts are good. The turn penalty discourages a policy that wobbles left and right, which a real robot’s motors and odometry handle poorly. The time penalty of 0.01 per step makes waiting in place slightly worse than moving. These weights are design choices. The turtlebot3_drlnav project uses the same structure with different numbers: a goal bonus of 2500, a collision penalty of 2000, and dense penalties on the heading error and on driving slower than full speed. The TD3 section shows what happens to the learned behavior when the collision penalty is ten times larger.Episodes, termination and discount
An episode ends in one of three ways. Reaching within 0.25 m of the goal and colliding are terminal. In the chapter’s terms they lead to absorbing states that pay no further reward, so their value is zero and every Bellman equation below stops there. Running out of time after 200 steps (40 s) is different. It is a truncation imposed for convenience, and the robot’s state at that moment still has a future. The learning algorithms must treat the two cases differently: they bootstrap from the next state’s value after a truncation and not after a terminal event. The return from step is the discounted sum of the rewards that follow: The second form, one reward plus the discounted return from the next step, is what the Bellman equations are built on. The discount factor is . A reward steps ahead is weighted by , and these weights sum to steps, or 20 s. A typical trip of 2 to 3 m takes 8 to 12 s at full speed, so the goal bonus is still visible from the start of most episodes.A hand-written policy
Before learning anything, try a simple controller: turn toward the goal, and drive forward only when roughly facing it. It is a deterministic policy: names one action, where a stochastic policy would give a distribution . It ignores the lidar entirely. The figure runs it from four start poses.Code
Code
Code
Code
The value of a policy
The state-value function says how good it is to start in and follow . Substituting gives the Bellman expectation equation. For a deterministic policy the sum over actions has one term: The value of a state is the expected reward of the policy’s action plus the discounted value of where that action leads. Policy evaluation solves this equation, either as a linear system or by applying it repeatedly as an update. Both need the sum over . Here ranges over a continuous 28-dimensional set whose density nobody can write, so neither is possible. Monte Carlo estimation avoids the sum. It works from the definition , as the Monte Carlo prediction section does: start the robot at a point, run the policy, record the discounted return, and average over a few runs. The map below does this for a grid of start positions and a fixed goal, with the robot always facing the goal at the start. The full state has 28 dimensions, so this map is a two-dimensional slice of : it varies the start position and fixes everything else.Code
Code
A finite model of the room
Dynamic programming needs three things this MDP does not have: a finite state set, a finite action set and the transition probabilities as a table. You can build all three if you give up the lidar state and use the robot’s true pose instead. The pose is a Markov state for this simulator. The room is fixed, the goal is fixed, and the unicycle model computes the next pose from the current pose and the action alone. The 28 lidar numbers are a function of the pose, so the pose carries everything they carry and more. Fix the goal at and cut the pose into cells:- the position into a grid of squares, each 11 cm on a side,
- the heading into 16 bins of 22.5 degrees.
Code
Code
Policy evaluation
Iterative policy evaluation turns the Bellman expectation equation into an update and applies it to every state at once: Each sweep is one backup of the whole value table. In the code, the sum over is the mean of over the 24 sampled successors of , which is exactly the sum weighted by the estimated probabilities. The terminal states keep the value 0. The update stops when no state changes by more than . To evaluate the go-to-goal controller, its own action at each of the 24 sampled poses is used. That gives the table for this policy directly, without rounding its action to one of the five commands.Code
Code
Policy improvement and policy iteration
Given , the action value of taking in and following afterward is Policy improvement makes the policy greedy with respect to it, . The policy improvement theorem guarantees in every state. In the shadows, tells the robot that turning away from the obstacle now is worth more than driving at the goal, because go-to-goal recovers from a detour but not from a collision. Policy iteration alternates the two steps: evaluate the current policy, make it greedy, and repeat until the greedy policy stops changing. Here it starts from go-to-goal. Each policy is also run in the continuous simulator on 300 episodes with random starts and the fixed goal. The robot looks up the cell that contains its true pose and applies that cell’s action. The guarantee does not cover the first step. Go-to-goal sends continuous commands, and the greedy step chooses only among the five discrete commands, so go-to-goal’s own action is not one of the candidates. The proof of the theorem needs the current action to stay available. Without it, the first greedy policy can lower the value of some states even when the average value rises. From the second step on, every policy uses the five commands, and the theorem applies to each step.Code
Code
Value iteration
Value iteration reaches the same answer without keeping a policy. It applies the Bellman optimality backup to every state: This is one evaluation sweep with the greedy step folded into it. Starting from , the values converge to , and the greedy policy is optimal.Code
Code
What dynamic programming needed
Dynamic programming solved this navigation problem completely, but only after three changes to it. First, it needed a model. The table took 16,229 × 5 × 24, almost 2 million, simulated steps, each started from a pose chosen by the program. A real robot cannot be placed at an arbitrary pose a million times. It can only follow its own trajectory. Second, it needed a finite state set. The pose has three numbers, and cutting them into cells already gave 20,736 states. The lidar state has 28 numbers, and no grid over them would fit in memory. Third, it needed the true pose and a fixed goal. The table policy is tied to this room and this goal. A new goal means a new table and a new solution, and on a real robot the pose comes from a localization system with errors. The sections of the TurtleBot navigation chapter each remove one of these assumptions. Q-learning drops the model: its update is a sample of the value-iteration backup, taken along the trajectories the robot actually drives. It also works from a coarse lidar and goal-bearing state, which serves any goal in any room but is no longer Markov. DDPG and TD3 drop the finite state and action sets. They replace the tables with neural networks over all 28 numbers and the continuous .Found an error on this page? Report an issue.

