code: def test_sampling():
      memory = SequentialMemory(
          100, window_length=2, ignore_episode_boundaries=False
      )
      obs_size = (3, 4)
      actions = range(5)
  
      obs0 = np.random.random(obs_size)
      terminal0 = False
      action0 = np.random.choice(actions)
      reward0 = np.random.random()
  
      obs1 = np.random.random(obs_size)
      terminal1 = False
      action1 = np.random.choice(actions)
      reward1 = np.random.random()
  
      obs2 = np.random.random(obs_size)
      terminal2 = False
      action2 = np.random.choice(actions)
      reward2 = np.random.random()
  
      obs3 = np.random.random(obs_size)
      terminal3 = True
      action3 = np.random.choice(actions)
      reward3 = np.random.random()
  
      obs4 = np.random.random(obs_size)
      terminal4 = False
      action4 = np.random.choice(actions)
      reward4 = np.random.random()
  
      obs5 = np.random.random(obs_size)
      terminal5 = False
      action5 = np.random.choice(actions)
      reward5 = np.random.random()
  
      obs6 = np.random.random(obs_size)
      terminal6 = False
      action6 = np.random.choice(actions)
      reward6 = np.random.random()
  
      # memory.append takes the current observation, the reward after taking an action and if
      # the *new* observation is terminal, thus `obs0` and `terminal1` is correct.
      memory.append(obs0, action0, reward0, terminal1)
      memory.append(obs1, action1, reward1, terminal2)
      memory.append(obs2, action2, reward2, terminal3)
      memory.append(obs3, action3, reward3, terminal4)
      memory.append(obs4, action4, reward4, terminal5)
      memory.append(obs5, action5, reward5, terminal6)
      assert memory.nb_entries == 6
  
      experiences = memory.sample(batch_size=3, batch_idxs=[2, 3, 4])
      assert len(experiences) == 3
  
      assert_allclose(experiences[0].state0, np.array([obs1, obs2]))
      assert_allclose(experiences[0].state1, np.array([obs2, obs3]))
      assert experiences[0].action == action2
      assert experiences[0].reward == reward2
      assert experiences[0].terminal1 is True
  
      # Next experience has been re-sampled since since state0 would be terminal in which case we
      # cannot really have a meaningful transition because the environment gets reset. We thus
      # just ensure that state0 is not terminal.
      assert not np.all(experiences[1].state0 == np.array([obs2, obs3]))
  
      assert_allclose(
          experiences[2].state0, np.array([np.zeros(obs_size), obs4])
      )
      assert_allclose(experiences[2].state1, np.array([obs4, obs5]))
      assert experiences[2].action == action4
      assert experiences[2].reward == reward4
      assert experiences[2].terminal1 is False
  
file_path: keras-rl/tests/rl/test_memory.py
imports: from __future__ import division
  import pytest
  import numpy
  from numpy.testing import assert_allclose
  from rl.memory import SequentialMemory, RingBuffer
label_desc: Use image node_id 5 for calling a global function with example usage: test_sampling() without return types