MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / main

Function main

rl2/mountaincar/q_learning.py:157–192  ·  view source on GitHub ↗
(show_plots=True)

Source from the content-addressed store, hash-verified

155
156
157def 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
195if __name__ == '__main__':

Callers 1

q_learning.pyFile · 0.70

Calls 5

plot_cost_to_goFunction · 0.85
FeatureTransformerClass · 0.70
ModelClass · 0.70
play_oneFunction · 0.70
plot_running_avgFunction · 0.70

Tested by

no test coverage detected