code: def test_record_and_pretrain(self):
      self.start_tests(name="record-and-pretrain")
  
      with TemporaryDirectory() as directory:
          # ====================
  
          # Start recording traces after 80 episodes -- by then, the environment is solved
          runner = Runner(
              agent=dict(
                  agent="benchmarks/configs/ppo.json",
                  recorder=dict(directory=directory, start=8),
              ),
              environment="benchmarks/configs/cartpole.json",
          )
          runner.run(num_episodes=10)
          runner.close()
  
          # ====================
  
          # Trivial custom act function
          def fn_act(states):
              return int(states[2] < 0.0)
  
          # Record 20 episodes
          runner = Runner(
              agent=dict(
                  agent=fn_act, recorder=dict(directory=directory)
              ),
              environment="benchmarks/configs/cartpole.json",
          )
          # or: agent = Agent.create(agent=fn_act, recorder=dict(directory=directory))
          runner.run(num_episodes=2)
          runner.close()
  
          # ====================
  
          # Start recording traces after 80 episodes -- by then, the environment is solved
          environment = Environment.create(
              environment="benchmarks/configs/cartpole.json"
          )
          agent = Agent.create(
              agent="benchmarks/configs/ppo.json",
              environment=environment,
          )
          runner = Runner(agent=agent, environment=environment)
          runner.run(num_episodes=8)
          runner.close()
  
          # Record 20 episodes
          for episode in range(2, 4):
              # Record episode experience
              episode_states = list()
              episode_actions = list()
              episode_terminal = list()
              episode_reward = list()
  
              # Evaluation episode
              states = environment.reset()
              terminal = False
              while not terminal:
                  episode_states.append(states)
                  actions = agent.act(
                      states=states,
                      independent=True,
                      deterministic=True,
                  )
                  episode_actions.append(actions)
                  states, terminal, reward = environment.execute(
                      actions=actions
                  )
                  episode_terminal.append(terminal)
                  episode_reward.append(reward)
  
              # Write recorded episode trace to npz file
              np.savez_compressed(
                  file=os.path.join(
                      directory, "trace-{:09d}.npz".format(episode)
                  ),
                  states=np.stack(episode_states, axis=0),
                  actions=np.stack(episode_actions, axis=0),
                  terminal=np.stack(episode_terminal, axis=0),
                  reward=np.stack(episode_reward, axis=0),
              )
  
          # ====================
  
          # Pretrain a new agent on the recorded traces: for 30 iterations, feed the
          # experience of one episode to the agent and subsequently perform one update
          environment = Environment.create(
              environment="benchmarks/configs/cartpole.json"
          )
          agent = Agent.create(
              agent="benchmarks/configs/ppo.json",
              environment=environment,
          )
          agent.pretrain(
              directory=directory,
              num_iterations=30,
              num_traces=1,
              num_updates=1,
          )
  
          # Evaluate the pretrained agent
          runner = Runner(agent=agent, environment=environment)
          runner.run(num_episodes=10, evaluation=True)
          runner.close()
  
          # Close agent and environment
          agent.close()
          environment.close()
  
          # ====================
  
          # Performance test
          environment = Environment.create(
              environment="benchmarks/configs/cartpole.json"
          )
          agent = Agent.create(
              agent="benchmarks/configs/ppo.json",
              environment=environment,
          )
          agent.pretrain(
              directory="test/data/ppo-traces",
              num_iterations=30,
              num_traces=1,
              num_updates=1,
          )
          runner = Runner(agent=agent, environment=environment)
          runner.run(num_episodes=10, evaluation=True)
          self.assertTrue(
              all(
                  episode_return == 500.0
                  for episode_return in runner.episode_returns
              )
          )
          runner.close()
          agent.close()
          environment.close()
  
          files = sorted(os.listdir(path=directory))
          self.assertEqual(len(files), 6)
          self.assertTrue(
              all(
                  file.startswith("trace-")
                  and file.endswith("0000000{}.npz".format(n))
                  for n, file in zip([0, 1, 2, 3, 8, 9], files)
              )
          )
  
      self.finished_test()
  
imports: import os
  from tempfile import TemporaryDirectory
  from threading import Thread
  import unittest
  import numpy
  import tensorflow
  from tensorforce import Agent, Environment, Runner
  from test.unittest_base import UnittestBase
label_desc: Use image node_id 9 for calling the TestExamples obj's underlying member method code with example usage: obj.test_record_and_pretrain() and returns: int