-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main_Easy21_Sarsa.py
219 lines (159 loc) · 6.88 KB
/
Main_Easy21_Sarsa.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
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
import utils
import logging
import environment
import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D
# The logger
utils.init_logger(logging.DEBUG, fileName="log/app.log")
logger = logging.getLogger('Easy21')
# set the random seed
random.seed(a=None, version=2)
# constants
n0 = 100
lam_range = np.arange(0, 1.1, 0.1)
nIter = 10000 # number of episodes
# define the indices of the different values
q_hit_index = 0 # q value for action hit
q_stick_index = 1 # q value for action stick
e_hit_index = 2 # eligibility trace for the hit action
e_stick_index = 3 # eligibility trace for the stick action
ns_index = 4 # number of times the state was visited
ns_hit_index = 5 # number of times the action hit was chosen in this state
ns_stick_index = 6 # number of times the action stick was chosen in this state
# initialize the value function
# x-index: dealer card -1
# y-index: player sum -1
# z all properties for this state
state_info = np.zeros((10, 21, 7))
def sarsa_episode(lam):
"""
executes one sarsa episode
:param lam: the lambda parameter
:return:
"""
# reset all eligibility traces
state_info[:, :, e_hit_index] = 0
state_info[:, :, e_stick_index] = 0
# initialize the state S
dealer_card = random.randint(1,10)
player_card = random.randint(1,10)
state = environment.State(dealer_card, player_card)
# initialize the action A
action = environment.Action.HIT
if random.random() < 0.5:
action = environment.Action.STICK
# run one episode
while not state.terminated:
# define the starting state indices for the state matrix
dealer_state_index = state.dealer_card - 1
player_state_index = state.player_sum - 1
# take the action A
state_new = environment.step(state, action)
reward = state_new.reward
# define the indices of the new state
dealer_state_index_new = state_new.dealer_card - 1
player_state_index_new = state_new.player_sum - 1
# pick the next action A' by using epsilon greedy
if state_new.terminated:
action_new = environment.Action.NONE
else:
epsilon = n0 / (n0 + state_info[dealer_state_index_new, player_state_index_new, ns_index])
if random.random() < epsilon:
# exploration, pick a random action
if random.random() < 0.5:
action_new = environment.Action.HIT
else:
action_new = environment.Action.STICK
else:
# pick the action greedily (largest action value)
if state_info[dealer_state_index_new, player_state_index_new, q_hit_index] > state_info[dealer_state_index_new, player_state_index_new, q_stick_index]:
action_new = environment.Action.HIT
else:
action_new = environment.Action.STICK
# increment the counts
state_info[dealer_state_index, player_state_index, ns_index] += 1
if action == environment.Action.HIT:
state_info[dealer_state_index, player_state_index, ns_hit_index] += 1
if action == environment.Action.STICK:
state_info[dealer_state_index, player_state_index, ns_stick_index] += 1
# calculate delta
if action == environment.Action.HIT:
qValue = state_info[dealer_state_index, player_state_index, q_hit_index]
else:
qValue = state_info[dealer_state_index, player_state_index, q_stick_index]
if state_new.terminated:
q_value_new = 0
else:
if action_new == environment.Action.HIT:
q_value_new = state_info[dealer_state_index_new, player_state_index_new, q_hit_index]
else:
q_value_new = state_info[dealer_state_index_new, player_state_index_new, q_stick_index]
delta = reward + q_value_new - qValue
# increment eligibility trace
alpha = None
if action == environment.Action.HIT:
alpha = 1 / state_info[dealer_state_index, player_state_index, ns_hit_index]
state_info[dealer_state_index, player_state_index, e_hit_index] += 1
else:
alpha = 1 / state_info[dealer_state_index, player_state_index, ns_stick_index]
state_info[dealer_state_index, player_state_index, e_stick_index] += 1
# update all values
state_info[:, :, q_hit_index] += alpha * delta * state_info[:, :, e_hit_index]
state_info[:, :, q_stick_index] += alpha * delta * state_info[:, :, e_stick_index]
# update all eligibility traces
state_info[:, :, e_hit_index] = lam * state_info[:, :, e_hit_index]
state_info[:, :, e_stick_index] = lam * state_info[:, :, e_stick_index]
# end this step
state = state_new
action = action_new
mc_q_values = np.load("action_values.npy") # load the mc results
mse_error = np.zeros((len(lam_range), nIter)) # define the error array
def calc_error():
"""
returns the mean squared error compared to the mc simulation
:return:
"""
sarsa_q_values = np.maximum(state_info[:, :, q_hit_index], state_info[:, :, q_stick_index])
mse = np.mean(np.square(sarsa_q_values - mc_q_values))
return mse
# start the sarsa control
for i in range(0, len(lam_range)):
# initialize the state info
state_info = np.zeros((10, 21, 7))
for j in range(0, nIter):
mse_error[i, j] = calc_error()
sarsa_episode(lam_range[i])
# plot the action value function
fig = plt.figure(1)
ax = fig.gca(projection='3d')
X = np.arange(1, 22, 1)
Y = np.arange(1, 11, 1)
X, Y = np.meshgrid(X, Y)
Z = np.maximum(state_info[:, :, q_hit_index], state_info[:, :, q_stick_index])
# plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=plt.get_cmap('viridis'), linewidth=0, antialiased=False)
# customize the z axis
# ax.set_zlim(-0.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
# plot the means squared error at the end of all iterations vs different lambdas
fig = plt.figure(2)
x = lam_range
y = mse_error[:, mse_error.shape[1] - 1]
plt.plot(x, y)
plt.xlabel("lambda")
plt.ylabel("MSE")
# plot the evolution of the mean squared error vs the episodes for all different lambdas
fig = plt.figure(3)
x = np.arange(nIter)
for i in range(0, mse_error.shape[0]):
plt.plot(x, mse_error[i, :], label="{:.1f}".format(lam_range[i]))
plt.legend(loc='best')
plt.xlabel("Episode")
plt.ylabel("MSE")
plt.show()