| 99 | |
| 100 | # returns a list of states_and_rewards, and the total reward |
| 101 | def play_one(model, env, eps, gamma): |
| 102 | observation = env.reset()[0] |
| 103 | done = False |
| 104 | totalreward = 0 |
| 105 | iters = 0 |
| 106 | while not done and iters < 10000: |
| 107 | action = model.sample_action(observation, eps) |
| 108 | prev_observation = observation |
| 109 | observation, reward, done, truncated, info = env.step(action) |
| 110 | |
| 111 | # update the model |
| 112 | if done: |
| 113 | G = reward |
| 114 | else: |
| 115 | Qnext = model.predict(observation) |
| 116 | # assert(next.shape == (1, env.action_space.n)) |
| 117 | G = reward + gamma*np.max(Qnext[0]) |
| 118 | |
| 119 | model.update(prev_observation, action, G) |
| 120 | |
| 121 | totalreward += reward |
| 122 | iters += 1 |
| 123 | |
| 124 | return totalreward |
| 125 | |
| 126 | |
| 127 | def plot_cost_to_go(env, estimator, num_tiles=20): |