Reinforcement Learning
SARSA Algorithm
Cart Pole Problem
Value Approximation
Machine Learning

SARSA value approximation for Cart Pole

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The Cart Pole problem is a classic reinforcement learning (RL) challenge where an agent must balance a pole on a moving cart. The agent receives continuous state inputs representing the cart's position, velocity, pole angle, and angular velocity, and it chooses discrete actions (left or right) to keep the pole balanced for as long as possible. One efficient way to approach this problem in a reinforcement learning context is using SARSA (State-Action-Reward-State-Action) value approximation.

Understanding SARSA

SARSA is an on-policy temporal difference (TD) learning algorithm. It updates the Q-value for a state-action pair (s,a)(s,a) by considering the state-action sequence: (s,a,r,s,a)(s, a, r, s', a'). The update equation for SARSA is:

Q(s,a)Q(s,a)+α[r+γQ(s,a)Q(s,a)]Q(s, a) \leftarrow Q(s, a) + \alpha \left[ r + \gamma Q(s', a') - Q(s, a) \right]

Where:

  • Q(s,a)Q(s, a): Current estimate of the Q-value for state-action pair.
  • α\alpha: Learning rate, dictating how much new information affects our estimate.
  • rr: Reward received after taking action aa in state ss.
  • γ\gamma: Discount factor, controlling the importance of future rewards.
  • aa': Action taken in the next state ss' following the current policy.

Applying SARSA to the Cart Pole Problem

State Representation

In the Cart Pole problem, the state is typically represented as a set of continuous values. An effective approach is to discretize these values into bins. For example, angles may be represented in small discrete increments. Discretizing the state space allows us to handle the infinite nature of continuous variables.

Action Space

The action space in Cart Pole is discrete, usually consisting of two actions: move the cart left or right. This limited action space makes it approachable for techniques like table-based SARSA.

Reward Structure

A simple reward mechanism is employed, such as giving +1 for every time-step the pole remains balanced and 0 otherwise. This reward strategy encourages the agent to maximize the duration for which it keeps the pole upright.

Algorithm Implementation

  1. Initialize Q-values and Parameters:
    • Initialize Q(s,a)Q(s, a) for all state-action pairs to small random numbers.
    • Set learning rate α\alpha, discount factor γ\gamma, and exploration rate ϵ\epsilon.
  2. Episode Loop:
    • Start from the initial state ss.
    • Choose aa using an ϵ\epsilon-greedy policy based on Q(s,a)Q(s, a).
  3. Time-step Loop:
    • Execute action aa, observe reward rr and new state ss'.
    • Choose aa' from ss' using an ϵ\epsilon-greedy policy.
    • Update Q(s,a)Q(s, a) using the SARSA update rule.
    • Set sss \leftarrow s', aaa \leftarrow a'.
    • If terminal state is reached, begin next episode.

Exploration vs. Exploitation

The exploration-exploitation trade-off is critical in SARSA. An ϵ\epsilon-greedy policy is commonly used, where the agent explores with probability ϵ\epsilon and exploits with probability 1ϵ1-\epsilon by choosing the action with the highest Q-value.

Convergence and Challenges

  • Selecting Hyperparameters: The learning rate (α\alpha), discount factor (γ\gamma), and exploration rate (ϵ\epsilon) significantly impact performance and convergence speed. Tuning these parameters through experimentation is often necessary.
  • State Space Granularity: Too coarse discretization may lead to a lack of precision, while too fine discretization may make learning impractical due to the curse of dimensionality.

Example Implementation

Here's a Python snippet that highlights how SARSA could be implemented for the Cart Pole problem using OpenAI Gym:


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.