Study interactive :: Progress tools open in the Study Hub reader.

Reinforcement Learning Basics

Introduction to Reinforcement Learning (RL), a type of machine learning where agents learn to make decisions by interacting with an environment.

Table of Contents


Introduction

Reinforcement Learning (RL) is a type of machine learning where an agent learns to make decisions by interacting with an environment. The agent receives rewards or penalties for its actions and learns to maximize cumulative rewards over time.

Key Characteristics

Example: Game Playing


Key Concepts

1. Markov Decision Process (MDP)

Mathematical framework for RL:

2. Policy (π)

Strategy for choosing actions:

3. Value Functions

State Value Function V(s): Expected cumulative reward from state s

Action Value Function Q(s,a): Expected cumulative reward from taking action a in state s

4. Exploration vs Exploitation


RL vs Other ML Types

Supervised Learning

Unsupervised Learning

Reinforcement Learning


Basic Algorithms

1. Q-Learning

Value-based algorithm that learns action values.

Key Idea: Learn Q(s,a) - value of taking action 'a' in state 's'

Update Rule:

Q(s,a) ← Q(s,a) + α[r + γ max Q(s',a') - Q(s,a)]

Where:

Example Implementation:

import numpy as np
import random

class QLearning:
    def __init__(self, states, actions, learning_rate=0.1, 
                 discount=0.95, epsilon=0.1):
        self.states = states
        self.acti>
        self.lr = learning_rate
        self.gamma = discount
        self.epsilon = epsilon
        self.q_table = np.zeros((states, actions))
    
    def choose_action(self, state):
        # Epsilon-greedy: explore or exploit
        if random.random() < self.epsilon:
            return random.choice(range(self.actions))
        else:
            return np.argmax(self.q_table[state])
    
    def update(self, state, action, reward, next_state):
        # Q-learning update
        current_q = self.q_table[state, action]
        max_next_q = np.max(self.q_table[next_state])
        new_q = current_q + self.lr * (
            reward + self.gamma * max_next_q - current_q
        )
        self.q_table[state, action] = new_q

2. Policy Gradient Methods

Directly optimize the policy (strategy).

REINFORCE Algorithm:

3. Actor-Critic Methods

Combine value-based and policy-based approaches.


Applications

1. Game Playing

2. Robotics

3. Autonomous Systems

4. Recommendation Systems

5. Finance

6. Healthcare


Getting Started

Libraries

1. OpenAI Gym

Standard toolkit for RL environments.

pip install gym
import gym

# Create environment
env = gym.make('CartPole-v1')

# Reset environment
state = env.reset()

# Take action
action = env.action_space.sample()  # Random action
next_state, reward, done, info = env.step(action)

# Close environment
env.close()

2. Stable-Baselines3

High-quality RL algorithm implementations.

pip install stable-baselines3
from stable_baselines3 import PPO
import gym

# Create environment
env = gym.make('CartPole-v1')

# Create and train agent
model = PPO('MlpPolicy', env, verbose=1)
model.learn(total_timesteps=10000)

# Test agent
obs = env.reset()
for i in range(1000):
    action, _states = model.predict(obs)
    obs, rewards, dones, info = env.step(action)
    if dones:
        obs = env.reset()

3. Ray RLlib

Scalable RL library.

pip install ray[rllib]

Simple Example: CartPole

import gym
import numpy as np
from collections import defaultdict

class QLearningAgent:
    def __init__(self, alpha=0.1, gamma=0.95, epsilon=0.1):
        self.alpha = alpha
        self.gamma = gamma
        self.epsilon = epsilon
        self.q_table = defaultdict(lambda: np.zeros(2))
    
    def discretize_state(self, state):
        # Simple discretization (for demonstration)
        return tuple(np.round(state, 1))
    
    def choose_action(self, state):
        state_key = self.discretize_state(state)
        if np.random.random() < self.epsilon:
            return np.random.randint(2)  # Explore
        else:
            return np.argmax(self.q_table[state_key])  # Exploit
    
    def update(self, state, action, reward, next_state, done):
        state_key = self.discretize_state(state)
        next_state_key = self.discretize_state(next_state)
        
        if done:
            target = reward
        else:
            target = reward + self.gamma * np.max(self.q_table[next_state_key])
        
        self.q_table[state_key][action] += self.alpha * (
            target - self.q_table[state_key][action]
        )

# Training
env = gym.make('CartPole-v1')
agent = QLearningAgent()

for episode in range(1000):
    state = env.reset()
    total_reward = 0
    
    while True:
        action = agent.choose_action(state)
        next_state, reward, done, _ = env.step(action)
        agent.update(state, action, reward, next_state, done)
        state = next_state
        total_reward += reward
        
        if done:
            break
    
    if episode % 100 == 0:
        print(f"Episode {episode}, Total Reward: {total_reward}")

env.close()

Resources

Books

  1. "Reinforcement Learning: An Introduction" by Sutton & Barto

  2. "Deep Reinforcement Learning" by Pieter Abbeel et al.

    • Advanced topics in deep RL

Online Courses

  1. "Reinforcement Learning Specialization" - Coursera (University of Alberta)

  2. "Deep Reinforcement Learning" - UC Berkeley CS285

Libraries & Tools

Papers

  1. "Playing Atari with Deep Reinforcement Learning" (DQN)

    • Mnih et al., 2013
  2. "Mastering the game of Go with deep neural networks" (AlphaGo)

    • Silver et al., 2016
  3. "Mastering Chess and Shogi by Self-Play" (AlphaZero)

    • Silver et al., 2017

Communities


Key Takeaways

  1. RL is Different: No labeled data, learns from experience
  2. Exploration vs Exploitation: Balance trying new things vs using what works
  3. Delayed Rewards: Actions have long-term consequences
  4. Start Simple: Begin with simple environments (CartPole, FrozenLake)
  5. Use Libraries: Leverage established libraries (Gym, Stable-Baselines3)
  6. Practice: RL requires hands-on experience

Note: Reinforcement Learning is a complex field. This guide provides basics. For production applications, study advanced topics like Deep RL, Multi-Agent RL, and Imitation Learning.