Reinforcement Learning
Let's get straight to the point.
Q-learning¶
I suspect it's the concise method used in simple cases like discrete states and actions maybe.
I'm not going to talk about the RL so formally like defining the agent, reward, state ... so cleanly because they are all intuitive.
Let's directly get into a scenario:
Here's a "maze", where S is the starting point while G stands for "Goal", at which the game will finalize.
Let's consider the sequence of points to be an array with index from 0 to 8, which then represent the number of each state in this world.
So the starting state is 0 and goal state is 8. We want the agent to learn how to find the G from S after exploring for some time.
Q table stores the reward the agent thinks under specific state and doing actions, as far as it learnt so far.
So now the Q table will be a two-dimension matrix, where dim 0 is for different actions, dim 1 is for states, a.k.a Q[state][action]. The size will be \(num_{state} \times num_{action}\)
Initially, the Q-table will be empty, filled by all 0.
We give an environment for our agent kept trying:
-
For every trial, it will be \(\epsilon\) probability for the agent to randomly choose an action; And \(1 - \epsilon\) probability to choose the action with highest value in Q table.
-
Make a step. And the world state changed. The agent will get some reward.
-
Update the Q table based on this action.
The formula is:
\[ Q[state][action] += \alpha \times (reward + \gamma \times max(Q[next\_state]) - Q[state][action]) \]Here \(\alpha\) is the learning rate, each time we only add some portion of update.
\(\gamma\) is the discount rate, we encourage a sooner reward is it's less than 1.
\(reward\) is simply the achievement from this step.
-
Move the state to the next and continue doing above steps.
Code is below: