|
import os |
|
import torch |
|
from tensorboardX import SummaryWriter |
|
from ding.config import compile_config |
|
from ding.worker import BaseLearner, SampleSerialCollector, InteractionSerialEvaluator, AdvancedReplayBuffer |
|
from ding.policy import FQFPolicy |
|
from ding.model import FQF |
|
from ding.utils import set_pkg_seed |
|
from ding.rl_utils import get_epsilon_greedy_fn |
|
from dizoo.atari.config.serial.phoenix.phoenix_fqf_config import phoenix_fqf_config, create_config |
|
from ding.utils import DistContext |
|
from functools import partial |
|
from ding.envs import get_vec_env_setting, create_env_manager |
|
|
|
|
|
def main(cfg, create_cfg, seed=0): |
|
|
|
cfg = compile_config(cfg, seed=seed, env=None, auto=True, create_cfg=create_cfg, save_cfg=True) |
|
|
|
|
|
env_fn, collector_env_cfg, evaluator_env_cfg = get_vec_env_setting(cfg.env) |
|
collector_env = create_env_manager(cfg.env.manager, [partial(env_fn, cfg=c) for c in collector_env_cfg]) |
|
evaluator_env = create_env_manager(cfg.env.manager, [partial(env_fn, cfg=c) for c in evaluator_env_cfg]) |
|
|
|
|
|
collector_env.seed(seed) |
|
evaluator_env.seed(seed, dynamic_seed=False) |
|
set_pkg_seed(seed, use_cuda=cfg.policy.cuda) |
|
|
|
|
|
model = FQF(**cfg.policy.model) |
|
policy = FQFPolicy(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) |
|
|
|
|
|
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): |
|
train_data = replay_buffer.sample(learner.policy.get_attribute('batch_size'), learner.train_iter) |
|
if train_data is None: |
|
break |
|
learner.train(train_data, collector.envstep) |
|
|
|
if collector.envstep >= int(1e7): |
|
break |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
main(phoenix_fqf_config, create_config) |
|
|