Created
December 29, 2016 13:22
-
-
Save sunskyhsh/e966aa5f09b14e897aec142a076fa396 to your computer and use it in GitHub Desktop.
qlearning algorithm to solve CartPole on openAI gym.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def qlearning(env, policy, num_iter1, alpha, gamma): | |
actions = policy.actions | |
for i in xrange(len(policy.theta)): | |
policy.theta[i] = 0.1 | |
for iter1 in xrange(num_iter1): | |
s_f = env.reset() | |
a = policy.epsilon_greedy(s_f) | |
count = 0 | |
t = False | |
while False == t and count < 10000: | |
s_f1,r,t,i = env.step(a) | |
qmax = policy.qfunc(s_f1,a) #random | |
for a1 in policy.actions: | |
pvalue = policy.qfunc(s_f1, a1) | |
if qmax < pvalue: | |
qmax = pvalue; | |
update(policy, s_f, a, r + gamma * qmax, alpha); | |
s_f = s_f1 | |
a = policy.epsilon_greedy(s_f) | |
count += 1 | |
if iter1%100 == 0: | |
print "complete the %d epoches"%(iter1) | |
return policy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on PEP8 you should avoid unnecessary whitespace.
Just a suggestion though.