class_docstr: Replay buffer for HER (Hindsight Experience Replay).
  
  It constructs hindsight examples using future strategy.
  
  Args:
      replay_k (int): Number of HER transitions to add for each regular
          Transition. Setting this to 0 means that no HER replays will
          be added.
      reward_fn (callable): Function to re-compute the reward with
          substituted goals.
      capacity_in_transitions (int): total size of transitions in the buffer.
      env_spec (EnvSpec): Environment specification.
class_docstr_tok: ["Replay","buffer","for","HER","(","Hindsight","Experience","Replay",")",".","It","constructs","hindsight","examples","using","future","strategy",".","Args",":","replay_k","(","int",")",":","Number","of","HER","transitions","to","add","for","each","regular","Transition",".","Setting","this","to","0","means","that","no","HER","replays","will","be","added",".","reward_fn","(","callable",")",":","Function","to","re-compute","the","reward","with","substituted","goals",".","capacity_in_transitions","(","int",")",":","total","size","of","transitions","in","the","buffer",".","env_spec","(","EnvSpec",")",":","Environment","specification","."]
docstr: Adds a path to the replay buffer.
  
  For each transition in the given path except the last one,
  replay_k HER transitions will added to the buffer in addition
  to the one in the path. The last transition is added without
  sampling additional HER goals.
  
  Args:
      path(dict[str, np.ndarray]): Each key in the dict must map
          to a np.ndarray of shape :math:`(T, S^*)`.
docstr_tok: ["Adds","a","path","to","the","replay","buffer",".","For","each","transition","in","the","given","path","except","the","last","one",",","replay_k","HER","transitions","will","added","to","the","buffer","in","addition","to","the","one","in","the","path",".","The","last","transition","is","added","without","sampling","additional","HER","goals",".","Args",":","path","(","dict","[","str",",","np.ndarray","]",")",":","Each","key","in","the","dict","must","map","to","a","np.ndarray","of","shape",":","math",":","`","(","T",",","S^","*",")","`","."]
code: def add_path(self, path):
      """Adds a path to the replay buffer.
  
      For each transition in the given path except the last one,
      replay_k HER transitions will added to the buffer in addition
      to the one in the path. The last transition is added without
      sampling additional HER goals.
  
      Args:
          path(dict[str, np.ndarray]): Each key in the dict must map
              to a np.ndarray of shape :math:`(T, S^*)`.
  
      """
      obs_space = self._env_spec.observation_space
      if not isinstance(path["observations"][0], dict):
          # unflatten dicts if they've been flattened
          path["observations"] = obs_space.unflatten_n(
              path["observations"]
          )
          path["next_observations"] = obs_space.unflatten_n(
              path["next_observations"]
          )
  
      # create HER transitions and add them to the buffer
      for idx in range(path["actions"].shape[0] - 1):
          transition = {
              key: sample[idx] for key, sample in path.items()
          }
          her_goals = self._sample_her_goals(path, idx)
  
          # create replay_k transitions using the HER goals
          for goal in her_goals:
              t_new = copy.deepcopy(transition)
              a_g = t_new["next_observations"]["achieved_goal"]
  
              t_new["rewards"] = np.array(
                  self._reward_fn(a_g, goal, None)
              )
              t_new["observations"]["desired_goal"] = goal
              t_new["next_observations"][
                  "desired_goal"
              ] = copy.deepcopy(goal)
              t_new["terminals"] = np.array(False)
  
              # flatten the observation dicts now that we're done with them
              self._flatten_dicts(t_new)
  
              for key in t_new.keys():
                  t_new[key] = t_new[key].reshape(1, -1)
  
              # Since we're using a PathBuffer, add each new transition
              # as its own path.
              super().add_path(t_new)
  
      self._flatten_dicts(path)
      super().add_path(path)
  
file_path: garage/src/garage/replay_buffer/her_replay_buffer.py
imports: import copy
  import numpy
  from garage.replay_buffer.path_buffer import PathBuffer
label_desc: Use image node_id 4 for calling the HERReplayBuffer obj's underlying member method code with example usage: obj.add_path(path) without return types