-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcartpole-random.py
55 lines (46 loc) · 1.34 KB
/
cartpole-random.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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