Robot Learning, step 3
Learning from trial and error
Forget P and R: learn action values from the arm's own moves with Q-learning and ε-greedy exploration, and watch the success rate climb.
Without the model
Value iteration read and . A real robot doesn't have them: it can only try a move and see what happens. env = rl.grid_env() is the same grid as an environment. env.reset() puts the arm in a random free cell and returns its state; env.step(a) makes one move and returns (s2, r, done), the new state, the reward and whether the episode is over. It is over at the goal, or after 200 moves, and then env.truncated is True too.
Q-learning
Q-learning learns the action values from those moves. After each move from with action , reward and new state , it nudges towards the TD target (TD for temporal difference):
That's step 2's backup with the sum over replaced by the one that happened; the learning rate averages over many tries. At the goal nothing follows, so . A cut-off episode is different: the arm only ran out of time, and still has a future, so keep the term.
Exploring
Always taking the best-looking action, , repeats whatever worked first and never finds anything better. ε-greedy exploration takes a random action with probability and the greedy one otherwise.
Your task
- Write
epsilon_greedy(Q, s, eps), usingnp.random(the program seeds it). - Write
td_update(Q, s, a, r, s2, terminal, alpha, gamma), changingQ[s, a]in place. - Write
run_episode(env, Q, alpha, gamma, eps): reset, then choose, step and update until done, and return the total reward. Passterminal = done and not env.truncated.
The program trains for 8,000 episodes (, , ). The Plots tab shows how often a greedy arm reaches the goal as training goes on, and the Images tab what has been learned so far (dark cells: nothing yet). Acting greedily, the arm must reach the goal from 45 of 50 random starts at least 90 % of the time, then drive there from its own start.