Algorithm API
Textbook-oriented reinforcement-learning algorithms and policy utilities.
The available families are grouped into submodules:
dynamic_programming: model-based policy evaluation, value iteration, and policy iteration.monte_carlo_methods: prediction and exploring-starts control using sampled episodes.temporal_difference_learning: tabular Sarsa(0) and Q-learning.linear_approximation: semi-gradient TD(0) prediction and Sarsa(0) control with feature-based value functions.eligibility_traces: semi-gradient Sarsa(lambda) control.policy_gradient_methods: REINFORCE and actor-critic with parameterized policies.
The policy and schedules submodules provide policy helpers and schedules
for exploration rates and step sizes. Import functions from the specific
submodule that defines them, for example::
from gym_classics2.algorithms.dynamic_programming import value_iteration
Dynamic programming
gym_classics2.algorithms.dynamic_programming
This file implements dynamic programming algorithms for solving Markov Decision Processes (MDPs) in tabular gym-classics2 environments with model access. The algorithms include value iteration and policy iteration, which are fundamental methods in reinforcement learning for computing optimal policies and value functions.
backup
Computes a Bellman backup for a given state, action, and value function. This is the building block for the other algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
A gym-classics environment with model access. |
required | |
discount
|
The discount factor. |
required | |
V
|
The current value function. |
required | |
state
|
The current state. |
required | |
action
|
The action to evaluate. |
required |
Returns: The computed Q-value for the given state and action.
Source code in gym_classics2/algorithms/dynamic_programming.py
value_iteration
Performs value iteration to calculate the optimal value function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
A gym-classics environment with model access. |
required | |
discount
|
The discount factor (0 <= discount <= 1). |
required | |
precision
|
The precision for convergence (default: 1e-3). |
0.001
|
|
history
|
If True, returns a list of intermediate value functions. |
False
|
|
verbose
|
If True, prints progress information. |
False
|
Returns:
| Type | Description |
|---|---|
|
The optimal value function V. If history is True, returns a list of intermediate value functions. |
Source code in gym_classics2/algorithms/dynamic_programming.py
policy_evaluation
Performs (modified) policy evaluation to approximate the given policy's value function. Regular policy evaluation does not limit the number of backups and converges to the true value funciton. Modified policy evaluation limits the number of backups and produces an estimate of the true value function. This has several advantages: It is faster and it prevents infinite loops for undiscounted problems where the evaluated policy does not reach a terminal state.
Policy iteration using modified policy evaluation still converges. Note: Value iteration is a special case of policy iteration where policy evaluation is limited to a single backup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
A gym-classics environment with model access. |
required | |
discount
|
The discount factor (0 <= discount <= 1). |
required | |
policy
|
The policy to evaluate. |
required | |
precision
|
The precision for convergence (default: 1e-3). |
0.001
|
|
max_backups
|
Maximum number of sweeps over all states (default: 1000). Limits computation and prevents infinite loops for undiscounted problems. |
1000
|
|
verbose
|
If True, prints progress information. |
False
|
Returns:
| Type | Description |
|---|---|
|
The value function for the given policy. |
Source code in gym_classics2/algorithms/dynamic_programming.py
policy_improvement
Improves the policy based on the given value function. It does this by calculating a Q-value function for all states and actions using Bellman updates and then extracts a greedy policy.
Policy evaluation adn policy improvement are the steps of policy iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
The current policy to improve. |
required | |
V_policy
|
The value function of the current policy. |
required | |
env
|
A gym-classics environment with model access. |
required | |
discount
|
The discount factor (0 <= discount <= 1). |
required | |
precision
|
The precision for determining stability (default: 1e-3). |
0.001
|
Returns:
| Type | Description |
|---|---|
|
A tuple (improved_policy, stable) where stable is True if the policy did not change. |
Source code in gym_classics2/algorithms/dynamic_programming.py
policy_iteration
policy_iteration(env, discount, precision=0.001, max_backups=1000, history=False, verbose=False, rng=None)
Performs policy iteration to find the optimal policy. It repeats modified policy evaluation followed by policy improvement till it converges to the optimal policy.
Using modified policy evaluation has several advantages: It is faster and it prevents infinite loops for undiscounted problems where the evaluated policy does not reach a terminal state. Policy iteration using modified policy evaluation still converges. Note: Value iteration is a special case of policy iteration where policy evaluation is limited to a single backup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
A gym-classics environment with model access. |
required | |
discount
|
The discount factor (0 <= discount <= 1). |
required | |
precision
|
The precision for convergence (default: 1e-3). |
0.001
|
|
max_backups
|
Maximum number of iterations used in modified policy evaluation. |
1000
|
|
history
|
If True, returns lists of intermediate policies and value functions. |
False
|
|
verbose
|
If True, prints progress information. |
False
|
|
rng
|
NumPy generator or integer seed used to initialize the policy. |
None
|
Returns:
| Type | Description |
|---|---|
|
The optimal policy. If history is True, returns a tuple (policy_list, V_list) containing lists of intermediate policies and value functions. |
Source code in gym_classics2/algorithms/dynamic_programming.py
Policy helpers
gym_classics2.algorithms.policy
Create, convert, and select actions from tabular policies.
A deterministic tabular policy is a vector of integer action IDs indexed by state ID. For gym-classics2 environments with MultiDiscrete observations, policies can instead be dictionaries mapping coordinate tuples to action IDs.
Greedy policies can be derived from state values using model access or directly
from action values. epsilon_greedy_action samples from action values rather
than from a deterministic policy vector. Random choices accept a NumPy
generator, an integer seed, or None through rng.
random_policy
Create a deterministic policy by sampling one action per state.
Actions are drawn uniformly and independently when the policy is created. Looking up a state in the resulting policy always gives the same action.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment with a discrete action space. Observations must be Discrete, or MultiDiscrete in a gym-classics2 environment. |
required | |
rng
|
NumPy generator, integer seed, or None for sampling actions. |
None
|
Returns:
| Type | Description |
|---|---|
|
NumPy array of action IDs indexed by state ID for Discrete observations, |
|
|
or a dictionary mapping coordinate tuples to action IDs for |
|
|
MultiDiscrete observations. |
Source code in gym_classics2/algorithms/policy.py
encode_policy
Convert a sequence of action IDs to labels for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Deterministic policy vector containing integer action IDs. |
required | |
env
|
gym-classics2 environment providing |
required | |
type
|
Label format passed to |
'text'
|
Returns:
| Type | Description |
|---|---|
|
List of action labels in the same order as the input policy. |
Source code in gym_classics2/algorithms/policy.py
greedy_policy
Create a greedy policy by one-step look-ahead from state values.
For each state, select an action maximizing the expected reward plus the discounted next-state value. Terminal transitions do not bootstrap. Ties are broken uniformly at random when the policy is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
One-dimensional state-value array indexed by state ID. |
required | |
env
|
gym-classics2 environment with Discrete observation and action spaces and access to the transition and reward model. |
required | |
discount
|
Discount factor in |
1
|
|
rng
|
NumPy generator, integer seed, or None for random tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
NumPy array of integer action IDs indexed by state ID. |
Source code in gym_classics2/algorithms/policy.py
greedy_policy_Q
Create a greedy policy by maximizing action values in each state.
Ties are broken uniformly at random when the policy is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Q
|
Two-dimensional action-value array indexed by state and action. |
required | |
env
|
Environment with discrete observation and action spaces. |
required | |
discount
|
Must be in |
1
|
|
rng
|
NumPy generator, integer seed, or None for random tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
NumPy array of integer action IDs indexed by state ID. |
Source code in gym_classics2/algorithms/policy.py
epsilon_greedy_action
Select an epsilon-greedy action from tabular action values.
With probability epsilon, sample uniformly from all actions, including
greedy ones. Otherwise, maximize the action values and break ties
uniformly at random.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
One-dimensional action-value vector, or a table indexed by state and then action. Despite the name, this argument contains action values, not the action IDs of a deterministic policy. |
required | |
state
|
Current state index. If omitted, |
None
|
|
epsilon
|
Exploration probability in |
0
|
|
rng
|
NumPy generator, integer seed, or None for action sampling and tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
Selected integer action ID. |
Source code in gym_classics2/algorithms/policy.py
make_multidiscrete_policy
Convert a policy indexed by state ID to a coordinate-keyed dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Deterministic policy vector indexed by state ID, or an existing dictionary mapping coordinate tuples to action IDs. |
required | |
env
|
Environment with MultiDiscrete observations. Converting a vector
requires a gym-classics2 environment providing |
required |
Returns:
| Type | Description |
|---|---|
|
Dictionary mapping coordinate tuples to integer action IDs. An input |
|
|
dictionary is returned unchanged, without copying or validating entries. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the observation space is not MultiDiscrete. |
ValueError
|
If converting a vector for an unsupported environment type. |
Source code in gym_classics2/algorithms/policy.py
Monte Carlo methods
gym_classics2.algorithms.monte_carlo_methods
This file implements tabular Monte Carlo methods for policy evaluation and control for environments with discrete state spaces.
sample_episode
sample_episode(env, policy=None, start_state=None, start_action=None, epsilon=0, max_len=1000, verbose=False, rng=None)
Samples an episode from the environment using the given policy and starting conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
The environment to sample from. |
required | |
policy
|
A mapping from states to actions. For discrete state spaces, use
an action list in state order. If |
None
|
|
start_state
|
The state to start the episode from (if None, the environment's default starting state will be used). |
None
|
|
start_action
|
The action to take in the first step of the episode (if None, the action will be chosen according to the policy or randomly if no policy is given). |
None
|
|
epsilon
|
The probability of taking a random action instead of the policy's action at each step (for epsilon-greedy exploration). |
0
|
|
max_len
|
The maximum length of the episode to prevent infinite loops. |
1000
|
|
verbose
|
If True, prints the state transitions and rewards for each step in the episode. |
False
|
|
rng
|
NumPy generator or integer seed for policy and exploration choices. |
None
|
Returns:
| Type | Description |
|---|---|
|
A list of (state, action, reward, next_state) tuples representing the episode. |
|
|
The episode ends when a terminal state is reached or when max_len steps have been taken. |
|
|
Note that the last tuple in the episode will have a next_state that is either terminal or the |
|
|
state at which the episode was truncated due to max_len. |
Source code in gym_classics2/algorithms/monte_carlo_methods.py
on_policy_state_distribution
Estimate a policy's state distribution using rng for exploration.
Source code in gym_classics2/algorithms/monte_carlo_methods.py
MC_prediction
Estimate a policy's state values with first-visit Monte Carlo prediction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
Array-like mapping from state IDs to action IDs. |
required | |
env
|
Gymnasium environment with a discrete observation space. |
required | |
discount
|
Reward discount factor. |
required | |
n
|
Number of episodes to sample. |
100
|
|
max_episode_len
|
Maximum sampled steps per episode. |
100
|
|
verbose
|
Print episode progress when true. |
False
|
|
rng
|
NumPy generator or integer seed for episode sampling. |
None
|
Returns:
| Type | Description |
|---|---|
|
A NumPy value array with one entry per state. Unvisited states contain |
|
|
|
Source code in gym_classics2/algorithms/monte_carlo_methods.py
MC_control_ES_textbook
MC_control_ES_textbook(env, discount, n=100, Q=None, max_episode_len=100, history=False, verbose=False, rng=None)
Monte Carlo control with exploring starts and stored sample returns.
This direct textbook implementation stores every first-visit return. Prefer
:func:MC_control_ES for larger experiments because its running-average
update uses less memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
A tabular |
required | |
discount
|
Reward discount factor. |
required | |
n
|
Number of episodes to sample. |
100
|
|
Q
|
Optional initial action-value array. |
None
|
|
max_episode_len
|
Maximum sampled steps per episode. |
100
|
|
history
|
Retain intermediate policies, Q arrays, episodes, and returns. |
False
|
|
verbose
|
Print episode details; values greater than one print transitions. |
False
|
|
rng
|
NumPy generator or integer seed for all algorithm choices. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in gym_classics2/algorithms/monte_carlo_methods.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
MC_control_ES
MC_control_ES(env, discount, n=100, Q=None, max_episode_len=100, history=False, verbose=False, rng=None)
Monte Carlo Control with Exploring Starts (incremental version). This algorithm estimates the optimal action-value function Q and the corresponding greedy policy by sampling episodes with exploring starts. It uses incremental updates to compute the average returns for each (s,a) pair, which is more memory efficient than storing all returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
A tabular |
required | |
discount
|
Reward discount factor. |
required | |
n
|
Number of episodes to sample. |
100
|
|
Q
|
Optional initial action-value array. |
None
|
|
max_episode_len
|
Maximum sampled steps per episode. |
100
|
|
history
|
Retain intermediate policies, Q arrays, episodes, and returns. Note: retaining the history may require a lot of memory. |
False
|
|
verbose
|
Print episode details; values greater than one print transitions. |
False
|
|
rng
|
NumPy generator or integer seed for all algorithm choices. |
None
|
Returns:
| Type | Description |
|---|---|
|
|
Source code in gym_classics2/algorithms/monte_carlo_methods.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
Temporal-difference learning
gym_classics2.algorithms.temporal_difference_learning
This file implements tabular temporal difference learning algorithms for policy evaluation and control for discrete environments. The algorithms include Sarsa(0) and Q-learning.
Sarsa_0
Learn action values with one-step on-policy Sarsa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Gymnasium environment with discrete observation and action spaces. |
required | |
discount
|
Reward discount factor in |
required | |
alpha
|
Scalar step size or |
required | |
epsilon
|
Scalar exploration probability or |
required | |
Q
|
Optional initial action-value array shaped
|
None
|
|
n
|
Number of training episodes. |
100
|
|
verbose
|
Print individual updates when true. |
False
|
|
history
|
Retain Q arrays, discounted episode returns, and episode lengths. |
False
|
|
rng
|
NumPy generator or integer seed for exploration and tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
The learned Q array. If |
|
|
the dictionary contains |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the observation space is not discrete. |
Source code in gym_classics2/algorithms/temporal_difference_learning.py
Q_learning
Learn action values with one-step off-policy Q-learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Gymnasium environment with discrete observation and action spaces. |
required | |
discount
|
Reward discount factor in |
required | |
alpha
|
Scalar step size or |
required | |
epsilon
|
Scalar exploration probability or |
required | |
Q
|
Optional initial action-value array shaped
|
None
|
|
n
|
Number of training episodes. |
100
|
|
verbose
|
Print progress information when true. |
False
|
|
history
|
Retain Q arrays, discounted episode returns, episode lengths, and state-visit counts. |
False
|
|
rng
|
NumPy generator or integer seed for exploration and tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
The learned Q array. If |
|
|
the dictionary contains |
|
|
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the observation space is not discrete. |
Source code in gym_classics2/algorithms/temporal_difference_learning.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
Linear function approximation
gym_classics2.algorithms.linear_approximation
Linear function approximation algorithms for policy evaluation and control.
Implements linear v and q-value function approximation, prediction with semi-gradient TD(0), control with semi-gradient Sarsa(0) and feature construction with Fourier basis functions.
The algorithms do not require discrete state spaces. Callers have to provide a
state_features(state, env) function that converts states to state feature vectors x(s).
For an environment where the state is a set of numbers, e.g., a x- and y-coordinate. The leading 1 is the intercept.
state_action_features
Construct the state-action feature vector x(s, a).
The state feature vector is expected to have the form
[1, x1, ..., xd], with a leading intercept. The returned vector keeps
one s hared intercept and has a separate block of the remaining state
features for each action. Only the intercept and the weights for a are
selected; all other elements are set to zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
State to represent. |
required | |
a
|
Integer ID of the action to represent. |
required | |
env
|
Environment providing the discrete action space. |
required | |
state_features
|
Callable that returns |
required |
Returns:
| Type | Description |
|---|---|
|
Block-coded NumPy feature vector for the state-action pair |
Source code in gym_classics2/algorithms/linear_approximation.py
v_hat
Compute the linear state-value approximation v_hat(s, w).
Approximates the state value as w^T x(s), the weighted sum of the components in the
state feature vector x(s). The weight and feature vectors must have the
same length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
State to evaluate. |
required | |
w
|
Weight vector of the linear approximator. |
required | |
env
|
Environment containing the state. |
required | |
state_features
|
Callable returning the feature vector |
required |
Returns:
| Type | Description |
|---|---|
|
Scalar estimate of the expected return from |
Source code in gym_classics2/algorithms/linear_approximation.py
q_hat
Compute the linear action-value approximation q_hat(s, a, w).
Estimates the q-value as w^T x(s, a) using the block-coded state-action
features produced by state_action_features(). The weight vector must
have the same length as that feature vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
State to evaluate. |
required | |
a
|
Integer ID of the action to evaluate. |
required | |
w
|
Weight vector of the linear approximator. |
required | |
env
|
Environment providing the discrete action space. |
required | |
state_features
|
Callable returning a state feature vector with a
leading intercept for |
required |
Returns:
| Type | Description |
|---|---|
|
Scalar estimate of the expected return from taking |
Source code in gym_classics2/algorithms/linear_approximation.py
epsilon_greedy_action_w
Select an epsilon-greedy action from an approximate action-values function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
Current state. |
required | |
w
|
Weight vector for the action-value approximator. |
required | |
env
|
Environment providing the discrete action space. |
required | |
state_features
|
Callable converting |
required | |
epsilon
|
Probability of selecting a uniformly random action. |
0
|
|
rng
|
NumPy generator or integer seed. |
None
|
Returns:
| Type | Description |
|---|---|
|
Selected integer action ID. |
Source code in gym_classics2/algorithms/linear_approximation.py
MSVE
Calculate the weighted mean squared value error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
V
|
Estimated value for each state. |
required | |
V_true
|
Reference value for each state. |
required | |
weight
|
Weight for each state, typically its stationary visitation probability. If omitted, use unit weights. |
None
|
Returns:
| Type | Description |
|---|---|
|
Weighted sum of squared value errors. |
Source code in gym_classics2/algorithms/linear_approximation.py
semi_gradient_TD0
semi_gradient_TD0(env, state_features, policy, n, alpha, gamma, max_episode_length=1000, verbose=False)
Estimate state values with semi-gradient TD(0).
This function runs TD(0) learning with function approximation over multiple episodes generated from a given policy and environment. Updates are performed using the semi-gradient of the value function approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Episodic Gymnasium environment used to generate experience. |
required | |
state_features
|
Callable converting |
required | |
policy
|
Deterministic policy indexed by state. For multidiscrete gym-classics environments, a policy array in state-ID order is converted to a dictionary indexed by coordinate states. |
required | |
n
|
Number of training episodes. |
required | |
alpha
|
Step size or schedule. |
required | |
gamma
|
Discount factor in |
required | |
max_episode_length
|
Maximum number of steps per episode. |
1000
|
|
verbose
|
Whether to print step-by-step diagnostics. |
False
|
Returns:
| Type | Description |
|---|---|
|
Learned weight vector for the approximate value function. |
Source code in gym_classics2/algorithms/linear_approximation.py
semi_gradient_Sarsa0
semi_gradient_Sarsa0(env, state_features, n, epsilon, alpha, gamma, w=None, max_episode_length=1000, verbose=False, history=False, rng=None)
Run semi-gradient Sarsa(0) with function approximation for control.
Implements the semi-gradient Sarsa(0) algorithm for estimating the optimal action-value function q_*(s, a) using a differentiable function approximator q̂(s, a, w). Actions are selected according to an ε-greedy policy derived from the current action-value estimate.
Episodes are truncated after max_episode_length time steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Episodic environment used to generate experience. |
required | |
state_features
|
Callable converting |
required | |
n
|
Number of training episodes. |
required | |
epsilon
|
Exploration rate or schedule for the epsilon-greedy policy. |
required | |
alpha
|
Step size or schedule. |
required | |
gamma
|
Discount factor in |
required | |
w
|
Initial action-value weight vector. If omitted, initialize it to zeros. |
None
|
|
max_episode_length
|
Maximum number of steps per episode. |
1000
|
|
verbose
|
Whether to print step-by-step diagnostics. |
False
|
|
history
|
Whether to return weights, returns, and episode lengths collected during training. |
False
|
|
rng
|
NumPy generator or integer seed for exploration and tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
Learned weight vector. If |
Source code in gym_classics2/algorithms/linear_approximation.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
create_fourier_basis_coefs
Create Fourier basis coefficient vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
Number of state-feature dimensions. |
required | |
order
|
Maximum coefficient in each dimension. |
required |
Returns:
| Type | Description |
|---|---|
|
Array containing the Cartesian product of coefficients from zero through |
|
|
|
Source code in gym_classics2/algorithms/linear_approximation.py
transformation_fourier_basis
Create a Fourier basis feature transformation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min
|
Minimum state value in each dimension. |
required | |
max
|
Maximum state value in each dimension. |
required | |
order
|
Maximum Fourier coefficient in each dimension. |
required |
Returns:
| Type | Description |
|---|---|
|
Callable that normalizes a state to the unit hypercube and returns its |
|
|
Fourier basis features. |
Example
Source code in gym_classics2/algorithms/linear_approximation.py
Eligibility traces
gym_classics2.algorithms.eligibility_traces
Semi-gradient Sarsa(lambda) with linear function approximation.
Callers provide a state_features(state, env) function that converts states
to feature vectors.
semi_gradient_Sarsa_lambda
semi_gradient_Sarsa_lambda(env, state_features, n, epsilon, alpha, gamma, lam, w=None, max_episode_length=1000, verbose=False, history=False, rng=None)
Run semi-gradient Sarsa(lambda) with linear function approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Episodic environment used to generate experience. |
required | |
state_features
|
Callable converting |
required | |
n
|
Number of episodes. |
required | |
epsilon
|
Exploration rate or schedule for the epsilon-greedy policy. |
required | |
alpha
|
Step size or schedule. |
required | |
gamma
|
Discount factor in |
required | |
lam
|
Trace-decay parameter lambda in |
required | |
w
|
Initial weight vector. If omitted, initialize it to zeros. |
None
|
|
max_episode_length
|
Maximum number of steps per episode. |
1000
|
|
verbose
|
Whether to print step-by-step diagnostics. |
False
|
|
history
|
Whether to return weights, returns, and episode lengths collected during training. |
False
|
|
rng
|
NumPy generator or integer seed for exploration and tie-breaking. |
None
|
Returns:
| Type | Description |
|---|---|
|
Learned weight vector. If |
Source code in gym_classics2/algorithms/eligibility_traces.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
Policy-gradient methods
gym_classics2.algorithms.policy_gradient_methods
Policy-gradient control with linear function approximation.
REINFORCE uses sampled returns; one-step actor-critic (AC) uses a learned state-value function to estimate the policy update. Both learn a stochastic softmax policy over linear action preferences for a discrete action space.
Callers supply state_features(state, env), returning a fixed-length NumPy
vector with a leading intercept, such as [1, x, y]. Policy features are
constructed by state_action_features; AC also uses the state features
directly for its value approximation.
h
Compute the linear action preference h(s, a) = theta^T x(s, a).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
State to evaluate. |
required | |
a
|
Integer action ID in |
required | |
theta
|
Policy parameter vector matching |
required | |
env
|
Environment providing the discrete action space. |
required | |
state_features
|
Callable returning state features with a leading
intercept for |
required |
Returns:
| Type | Description |
|---|---|
|
Scalar preference used as the action's softmax logit. |
Source code in gym_classics2/algorithms/policy_gradient_methods.py
pi
Compute the softmax policy pi(a | s) over linear preferences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
State to evaluate. |
required | |
theta
|
Policy parameter vector matching |
required | |
env
|
Environment providing the discrete action space. |
required | |
state_features
|
Callable returning state features with a leading
intercept for |
required |
Returns:
| Type | Description |
|---|---|
|
NumPy vector of length |
|
|
with probabilities proportional to |
Source code in gym_classics2/algorithms/policy_gradient_methods.py
sample_episode_approx_policy
Sample an episode from a parameterized policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Episodic Gymnasium environment with a discrete action space. |
required | |
pi
|
Callable |
required | |
theta
|
Policy parameter vector. |
required | |
state_features
|
Feature callable passed through to |
required | |
max_episode_length
|
Maximum number of transitions to sample. |
1000
|
|
rng
|
NumPy generator, integer seed, or None for action sampling. Does not seed the environment. |
None
|
Returns:
| Type | Description |
|---|---|
|
List of |
|
|
with a fresh environment reset. Stops on termination or after |
|
|
|
Source code in gym_classics2/algorithms/policy_gradient_methods.py
choose_action_w
Sample an action from a parameterized policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment providing the discrete action space. |
required | |
pi
|
Callable |
required | |
theta
|
Policy parameter vector. |
required | |
state
|
Current state. |
required | |
state_features
|
Feature callable passed through to |
required | |
rng
|
NumPy generator, integer seed, or None for action sampling. |
None
|
Returns:
| Type | Description |
|---|---|
|
Selected integer action ID. |
Source code in gym_classics2/algorithms/policy_gradient_methods.py
REINFORCE
REINFORCE(env, state_features, n, alpha, gamma, theta=None, max_episode_length=1000, verbose=False, history=False, rng=None)
Learn a linear softmax policy with episodic REINFORCE (no baseline).
Sample an episode, then update theta at each visited state using
alpha(episode) * gamma**t * G_t * grad_log_pi, where G_t is the
discounted return from step t to the end of the sampled trajectory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Episodic Gymnasium environment with a discrete action space. |
required | |
state_features
|
Callable returning a fixed-length NumPy feature vector
with a leading intercept for |
required | |
n
|
Positive number of training episodes. |
required | |
alpha
|
Float step size or callable of the zero-based episode index. |
required | |
gamma
|
Discount factor in |
required | |
theta
|
Initial NumPy policy parameter vector, updated in place. Its
length must match |
None
|
|
max_episode_length
|
Positive maximum number of steps per episode. |
1000
|
|
verbose
|
Print step-by-step diagnostics instead of a progress bar. |
False
|
|
history
|
Whether to return training history alongside the parameters. |
False
|
|
rng
|
NumPy generator, integer seed, or None for action sampling. Does not seed the environment. |
None
|
Returns:
| Type | Description |
|---|---|
|
Learned parameter vector |
|
|
|
|
|
before training and after each episode), |
|
|
episode returns), and |
Notes
Sampling ignores environment truncation. If the step limit cuts an episode short, returns use only the sampled rewards, without bootstrapping a value for the final state.
Source code in gym_classics2/algorithms/policy_gradient_methods.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
AC
AC(env, state_features, n, alpha_policy, alpha_value, gamma, max_episode_length=1000, verbose=False, history=False, rng=None)
Learn a linear softmax policy with one-step actor-critic.
The critic approximates v(s) = w^T x(s) and learns from the TD error
delta = r + gamma * v(next_state) - v(state). The actor updates its
policy using alpha_policy(episode) * gamma**t * delta * grad_log_pi.
Both parameter vectors start at zero, giving a uniform initial policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Episodic Gymnasium environment with a discrete action space. |
required | |
state_features
|
Callable returning a fixed-length NumPy feature vector
with a leading intercept for |
required | |
n
|
Positive number of training episodes. |
required | |
alpha_policy
|
Float policy step size or callable of the zero-based episode index. |
required | |
alpha_value
|
Float value-function step size or callable of the zero-based episode index. |
required | |
gamma
|
Discount factor in |
required | |
max_episode_length
|
Positive maximum number of steps per episode. |
1000
|
|
verbose
|
Print episode numbers instead of a progress bar. |
False
|
|
history
|
Whether to return training history alongside the parameters. |
False
|
|
rng
|
NumPy generator, integer seed, or None for action sampling. Does not seed the environment. |
None
|
Returns:
| Type | Description |
|---|---|
|
Tuple |
|
|
vectors, or |
|
|
dictionary contains |
|
|
episode), |
|
|
(episode lengths in steps). |
Notes
This implementation ignores environment truncation and bootstraps from the next state's value even on a terminal transition. For an episodic task, terminal-state features should therefore yield a value of zero.
Source code in gym_classics2/algorithms/policy_gradient_methods.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
Schedules
gym_classics2.algorithms.schedules
Scalar schedules for reinforcement-learning hyperparameters.
A schedule is a callable that maps a nonnegative episode or step index t to
a floating-point value. The included algorithms use schedules to vary parameters
such as the step size (alpha) and exploration rate (epsilon) during training.
For example, a linear schedule can decrease epsilon from 1.0 to 0.1 over 10 steps:
Example
```python schedule = LinearDecaySchedule(1.0, min_value=0.1, decay_steps=10) for t in (0, 5, 10, 15): print(t, schedule(t))
```
Schedule
Interface for a scalar value indexed by episode or time step.
Source code in gym_classics2/algorithms/schedules.py
__call__
Evaluate the schedule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Nonnegative episode or step index. |
required |
Returns:
| Type | Description |
|---|---|
|
Scheduled scalar value at |
ConstantSchedule
Bases: Schedule
Return the same value for every index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Constant value returned by the schedule. |
required |
Source code in gym_classics2/algorithms/schedules.py
StepSchedule
Bases: Schedule
Switch once from a high value to a low value.
Typical use: Only when you have observed in experiments when training performance plateaus.
The schedule returns high_value while t < steps and low_value
from t == steps onward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
high_value
|
Value before the switch. |
required | |
low_value
|
Value at and after the switch. |
required | |
steps
|
Index at which to switch to |
required |
Source code in gym_classics2/algorithms/schedules.py
LinearDecaySchedule
Bases: Schedule
Interpolate linearly from an initial value to a final value.
Typical use: Transformers, NLP fine-tuning
The schedule returns initial_value at t = 0, changes linearly through
decay_steps, and returns min_value thereafter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Value at index zero. |
required | |
min_value
|
Final value and lower endpoint of a decreasing schedule. |
required | |
decay_steps
|
Number of indices over which to interpolate. |
required |
Source code in gym_classics2/algorithms/schedules.py
ExponentialDecaySchedule
Bases: Schedule
Decay geometrically to a minimum value.
Deterministic or stationary environments. Rapidly scaling down works
well with algorithms that start with no information (e.g., all Q-values are randomly initialized for tabular methods).
Do not use this for complex, multi-stage RL tasks.
At index t, the value is max(min_value, initial_value * decay_rate**t).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Unclipped value at index zero. |
required | |
min_value
|
Lower bound for the returned value. |
required | |
decay_rate
|
Multiplicative factor applied at each successive index. |
required |
Source code in gym_classics2/algorithms/schedules.py
InverseDecaySchedule
Bases: Schedule
Decay in inverse proportion to the index.
The schedule returns initial_value at t = 0. For t > 0, it
returns max(min_value, initial_value / t).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Value at index zero and numerator of the inverse schedule. |
required | |
min_value
|
Lower bound for the returned value. |
0.0
|
Source code in gym_classics2/algorithms/schedules.py
CosineAnnealingSchedule
Bases: Schedule
Anneal from an initial value to a final value along a half-cosine curve.
It starts with lots of exploration and then switches to to fine grained-exploration.
Typical use: PPO and deep RL
The schedule returns initial_value at t = 0, reaches min_value
at decay_steps, and stays there without restarting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initial_value
|
Value at index zero. |
required | |
min_value
|
Final value and lower endpoint of a decreasing schedule. |
required | |
decay_steps
|
Number of indices over which to anneal. |
required |
Source code in gym_classics2/algorithms/schedules.py
plot_schedule
Plot scheduled values for indices 0 through steps - 1.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schedule
|
Callable that accepts an integer index and returns a scalar. |
required | |
steps
|
Number of scheduled values to plot. |
1000
|