Non-FictionInformativeEducational

SARSA Operator eBook

A Coursebook on SARSA Operator

by Watt Cheri

  • English
  • 124 pages
  • 2026
  • SamEnrico

A Coursebook on SARSA Operator

Formats

Free sample

Chapter 1: The Cliff's Secret--–- The first time I watched a Q-learning agent walk off a cliff, I assumed it was a bug. The agent had learned the optimal policy — the shortest path from start to goal — yet it kept stepping into the void, episode after episode, as if it had forgotten everything it learned. I restarted the simulation, checked the code, verified the rewards. Everything was correct. The agent was behaving exactly as designed.That was the moment I realized something profound: in reinforcement learning, the optimal solution is not always the safest solution, and the algorithm that finds the fastest path may be the worst choice when mistakes have real consequences.This book is about that realization, and about the algorithm that solves it: SARSA.The Day Optimal Became Dangerous Let me tell you about a problem that changed how I think about artificial intelligence.

It's called Cliff Walking, and it's deceptively simple.Imagine a grid. Four rows tall, twelve columns wide. In the bottom-left corner sits an agent — let's call it a robot, though it could just as easily be a self-driving car, a stock-trading algorithm, or a doctor deciding on a treatment plan. In the bottom-right corner lies the goal. Between them, running along the bottom edge of the grid, is a cliff.

Every step onto the cliff costs 100 points — a catastrophic penalty. Every other step costs 1 point. Reaching the goal earns 0 points and ends the episode.The task: get from start to goal while losing as few points as possible.Seems straightforward, right? The shortest path runs directly along the edge of the cliff, hugging the precipice for ten steps before reaching the goal. The safe path runs one row above the cliff, adding two extra steps to the journey but staying entirely away from danger.Here's what happens when you train two different algorithms on this problem.Q-learning, one of the most famous algorithms in reinforcement learning, learns the optimal policy.

It finds that shortest path along the cliff edge. And during training, it falls off that cliff approximately 47 times per million steps of learning. Each fall costs 100 points. Each fall represents a failure that, in the real world, might be a crashed car, a broken robot arm, or a patient who received the wrong treatment.SARSA (State-Action-Reward-State-Action), the subject of this book, learns a different policy. It learns to walk one row above the cliff, taking the longer but safer path.

During training, it falls off the cliff approximately 0 to 3 times per million steps, depending on how aggressively it explores.Same environment. Same reward structure. Same goal. Completely different behavior.Why? And more importantly, when should you choose one over the other?Those questions are the reason I wrote this book.A Note on What You Need to Know Before we go further, let me be honest about what you need to bring to this journey.You do not need a Ph D in mathematics.

You do not need to have implemented a neural network from scratch. You do not need to have read Sutton and Barto's classic textbook (though it's wonderful, and I'll reference it).What you do need is curiosity about how agents learn from experience, and comfort with basic programming concepts like loops, functions, and data structures. I'll teach you the mathematics as we go, starting from first principles in Chapter 2. Every equation comes with intuition. Every algorithm comes with working code.If you have seen terms like "Markov Decision Process" or "Bellman equation" before, excellent — you'll have a head start.

But if you haven't, don't worry. Chapter 2 provides a complete refresher. The only prerequisite is a willingness to think carefully about decisions, consequences, and risk.The Hidden Choice That Changes Everything Here's what makes Cliff Walking so instructive: both Q-learning and SARSA are trying to maximize total reward. Both are using the same fundamental mathematics. Both have been proven to converge to optimal policies under the right conditions.Yet they behave differently.The difference comes down to one question: What policy do you evaluate while you learn?Q-learning evaluates the optimal policy — the greedy policy that always takes the highest-valued action — even while it follows a different policy for exploration.

It assumes that eventually, it will stop exploring and follow that optimal path. So it learns the value of actions under the assumption that it will always choose the best action from the next state.SARSA evaluates the policy it is currently following — the same policy that generates its actions, exploration and all. It learns the value of actions under the assumption that it will continue to behave the way it currently behaves, including its exploration mistakes.This one difference — a single character in the update rule, comparing max to sample — changes everything about what the agent learns and how it behaves during learning.In Cliff Walking, Q-learning's update assumes that from the next state, the agent will take the best possible action. That assumption is wrong during training (since the agent explores), but Q-learning doesn't care — it's learning for the policy it will use eventually. SARSA's update uses the action the agent actually takes from the next state, learning for the policy it uses right now.This means SARSA's value estimates reflect the true consequences of exploration.

If the agent sometimes falls off cliffs while exploring, SARSA learns that standing near cliffs is dangerous. Q-learning, by contrast, assumes it will never fall (because eventually it will stop exploring and always take the safe step away from the cliff), so it learns that standing near cliffs is fine.The result: Q-learning learns the optimal policy but learns it dangerously. SARSA learns a safe policy that may be slightly suboptimal but that it can execute without catastrophes during learning.This is the central insight of this book: the choice between on-policy and off-policy learning is a choice about risk, not just about optimality.Why This Book Exists You might be wondering: if Q-learning is so popular and so widely used, why dedicate an entire book to SARSA?Three reasons.First, because safety matters more than ever. We are deploying reinforcement learning agents in the real world: self-driving cars, warehouse robots, medical treatment recommenders, trading algorithms. In all of these domains, mistakes during learning have real costs.

A robot that crashes into a wall while exploring is not just inefficient — it's broken. A trading algorithm that loses money testing a strategy is not just suboptimal — it's fired. SARSA's on-policy nature provides an implicit safety mechanism that off-policy algorithms lack.Second, because SARSA is misunderstood. Ask most machine learning practitioners what SARSA is, and they'll say "it's like Q-learning but on-policy." Ask them when to use it, and they'll say "almost never — Q-learning is better." This is wrong. SARSA is not a lesser version of Q-learning; it is a different tool for different problems.

This book will give you the framework to know when each is appropriate.Third, because the theory has advanced. Recent research — including the Mellowmax operator (which we'll cover in Chapter 10) and new convergence results for function approximation (Chapter 7) — has transformed our understanding of on-policy learning. These advances make SARSA more powerful and more stable than…

This is the end of the free sample. Get the eBook to keep going. Buy now

About this book

A Coursebook on SARSA Operator

Details

Author
Watt Cheri
Publisher
SamEnrico
Language
English
Genre
Non-Fiction, Informative, Educational
Released
2026-06-02
Pages (approx.)
124
Available as
eBook, Audiobook

Read better in the ZLOADR app

Your eBooks on your phone or tablet. Sign in with your SamEnrico account and start reading.

More by Watt Cheri

See all

You may also like

See all