class_docstr: Stationary Stochastic Bandit environment with per-arm features.
class_docstr_tok: ["Stationary","Stochastic","Bandit","environment","with","per-arm","features","."]
docstr: Initializes the environment.
  
  In each round, global context is generated by global_sampling_fn, item
  contexts are generated by item_sampling_fn. The score matrix is of shape
  `[item_dim, global_dim]`, and plays the role of the weight matrix in the
  inner product of item and global features. This inner product gives scores
  for items, based on which the modelled user chooses items.
  
  In veery round, an extra all-zero item is mixed in the recommendation. If
  the modelled user chooses this ghost item, it will count as a no-click.
  
  Args:
    global_sampling_fn: A function that outputs a random 1d array or list of
      ints or floats. This output is the global context. Its shape and type
      must be consistent across calls.
    item_sampling_fn: A function that outputs a random 1 array or list of ints
      or floats (same type as the output of `global_context_sampling_fn`).
      This output is the per-arm context. Its shape must be consistent across
      calls.
    num_items: (int) the number of items in every sample.
    num_slots: (int) the number of items recommended in every round.
    scores_weight_matrix: A tensor of shape `[item_dim, global_dim]`. The
      score of an item is calculated as `global * M * item + noise`.
    feedback_model: The type of feedback model. Currently only implemented is
      -- `cascading`: the feedback is a tuple `(k, v)`, where `k` is the index
      of the chosen item, and `v` is the value of the choice.
    click_model: The way the environment models that diversity is desired. --
      `ghost_actions`: For every dimension of the item space, a unit vector is
      added to the list of available items. If one of these unit-vector items
      gets selected, it results in a `no-click`. -- `distance_based`:
      Inner-product scores are calculated, and if none of the scores exceed a
      given parameter, no item is clicked.
    real_cascade: If true, the user is actually modeled as a sequentional
      decision maker, clicking on the first relevant item.
    distance_threshold: (float) In case the diversity model is distance based,
      this threshold governs if the user actually clicked on any of the items.
    batch_size: The batch size.
    name: The name of this environment instance.
