-
Notifications
You must be signed in to change notification settings - Fork 0
/
gym_ddpg.py
executable file
·57 lines (50 loc) · 1.69 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import filter_env
from ddpg import *
import gc
gc.enable()
import time
ENV_NAME = 'gym_finger:Finger-v0'
EPISODES = 100000
TEST = 10
def main():
env = gym.make(ENV_NAME)#filter_env.makeFilteredEnv(gym.make(ENV_NAME))
# env.setRealTimeSimulation()
agent = DDPG(env)
timestep_limit = 300
# env.monitor.start('experiments/' + ENV_NAME,force=True)
for episode in xrange(EPISODES):
state = env.reset()
print("********RESET*********")
print("episode:",episode)
# Train
for step in xrange(timestep_limit):
action = agent.noise_action(state)
next_state,reward,done,_ = env.step(action)
print(reward, action)
agent.perceive(state,action,reward,next_state,done)
state = next_state
env.render()
if done:
break
# Testing:
if episode % 20 == 0 and episode > 0:
print("# # # # TEST # # # #")
total_reward = 0
for i in xrange(TEST):
state = env.reset()
for j in xrange(timestep_limit):
action = agent.action(state) # direct action for test
state,reward,done,_ = env.step(action)
total_reward += reward
env.render()
if done:
break
ave_reward = total_reward/TEST
print ('episode: ',episode,'Evaluation Average Reward:',ave_reward)
print("# # # # # # # # # # #")
# Saving the model
if episode % 20 == 0 and episode > 0:
agent.save(episode)
# env.monitor.close()
if __name__ == '__main__':
main()