SARSAAgent<T>
Models & Types · AiDotNet.ReinforcementLearning.Agents.SARSA
SARSA (State-Action-Reward-State-Action) agent using tabular methods.
Inheritance: ReinforcementLearningAgentBase<T> → SARSAAgent<T>
Implements: IGradientComputable<T, Vector<T>, Vector<T>>
For Beginners
SARSA is like Q-Learning’s more cautious cousin. While Q-Learning learns the optimal policy assuming perfect future actions, SARSA learns based on what you actually do (including exploratory mistakes).
Key differences from Q-Learning:
- On-Policy: Learns from actions it actually takes
- More Conservative: Safer in risky environments (cliff walking)
- Exploration Aware: Updates reflect exploration strategy
- Convergence: Converges to optimal policy only if exploration decreases
Update rule: Q(s,a) ← Q(s,a) + α[r + γQ(s’,a’) - Q(s,a)] (Uses actual next action a’, not max)
Perfect for: Environments where safety matters, risky state transitions Famous for: Rummery & Niranjan 1994, on-policy TD control
How It Works
SARSA is an on-policy TD control algorithm that learns Q-values based on the action actually taken by the current policy, not the optimal action.
Example
// Create an on-policy SARSA agent for safe exploration
var options = new SARSAOptions<double> { LearningRate = 0.1, StateSize = 4, ActionSize = 2 };
var agent = new SARSAAgent<double>(options);
// Select an action for the current state
var state = new Vector<double>(new double[] { 0.5, -0.3, 1.0, 0.2 });
var action = agent.SelectAction(state);
Constructors
| Constructor | Summary |
|---|---|
SARSAAgent() |
|
SARSAAgent(SARSAOptions<T> options) |
Properties
| Property | Summary |
|---|---|
int FeatureCount { get; } |
Methods
| Method | Summary |
|---|---|
ModelMetadata<T> GetModelMetadata() |
|
T Train() |
|
Vector<T> ComputeGradients(Vector<T> input, Vector<T> target, ILossFunction<T> lossFunction) |
|
Vector<T> SelectAction(Vector<T> state, bool training) |
|
byte[] Serialize() |
Auto-generated common model serialization surface. |
void ApplyGradients(Vector<T> gradients, T learningRate) |
|
void Deserialize(byte[] data) |
Auto-generated common model deserialization surface. |
void LoadModel(string filepath) |
|
void ResetEpisode() |
|
void SaveModel(string filepath) |
|
void StoreExperience(Vector<T> state, Vector<T> action, T reward, Vector<T> nextState, bool done) |