class_docstr: Base class for memories organized as a queue / circular buffer.
  
  Args:
      capacity (int > 0): Memory capacity
          (<span style="color:#00C000"><b>default</b></span>: minimum capacity).
      device (string): Device name
          (<span style="color:#00C000"><b>default</b></span>: CPU:0).
      name (string): <span style="color:#0000C0"><b>internal use</b></span>.
      values_spec (specification): <span style="color:#0000C0"><b>internal use</b></span>.
      min_capacity (int >= 0): <span style="color:#0000C0"><b>internal use</b></span>.
class_docstr_tok: ["Base","class","for","memories","organized","as","a","queue","\/","circular","buffer",".","Args",":","capacity","(","int",">","0",")",":","Memory","capacity","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","minimum","capacity",")",".","device","(","string",")",":","Device","name","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","CPU:0",")",".","name","(","string",")",":","<","span","style=","''","color",":","#","0000C0","''",">","<","b",">","internal","use","<","\/b",">","<","\/span",">",".","values_spec","(","specification",")",":","<","span","style=","''","color",":","#","0000C0","''",">","<","b",">","internal","use","<","\/b",">","<","\/span",">",".","min_capacity","(","int",">","=","0",")",":","<","span","style=","''","color",":","#","0000C0","''",">","<","b",">","internal","use","<","\/b",">","<","\/span",">","."]
code: def predecessors(
      self, *, indices, horizon, sequence_values, initial_values
  ):
      assert isinstance(sequence_values, tuple)
      assert isinstance(initial_values, tuple)
  
      zero = tf_util.constant(value=0, dtype="int")
      one = tf_util.constant(value=1, dtype="int")
      capacity = tf_util.constant(value=self.capacity, dtype="int")
  
      def body(lengths, predecessor_indices, mask):
          previous_index = tf.math.mod(
              x=(predecessor_indices[:, :1] - one), y=capacity
          )
          predecessor_indices = tf.concat(
              values=(previous_index, predecessor_indices), axis=1
          )
          previous_terminal = tf.gather(
              params=self.buffers["terminal"], indices=previous_index
          )
          is_not_terminal = tf.math.logical_and(
              x=tf.math.logical_not(
                  x=tf.math.greater(x=previous_terminal, y=zero)
              ),
              y=mask[:, :1],
          )
          mask = tf.concat(values=(is_not_terminal, mask), axis=1)
          is_not_terminal = tf.squeeze(input=is_not_terminal, axis=1)
          zeros = tf.zeros_like(
              input=is_not_terminal, dtype=tf_util.get_dtype(type="int")
          )
          ones = tf.ones_like(
              input=is_not_terminal, dtype=tf_util.get_dtype(type="int")
          )
          lengths += tf.where(
              condition=is_not_terminal, x=ones, y=zeros
          )
          return lengths, predecessor_indices, mask
  
      lengths = tf.ones_like(
          input=indices, dtype=tf_util.get_dtype(type="int")
      )
      predecessor_indices = tf.math.mod(
          x=tf.expand_dims(input=indices, axis=1), y=capacity
      )
      mask = tf.ones_like(
          input=predecessor_indices,
          dtype=tf_util.get_dtype(type="bool"),
      )
      shape = tf.TensorShape(dims=((None, None)))
  
      lengths, predecessor_indices, mask = tf.while_loop(
          cond=tf_util.always_true,
          body=body,
          loop_vars=(lengths, predecessor_indices, mask),
          shape_invariants=(lengths.get_shape(), shape, shape),
          maximum_iterations=tf_util.int32(x=horizon),
      )
  
      predecessor_indices = tf.reshape(
          tensor=predecessor_indices, shape=(-1,)
      )
      mask = tf.reshape(tensor=mask, shape=(-1,))
      predecessor_indices = tf.boolean_mask(
          tensor=predecessor_indices, mask=mask, axis=0
      )
  
      assertions = list()
      if self.config.create_tf_assertions:
          assertions.append(
              tf.debugging.assert_greater_equal(
                  x=tf.math.mod(
                      x=(predecessor_indices - self.buffer_index),
                      y=capacity,
                  ),
                  y=zero,
                  message="Predecessor check.",
              )
          )
  
      with tf.control_dependencies(control_inputs=assertions):
          function = lambda buffer: tf.gather(
              params=buffer, indices=predecessor_indices
          )
          sequence_values = self.buffers[sequence_values].fmap(
              function=function, cls=TensorDict
          )
  
          starts = tf.math.cumsum(x=lengths, exclusive=True)
          initial_indices = tf.gather(
              params=predecessor_indices, indices=starts
          )
          function = lambda buffer: tf.gather(
              params=buffer, indices=initial_indices
          )
          initial_values = self.buffers[initial_values].fmap(
              function=function, cls=TensorDict
          )
  
      if len(sequence_values) == 0:
          if len(initial_values) == 0:
              return lengths
          else:
              return lengths, initial_values
  
      elif len(initial_values) == 0:
          return (
              tf.stack(values=(starts, lengths), axis=1),
              sequence_values,
          )
  
      else:
          return (
              tf.stack(values=(starts, lengths), axis=1),
              sequence_values,
              initial_values,
          )
  
file_path: tensorforce/tensorforce/core/memories/queue.py
imports: import numpy
  import tensorflow
  from tensorforce import TensorforceError, util
  from tensorforce.core import TensorDict, TensorSpec, tf_function, tf_util, VariableDict
  from tensorforce.core.memories import Memory
label_desc: Use image node_id 6 for calling the Queue obj's underlying member method code with example usage: obj.predecessors() and returns: lengths,  predecessor_indices,  mask, lengths, lengths,  initial_values, tf,  sequence_values, tf,  sequence_values,  initial_values