Class PolicyBase<T>
- Namespace
- AiDotNet.ReinforcementLearning.Policies
- Assembly
- AiDotNet.dll
Abstract base class for policy implementations. Provides common functionality for numeric operations, random number generation, and resource management.
public abstract class PolicyBase<T> : IPolicy<T>, IDisposable
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
PolicyBase<T>
- Implements
-
IPolicy<T>
- Derived
- Inherited Members
Constructors
PolicyBase(Random?)
Initializes a new instance of the PolicyBase class.
protected PolicyBase(Random? random = null)
Parameters
randomRandomOptional random number generator. If null, a new instance will be created.
Fields
NumOps
Numeric operations helper for type-agnostic calculations.
protected static readonly INumericOperations<T> NumOps
Field Value
- INumericOperations<T>
_disposed
Tracks whether the object has been disposed.
protected bool _disposed
Field Value
_random
Random number generator for stochastic policies.
protected readonly Random _random
Field Value
Methods
ComputeLogProb(Vector<T>, Vector<T>)
Computes the log probability of a given action in a given state. Used by policy gradient methods (PPO, A2C, etc.).
public abstract T ComputeLogProb(Vector<T> state, Vector<T> action)
Parameters
stateVector<T>The state observation.
actionVector<T>The action taken.
Returns
- T
The log probability of the action.
Dispose()
Releases all resources used by the policy.
public void Dispose()
Dispose(bool)
Releases the unmanaged resources used by the policy and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)
Parameters
disposingboolTrue to release both managed and unmanaged resources; false to release only unmanaged resources.
GetNetworks()
Gets the neural networks used by this policy.
public abstract IReadOnlyList<INeuralNetwork<T>> GetNetworks()
Returns
- IReadOnlyList<INeuralNetwork<T>>
A read-only list of neural networks.
Reset()
Resets any internal state (e.g., for recurrent policies, exploration noise).
public virtual void Reset()
SelectAction(Vector<T>, bool)
Selects an action given the current state.
public abstract Vector<T> SelectAction(Vector<T> state, bool training = true)
Parameters
stateVector<T>The current state observation.
trainingboolWhether the agent is training (enables exploration).
Returns
- Vector<T>
The selected action vector.
ValidateActionSize(int, int, string)
Validates that an action vector has the expected size.
protected void ValidateActionSize(int expected, int actual, string paramName)
Parameters
expectedintThe expected action size.
actualintThe actual action size.
paramNamestringThe parameter name for error reporting.
Exceptions
- ArgumentException
Thrown when action size doesn't match expected size.
ValidateState(Vector<T>, string)
Validates that a state vector is not null and has positive size.
protected void ValidateState(Vector<T> state, string paramName)
Parameters
stateVector<T>The state vector to validate.
paramNamestringThe parameter name for error reporting.
Exceptions
- ArgumentNullException
Thrown when state is null.
- ArgumentException
Thrown when state has invalid size.