import os import gym import torch from tensorboardX import SummaryWriter from easydict import EasyDict from ding.config import compile_config from ding.worker import BaseLearner, SampleSerialCollector, InteractionSerialEvaluator, AdvancedReplayBuffer from ding.envs import BaseEnvManager, DingEnvWrapper 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.classic_control.cartpole.config.cartpole_dqn_config import cartpole_dqn_config # Get DI-engine form env class def wrapped_cartpole_env(): return DingEnvWrapper( gym.make('CartPole-v0'), EasyDict(env_wrapper='default'), ) # from dizoo.classic_control.cartpole.envs.cartpole_env import CartPoleEnv # return CartPoleEnv({}) def main(cfg, seed=0): cfg = compile_config( cfg, BaseEnvManager, DQNPolicy, BaseLearner, SampleSerialCollector, InteractionSerialEvaluator, AdvancedReplayBuffer, save_cfg=True ) evaluator_env_num = cfg.env.evaluator_env_num evaluator_env = BaseEnvManager(env_fn=[wrapped_cartpole_env for _ in range(evaluator_env_num)], cfg=cfg.env.manager) evaluator_env.enable_save_replay(cfg.env.replay_path) # switch save replay interface # Set random seed for all package and instance evaluator_env.seed(seed, dynamic_seed=False) set_pkg_seed(seed, use_cuda=cfg.policy.cuda) # Set up RL Policy model = DQN(**cfg.policy.model) policy = DQNPolicy(cfg.policy, model=model) policy.eval_mode.load_state_dict(torch.load(cfg.policy.load_path, map_location='cpu')) # evaluate tb_logger = SummaryWriter(os.path.join('./{}/log/'.format(cfg.exp_name), 'serial')) evaluator = InteractionSerialEvaluator( cfg.policy.eval.evaluator, evaluator_env, policy.eval_mode, tb_logger, exp_name=cfg.exp_name ) evaluator.eval() if __name__ == "__main__": main(cartpole_dqn_config)