-
Notifications
You must be signed in to change notification settings - Fork 1
/
QLearnFA.py
212 lines (180 loc) · 6.27 KB
/
QLearnFA.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
import itertools
import matplotlib
import numpy as np
import sys
import copy
import sklearn.pipeline
import sklearn.preprocessing
import random
import operator
import warnings
import os
import time
import matplotlib.pyplot as plt
from ensembleEnv import EnsembleEnv
if "../" not in sys.path:
sys.path.append("../")
from sklearn.linear_model import SGDRegressor
from sklearn.kernel_approximation import RBFSampler
warnings.filterwarnings('ignore')
if(len(sys.argv)<14):
print "Wrong No: of Input Parameters"
print "If no of arguments less than all optional arguments would be set with their default value"
print "Required format:"
print "Argument 1: Path to validation set file"
print "Argument 2: Path to predictors f1 score file"
print "Argument 3: Path to predictors prediction directory"
print "Argument 4: No: of predictors(optional = 10)"
print "Argument 5: Path to output file(optional = QLearn.txt)"
print "Argument 6: Path to step file(optional = stepQL.txt)"
print "Argument 7: Discount factor(optional = 0.9)"
print "Argument 8: Alpha(optional = .1)"
print "Argument 9: Epsilon(optional = .01)"
print "Argument 10: Epsilon Decay(optional = 1.0)"
print "Argument 11: Steps(optional = 500)"
print "Argument 12: Time File(optional = time.txt)"
print "Argument 13: Plot File(optional = plot.txt)"
print "Argument 14: output(optional = plot.txt)"
sys.exit()
else:
NoPredictors = int(sys.argv[4])
OpFile = sys.argv[5]
stepFile = sys.argv[6]
DiscountFactor = float(sys.argv[7])
Alpha = float(sys.argv[8])
Epsilon = float(sys.argv[9])
EpsilonDecay = float(sys.argv[10])
TotalSteps = int(sys.argv[11])
timeFile = sys.argv[12]
plotFile = sys.argv[13]
opDir = sys.argv[14]
ValidFile = sys.argv[1]
F1File = sys.argv[2]
PredDir = sys.argv[3]
if not os.path.exists(opDir+"/results("+str(int(Epsilon*100))+")"):
os.makedirs(opDir+"/results("+str(int(Epsilon*100))+")")
if not os.path.exists(opDir+"/results("+str(int(Epsilon*100))+")"+"/Ensemble/QLearnFA"):
os.makedirs(opDir+"/results("+str(int(Epsilon*100))+")"+"/Ensemble/QLearnFA")
if not os.path.exists(opDir+"/results("+str(int(Epsilon*100))+")"+"/EpisodePlot/QLearnFA"):
os.makedirs(opDir+"/results("+str(int(Epsilon*100))+")"+"/EpisodePlot/QLearnFA")
if not os.path.exists(opDir+"/results("+str(int(Epsilon*100))+")"+"/StepSize/QLearnFA"):
os.makedirs(opDir+"/results("+str(int(Epsilon*100))+")"+"/StepSize/QLearnFA")
if not os.path.exists(opDir+"/results("+str(int(Epsilon*100))+")"+"/Time/QLearnFA"):
os.makedirs(opDir+"/results("+str(int(Epsilon*100))+")"+"/Time/QLearnFA")
InitialState ={}
for i in range(NoPredictors):
InitialState[i] = False
env = EnsembleEnv(NoPredictors, F1File, ValidFile, PredDir)
class Estimator():
def __init__(self):
self.model = SGDRegressor(learning_rate="constant")
self.model.partial_fit((env.start()).values(),[0])
def predict(self, state, a=None):
if not a:
pred = {}
sTemp = copy.copy(state)
for i in range(len(sTemp)):
if sTemp[i] == False:
sTemp[i] = True
pred[i] = self.model.predict(sTemp.values())
sTemp[i] = False
return pred
else:
state[a] = True
return self.model.predict(s.values())
def update(self, s, a, y):
s[a] = True
self.model.partial_fit(s.values(), [y])
def finish(state, nPredictors):
for i in range(nPredictors):
if state[i] == False:
return True
return False
def makeEpsilonGreedyPolicy(estimator, epsilon, nStates):
def policyFn(observation):
A = {}
for i in range(nStates):
if observation[i] == False:
A[i] = epsilon
keys = A.keys()
nActions = len(A)
for key in keys:
A[key] /= nActions
qValues = estimator.predict(observation)
bestAction = max(qValues.iteritems(), key=operator.itemgetter(1))[0]
A[bestAction] += (1.0 - epsilon)
a1 = A.keys()
b1 = A.values()
return b1, a1
return policyFn
def qLearning(env, estimator, discount_factor=0.9, alpha=0.1, epsilon=0.1, epsilon_decay=1.0):
start = time.time()
totalPredictors = env.noBasePredictors()
counter = 0
result = []
prevPolicy = []
prevTop = 0
prevTopState = 0
noSteps = 0
Episodes = []
Rewards = []
fp = open(stepFile,"w")
for iEpisode in itertools.count():
policy = makeEpsilonGreedyPolicy(estimator, epsilon * epsilon_decay**iEpisode, totalPredictors)
state = env.start()
tReward = 0
for t in itertools.count():
noSteps = noSteps+1
if noSteps == 100000 or noSteps == 200000 or noSteps == 300000 or noSteps == 400000 :
fp.write(str(noSteps)+"\n")
actionProbs, actions = policy(state)
action = np.random.choice(actions, p=actionProbs)
nextState, reward, done, _ = env.step(action)
tReward += reward
if done:
break
qValuesNext = estimator.predict(nextState)
tdTarget = reward + discount_factor * max(qValuesNext, key=qValuesNext.get)
estimator.update(state, action, tdTarget)
state = nextState
print noSteps
tReward /= totalPredictors
Episodes.append(iEpisode)
Rewards.append(tReward)
if noSteps > TotalSteps or (time.time()-start) > 561600:
break
plt.plot(Episodes, Rewards)
plt.xlabel('Episodes')
plt.ylabel('Episode Reward')
plt.title('Episode Reward over time')
plt.savefig(plotFile)
top = 0
topState = []
currState = env.start()
step = 0
prevReward = 0
prevState = {}
prevScore = 0
while finish(currState, totalPredictors):
action = max(estimator.predict(currState).iteritems(), key=operator.itemgetter(1))[0]
temp = max(estimator.predict(currState), key=estimator.predict(currState).get)
currState, reward, _, _ = env.step(action)
if temp-prevScore>top:
top = temp-prevScore
topState = copy.copy(currState)
prevScore = temp
step = step+1
fp.write(str(noSteps)+"\n")
return topState
ftime = open(timeFile,"w")
estimator = Estimator()
start = time.time()
result = qLearning(env, estimator, DiscountFactor, Alpha, Epsilon, EpsilonDecay )
end = time.time()
ftime.write(str(end-start))
f = open(OpFile,"w")
for i in range(NoPredictors):
if result[i] == True:
f.write(str(i)+"\n")
f.close()
print "Result = "+str(result)