-
Notifications
You must be signed in to change notification settings - Fork 68
/
cartpole-hill.py
55 lines (48 loc) · 1.41 KB
/
cartpole-hill.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
counter = 0
for _ in xrange(200):
# env.render()
action = 0 if np.matmul(parameters,observation) < 0 else 1
observation, reward, done, info = env.step(action)
totalreward += reward
counter += 1
if done:
break
return totalreward
def train(submit):
env = gym.make('CartPole-v0')
if submit:
env.monitor.start('cartpole-hill/', force=True)
episodes_per_update = 5
noise_scaling = 0.1
parameters = np.random.rand(4) * 2 - 1
bestreward = 0
counter = 0
for _ in xrange(2000):
counter += 1
newparams = parameters + (np.random.rand(4) * 2 - 1)*noise_scaling
# print newparams
# reward = 0
# for _ in xrange(episodes_per_update):
# run = run_episode(env,newparams)
# reward += run
reward = run_episode(env,newparams)
# print "reward %d best %d" % (reward, bestreward)
if reward > bestreward:
# print "update"
bestreward = reward
parameters = newparams
if reward == 200:
break
if submit:
for _ in xrange(100):
run_episode(env,parameters)
env.monitor.close()
return counter
r = train(submit=False)
print r