Table of Contents

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

T

The numeric type used for calculations.

Inheritance
PolicyBase<T>
Implements
Derived
Inherited Members

Constructors

PolicyBase(Random?)

Initializes a new instance of the PolicyBase class.

protected PolicyBase(Random? random = null)

Parameters

random Random

Optional 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

bool

_random

Random number generator for stochastic policies.

protected readonly Random _random

Field Value

Random

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

state Vector<T>

The state observation.

action Vector<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

disposing bool

True 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

state Vector<T>

The current state observation.

training bool

Whether 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

expected int

The expected action size.

actual int

The actual action size.

paramName string

The 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

state Vector<T>

The state vector to validate.

paramName string

The parameter name for error reporting.

Exceptions

ArgumentNullException

Thrown when state is null.

ArgumentException

Thrown when state has invalid size.