MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / play_one

Function play_one

rl2/cartpole/q_learning.py:94–120  ·  view source on GitHub ↗
(env, model, eps, gamma)

Source from the content-addressed store, hash-verified

92
93
94def play_one(env, model, eps, gamma):
95 observation = env.reset()
96 done = False
97 totalreward = 0
98 iters = 0
99 while not done and iters < 2000:
100 # if we reach 2000, just quit, don't want this going forever
101 # the 200 limit seems a bit early
102 action = model.sample_action(observation, eps)
103 prev_observation = observation
104 observation, reward, done, info = env.step(action)
105
106 if done:
107 reward = -200
108
109 # update the model
110 next = model.predict(observation)
111 # print(next.shape)
112 assert(next.shape == (1, env.action_space.n))
113 G = reward + gamma*np.max(next)
114 model.update(prev_observation, action, G)
115
116 if reward == 1: # if we changed the reward to -200
117 totalreward += reward
118 iters += 1
119
120 return totalreward
121
122
123def main():

Callers 1

mainFunction · 0.70

Calls 5

resetMethod · 0.45
sample_actionMethod · 0.45
stepMethod · 0.45
predictMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected