-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
69 lines (56 loc) · 2.14 KB
/
Copy pathhelper.py
File metadata and controls
69 lines (56 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
import numpy as np
import matplotlib.pyplot as plt
import math
import time
def rolling_window(a, window, step_size):
"""Create a rolling window view of a numpy array."""
shape = a.shape[:-1] + (a.shape[-1] - window + 1 - step_size + 1, window)
strides = a.strides + (a.strides[-1] * step_size,)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
fig = None
def episode_reward_plot(rewards, frame_idx, window_size=5, step_size=1, updating=False):
"""Plot episode rewards rolling window mean, min-max range and standard deviation.
Parameters
----------
rewards : list
List of episode rewards.
frame_idx : int
Current frame index.
window_size : int
Rolling window size.
step_size: int
Step size between windows.
updating: bool
You can try to set updating to True, which hinders matplotlib to create a new window for every plot.
Doesn't work with my Pycharm SciView currently.
"""
global fig
plt.ion()
rewards_rolling = rolling_window(np.array(rewards), window_size, step_size)
mean = np.mean(rewards_rolling, axis=1)
std = np.std(rewards_rolling, axis=1)
min = np.min(rewards_rolling, axis=1)
max = np.max(rewards_rolling, axis=1)
x = np.arange(math.floor(window_size / 2), len(rewards) - math.floor(window_size / 2), step_size)
if fig is None:
fig = plt.figure()
fig.clf()
ax = fig.add_subplot(111)
ax.plot(x, mean, color='blue')
ax.fill_between(x, mean - std, mean + std, alpha=0.3, facecolor='blue')
ax.fill_between(x, min, max, alpha=0.1, facecolor='red')
ax.set_xlabel('Timestep(s)')
ax.set_ylabel('Reward')
fig.canvas.draw()
fig.canvas.flush_events()
def visualize_agent(env, agent, timesteps=500):
""" Visualize an agent performing inside a Gym environment. """
obs, _ = env.reset()
for timestep in range(1, timesteps + 1):
env.render()
action = agent.predict(obs)
obs, reward, terminated, truncated, _ = env.step(action)
if terminated or truncated:
obs, _ = env.reset()
# 30 FPS
time.sleep(0.033)