-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
140 lines (106 loc) · 4.81 KB
/
utils.py
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
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
import base64
import random
from itertools import zip_longest
import imageio
import IPython
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
import pandas as pd
import tensorflow as tf
from statsmodels.iolib.table import SimpleTable
SEED = 0 # seed for pseudo-random number generator
MINIBATCH_SIZE = 64 # mini-batch size
TAU = 1e-3 # soft update parameter
E_DECAY = 0.995 # ε decay rate for ε-greedy policy
E_MIN = 0.01 # minimum ε value for ε-greedy policy
random.seed(SEED)
def get_experiences(memory_buffer):
experiences = random.sample(memory_buffer, k=MINIBATCH_SIZE)
states = tf.convert_to_tensor(np.array([e.state for e in experiences if e is not None]),dtype=tf.float32)
actions = tf.convert_to_tensor(np.array([e.action for e in experiences if e is not None]), dtype=tf.float32)
rewards = tf.convert_to_tensor(np.array([e.reward for e in experiences if e is not None]), dtype=tf.float32)
next_states = tf.convert_to_tensor(np.array([e.next_state for e in experiences if e is not None]),dtype=tf.float32)
done_vals = tf.convert_to_tensor(np.array([e.done for e in experiences if e is not None]).astype(np.uint8),
dtype=tf.float32)
return (states, actions, rewards, next_states, done_vals)
def check_update_conditions(t, num_steps_upd, memory_buffer):
if (t + 1) % num_steps_upd == 0 and len(memory_buffer) > MINIBATCH_SIZE:
return True
else:
return False
def get_new_eps(epsilon):
return max(E_MIN, E_DECAY*epsilon)
def get_action(q_values, epsilon=0):
if np.random.rand() > epsilon:
return np.argmax(q_values.numpy()[0])
else:
return np.random.choice(np.arange(4))
def update_target_network(q_network, target_q_network):
for target_weights, q_net_weights in zip(target_q_network.weights, q_network.weights):
target_weights.assign(TAU * q_net_weights + (1.0 - TAU) * target_weights)
def plot_history(reward_history, rolling_window=20, lower_limit=None,
upper_limit=None, plot_rw=True, plot_rm=True):
if lower_limit is None or upper_limit is None:
rh = reward_history
xs = [x for x in range(len(reward_history))]
else:
rh = reward_history[lower_limit:upper_limit]
xs = [x for x in range(lower_limit,upper_limit)]
df = pd.DataFrame(rh)
rollingMean = df.rolling(rolling_window).mean()
plt.figure(figsize=(10,7), facecolor='white')
if plot_rw:
plt.plot(xs, rh, linewidth=1, color='cyan')
if plot_rm:
plt.plot(xs, rollingMean, linewidth=2, color='magenta')
text_color = 'black'
ax = plt.gca()
ax.set_facecolor('black')
plt.grid()
# plt.title("Total Point History", color=text_color, fontsize=40)
plt.xlabel('Episode', color=text_color, fontsize=30)
plt.ylabel('Total Points', color=text_color, fontsize=30)
yNumFmt = mticker.StrMethodFormatter('{x:,}')
ax.yaxis.set_major_formatter(yNumFmt)
ax.tick_params(axis='x', colors=text_color)
ax.tick_params(axis='y', colors=text_color)
plt.show()
def display_table(initial_state, action, next_state, reward, done):
action_labels = ["Do nothing", "Fire right engine", "Fire main engine", "Fire left engine"]
# Do not use column headers
column_headers = None
with np.printoptions(formatter={'float': '{:.3f}'.format}):
table_info = [("Initial State:", [f"{initial_state}"]),
("Action:", [f"{action_labels[action]}"]),
("Next State:", [f"{next_state}"]),
("Reward Received:", [f"{reward:.3f}"]),
("Episode Terminated:", [f"{done}"])]
# Generate table
row_labels, data = zip_longest(*table_info)
table = SimpleTable(data, column_headers, row_labels)
return table
def embed_mp4(filename):
"""Embeds an mp4 file in the notebook."""
video = open(filename,'rb').read()
b64 = base64.b64encode(video)
tag = '''
<video width="840" height="480" controls>
<source src="data:video/mp4;base64,{0}" type="video/mp4">
Your browser does not support the video tag.
</video>'''.format(b64.decode())
return IPython.display.HTML(tag)
def create_video(filename, env, q_network, fps=30):
with imageio.get_writer(filename, fps=fps) as video:
done = False
state = env.reset()
state = state[0]
frame = env.render()
video.append_data(frame)
while not done:
state = np.expand_dims(state, axis=0)
q_values = q_network(state)
action = np.argmax(q_values.numpy()[0])
state, reward, done, info, *rest = env.step(action)
frame = env.render()
video.append_data(frame)