forked from johnjim0816/joyrl-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
232 lines (225 loc) · 10.3 KB
/
trainer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
# coding=utf-8
'''
Author: JiangJi
Email: [email protected]
Date: 2023-02-21 20:32:11
LastEditor: Guoshicheng
LastEditTime: 2023-04-15 21:38:14
Discription:
'''
import torch
import torch.multiprocessing as mp
import ray
from utils.utils import all_seed
class Trainer:
def __init__(self) -> None:
pass
def train_one_episode(self, env, agent, cfg):
ep_reward = 0 # reward per episode
ep_step = 0
state = env.reset() # reset and obtain initial state
for _ in range(cfg.max_steps):
ep_step += 1
action = agent.sample_action(state) # sample action
next_state, reward, terminated, truncated , info = env.step(action) # update env and return transitions under new_step_api of OpenAI Gym
agent.memory.push((state, action, reward,next_state, terminated)) # save transitions
agent.update() # update agent
state = next_state # update next state for env
ep_reward += reward #
if terminated:
break
res = {'ep_reward':ep_reward,'ep_step':ep_step}
return agent,res
def test_one_episode(self, env, agent, cfg):
ep_reward = 0 # reward per episode
ep_step = 0
state = env.reset() # reset and obtain initial state
for _ in range(cfg.max_steps):
ep_step += 1
action = agent.predict_action(state) # sample action
next_state, reward, terminated, _ , _ = env.step(action) # update env and return transitions under new_step_api of OpenAI Gym
state = next_state # update next state for env
ep_reward += reward #
if terminated:
break
res = {'ep_reward':ep_reward,'ep_step':ep_step}
return agent,res
class Worker(mp.Process):
def __init__(self,cfg,worker_id,share_agent,env,local_agent, global_ep = None,global_r_que = None,global_best_reward = None):
super(Worker,self).__init__()
self.mode = cfg.mode
self.worker_id = worker_id
self.global_ep = global_ep
self.global_r_que = global_r_que
self.global_best_reward = global_best_reward
self.share_agent = share_agent
self.local_agent = local_agent
self.env = env
self.seed = cfg.seed
self.worker_seed = cfg.seed + worker_id
self.train_eps = cfg.train_eps
self.test_eps = cfg.test_eps
self.max_steps = cfg.max_steps
self.eval_eps = cfg.eval_eps
self.model_dir = cfg.model_dir
def train(self):
while self.global_ep.value <= self.train_eps:
state = self.env.reset(seed = self.worker_seed)
ep_r = 0 # reward per episode
ep_step = 0
while True:
ep_step += 1
action = self.local_agent.sample_action(state)
next_state, reward, terminated, truncated, info = self.env.step(action)
self.local_agent.memory.push((state, action, reward,next_state, terminated))
self.local_agent.update(share_agent=self.share_agent)
state = next_state
ep_r += reward
if terminated or ep_step >= self.max_steps:
print(f"Worker {self.worker_id} finished episode {self.global_ep.value} with reward {ep_r:.3f}")
with self.global_ep.get_lock():
self.global_ep.value += 1
self.global_r_que.put(ep_r)
break
if (self.global_ep.value+1) % self.eval_eps == 0:
mean_eval_reward = self.evaluate()
if mean_eval_reward > self.global_best_reward.value:
self.global_best_reward.value = mean_eval_reward
self.share_agent.save_model(self.model_dir)
print(f"Worker {self.worker_id} saved model with current best eval reward {mean_eval_reward:.3f}")
self.global_r_que.put(None)
def test(self):
while self.global_ep.value <= self.test_eps:
state = self.env.reset(seed = self.worker_seed)
ep_r = 0 # reward per episode
ep_step = 0
while True:
ep_step += 1
action = self.local_agent.predict_action(state)
next_state, reward, terminated, truncated, info = self.env.step(action)
state = next_state
ep_r += reward
if terminated or ep_step >= self.max_steps:
print("Worker {} finished episode {} with reward {}".format(self.worker_id,self.global_ep.value,ep_r))
with self.global_ep.get_lock():
self.global_ep.value += 1
self.global_r_que.put(ep_r)
break
def evaluate(self):
sum_eval_reward = 0
for _ in range(self.eval_eps):
state = self.env.reset(seed = self.worker_seed)
ep_r = 0 # reward per episode
ep_step = 0
while True:
ep_step += 1
action = self.local_agent.predict_action(state)
next_state, reward, terminated, truncated, info = self.env.step(action)
state = next_state
ep_r += reward
if terminated or ep_step >= self.max_steps:
break
sum_eval_reward += ep_r
mean_eval_reward = sum_eval_reward / self.eval_eps
return mean_eval_reward
def run(self):
all_seed(self.seed)
print("worker {} started".format(self.worker_id))
if self.mode == 'train':
self.train()
elif self.mode == 'test':
self.test()
@ray.remote
class WorkerRay:
def __init__(self,cfg,worker_id,share_agent,env,local_agent, global_r_que, global_data = None):
self.mode = cfg.mode
self.worker_id = worker_id
self.global_data_objectRef = global_data
self.global_ep = ray.get(self.global_data_objectRef.add_read_episode.remote())
self.global_best_reward = ray.get(self.global_data_objectRef.read_best_reward.remote())
self.global_r_que = global_r_que
self.cfg = cfg
self.share_agent = share_agent
self.local_agent = local_agent
self.env = env
self.seed = cfg.seed
self.worker_seed = cfg.seed + worker_id
self.train_eps = cfg.train_eps
self.test_eps = cfg.test_eps
self.max_steps = cfg.max_steps
self.eval_eps = cfg.eval_eps
self.model_dir = cfg.model_dir
def train(self):
while self.global_ep <= (self.train_eps):
state = self.env.reset(seed = self.worker_seed)
ep_r = 0 # reward per episode
ep_step = 0
while True:
ep_step += 1
action = self.local_agent.sample_action(state)
next_state, reward, terminated, truncated, info = self.env.step(action)
self.local_agent.memory.push((state, action, reward, next_state, terminated))
# get share_agent parameters
share_agent_policy_state_dict= ray.get(self.share_agent.get_parameters.remote())
# update share_agent
share_agent_policy_state_dict = self.local_agent.update_ray(share_agent_policy_state_dict)
# return share_agent to ShareAent
ray.get(self.share_agent.receive_parameters.remote(share_agent_policy_state_dict))
state = next_state
ep_r += reward
if terminated or ep_step >= self.max_steps:
print(f"Worker {self.worker_id} finished episode {self.global_ep} with reward {ep_r:.3f}")
# record each episode and its corresponding reward in the form of a dictionary
self.global_r_que.put({self.global_ep:ep_r})
# add one to global_ep
self.global_ep = ray.get(self.global_data_objectRef.add_read_episode.remote())
break
if (self.global_ep) % self.eval_eps == 0:
mean_eval_reward = self.evaluate()
if mean_eval_reward >= ray.get(self.global_data_objectRef.read_best_reward.remote()):
ray.get(self.global_data_objectRef.set_best_reward.remote(mean_eval_reward))
ray.get(self.share_agent.save_model.remote(self.model_dir))
print(f"Worker {self.worker_id} saved model with current best eval reward {mean_eval_reward:.3f}")
def test(self):
while self.global_ep <= self.test_eps:
state = self.env.reset(seed = self.worker_seed)
ep_r = 0 # reward per episode
ep_step = 0
while True:
ep_step += 1
action = self.local_agent.predict_action(state)
next_state, reward, terminated, truncated, info = self.env.step(action)
# print('xxxxxxx')
state = next_state
ep_r += reward
if terminated or ep_step >= self.max_steps:
print("Worker {} finished episode {} with reward {}".format(self.worker_id,self.global_ep,ep_r))
self.global_r_que.put({self.global_ep:ep_r})
self.global_ep = ray.get(self.global_data_objectRef.add_read_episode.remote())
break
def evaluate(self):
sum_eval_reward = 0
for _ in range(self.eval_eps):
state = self.env.reset(seed = self.worker_seed)
ep_r = 0 # reward per episode
ep_step = 0
while True:
ep_step += 1
action = self.local_agent.predict_action(state)
next_state, reward, terminated, truncated, info = self.env.step(action)
state = next_state
ep_r += reward
if terminated or ep_step >= self.max_steps:
break
sum_eval_reward += ep_r
mean_eval_reward = sum_eval_reward / self.eval_eps
return mean_eval_reward
def run(self):
all_seed(self.seed)
print("worker {} started".format(self.worker_id))
# print(self.mode)
if self.mode == 'train':
self.train()
elif self.mode == 'test':
self.test()