Utility API
Performance plots
gym_classics2.performance
simple_moving_average
Return a centered simple moving average padded with NaN values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
One-dimensional numeric sequence. |
required | |
window_size
|
Number of observations in the averaging window. |
100
|
Returns:
| Type | Description |
|---|---|
|
NumPy array with the same length as |
Source code in gym_classics2/performance.py
cum_avg
Return the cumulative average at every position in a numeric sequence.
The value at position i is the mean of data[:i + 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
One-dimensional numeric sequence. |
required |
Returns:
| Type | Description |
|---|---|
|
NumPy array containing the cumulative mean at each position. The array |
|
|
has the same length as |
Source code in gym_classics2/performance.py
plot_returns
plot_returns(returns, y_label='Episode Return', title='', window_size=100, y_range=None, log_scale=False)
Plot episode returns, their moving average, and cumulative average.
Displays the episode returns and both averages on one Matplotlib plot. The moving average is centered and padded with NaNs at its ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
One-dimensional sequence of returns, usually one value per episode. |
required | |
y_label
|
Label for the y-axis. |
'Episode Return'
|
|
title
|
Plot title. |
''
|
|
window_size
|
Number of episodes in the moving-average window. |
100
|
|
y_range
|
Optional |
None
|
|
log_scale
|
If |
False
|
Returns:
| Type | Description |
|---|---|
|
None. Displays the plot using Matplotlib. |
Source code in gym_classics2/performance.py
plot_episode_lengths
plot_episode_lengths(ep_lens, y_label='Episode Length', title='', window_size=100, y_range=None, log_scale=False)
Plot episode lengths, their moving average, and cumulative average.
Displays the episode lengths and both averages on one Matplotlib plot. The moving average is centered and padded with NaNs at its ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ep_lens
|
One-dimensional sequence of episode lengths, usually one value per episode. |
required | |
y_label
|
Label for the y-axis. |
'Episode Length'
|
|
title
|
Plot title. |
''
|
|
window_size
|
Number of episodes in the moving-average window. |
100
|
|
y_range
|
Optional |
None
|
|
log_scale
|
If |
False
|
Returns:
| Type | Description |
|---|---|
|
None. Displays the plot using Matplotlib. |
Source code in gym_classics2/performance.py
Gridworld animation
gym_classics2.animation
gridworld_animate
gridworld_animate(env, Vs, policies=None, interval=1000, repeat=False, cmap='coolwarm', clim=None, origin='lower', progress=False)
Animate a sequence of gridworld value functions and policies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Gridworld used to map state vectors to cells. |
required | |
Vs
|
Sequence of value functions, one per animation frame. |
required | |
policies
|
Optional sequence of policies aligned with |
None
|
|
interval
|
Delay between frames in milliseconds. |
1000
|
|
repeat
|
Whether to restart after the final frame. |
False
|
|
cmap
|
Matplotlib colormap name. |
'coolwarm'
|
|
clim
|
Optional |
None
|
|
origin
|
|
'lower'
|
|
progress
|
Show a progress bar when frames are rendered for display or saving. |
False
|
Returns:
| Type | Description |
|---|---|
|
Matplotlib |
Source code in gym_classics2/animation.py
14 15 16 17 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 | |
General utilities
gym_classics2.utils
get_rng
Return rng as a NumPy random generator.
rng may be a NumPy Generator, an integer seed, or
None. Passing a generator lets callers share one reproducible random
stream across an algorithm and all of its helpers.
Source code in gym_classics2/utils.py
clip
random_argmax
Argmax that breaks ties randomly. If axis is None, returns a single index.
If axis is specified, returns an array of indices along that axis. rng
may be a NumPy generator or an integer seed.