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.
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 by considering the state-action sequence: . The update equation for SARSA is:
Where:
- : Current estimate of the Q-value for state-action pair.
- : Learning rate, dictating how much new information affects our estimate.
- : Reward received after taking action in state .
- : Discount factor, controlling the importance of future rewards.
- : Action taken in the next state 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
- Initialize Q-values and Parameters:
- Initialize for all state-action pairs to small random numbers.
- Set learning rate , discount factor , and exploration rate .
- Episode Loop:
- Start from the initial state .
- Choose using an -greedy policy based on .
- Time-step Loop:
- Execute action , observe reward and new state .
- Choose from using an -greedy policy.
- Update using the SARSA update rule.
- Set , .
- If terminal state is reached, begin next episode.
Exploration vs. Exploitation
The exploration-exploitation trade-off is critical in SARSA. An -greedy policy is commonly used, where the agent explores with probability and exploits with probability by choosing the action with the highest Q-value.
Convergence and Challenges
- Selecting Hyperparameters: The learning rate (), discount factor (), and exploration rate () 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

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 courseTrack 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.