|
import os |
|
from easydict import EasyDict |
|
from tensorboardX import SummaryWriter |
|
|
|
from ding.config import compile_config |
|
from ding.worker import BaseLearner, SampleSerialCollector, InteractionSerialEvaluator, AdvancedReplayBuffer |
|
from ding.envs.env_manager.envpool_env_manager import PoolEnvManager |
|
from ding.policy import DQNPolicy |
|
from ding.model import DQN |
|
from ding.utils import set_pkg_seed |
|
from ding.rl_utils import get_epsilon_greedy_fn |
|
from dizoo.atari.config.serial import pong_dqn_envpool_config |
|
|
|
|
|
def main(cfg, seed=0, max_iterations=int(1e10)): |
|
cfg.exp_name = 'atari_dqn_envpool' |
|
cfg = compile_config( |
|
cfg, |
|
PoolEnvManager, |
|
DQNPolicy, |
|
BaseLearner, |
|
SampleSerialCollector, |
|
InteractionSerialEvaluator, |
|
AdvancedReplayBuffer, |
|
save_cfg=True |
|
) |
|
collector_env_cfg = EasyDict( |
|
{ |
|
'env_id': cfg.env.env_id, |
|
'env_num': cfg.env.collector_env_num, |
|
'batch_size': cfg.env.collector_batch_size, |
|
|
|
'episodic_life': True, |
|
'reward_clip': True, |
|
'gray_scale': cfg.env.get('gray_scale', True), |
|
'stack_num': cfg.env.get('stack_num', 4), |
|
'frame_skip': cfg.env.get('frame_skip', 4), |
|
} |
|
) |
|
collector_env = PoolEnvManager(collector_env_cfg) |
|
evaluator_env_cfg = EasyDict( |
|
{ |
|
'env_id': cfg.env.env_id, |
|
'env_num': cfg.env.evaluator_env_num, |
|
'batch_size': cfg.env.evaluator_batch_size, |
|
|
|
'episodic_life': False, |
|
'reward_clip': False, |
|
'gray_scale': cfg.env.get('gray_scale', True), |
|
'stack_num': cfg.env.get('stack_num', 4), |
|
'frame_skip': cfg.env.get('frame_skip', 4), |
|
} |
|
) |
|
evaluator_env = PoolEnvManager(evaluator_env_cfg) |
|
collector_env.seed(seed) |
|
evaluator_env.seed(seed) |
|
set_pkg_seed(seed, use_cuda=cfg.policy.cuda) |
|
|
|
model = DQN(**cfg.policy.model) |
|
policy = DQNPolicy(cfg.policy, model=model) |
|
tb_logger = SummaryWriter(os.path.join('./{}/log/'.format(cfg.exp_name), 'serial')) |
|
learner = BaseLearner(cfg.policy.learn.learner, policy.learn_mode, tb_logger, exp_name=cfg.exp_name) |
|
collector = SampleSerialCollector( |
|
cfg.policy.collect.collector, collector_env, policy.collect_mode, tb_logger, exp_name=cfg.exp_name |
|
) |
|
evaluator = InteractionSerialEvaluator( |
|
cfg.policy.eval.evaluator, evaluator_env, policy.eval_mode, tb_logger, exp_name=cfg.exp_name |
|
) |
|
replay_buffer = AdvancedReplayBuffer( |
|
cfg.policy.other.replay_buffer, tb_logger, exp_name=cfg.exp_name, instance_name='replay_buffer' |
|
) |
|
eps_cfg = cfg.policy.other.eps |
|
epsilon_greedy = get_epsilon_greedy_fn(eps_cfg.start, eps_cfg.end, eps_cfg.decay, eps_cfg.type) |
|
|
|
while True: |
|
if evaluator.should_eval(learner.train_iter): |
|
stop, reward = evaluator.eval(learner.save_checkpoint, learner.train_iter, collector.envstep) |
|
if stop: |
|
break |
|
eps = epsilon_greedy(collector.envstep) |
|
new_data = collector.collect(train_iter=learner.train_iter, policy_kwargs={'eps': eps}) |
|
replay_buffer.push(new_data, cur_collector_envstep=collector.envstep) |
|
for i in range(cfg.policy.learn.update_per_collect): |
|
batch_size = learner.policy.get_attribute('batch_size') |
|
train_data = replay_buffer.sample(batch_size, learner.train_iter) |
|
if train_data is not None: |
|
learner.train(train_data, collector.envstep) |
|
|
|
|
|
if __name__ == "__main__": |
|
main(EasyDict(pong_dqn_envpool_config)) |
|
|