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

Function play_one

rl2/cartpole/td_lambda.py:85–118  ·  view source on GitHub ↗
(model, env, eps, gamma, lambda_)

Source from the content-addressed store, hash-verified

83
84# returns a list of states_and_rewards, and the total reward
85def play_one(model, env, eps, gamma, lambda_):
86 observation = env.reset()
87 done = False
88 totalreward = 0
89 states_actions_rewards = []
90 iters = 0
91 model.reset()
92 while not done and iters < 1000000:
93 action = model.sample_action(observation, eps)
94 prev_observation = observation
95 observation, reward, done, info = env.step(action)
96
97 if done:
98 reward = -300
99
100 # update the model
101 next = model.predict(observation)
102 assert(next.shape == (1, env.action_space.n))
103 G = reward + gamma*np.max(next[0])
104 model.update(prev_observation, action, G, gamma, lambda_)
105
106 states_actions_rewards.append((prev_observation, action, reward))
107
108 if reward == 1: # if we changed the reward to -200
109 totalreward += reward
110
111 iters += 1
112
113 # if iters > 0 and iters % 1000 == 0:
114 # print(iters)
115 # if done:
116 # print "finished in < 1000 steps!"
117
118 return states_actions_rewards, totalreward
119
120
121if __name__ == '__main__':

Callers 1

td_lambda.pyFile · 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