()
| 244 | |
| 245 | |
| 246 | def main(): |
| 247 | env = gym.make('CartPole-v0') |
| 248 | D = env.observation_space.shape[0] |
| 249 | K = env.action_space.n |
| 250 | pmodel = PolicyModel(D, K, []) |
| 251 | vmodel = ValueModel(D, [10]) |
| 252 | init = tf.global_variables_initializer() |
| 253 | session = tf.InteractiveSession() |
| 254 | session.run(init) |
| 255 | pmodel.set_session(session) |
| 256 | vmodel.set_session(session) |
| 257 | gamma = 0.99 |
| 258 | |
| 259 | if 'monitor' in sys.argv: |
| 260 | filename = os.path.basename(__file__).split('.')[0] |
| 261 | monitor_dir = './' + filename + '_' + str(datetime.now()) |
| 262 | env = wrappers.Monitor(env, monitor_dir) |
| 263 | |
| 264 | N = 1000 |
| 265 | totalrewards = np.empty(N) |
| 266 | costs = np.empty(N) |
| 267 | for n in range(N): |
| 268 | totalreward = play_one_mc(env, pmodel, vmodel, gamma) |
| 269 | totalrewards[n] = totalreward |
| 270 | if n % 100 == 0: |
| 271 | print("episode:", n, "total reward:", totalreward, "avg reward (last 100):", totalrewards[max(0, n-100):(n+1)].mean()) |
| 272 | |
| 273 | print("avg reward for last 100 episodes:", totalrewards[-100:].mean()) |
| 274 | print("total steps:", totalrewards.sum()) |
| 275 | |
| 276 | plt.plot(totalrewards) |
| 277 | plt.title("Rewards") |
| 278 | plt.show() |
| 279 | |
| 280 | plot_running_avg(totalrewards) |
| 281 | |
| 282 | |
| 283 | if __name__ == '__main__': |
no test coverage detected