CQLAgent<T>

Models & Types · AiDotNet.ReinforcementLearning.Agents.CQL

Conservative Q-Learning (CQL) agent for offline reinforcement learning.

Inheritance: ReinforcementLearningAgentBase<T> → DeepReinforcementLearningAgentBase<T> → CQLAgent<T>

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

For Beginners

Unlike online RL (which tries actions and learns), CQL learns only from recorded data. This is crucial for domains where exploration is dangerous or expensive.

Key features:

  • Conservative Penalty: Lowers Q-values for unseen state-action pairs
  • Offline Learning: No environment interaction needed
  • Safe Policy Improvement: Guarantees improvement over behavior policy

Example use cases:

  • Learning from medical records (can’t experiment on patients)
  • Autonomous driving from dashcam data
  • Robotics from demonstration datasets

How It Works

CQL is designed for offline RL, learning from fixed datasets without environment interaction. It prevents overestimation by adding a conservative penalty that pushes down Q-values for out-of-distribution actions while maintaining accuracy on in-distribution actions.

Example

// Create a Conservative Q-Learning agent for offline RL from recorded data
var options = new CQLOptions<double> { StateSize = 4, ActionSize = 2};
var agent = new CQLAgent<double>(options);
            
// Select an action given 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
CQLAgent() Initializes a new instance with default settings.
CQLAgent(CQLOptions<T> options)

Properties

Property Summary
int FeatureCount { get; }

Methods

Method Summary
Dictionary<string, T> GetMetrics()
ModelMetadata<T> GetModelMetadata()
ModelOptions GetOptions()
T Train()
Task TrainAsync()
Task<Vector<T>> PredictAsync(Vector<T> input)
Vector<T> ComputeGradients(Vector<T> input, Vector<T> target, ILossFunction<T> lossFunction)
Vector<T> Predict(Vector<T> input)
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 LoadOfflineData(List<ValueTuple<Vector<T>, Vector<T>, T, Vector<T>, bool>> dataset) Load offline dataset into the replay buffer.
void ResetEpisode()
void SaveModel(string filepath)
void StoreExperience(Vector<T> state, Vector<T> action, T reward, Vector<T> nextState, bool done)