docstr: Tests, whether a Learner's state is properly saved and restored.
docstr_tok: ["Tests",",","whether","a","Learner","'s","state","is","properly","saved","and","restored","."]
code: def test_save_load_state(self):
      """Tests, whether a Learner's state is properly saved and restored."""
      config = BaseTestingAlgorithmConfig()
  
      for fw in framework_iterator(config, frameworks=("torch", "tf2")):
          # Get a Learner instance for the framework and env.
          learner1 = config.build_learner(env=self.ENV)
          with tempfile.TemporaryDirectory() as tmpdir:
              learner1.save_state(tmpdir)
  
              learner2 = config.build_learner(env=self.ENV)
              learner2.load_state(tmpdir)
              self._check_learner_states(fw, learner1, learner2)
  
          # Add a module then save/load and check states.
          with tempfile.TemporaryDirectory() as tmpdir:
              rl_module_spec = config.get_default_rl_module_spec()
              rl_module_spec.observation_space = (
                  self.ENV.observation_space
              )
              rl_module_spec.action_space = self.ENV.action_space
              learner1.add_module(
                  module_id="test",
                  module_spec=rl_module_spec,
              )
              learner1.save_state(tmpdir)
              learner2.load_state(tmpdir)
              self._check_learner_states(fw, learner1, learner2)
  
          # Remove a module then save/load and check states.
          with tempfile.TemporaryDirectory() as tmpdir:
              learner1.remove_module(module_id=DEFAULT_POLICY_ID)
              learner1.save_state(tmpdir)
              learner2.load_state(tmpdir)
              self._check_learner_states(fw, learner1, learner2)
  
imports: import gymnasium
  import numpy
  import tempfile
  import unittest
  import ray
  from ray.rllib.core.learner.learner import Learner
  from ray.rllib.core.testing.testing_learner import BaseTestingAlgorithmConfig
  from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
  from ray.rllib.utils.numpy import convert_to_numpy
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
  from ray.rllib.utils.test_utils import check, framework_iterator, get_cartpole_dataset_reader
  from ray.rllib.utils.metrics import ALL_MODULES
label_desc: Use image node_id 8 for calling the TestLearner obj's underlying member method code with example usage: obj.test_save_load_state() without return types