-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain_offline.py
141 lines (102 loc) · 5.05 KB
/
train_offline.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
import time
from recoder import VideoRecorder
from logger import Logger
from replay_buffer import ReplayBuffer
from utils.train import set_seed_everywhere
from utils.environment import get_agent_types
from overcooked_ai_py.env import OverCookedEnv
from model.utils.model import *
from utils.agent import find_index
import hydra
from omegaconf import DictConfig
class Workspace(object):
def __init__(self, cfg):
self.work_dir = os.getcwd()
print(f'Workspace: {self.work_dir}')
self.cfg = cfg
self.logger = Logger(self.work_dir,
save_tb=cfg.log_save_tb,
log_frequency=cfg.log_frequency,
agent=cfg.agent.name)
set_seed_everywhere(cfg.seed)
self.device = torch.device(cfg.device)
self.discrete_action = cfg.discrete_action_space
self.env = OverCookedEnv(scenario=self.cfg.env, episode_length=self.cfg.episode_length)
self.env_agent_types = get_agent_types(self.env)
self.agent_indexes = find_index(self.env_agent_types, 'ally')
self.adversary_indexes = find_index(self.env_agent_types, 'adversary')
if self.discrete_action:
cfg.agent.params.obs_dim = self.env.observation_space.n
cfg.agent.params.action_dim = self.env.action_space.n
cfg.agent.params.action_range = list(range(cfg.agent.params.action_dim))
else:
cfg.agent.params.obs_dim = self.env.observation_space[0].shape[0]
cfg.agent.params.action_dim = self.env.action_space[0].shape[0]
cfg.agent.params.action_range = [-1, 1]
cfg.agent.params.agent_index = self.agent_indexes
cfg.agent.params.critic.input_dim = cfg.agent.params.obs_dim + cfg.agent.params.action_dim
self.agent = hydra.utils.instantiate(cfg.agent)
self.common_reward = cfg.common_reward
obs_shape = [len(self.env_agent_types), cfg.agent.params.obs_dim]
action_shape = [len(self.env_agent_types), cfg.agent.params.action_dim if not self.discrete_action else 1]
reward_shape = [len(self.env_agent_types), 1]
dones_shape = [len(self.env_agent_types), 1]
self.replay_buffer = ReplayBuffer(obs_shape=obs_shape,
action_shape=action_shape,
reward_shape=reward_shape,
dones_shape=dones_shape,
capacity=0,
device=self.device)
for dir_path in cfg.data.data_dirs:
self.replay_buffer.append_data(dir_path)
self.video_recorder = VideoRecorder(self.work_dir if cfg.save_video else None)
self.step = 0
self.estimated_step = 0
def evaluate(self):
average_episode_reward = 0
self.video_recorder.init(enabled=True)
for episode in range(self.cfg.num_eval_episodes):
obs = self.env.reset()
episode_step = 0
done = False
episode_reward = 0
while not done:
action = self.agent.act(obs, sample=False)
obs, rewards, done, info = self.env.step(action)
rewards = np.array(info['shaped_r_by_agent']).reshape(-1, 1)
self.video_recorder.record(self.env)
episode_reward += sum(rewards)[0]
episode_step += 1
average_episode_reward += episode_reward
self.video_recorder.save(f'{self.step}.mp4')
average_episode_reward /= self.cfg.num_eval_episodes
self.logger.log('eval/episode_reward', average_episode_reward, self.step)
self.logger.dump(self.step)
def run(self):
episode, episode_reward, done = 0, 0, True
start_time = time.time()
while self.step < self.cfg.num_train_steps + 1:
if done or self.step % self.cfg.eval_frequency == 0:
if self.step > 0:
self.logger.log('train/duration', time.time() - start_time, self.estimated_step)
start_time = time.time()
self.logger.dump(self.estimated_step, save=True)
if self.step > 0 and self.step % self.cfg.eval_frequency == 0:
self.logger.log('eval/episode', episode, self.estimated_step)
self.evaluate()
start_time = time.time()
self.logger.log('train/episode_reward', episode_reward, self.estimated_step)
episode_reward = 0
episode_step = 0
episode += 1
self.logger.log('train/episode', episode, self.estimated_step)
self.agent.update(self.replay_buffer, self.logger, self.estimated_step)
episode_step += 1
self.step += 1
self.estimated_step = self.step * self.agent.batch_size
@hydra.main(config_path='config', config_name='offline_rl')
def main(cfg: DictConfig) -> None:
workspace = Workspace(cfg)
workspace.run()
if __name__ == '__main__':
main()