Q-Learning
Q-Learning is an off-policy reinforcement learning algorithm that learns the optimal action-value function independently of the agent's actual behavior. The "Q" stands for quality, representing how good each action is in each state. Unlike SARSA, Q-Learning is off-policy because it learns about the greedy (optimal) policy while potentially following a different exploratory policy. This separation allows it to learn the best possible strategy even while taking random exploratory actions during training.
How Q-Learning Works
The algorithm maintains a Q-table storing value estimates for every state-action pair. When the agent takes an action and observes the result, it updates the Q-value using the formula:
Q(s,a) ← Q(s,a) + α[r + γ·max Q(s',a') - Q(s,a)],
where α is the learning rate, γ is the discount factor, and critically, max Q(s',a') represents the value of the best possible next action regardless of what action the agent will actually take. This "max" operation makes Q-Learning optimistic—it always assumes the agent will make perfect decisions in the future, allowing it to learn the optimal policy even when following a suboptimal exploratory policy like ε-greedy.
When to Use Q-Learning
Q-Learning is ideal for environments where exploration is safe and you want to discover the truly optimal policy. It's widely used in simulations, games, and grid-world problems where the agent can afford to make mistakes during learning without catastrophic consequences. Its off-policy nature makes it sample-efficient and faster to converge to optimal behavior compared to on-policy methods. However, this optimistic assumption can be dangerous in real-world applications with high-cost failure states, as Q-Learning may learn risky strategies that depend on perfect execution and don't account for the possibility of exploratory mistakes.