-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearFunctionApprox.py
129 lines (104 loc) · 4.17 KB
/
LinearFunctionApprox.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
import numpy as np
from easy21 import easy21
import csv
import pickle
class player:
def __init__(self, tdLambda,epsilon,stepSize):
self.actions = [0, 1]
self.weights = np.zeros((36, 1))
self.eligibilityTrace = np.zeros((36, 1))
self.game = easy21()
self.tdLambda = tdLambda
self.epsilon = epsilon
self.stepSize = stepSize
def chooseAction(self, d, p):
#exploratory Action
if np.random.uniform(0, 1) < self.epsilon:
action = np.random.choice(self.actions)
else:
#Greedy action
action = np.argmax([self.qValue(d, p, a) for a in self.actions])
return action
#Outputs value function for states
def OptimalValueFunction(self):
v = {}
for i in range(1, 11):
for j in range(1, 22):
v[i, j] = max(self.qValue(i, j, 0), self.qValue(i, j, 1))
return v
#creates Feature vector
def featureVector(self, d, p, a):
f = np.zeros((3, 6, 2))
dp = []
for index, (l, u) in enumerate(zip(range(1, 8, 3), range(4, 11, 3))):
if (l <= d <= u):
dp.append(index)
pp = []
for index, (l, u) in enumerate(zip(range(1, 17, 3), range(6, 22, 3))):
if (l <= p <= u):
pp.append(index)
for i in dp:
for j in pp:
f[i, j, a] = 1
return f.reshape(1, -1)
#Linear function approximator for state-action value
def qValue(self, d, p, a):
q = self.featureVector(d, p, a).dot(self.weights)
return q[0][0]
#Trains agent
def playLinearFunctionApprox(self, NumEpisodes, trueQ):
wins = 0
meanReturn = 0
for z in range(NumEpisodes):
terminated = False
self.eligibilityTrace = np.zeros((36, 1))
d, p = self.game.startGame()
a = self.chooseAction(d, p)
while not terminated:
p_next, d_next, r, terminated = self.game.step(p, d, a)
if not terminated:
a_next = self.chooseAction(d_next, p_next)
delta = r+self.qValue(d_next, p_next,
a_next)-self.qValue(d, p, a)
else:
delta = r-self.qValue(d, p, a)
self.eligibilityTrace = self.tdLambda*self.eligibilityTrace + \
self.featureVector(d, p, a).reshape(36, -1)
change = self.stepSize*delta*self.eligibilityTrace
self.weights += change
if not terminated:
d, p, a = d_next, p_next, a_next
meanReturn = meanReturn + 1/(z+1) * (r - meanReturn)
if r == 1:
wins += 1
if (((z) % 1000) == 0):
print("Episode %i, Mean-Return %.3f, MSE %.3f, Wins %.3f" %
(z, meanReturn, self.calculateMSE(trueQ), wins/(z+1)))
#Calculates MSE of current Q function vs some true Q function
def calculateMSE(self, trueQ):
MSE = 0
for i in range(1, 11):
for j in range(1, 22):
for k in range(0, 2):
MSE += pow(self.qValue(i, j, k) - trueQ[i, j, k], 2)
return MSE/(10*21*2)
#Creates CSV of Value Function
def outputValueCSV(self, v):
with open('valueFunction.csv', mode='w') as csv_file:
value_writer = csv.writer(
csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
value_writer.writerow(['dealer', 'player', 'value'])
for i in range(1, 11):
for j in range(1, 22):
value_writer.writerow([i, j, v[i, j]])
if __name__ == "__main__":
mse = []
with open('Q.mc', 'rb') as Qfile:
TrueQ = pickle.load(Qfile)
for i in range(0, 11):
p = player(i/10,0.05,0.01)
p.playLinearFunctionApprox(10000, TrueQ)
print("Lambda", i/10, "finished")
mse.append(p.calculateMSE(TrueQ))
print("final MSE:")
print(mse)