'Sample steps from the environment until we have enough for a batch.'

def sample_episodes(self, sess):

    if self.unify_episodes:
        self.all_new_ep = self.start_episode[0]
    episodes = []
    total_steps = 0
    while (total_steps < (self.max_step * len(self.env))):
        (initial_state, observations, actions, rewards, pads) = self._sample_episodes(sess)
        observations = zip(*observations)
        actions = zip(*actions)
        terminated = np.array(self.env.dones)
        self.total_rewards = np.sum((np.array(rewards[self.start_id:]) * (1 - np.array(pads[self.start_id:]))), axis=0)
        self.episode_running_rewards *= (1 - self.start_episode)
        self.episode_running_lengths *= (1 - self.start_episode)
        self.episode_running_rewards += self.total_rewards
        self.episode_running_lengths += np.sum((1 - np.array(pads[self.start_id:])), axis=0)
        episodes.extend(self.convert_from_batched_episodes(initial_state, observations, actions, rewards, terminated, pads))
        total_steps += np.sum((1 - np.array(pads)))
        self.start_episode = np.logical_or(terminated, (self.step_count >= self.cutoff_agent))
        episode_rewards = self.episode_running_rewards[self.start_episode].tolist()
        self.episode_rewards.extend(episode_rewards)
        self.episode_lengths.extend(self.episode_running_lengths[self.start_episode].tolist())
        self.episode_rewards = self.episode_rewards[(-100):]
        self.episode_lengths = self.episode_lengths[(-100):]
        if ((self.save_trajectories_file is not None) and ((self.best_batch_rewards is None) or (np.mean(self.total_rewards) > self.best_batch_rewards))):
            self.best_batch_rewards = np.mean(self.total_rewards)
            my_episodes = self.convert_from_batched_episodes(initial_state, observations, actions, rewards, terminated, pads)
            with gfile.GFile(self.save_trajectories_file, 'w') as f:
                pickle.dump(my_episodes, f)
        if (not self.batch_by_steps):
            return (initial_state, observations, actions, rewards, terminated, pads)
    return self.convert_to_batched_episodes(episodes)
