Q-Learning

अब हम Reinforcement Learning की सबसे प्रसिद्ध और foundational algorithm को समझेंगे —
🧠 Q-Learning

यह एक model-free reinforcement learning technique है, जिसे किसी भी environment में optimal decision-making के लिए use किया जाता है — बिना उसके अंदर के dynamics को जाने।


🔶 1. Q-Learning क्या है?

Q-Learning एक off-policy, model-free RL algorithm है जो agent को यह सीखने में मदद करता है कि किसी state में कौन-सा action लेने से long-term reward ज्यादा मिलेगा।

🎯 “Q-Learning finds the best action for each state — without needing to model the environment.”


📊 2. Key Idea: Learn Q-Value

📌 Q(s, a):

  • Q-value या Action-Value Function बताता है: “अगर agent state sss में है और action aaa लेता है, तो उसे future में कितना total reward मिल सकता है।”

Q(s,a)=Expected future reward


🧠 3. Bellman Equation for Q-Learning

Q-values को update करने के लिए हम use करते हैं Bellman update rule:

SymbolMeaning
Q(s,a)Q-value for state-action pair
αLearning rate (0 to 1)
γDiscount factor (importance of future reward)
rtImmediate reward
max⁡a′Q(s′,a′)Best future Q-value from next state

🔁 4. Q-Learning Algorithm Steps

Initialize Q(s, a) arbitrarily (e.g., all 0s)
Repeat for each episode:
Start at initial state s
Repeat until terminal state:
Choose action a using ε-greedy policy from Q(s, a)
Take action a → observe reward r and next state s'
Update Q(s, a) using Bellman equation
Move to new state s ← s'

🔧 5. Example: Gridworld (Maze)

Imagine a 5×5 maze:

  • Agent starts at top-left
  • Goal is bottom-right
  • Agent learns which path gives maximum reward (shortest way)
Q[state][action] += alpha * (reward + gamma * max(Q[next_state]) - Q[state][action])

📈 6. Exploration vs Exploitation

  • Exploration: Try new actions to discover better rewards
  • Exploitation: Use known actions with best Q-values

👉 Use ε-greedy strategy:

  • With probability ε → random action
  • With probability (1–ε) → best action

📦 7. Summary Table

TermDescription
Q(s, a)Expected total reward for action aaa in state sss
αLearning rate – कितनी तेज़ी से सीखना है
γFuture rewards की importance
εRandomness (exploration)
Bellman UpdateQ-values को improve करने का तरीका

📝 Practice Questions:

  1. Q-learning को model-free क्यों कहा जाता है?
  2. Q-value क्या होता है?
  3. Bellman equation का role क्या है?
  4. ε-greedy strategy क्यों उपयोग होती है?
  5. Q-Learning और SARSA में क्या फर्क है?