-
Notifications
You must be signed in to change notification settings - Fork 178
/
gym_ddpg.py
executable file
·43 lines (39 loc) · 1.18 KB
/
gym_ddpg.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
import filter_env
from ddpg import *
import gc
gc.enable()
ENV_NAME = 'InvertedPendulum-v1'
EPISODES = 100000
TEST = 10
def main():
env = filter_env.makeFilteredEnv(gym.make(ENV_NAME))
agent = DDPG(env)
env.monitor.start('experiments/' + ENV_NAME,force=True)
for episode in xrange(EPISODES):
state = env.reset()
#print "episode:",episode
# Train
for step in xrange(env.spec.timestep_limit):
action = agent.noise_action(state)
next_state,reward,done,_ = env.step(action)
agent.perceive(state,action,reward,next_state,done)
state = next_state
if done:
break
# Testing:
if episode % 100 == 0 and episode > 100:
total_reward = 0
for i in xrange(TEST):
state = env.reset()
for j in xrange(env.spec.timestep_limit):
#env.render()
action = agent.action(state) # direct action for test
state,reward,done,_ = env.step(action)
total_reward += reward
if done:
break
ave_reward = total_reward/TEST
print 'episode: ',episode,'Evaluation Average Reward:',ave_reward
env.monitor.close()
if __name__ == '__main__':
main()