docstr_tok: ["Initializes","the","environment",".","In","each","round",",","global","context","is","generated","by","global_sampling_fn",",","item","contexts","are","generated","by","item_sampling_fn",".","The","score","matrix","is","of","shape","`","[","item_dim",",","global_dim","]","`",",","and","plays","the","role","of","the","weight","matrix","in","the","inner","product","of","item","and","global","features",".","This","inner","product","gives","scores","for","items",",","based","on","which","the","modelled","user","chooses","items",".","In","veery","round",",","an","extra","all-zero","item","is","mixed","in","the","recommendation",".","If","the","modelled","user","chooses","this","ghost","item",",","it","will","count","as","a","no-click",".","Args",":","global_sampling_fn",":","A","function","that","outputs","a","random","1d","array","or","list","of","ints","or","floats",".","This","output","is","the","global","context",".","Its","shape","and","type","must","be","consistent","across","calls",".","item_sampling_fn",":","A","function","that","outputs","a","random","1","array","or","list","of","ints","or","floats","(","same","type","as","the","output","of","`","global_context_sampling_fn","`",")",".","This","output","is","the","per-arm","context",".","Its","shape","must","be","consistent","across","calls",".","num_items",":","(","int",")","the","number","of","items","in","every","sample",".","num_slots",":","(","int",")","the","number","of","items","recommended","in","every","round",".","scores_weight_matrix",":","A","tensor","of","shape","`","[","item_dim",",","global_dim","]","`",".","The","score","of","an","item","is","calculated","as","`","global","*","M","*","item","+","noise","`",".","feedback_model",":","The","type","of","feedback","model",".","Currently","only","implemented","is","--","`","cascading","`",":","the","feedback","is","a","tuple","`","(","k",",","v",")","`",",","where","`","k","`","is","the","index","of","the","chosen","item",",","and","`","v","`","is","the","value","of","the","choice",".","click_model",":","The","way","the","environment","models","that","diversity","is","desired",".","--","`","ghost_actions","`",":","For","every","dimension","of","the","item","space",",","a","unit","vector","is","added","to","the","list","of","available","items",".","If","one","of","these","unit-vector","items","gets","selected",",","it","results","in","a","`","no-click","`",".","--","`","distance_based","`",":","Inner-product","scores","are","calculated",",","and","if","none","of","the","scores","exceed","a","given","parameter",",","no","item","is","clicked",".","real_cascade",":","If","true",",","the","user","is","actually","modeled","as","a","sequentional","decision","maker",",","clicking","on","the","first","relevant","item",".","distance_threshold",":","(","float",")","In","case","the","diversity","model","is","distance","based",",","this","threshold","governs","if","the","user","actually","clicked","on","any","of","the","items",".","batch_size",":","The","batch","size",".","name",":","The","name","of","this","environment","instance","."]
code: def __init__(
      self,
      global_sampling_fn: Callable[[], types.Array],
      item_sampling_fn: Callable[[], types.Array],
      num_items: int,
      num_slots: int,
      scores_weight_matrix: types.Float,
      feedback_model: int = FeedbackModel.CASCADING,
      click_model: int = ClickModel.GHOST_ACTIONS,
      real_cascade: bool = False,
      distance_threshold: Optional[float] = None,
      batch_size: Optional[int] = 1,
      name: Optional[Text] = "ranking_environment",
  ):
      """Initializes the environment.
  
      In each round, global context is generated by global_sampling_fn, item
      contexts are generated by item_sampling_fn. The score matrix is of shape
      `[item_dim, global_dim]`, and plays the role of the weight matrix in the
      inner product of item and global features. This inner product gives scores
      for items, based on which the modelled user chooses items.
  
      In veery round, an extra all-zero item is mixed in the recommendation. If
      the modelled user chooses this ghost item, it will count as a no-click.
  
      Args:
        global_sampling_fn: A function that outputs a random 1d array or list of
          ints or floats. This output is the global context. Its shape and type
          must be consistent across calls.
        item_sampling_fn: A function that outputs a random 1 array or list of ints
          or floats (same type as the output of `global_context_sampling_fn`).
          This output is the per-arm context. Its shape must be consistent across
          calls.
        num_items: (int) the number of items in every sample.
        num_slots: (int) the number of items recommended in every round.
        scores_weight_matrix: A tensor of shape `[item_dim, global_dim]`. The
          score of an item is calculated as `global * M * item + noise`.
        feedback_model: The type of feedback model. Currently only implemented is
          -- `cascading`: the feedback is a tuple `(k, v)`, where `k` is the index
          of the chosen item, and `v` is the value of the choice.
        click_model: The way the environment models that diversity is desired. --
          `ghost_actions`: For every dimension of the item space, a unit vector is
          added to the list of available items. If one of these unit-vector items
          gets selected, it results in a `no-click`. -- `distance_based`:
          Inner-product scores are calculated, and if none of the scores exceed a
          given parameter, no item is clicked.
        real_cascade: If true, the user is actually modeled as a sequentional
          decision maker, clicking on the first relevant item.
        distance_threshold: (float) In case the diversity model is distance based,
          this threshold governs if the user actually clicked on any of the items.
        batch_size: The batch size.
        name: The name of this environment instance.
      """
      self._global_sampling_fn = global_sampling_fn
      self._item_sampling_fn = item_sampling_fn
      self._num_items = num_items
      self._num_slots = num_slots
      self._scores_weight_matrix = scores_weight_matrix
      self._feedback_model = feedback_model
      self._batch_size = batch_size
      self._click_model = click_model
      if click_model == ClickModel.DISTANCE_BASED:
          assert distance_threshold is not None, (
              "If the diversity model is `DISTANCE_BASED`, "
              "the distance threshold must be set."
          )
      self._distance_threshold = distance_threshold
      self._real_cascade = real_cascade
  
      global_spec = array_spec.ArraySpec.from_array(
          global_sampling_fn()
      )
      item_spec = array_spec.add_outer_dims_nest(
          array_spec.ArraySpec.from_array(item_sampling_fn()),
          (num_items,),
      )
      observation_spec = {
          GLOBAL_KEY: global_spec,
          PER_ARM_KEY: item_spec,
      }
      self._global_dim = global_spec.shape[0]
      self._item_dim = item_spec.shape[-1]
  
      action_spec = array_spec.BoundedArraySpec(
          shape=(num_slots,),
          dtype=np.int32,
          minimum=0,
          maximum=num_items - 1,
          name="action",
      )
  
      if feedback_model == FeedbackModel.CASCADING:
          # `chosen_index == num_slots` means no recommended item was clicked.
          reward_spec = {
              "chosen_index": array_spec.BoundedArraySpec(
                  shape=[],
                  minimum=0,
                  maximum=num_slots,
                  dtype=np.int32,
                  name="chosen_index",
              ),
              "chosen_value": array_spec.ArraySpec(
                  shape=[], dtype=np.float32, name="chosen_value"
              ),
          }
      elif feedback_model == FeedbackModel.SCORE_VECTOR:
          reward_spec = array_spec.ArraySpec(
              shape=[num_slots], dtype=np.float32, name="score_vector"
          )
      else:
          raise NotImplementedError(
              "Feedback model {} not implemented".format(feedback_model)
          )
  
      super(RankingPyEnvironment, self).__init__(
          observation_spec, action_spec, reward_spec, name=name
      )
  
imports: from typing import Callable, Optional, Sequence, Text
  import numpy
  import tensorflow
  from tf_agents.bandits.environments import bandit_py_environment
  from tf_agents.bandits.specs import utils
  from tf_agents.specs import array_spec
  from tf_agents.trajectories import time_step
  from tf_agents.typing import types
label_desc: Use image node_id 1 to create a new RankingPyEnvironment object from inherited base classes: bandit_py_environment with example: obj = RankingPyEnvironment(global_sampling_fn, item_sampling_fn, num_items, num_slots, scores_weight_matrix, feedback_model, click_model, real_cascade, distance_threshold, batch_size, name)