Permalink
Switch branches/tags
Nothing to show
Find file Copy path
dc7f8a0 Jul 2, 2016
1 contributor

Users who have contributed to this file

56 lines (46 sloc) 1.34 KB
import gym
import numpy as np
import matplotlib.pyplot as plt
def run_episode(env, parameters):
observation = env.reset()
totalreward = 0
for _ in xrange(200):
action = 0 if np.matmul(parameters,observation) < 0 else 1
observation, reward, done, info = env.step(action)
totalreward += reward
if done:
break
return totalreward
def train(submit):
env = gym.make('CartPole-v0')
if submit:
env.monitor.start('cartpole-experiments/', force=True)
counter = 0
bestparams = None
bestreward = 0
for _ in xrange(10000):
counter += 1
parameters = np.random.rand(4) * 2 - 1
reward = run_episode(env,parameters)
if reward > bestreward:
bestreward = reward
bestparams = parameters
if reward == 200:
break
if submit:
for _ in xrange(100):
run_episode(env,bestparams)
env.monitor.close()
return counter
# train an agent to submit to openai gym
# train(submit=True)
# create graphs
results = []
for _ in xrange(1000):
results.append(train(submit=False))
plt.hist(results,50,normed=1, facecolor='g', alpha=0.75)
plt.xlabel('Episodes required to reach 200')
plt.ylabel('Frequency')
plt.title('Histogram of Random Search')
plt.show()
print np.sum(results) / 1000.0