-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathsac.py
395 lines (344 loc) · 16.9 KB
/
sac.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'''Soft Actor Critic (SAC)
Adapted from https://github.com/openai/spinningup/blob/master/spinup/algos/pytorch/sac/sac.py
References papers & code:
* [Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor](https://arxiv.org/pdf/1801.01290.pdf)
* [Soft Actor-Critic Algorithms and Applications](https://arxiv.org/pdf/1812.05905.pdf)
* [Soft Actor-Critic for Discrete Action Settings](https://arxiv.org/pdf/1910.07207.pdf)
* [openai spinning up - sac](https://github.com/openai/spinningup/tree/master/spinup/algos/pytorch/sac)
* [rlkit - sac](https://github.com/vitchyr/rlkit/tree/7daf34b0ef2277d545a0ee792399a2ae6c3fb6ad/rlkit/torch/sac)
* [ray rllib - sac](https://github.com/ray-project/ray/tree/master/rllib/agents/sac)
* [curl - curl_sac](https://github.com/MishaLaskin/curl/blob/master/curl_sac.py)
'''
import os
import time
from collections import defaultdict
import numpy as np
import torch
from safe_control_gym.controllers.base_controller import BaseController
from safe_control_gym.controllers.sac.sac_utils import SACAgent, SACBuffer
from safe_control_gym.envs.env_wrappers.record_episode_statistics import (RecordEpisodeStatistics,
VecRecordEpisodeStatistics)
from safe_control_gym.envs.env_wrappers.vectorized_env import make_vec_envs
from safe_control_gym.envs.env_wrappers.vectorized_env.vec_env_utils import _flatten_obs, _unflatten_obs
from safe_control_gym.math_and_models.normalization import (BaseNormalizer, MeanStdNormalizer,
RewardStdNormalizer)
from safe_control_gym.utils.logging import ExperimentLogger
from safe_control_gym.utils.utils import get_random_state, is_wrapped, set_random_state
class SAC(BaseController):
'''soft actor critic.'''
def __init__(self,
env_func,
training=True,
checkpoint_path='model_latest.pt',
output_dir='temp',
use_gpu=False,
seed=0,
**kwargs):
super().__init__(env_func, training, checkpoint_path, output_dir, use_gpu, seed, **kwargs)
# task
if self.training:
# training (+ evaluation)
self.env = make_vec_envs(env_func, None, self.rollout_batch_size, self.num_workers, seed)
self.env = VecRecordEpisodeStatistics(self.env, self.deque_size)
self.eval_env = env_func(seed=seed * 111)
self.eval_env = RecordEpisodeStatistics(self.eval_env, self.deque_size)
else:
# testing only
self.env = env_func()
self.env = RecordEpisodeStatistics(self.env)
# agent
self.agent = SACAgent(self.env.observation_space,
self.env.action_space,
hidden_dim=self.hidden_dim,
gamma=self.gamma,
tau=self.tau,
init_temperature=self.init_temperature,
use_entropy_tuning=self.use_entropy_tuning,
target_entropy=self.target_entropy,
actor_lr=self.actor_lr,
critic_lr=self.critic_lr,
entropy_lr=self.entropy_lr,
activation=self.activation)
self.agent.to(self.device)
# pre-/post-processing
self.obs_normalizer = BaseNormalizer()
if self.norm_obs:
self.obs_normalizer = MeanStdNormalizer(shape=self.env.observation_space.shape, clip=self.clip_obs, epsilon=1e-8)
self.reward_normalizer = BaseNormalizer()
if self.norm_reward:
self.reward_normalizer = RewardStdNormalizer(gamma=self.gamma, clip=self.clip_reward, epsilon=1e-8)
# logging
if self.training:
log_file_out = True
use_tensorboard = self.tensorboard
else:
# disable logging to texts and tfboard for testing
log_file_out = False
use_tensorboard = False
self.logger = ExperimentLogger(output_dir, log_file_out=log_file_out, use_tensorboard=use_tensorboard)
def reset(self):
'''Prepares for training or testing.'''
if self.training:
# set up stats tracking
self.env.add_tracker('constraint_violation', 0)
self.env.add_tracker('constraint_violation', 0, mode='queue')
self.eval_env.add_tracker('constraint_violation', 0, mode='queue')
self.eval_env.add_tracker('mse', 0, mode='queue')
self.total_steps = 0
obs, _ = self.env.reset()
self.obs = self.obs_normalizer(obs)
self.buffer = SACBuffer(self.env.observation_space, self.env.action_space, self.max_buffer_size, self.train_batch_size)
else:
# set up stats tracking
self.env.add_tracker('constraint_violation', 0, mode='queue')
self.env.add_tracker('constraint_values', 0, mode='queue')
self.env.add_tracker('mse', 0, mode='queue')
def close(self):
'''Shuts down and cleans up lingering resources.'''
self.env.close()
if self.training:
self.eval_env.close()
self.logger.close()
def save(self, path, save_buffer=False):
'''Saves model params and experiment state to checkpoint path.'''
path_dir = os.path.dirname(path)
os.makedirs(path_dir, exist_ok=True)
state_dict = {
'agent': self.agent.state_dict(),
'obs_normalizer': self.obs_normalizer.state_dict(),
'reward_normalizer': self.reward_normalizer.state_dict()
}
if self.training:
exp_state = {
'total_steps': self.total_steps,
'obs': self.obs,
'random_state': get_random_state(),
'env_random_state': self.env.get_env_random_state()
}
# latest checkpoint shoud enable save_buffer (for experiment restore),
# but intermediate checkpoint shoud not, to save storage (buffer is large)
if save_buffer:
exp_state['buffer'] = self.buffer.state_dict()
state_dict.update(exp_state)
torch.save(state_dict, path)
def load(self, path):
'''Restores model and experiment given checkpoint path.'''
state = torch.load(path)
# restore params
self.agent.load_state_dict(state['agent'])
self.obs_normalizer.load_state_dict(state['obs_normalizer'])
self.reward_normalizer.load_state_dict(state['reward_normalizer'])
# restore experiment state
if self.training:
self.total_steps = state['total_steps']
self.obs = state['obs']
set_random_state(state['random_state'])
self.env.set_env_random_state(state['env_random_state'])
if 'buffer' in state:
self.buffer.load_state_dict(state['buffer'])
self.logger.load(self.total_steps)
def learn(self, env=None, **kwargs):
'''Performs learning (pre-training, training, fine-tuning, etc).'''
if self.num_checkpoints > 0:
step_interval = np.linspace(0, self.max_env_steps, self.num_checkpoints)
interval_save = np.zeros_like(step_interval, dtype=bool)
while self.total_steps < self.max_env_steps:
results = self.train_step()
# checkpoint
if self.total_steps >= self.max_env_steps or (self.save_interval and self.total_steps % self.save_interval == 0):
# latest/final checkpoint
self.save(self.checkpoint_path, save_buffer=False)
self.logger.info(f'Checkpoint | {self.checkpoint_path}')
path = os.path.join(self.output_dir, 'checkpoints', 'model_{}.pt'.format(self.total_steps))
self.save(path)
if self.num_checkpoints > 0:
interval_id = np.argmin(np.abs(np.array(step_interval) - self.total_steps))
if interval_save[interval_id] is False:
# Intermediate checkpoint.
path = os.path.join(self.output_dir, 'checkpoints', f'model_{self.total_steps}.pt')
self.save(path, save_buffer=False)
interval_save[interval_id] = True
# eval
if self.eval_interval and self.total_steps % self.eval_interval == 0:
eval_results = self.run(env=self.eval_env, n_episodes=self.eval_batch_size)
results['eval'] = eval_results
self.logger.info('Eval | ep_lengths {:.2f} +/- {:.2f} | ep_return {:.3f} +/- {:.3f}'.format(eval_results['ep_lengths'].mean(),
eval_results['ep_lengths'].std(),
eval_results['ep_returns'].mean(),
eval_results['ep_returns'].std()))
# save best model
eval_score = eval_results['ep_returns'].mean()
eval_best_score = getattr(self, 'eval_best_score', -np.infty)
if self.eval_save_best and eval_best_score < eval_score:
self.eval_best_score = eval_score
self.save(os.path.join(self.output_dir, 'model_best.pt'), save_buffer=False)
# logging
if self.log_interval and self.total_steps % self.log_interval == 0:
self.log_step(results)
def select_action(self, obs, info=None):
'''Determine the action to take at the current timestep.
Args:
obs (ndarray): The observation at this timestep.
info (dict): The info at this timestep.
Returns:
action (ndarray): The action chosen by the controller.
'''
with torch.no_grad():
obs = torch.FloatTensor(obs).to(self.device)
action = self.agent.ac.act(obs, deterministic=True)
return action
def run(self, env=None, render=False, n_episodes=10, verbose=False, **kwargs):
'''Runs evaluation with current policy.'''
self.agent.eval()
self.obs_normalizer.set_read_only()
if env is None:
env = self.env
else:
if not is_wrapped(env, RecordEpisodeStatistics):
env = RecordEpisodeStatistics(env, n_episodes)
# Add episodic stats to be tracked.
env.add_tracker('constraint_violation', 0, mode='queue')
env.add_tracker('constraint_values', 0, mode='queue')
env.add_tracker('mse', 0, mode='queue')
obs, info = env.reset()
obs = self.obs_normalizer(obs)
ep_returns, ep_lengths = [], []
frames = []
while len(ep_returns) < n_episodes:
action = self.select_action(obs=obs, info=info)
obs, _, done, info = env.step(action)
if render:
env.render()
frames.append(env.render('rgb_array'))
if verbose:
print(f'obs {obs} | act {action}')
if done:
assert 'episode' in info
ep_returns.append(info['episode']['r'])
ep_lengths.append(info['episode']['l'])
obs, info = env.reset()
obs = self.obs_normalizer(obs)
# collect evaluation results
ep_lengths = np.asarray(ep_lengths)
ep_returns = np.asarray(ep_returns)
eval_results = {'ep_returns': ep_returns, 'ep_lengths': ep_lengths}
if len(frames) > 0:
eval_results['frames'] = frames
# Other episodic stats from evaluation env.
if len(env.queued_stats) > 0:
queued_stats = {k: np.asarray(v) for k, v in env.queued_stats.items()}
eval_results.update(queued_stats)
return eval_results
def train_step(self, **kwargs):
'''Performs a training step.'''
self.agent.train()
self.obs_normalizer.unset_read_only()
obs = self.obs
start = time.time()
if self.total_steps < self.warm_up_steps:
action = np.stack([self.env.action_space.sample() for _ in range(self.rollout_batch_size)])
else:
with torch.no_grad():
action = self.agent.ac.act(torch.FloatTensor(obs).to(self.device), deterministic=False)
next_obs, rew, done, info = self.env.step(action)
next_obs = self.obs_normalizer(next_obs)
rew = self.reward_normalizer(rew, done)
mask = 1 - np.asarray(done)
# time truncation is not true termination
terminal_idx, terminal_obs = [], []
for idx, inf in enumerate(info['n']):
if 'terminal_info' not in inf:
continue
inff = inf['terminal_info']
if 'TimeLimit.truncated' in inff and inff['TimeLimit.truncated']:
terminal_idx.append(idx)
terminal_obs.append(inf['terminal_observation'])
if len(terminal_obs) > 0:
terminal_obs = _unflatten_obs(self.obs_normalizer(_flatten_obs(terminal_obs)))
# collect the true next states and masks (accounting for time truncation)
true_next_obs = _unflatten_obs(next_obs)
true_mask = mask.copy()
for idx, term_ob in zip(terminal_idx, terminal_obs):
true_next_obs[idx] = term_ob
true_mask[idx] = 1.0
true_next_obs = _flatten_obs(true_next_obs)
self.buffer.push({
'obs': obs,
'act': action,
'rew': rew,
# 'next_obs': next_obs,
# 'mask': mask,
'next_obs': true_next_obs,
'mask': true_mask,
})
obs = next_obs
self.obs = obs
self.total_steps += self.rollout_batch_size
# learn
results = defaultdict(list)
if self.total_steps > self.warm_up_steps and not self.total_steps % self.train_interval:
# Regardless of how long you wait between updates,
# the ratio of env steps to gradient steps is locked to 1.
# alternatively, can update once each step
for _ in range(self.train_interval):
batch = self.buffer.sample(self.train_batch_size, self.device)
res = self.agent.update(batch)
for k, v in res.items():
results[k].append(v)
results = {k: sum(v) / len(v) for k, v in results.items()}
results.update({'step': self.total_steps, 'elapsed_time': time.time() - start})
return results
def log_step(self, results):
'''Does logging after a training step.'''
step = results['step']
# runner stats
self.logger.add_scalars(
{
'step': step,
'time': results['elapsed_time'],
'progress': step / self.max_env_steps,
},
step,
prefix='time')
# learning stats
if 'policy_loss' in results:
self.logger.add_scalars(
{
k: results[k]
for k in ['policy_loss', 'critic_loss', 'entropy_loss']
},
step,
prefix='loss')
# performance stats
ep_lengths = np.asarray(self.env.length_queue)
ep_returns = np.asarray(self.env.return_queue)
ep_constraint_violation = np.asarray(self.env.queued_stats['constraint_violation'])
self.logger.add_scalars(
{
'ep_length': ep_lengths.mean(),
'ep_return': ep_returns.mean(),
'ep_reward': (ep_returns / ep_lengths).mean(),
'ep_constraint_violation': ep_constraint_violation.mean()
},
step,
prefix='stat')
# total constraint violation during learning
total_violations = self.env.accumulated_stats['constraint_violation']
self.logger.add_scalars({'constraint_violation': total_violations}, step, prefix='stat')
if 'eval' in results:
eval_ep_lengths = results['eval']['ep_lengths']
eval_ep_returns = results['eval']['ep_returns']
eval_constraint_violation = results['eval']['constraint_violation']
eval_mse = results['eval']['mse']
self.logger.add_scalars(
{
'ep_length': eval_ep_lengths.mean(),
'ep_return': eval_ep_returns.mean(),
'ep_reward': (eval_ep_returns / eval_ep_lengths).mean(),
'constraint_violation': eval_constraint_violation.mean(),
'mse': eval_mse.mean()
},
step,
prefix='stat_eval')
# print summary table
self.logger.dump_scalars()