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

Function play_one_td

rl2/cartpole/pg_tf.py:162–189  ·  view source on GitHub ↗
(env, pmodel, vmodel, gamma)

Source from the content-addressed store, hash-verified

160
161
162def play_one_td(env, pmodel, vmodel, gamma):
163 observation = env.reset()
164 done = False
165 totalreward = 0
166 iters = 0
167
168 while not done and iters < 2000:
169 # if we reach 2000, just quit, don't want this going forever
170 # the 200 limit seems a bit early
171 action = pmodel.sample_action(observation)
172 prev_observation = observation
173 observation, reward, done, info = env.step(action)
174
175 # if done:
176 # reward = -200
177
178 # update the models
179 V_next = vmodel.predict(observation)[0]
180 G = reward + gamma*V_next
181 advantage = G - vmodel.predict(prev_observation)
182 pmodel.partial_fit(prev_observation, action, advantage)
183 vmodel.partial_fit(prev_observation, G)
184
185 if reward == 1: # if we changed the reward to -200
186 totalreward += reward
187 iters += 1
188
189 return totalreward
190
191
192

Callers

nothing calls this directly

Calls 5

resetMethod · 0.45
sample_actionMethod · 0.45
stepMethod · 0.45
predictMethod · 0.45
partial_fitMethod · 0.45

Tested by

no test coverage detected