-
Notifications
You must be signed in to change notification settings - Fork 1
/
variance_vs_n_steps_experiment_deterministic_gridworld.py
170 lines (141 loc) · 8.18 KB
/
variance_vs_n_steps_experiment_deterministic_gridworld.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
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
import numpy as np
from mdp_matrix import GridWorld
from n_step_sarsa import n_step_sarsa
from n_step_expected_sarsa import n_step_expected_sarsa
from q_sigma import n_step_q_sigma
from n_step_tree_backup import n_step_tree_backup
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from joblib import Parallel, delayed
test_rewards = [[i, j, -1.0] for i in range(5) for j in range(5)]
test_rewards[24] = [4, 4, 10]
terminal_states = [24]
start_state = [0, 0]
gw = GridWorld(5, test_rewards, terminal_states)
print test_rewards
average_reward_n_step_sarsa = []
all_rewards_per_episode_n_step_sarsa = []
q_var_n_step_sarsa = []
average_reward_n_step_tree_backup = []
all_rewards_per_episode_n_step_tree_backup = []
q_var_n_step_tree_backup = []
average_reward_n_step_expected_sarsa = []
all_rewards_per_episode_n_step_expected_sarsa = []
q_var_n_step_expected_sarsa = []
average_reward_qsigma = []
all_rewards_per_episode_qsigma = []
q_var_qsigma = []
epsilon = .1
max_episode=10000
# alphas = [x for x in np.arange(0.0, 1., .05)]
# alphas[0] = .01
alpha = 0.15
gamma = 0.9
n_step_values = [x for x in np.arange(0, 12, 2)]
n_step_values[0] = 1
# import pdb; pdb.set_trace()
number_of_runs = 5
for r in range(number_of_runs):
# for n_step in n_step_values:
# print(n_step)
# Q, average_reward, max_reward, all_rewards, Q_variances = double_sarsa(gw, n, epsilon=epsilon, alpha=alpha)
# average_reward_double_sarsa.append(average_reward)
# all_rewards_per_episode_double_sarsa.append(all_rewards)
# q_var_double_sarsa.append(Q_variances)
#
# Q, average_reward, max_reward, all_rewards, Q_variances = expected_sarsa(gw, n, epsilon=epsilon, alpha=alpha)
# average_reward_expected_sarsa.append(average_reward)
# q_var_expected_sarsa.append(Q_variances)
# all_rewards_per_episode_expected_sarsa.append(all_rewards)
#
# Q, average_reward, max_reward, all_rewards, Q_variances = double_expected_sarsa(gw, n, epsilon=epsilon, alpha=alpha)
# average_reward_double_expected_sarsa.append(average_reward)
# q_var_double_expected_sarsa.append(Q_variances)
# all_rewards_per_episode_double_expected_sarsa.append(all_rewards)
#
# Q, average_reward, max_reward, all_rewards, Q_variances = sarsa(gw, n, epsilon=epsilon, alpha=alpha)
# q_var_sarsa.append(Q_variances)
# average_reward_sarsa.append(average_reward)
# all_rewards_per_episode_sarsa.append(all_rewards)
n_step_sarsa_results = Parallel(n_jobs=-2, verbose=10)(delayed(n_step_sarsa)(gw, max_episode, alpha, gamma, epsilon, n) for n in n_step_values)
# Q, average_reward, max_reward, all_rewards, Q_variances = n_step_sarsa(gw, max_episode, epsilon=epsilon, alpha=alpha, n = n_step)
for result in n_step_sarsa_results:
average_reward_n_step_sarsa.append(result[1])
q_var_n_step_sarsa.append(result[4])
all_rewards_per_episode_n_step_sarsa.append(result[3])
n_step_expected_sarsa_results = Parallel(n_jobs=-2, verbose=10)(delayed(n_step_expected_sarsa)(gw, max_episode, alpha, gamma, epsilon, n) for n in n_step_values)
# Q, average_reward, max_reward, all_rewards, Q_variances = n_step_sarsa(gw, max_episode, epsilon=epsilon, alpha=alpha, n = n_step)
for result in n_step_expected_sarsa_results:
average_reward_n_step_expected_sarsa.append(result[1])
q_var_n_step_expected_sarsa.append(result[4])
all_rewards_per_episode_n_step_expected_sarsa.append(result[3])
n_step_tree_backup_results = Parallel(n_jobs=-2, verbose=10)(delayed(n_step_tree_backup)(gw, max_episode, alpha, gamma, epsilon, n) for n in n_step_values)
# Q, average_reward, max_reward, all_rewards, Q_variances = n_step_tree_backup(gw, max_episode, epsilon=epsilon, alpha=alpha, n = n_step)
for result in n_step_tree_backup_results:
average_reward_n_step_tree_backup.append(result[1])
q_var_n_step_tree_backup.append(result[4])
all_rewards_per_episode_n_step_tree_backup.append(result[3])
n_step_q_sigma_results = Parallel(n_jobs=-2, verbose=10)(delayed(n_step_q_sigma)(gw, max_episode, alpha, gamma, epsilon, n) for n in n_step_values)
# Q, average_reward, max_reward, all_rewards, Q_variances = n_step_q_sigma(gw, max_episode, epsilon=epsilon, alpha=alpha, n = n_step)
for result in n_step_q_sigma_results:
average_reward_qsigma.append(result[1])
q_var_qsigma.append(result[4])
all_rewards_per_episode_qsigma.append(result[3])
# import pdb; pdb.set_trace()
q_var_n_step_sarsa = np.mean(np.mean(np.split(np.array(q_var_n_step_sarsa), number_of_runs), axis = 0), axis=1)
q_var_n_step_expected_sarsa = np.mean(np.mean(np.split(np.array(q_var_n_step_expected_sarsa), number_of_runs), axis = 0), axis=1)
q_var_n_step_tree_backup = np.mean(np.mean(np.split(np.array(q_var_n_step_tree_backup), number_of_runs), axis = 0), axis=1)
q_var_qsigma = np.mean(np.mean(np.split(np.array(q_var_qsigma), number_of_runs), axis = 0), axis=1)
plt.plot(n_step_values, q_var_n_step_sarsa, label="n-step Sarsa")
plt.plot(n_step_values, q_var_n_step_expected_sarsa, label="n-step expected Sarsa")
plt.plot(n_step_values, q_var_n_step_tree_backup, label="n-step Tree Backup")
plt.plot(n_step_values, q_var_qsigma, label="Q-sigma")
plt.ylabel('Average Q Variance')
plt.xlabel('Number of steps n')
ax = plt.gca()
# ax.set_xscale('symlog')
ax.legend(loc='upper center', shadow=True)
plt.savefig('q_variance.png')
plt.close()
# TODO: plot all sarsa, expected_sarsa, double_Sarsa
# import pdb; pdb.set_trace()
average_reward_n_step_sarsa = np.mean(np.split(np.array(average_reward_n_step_sarsa), number_of_runs), axis=0)
average_reward_n_step_expected_sarsa = np.mean(np.split(np.array(average_reward_n_step_expected_sarsa), number_of_runs), axis=0)
average_reward_n_step_tree_backup = np.mean(np.split(np.array(average_reward_n_step_tree_backup), number_of_runs), axis=0)
average_reward_qsigma = np.mean(np.split(np.array(average_reward_qsigma), number_of_runs), axis=0)
plt.plot(n_step_values, average_reward_n_step_sarsa, label="n-step Sarsa")
plt.plot(n_step_values, average_reward_n_step_expected_sarsa, label="n-step expected Sarsa")
plt.plot(n_step_values, average_reward_n_step_tree_backup, label="n-step Tree Backup")
plt.plot(n_step_values, average_reward_qsigma, label="Q-sigma")
plt.ylabel('Average reward')
plt.xlabel('Number of steps n')
ax = plt.gca()
# ax.set_xscale('symlog')
ax.legend(loc='lower center', shadow=True)
plt.savefig('average_reward.png')
plt.close()
# import pdb; pdb.set_trace()
# print("Max alpha SARSA: %f" % alphas[np.argmax(average_reward_sarsa)])
# print("Max alpha Expected SARSA: %f" % alphas[np.argmax(average_reward_n_step_tree_backup)])
# print("Max alpha Double SARSA: %f" % alphas[np.argmax(average_reward_n_step_sarsa)])
# print("Max alpha Double Expected SARSA: %f" % alphas[np.argmax(average_reward_double_expected_sarsa)])
all_rewards_per_episode_n_step_sarsa = np.mean(np.split(np.array(all_rewards_per_episode_n_step_sarsa), number_of_runs), axis=0)
all_rewards_per_episode_n_step_expected_sarsa = np.mean(np.split(np.array(all_rewards_per_episode_n_step_expected_sarsa), number_of_runs), axis=0)
all_rewards_per_episode_n_step_tree_backup = np.mean(np.split(np.array(all_rewards_per_episode_n_step_tree_backup), number_of_runs), axis=0)
all_rewards_per_episode_qsigma = np.mean(np.split(np.array(all_rewards_per_episode_qsigma), number_of_runs), axis=0)
variance_n_step_sarsa = np.var(all_rewards_per_episode_n_step_sarsa, axis=1)
variance_n_step_expected_sarsa = np.var(all_rewards_per_episode_n_step_expected_sarsa, axis=1)
variance_qsigma = np.var(all_rewards_per_episode_qsigma, axis=1)
variance_n_step_tree_backup = np.var(all_rewards_per_episode_n_step_tree_backup, axis=1)
# import pdb; pdb.set_trace()/
plt.plot(n_step_values, variance_n_step_sarsa, label="n-step Sarsa")
plt.plot(n_step_values, variance_n_step_expected_sarsa, label="n-step expected Sarsa")
plt.plot(n_step_values, variance_n_step_tree_backup, label="n-step Tree Backup")
plt.plot(n_step_values, variance_qsigma, label="Q-sigma")
plt.ylabel('Variance in Reward')
plt.xlabel('Number of steps n')
ax = plt.gca()
# ax.set_xscale('symlog')
ax.legend(loc='upper center', shadow=True)
plt.savefig('variance_reward.png')