PYTHON Tutorial

Reinforcement Learning

What is Reinforcement Learning?

Reinforcement learning is a type of machine learning where an agent learns to make decisions in an environment by interacting with it and receiving rewards or penalties for its actions.

Key Concepts:

  • Markov Decision Process (MDP): A mathematical model that describes an environment where the agent's decisions at each time step affect the future states.
  • Rewards and Penalties: Feedback that the agent receives for its decisions. Rewards encourage desired behaviors, while penalties discourage them.
  • Policy Learning: The agent's strategy for making decisions based on past experiences.

Practical Steps:

  • Define the MDP: Specify the environment, actions, states, and rewards.
  • Initialize the Policy: Randomly or with a predetermined strategy.
  • Interact with the Environment: The agent takes actions and observes the consequences.
  • Update the Policy: The agent adjusts its policy based on the rewards/penalties received.
  • Repeat: Steps 3-4 until the agent finds an optimal policy.

Python Example:

import random

# Define the environment
states = [0, 1, 2, 3]
actions = ['left', 'right']
rewards = {
    (0, 'left'): 10,
    (0, 'right'): -1,
    (1, 'left'): -1,
    (1, 'right'): 10,
    (2, 'left'): 10,
    (2, 'right'): -1,
    (3, 'left'): -1,
    (3, 'right'): 10
}

# Initialize the policy
policy = {
    0: random.choice(actions),
    1: random.choice(actions),
    2: random.choice(actions),
    3: random.choice(actions)
}

# Interact with the environment
current_state = 0
while current_state != 3:
    action = policy[current_state]
    next_state = random.choice(states)
    reward = rewards[(current_state, action)]
    # Update the policy
    policy[current_state] = action if reward > 0 else random.choice(actions)
    current_state = next_state

# Optimal policy learned: policy == {'0': 'right', '1': 'right', '2': 'right'}

Applications:

Reinforcement learning has applications in:

  • Robotics
  • Game playing
  • Finance
  • Healthcare
  • Autonomous driving