docstr: Test DQN with CartPole environment.
docstr_tok: ["Test","DQN","with","CartPole","environment","."]
code: def test_dqn_cartpole_pickle(self):
      """Test DQN with CartPole environment."""
      deterministic.set_seed(100)
      with TFTrainer(snapshot_config, sess=self.sess) as trainer:
          n_epochs = 10
          steps_per_epoch = 10
          sampler_batch_size = 500
          num_timesteps = (
              n_epochs * steps_per_epoch * sampler_batch_size
          )
          env = GymEnv("CartPole-v0")
          replay_buffer = PathBuffer(capacity_in_transitions=int(1e4))
          qf = DiscreteMLPQFunction(
              env_spec=env.spec, hidden_sizes=(64, 64)
          )
          policy = DiscreteQFArgmaxPolicy(env_spec=env.spec, qf=qf)
          epilson_greedy_policy = EpsilonGreedyPolicy(
              env_spec=env.spec,
              policy=policy,
              total_timesteps=num_timesteps,
              max_epsilon=1.0,
              min_epsilon=0.02,
              decay_ratio=0.1,
          )
          sampler = LocalSampler(
              agents=epilson_greedy_policy,
              envs=env,
              max_episode_length=env.spec.max_episode_length,
              is_tf_worker=True,
              worker_class=FragmentWorker,
          )
          algo = DQN(
              env_spec=env.spec,
              policy=policy,
              qf=qf,
              exploration_policy=epilson_greedy_policy,
              replay_buffer=replay_buffer,
              sampler=sampler,
              qf_lr=1e-4,
              discount=1.0,
              min_buffer_size=int(1e3),
              double_q=False,
              n_train_steps=500,
              grad_norm_clipping=5.0,
              steps_per_epoch=steps_per_epoch,
              target_network_update_freq=1,
              buffer_batch_size=32,
          )
          trainer.setup(algo, env)
          with tf.compat.v1.variable_scope(
              "DiscreteMLPQFunction/mlp/hidden_0", reuse=True
          ):
              bias = tf.compat.v1.get_variable("bias")
              # assign it to all one
              old_bias = tf.ones_like(bias).eval()
              bias.load(old_bias)
              h = pickle.dumps(algo)
  
          with tf.compat.v1.Session(graph=tf.Graph()):
              pickle.loads(h)
              with tf.compat.v1.variable_scope(
                  "DiscreteMLPQFunction/mlp/hidden_0", reuse=True
              ):
                  new_bias = tf.compat.v1.get_variable("bias")
                  new_bias = new_bias.eval()
                  assert np.array_equal(old_bias, new_bias)
  
          env.close()
  
file_path: garage/tests/garage/tf/algos/test_dqn.py
imports: import pickle
  import numpy
  import pytest
  import tensorflow
  from garage.envs import GymEnv
  from garage.experiment import deterministic
  from garage.np.exploration_policies import EpsilonGreedyPolicy
  from garage.replay_buffer import PathBuffer
  from garage.sampler import FragmentWorker, LocalSampler
  from garage.tf.algos import DQN
  from garage.tf.policies import DiscreteQFArgmaxPolicy
  from garage.tf.q_functions import DiscreteMLPQFunction
  from garage.trainer import TFTrainer
  from tests.fixtures import snapshot_config, TfGraphTestCase
label_desc: Use image node_id 4 for calling the TestDQN obj's underlying member method code with example usage: obj.test_dqn_cartpole_pickle() without return types