Reinforcement Learning Overview


Reinforcement Learning Overview

Anna
Alright, Michael, let's talk about a whole new way of teaching machines: reinforcement learning. You've probably heard of supervised learning where you give the algorithm labeled data, and unsupervised learning where it finds patterns on its own. Reinforcement learning is different—it's inspired by how animals learn through trial and error.

Michael
Right, like that classic psychology experiment with a mouse pressing a lever for food. The mouse gets a reward, so it learns to press the lever more often. That's exactly the idea: the agent learns from sparse reward signals, not from a labeled dataset.

Anna
Exactly. And to make it concrete, let me introduce a simple grid world. Imagine a 4x4 board. The agent starts in the bottom-left corner and needs to reach the top-right corner to get a reward of +100. But there's a trap—one square that gives -100 and ends the episode. So the agent has to learn which moves lead to the good outcome.

Michael
Got it. And this environment is episodic, discrete, fully observable, and initially deterministic. The agent always knows exactly where it is, and when it moves up, it actually goes up. There's no randomness. So how does the agent start learning?

Anna
We use an algorithm called Q-learning. The idea is to assign a Q-value to each state-action pair. Initially, we set all Q-values to zero. The agent then explores randomly. When it takes an action and receives a reward, we update the Q-value for the previous state-action pair. For a terminal state like the reward or trap, we just copy the reward. But for non-terminal states, we update using the maximum Q-value of the next state.

Michael
So if the agent lands on the trap and gets -100, we set the Q-value for the action that led to that state to -100. Then next time, if it's in the same state, it'll avoid that action because -100 is less than zero. That's how it learns to avoid the trap.

Anna
Exactly! And the same thing happens with the reward: when it finally reaches +100, it copies that value back to the previous state. Over many episodes, the Q-values propagate backward, forming a path to the goal. The agent effectively learns a policy—a mapping from states to the best action.

Michael
But here's a problem. If the agent always picks the highest Q-value, it might never discover a better path. For instance, if it finds the top-right reward first, it will always go that way and never explore the bottom-right corner, which might also have a reward.

Anna
That's the exploration versus exploitation dilemma. The solution is something called epsilon-greedy. With a small probability, say 10%, the agent picks a random action instead of the best one. That way it continues to explore. It's like occasionally trying a new restaurant even though you have your favorites—you might discover something even better.

Michael
Okay, that handles exploration. But there's another issue. In the grid world, if the agent learns all possible paths, it might think all paths are equally good, even inefficient ones. How does it learn to prefer the shortest path?

Anna
We introduce a discount factor, gamma. It's a number between 0 and 1, like 0.9. When we update a Q-value, we multiply the future expected reward by gamma. So a reward that's farther away is worth less. That way, the agent prefers actions that lead to rewards sooner. After training, you'll see higher Q-values along the shortest path.

Michael
Makes sense. And what about when the environment is not deterministic? For example, sometimes the agent might slip and end up in a different square than intended. That would break the learning because a good action could randomly lead to a bad outcome.

Anna
Right, that's when we need a learning rate, alpha. Instead of overwriting the Q-value completely, we move it gradually toward the target. For example, if alpha is 0.2, the new Q-value is 20% of the way from the old value to the target. So if a good action sometimes leads to a trap, the Q-value will settle at an intermediate value that reflects the probability, not an extreme.

Michael
So the formula becomes: new Q = old Q + alpha * (reward + gamma * max Q(next state) - old Q). That's the full Bellman update. And we need to be careful with terminal states: they have no future, so the max Q(next state) part is zero.

Anna
Precisely. And there's another trick: optimistic initialization. If you start all Q-values at a high number, like 100, the agent will be curious and try actions it hasn't tried before, because it thinks they might be good. That naturally encourages exploration without needing epsilon-greedy, though you have to handle terminal states correctly—otherwise the agent thinks there's an afterlife.

Michael
That's clever. So, in the code, you can set the initial Q-values high, and then in the update, if the next state is terminal, you just use the immediate reward without the future term. That prevents the agent from expecting more reward after the episode ends.

Anna
Exactly. And that's pretty much the core of Q-learning. You can experiment with different grid sizes, add more rewards, or make the environment stochastic. The algorithm handles it all. And from here, you can move on to deep reinforcement learning for continuous state spaces.

Michael
So to sum up: reinforcement learning is about learning from rewards, Q-learning is a table-based method for discrete problems, and key parameters are epsilon for exploration, gamma for discounting future rewards, and alpha for learning rate. With these, an agent can learn optimal behavior even in tricky environments.

Anna
Well said! And for anyone interested, there are other algorithms like SARSA and Monte Carlo, but Q-learning is a great starting point. Ready to implement it and see it in action?

Michael
Absolutely. Let's get coding!

Ask anything
We've read this whole study set, so questions about it get the best answers.