FirstVisitMonteCarloAgent<T>

Models & Types · AiDotNet.ReinforcementLearning.Agents.MonteCarlo

First-Visit Monte Carlo agent for episodic tasks.

Inheritance: ReinforcementLearningAgentBase<T> → FirstVisitMonteCarloAgent<T>

Implements: IGradientComputable<T, Vector<T>, Vector<T>>

For Beginners

Monte Carlo methods learn from complete episodes. They wait until the episode ends, then update Q-values based on the actual returns received.

Unlike TD methods (Q-Learning, SARSA), MC methods:

  • Wait for episode completion: No bootstrapping
  • Use actual returns: Not estimates
  • Model-free: Don’t need environment dynamics
  • First-visit: Only count first occurrence of state-action

Perfect for: Episodic tasks (games with clear endings) Not good for: Continuing tasks (no episode end)

Famous for: Foundation of RL, unbiased estimates

How It Works

First-Visit MC estimates value functions by averaging returns following the first visit to each state in an episode.

Example

// Create a First-Visit Monte Carlo agent for episodic tasks
var options = new MonteCarloOptions<double> { StateSize = 4, ActionSize = 2 };
var agent = new FirstVisitMonteCarloAgent<double>(options);
            
// Select an action using epsilon-greedy over learned Q-values
var state = new Vector<double>(new double[] { 0.5, -0.3, 1.0, 0.2 });
var action = agent.SelectAction(state);

Constructors

Constructor Summary
FirstVisitMonteCarloAgent() Initializes a new instance with default settings.
FirstVisitMonteCarloAgent(MonteCarloOptions<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)