forked from liangyiting/DDPG-for-process-optimization-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgym_ddpg.py
54 lines (50 loc) · 1.58 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
#-*-coding:utf-8 -*-
import warnings
warnings.filterwarnings("ignore")
import filter_env
from ddpg import *
import gc
gc.enable()
from cobalt_simulation_2 import cobalt_removal
import pdb
#ENV_NAME = 'InvertedPendulum-v1'#倒立摆
#ENV_NAME = 'InvertedPendulum-v2'
ENV_NAME='Pendulum-v0'#'Acrobot-v1'#'MountainCar-v0'#‘CartPole_v0'#离散的
EPISODES = 10000
TEST = 5
def main():
#env = filter_env.makeFilteredEnv(gym.make(ENV_NAME))
env = filter_env.makeFilteredEnv(cobalt_removal())
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):
for t in state:
if np.isinf(t):
pdb.set_trace()
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 % 10 == 0 and episode > 5:
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()