(show_plots=True)
| 155 | |
| 156 | |
| 157 | def main(show_plots=True): |
| 158 | env = gym.make('MountainCar-v0') |
| 159 | ft = FeatureTransformer(env) |
| 160 | model = Model(env, ft, "constant") |
| 161 | gamma = 0.99 |
| 162 | |
| 163 | if 'monitor' in sys.argv: |
| 164 | filename = os.path.basename(__file__).split('.')[0] |
| 165 | monitor_dir = './' + filename + '_' + str(datetime.now()) |
| 166 | env = wrappers.Monitor(env, monitor_dir) |
| 167 | |
| 168 | |
| 169 | N = 300 |
| 170 | totalrewards = np.empty(N) |
| 171 | for n in range(N): |
| 172 | eps = 1.0/(0.1*n+1) |
| 173 | # eps = 0.1*(0.97**n) |
| 174 | if n == 199: |
| 175 | print("eps:", eps) |
| 176 | # eps = 1.0/np.sqrt(n+1) |
| 177 | totalreward = play_one(model, env, eps, gamma) |
| 178 | totalrewards[n] = totalreward |
| 179 | if (n + 1) % 10 == 0: |
| 180 | print("episode:", n, "total reward:", totalreward) |
| 181 | print("avg reward for last 100 episodes:", totalrewards[-100:].mean()) |
| 182 | print("total steps:", -totalrewards.sum()) |
| 183 | |
| 184 | if show_plots: |
| 185 | plt.plot(totalrewards) |
| 186 | plt.title("Rewards") |
| 187 | plt.show() |
| 188 | |
| 189 | plot_running_avg(totalrewards) |
| 190 | |
| 191 | # plot the optimal state-value function |
| 192 | plot_cost_to_go(env, model) |
| 193 | |
| 194 | |
| 195 | if __name__ == '__main__': |
no test coverage detected